-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266-433mhz.ino
88 lines (71 loc) · 2.01 KB
/
ESP8266-433mhz.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <RCSwitch.h>
// --- Replace with your network credentials ---
const char* ssid = "";
const char* password = "";
// --- Replace with your network credentials ---
RCSwitch mySwitch = RCSwitch();
MDNSResponder mdns;
ESP8266WebServer server(80);
const String helpStr = "Usage: \n"
"/changestate/housecode=<housecode>&socketcode=<socketcode>&state=<state> \n"
"e.g.: \n"
"/changestate/housecode=01001&socketcode=10001&state=on";
void setup(void) {
Serial.begin(115200);
mySwitch.enableTransmit(2);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.hostname("ESP-433-Gateway");
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// serial output of connection details
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("ESP-433-Gateway", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", []() {
server.send(200, "text/plain", helpStr);
});
server.on("/changestate", changestate);
server.begin();
Serial.println("HTTP server started");
}
void changestate() {
String house = server.arg("housecode");
const char* housecode = house.c_str();
String socket = server.arg("socketcode");
const char* socketcode = socket.c_str();
String state = server.arg("state").c_str();
Serial.println("/changestate");
Serial.print("housecode:\t");
Serial.println(housecode);
Serial.print("socketcode:\t");
Serial.println(socketcode);
Serial.print("state:\t\t");
Serial.println(state);
if (state == "off")
{
mySwitch.switchOff(housecode, socketcode);
server.send(200, "text/plain", "ok");
}
else if (state == "on")
{
mySwitch.switchOn(housecode, socketcode);
server.send(200, "text/plain", "ok");
}
}
void loop(void) {
server.handleClient();
}