-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266-12E-Wifi-Client-GUI-with-Wireless-Relay-Point.ino
132 lines (116 loc) · 4.38 KB
/
ESP8266-12E-Wifi-Client-GUI-with-Wireless-Relay-Point.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Original aRest modules marco schwartz modifications Dave Clapham
// Lights or power control via WiFi & Wireless using the ESP8266-12e nodeMCU device
// Message format for relays : ip address /digital/gpio pin #/ On/Off (0 or 1)
// example : 192.168.0.100/digital/2/0 so ON command to relay on gpio 2
//
// Message format for relay PULSE : ip address /pulse/gpio pin #/
// example : 192.168.0.100/pulse/2/ pulse relay ON & OFF for 500ms on gpio 2 (2 is only choice, couple both relays for 2 x contacts)
//
// Message format for wireless : ip address /wireless/wireless socket number/ Off/On (0 or 1)
// example : 192.168.0.100/2/0 so OFF command directed to wireless socket #2 (on/off notation reversed)
// use buttons for on and off relays, pulse for aircon for pulsed on/off, wireless for 315/433mhz wireless devices
// Import required libraries
// Globals - declare RC Wireless messages
const long w1_off = 3572860706;
const long w1_on = 3572862764;
const long w2_off = 3572859686;
const long w2_on = 3572861738;
const long w3_off = 3572860193;
const long w3_on = 3572862254; // 3572862254
const long w4_off = 3572860968; // all OFF
const long w4_on = 3572859936; // all ON
const long w5_off = 0;
const long w5_on = 0;
const long w6_off = 0;
const long w6_on = 0;
const int pulsedelay = 1500; // pulse delay for pulse command in ms
const boolean debug = true;
#include <ESP8266WiFi.h>
#include <RCSwitch.h> // wireless setup
RCSwitch mySwitch = RCSwitch(); // wireless setup
#include <aRESTm.h>
#include <aREST_UIm.h>
// Create aREST instance
aREST_UI rest = aREST_UI();
// WiFi parameters
const char* ssid = "Your SSID";
const char* password = "Your WiFi Password";
const String st_html = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
float eltime;
// The port to listen for incoming TCP connections
#define LISTEN_PORT 80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
void setup(void){
// Start Serial
Serial.begin(9600);
// Create UI
rest.title("Wireless Sockets");
rest.button(4); // GPIO4 D2
rest.pulse(5); // GPIO5 D1
rest.button(2); // D4 Blue LED
rest.wireless(21); // Wireless sockets/appliances
rest.wireless(22); // rest.wireless(wireless socket # + 20 offset)
rest.wireless(23);
// rest.wireless(24);
// Give name and ID to device
rest.set_id("4");
rest.set_name("Wireless Sockets TX");
// Wireless setup
mySwitch.setProtocol(1); // Optional set protocol 1 0r 2 (default is 1, will work for most outlets)
mySwitch.enableTransmit(16); // RC TX module is connected to ESP8266 PIN #16
mySwitch.setPulseLength(250); // Optional set pulse length for RC TX (must be after protocol set)
mySwitch.setRepeatTransmit(3); // Optional set number of transmission repetitions for RC TX.
digitalWrite(2,HIGH); // turn off LED
digitalWrite(4,HIGH); // turn off relay1 port (GPIO4 D2)
digitalWrite(5,HIGH); // turn off relay2 port (GPIO5 D1)
// Connect to WiFi
WiFi.begin(ssid, password);
IPAddress ip(192, 168, 0, 119); // * comment out 4 lines for DHP
IPAddress gateway(192, 168, 0, 1); // *
IPAddress subnet(255, 255, 255, 0); // *
WiFi.config(ip, gateway, subnet); // *
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(250); // 250
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
eltime = millis();
Serial.println(String(eltime/1000) + " Seconds");
}
void loop() {
// unsigned int ar = analogRead(A0); // read power drawn //
// unsigned int powerdrawn = ar / 1024 / * modrange * ACV; // power in watts test software
// if (debug) Serial.println(powerdrawn);
// Handle REST calls
WiFiClient client = server.available();
if (!client) {
return;
}
int timer = 120; // set a timeout
int onoff = 0;
while(!client.available()){
timer--;
if(!timer % 12) yield(); // dummy to preserve stack
if (timer <= 0) { // timeout
Serial.println("No Request");
client.print(st_html + "No Request" + "</html>\n");
client.flush();
client.stop();
return;
}
delay(1);
}
digitalWrite(0,HIGH); // send carrier burst to sync up receiver and signal level
delay(40);
digitalWrite(0,LOW);
delay(10);
rest.handle(client); // service client
}