-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleExampleRC.ino
132 lines (109 loc) · 3.04 KB
/
SimpleExampleRC.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
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <RCSwitch.h>
#define ledPin 2
const uint16_t kIrLed = 5; //Pin D1 for IR led
const uint16_t radio = 4; //Pin D2 for radio transmit
fauxmoESP fauxmo;
ESP8266WebServer server(81);
RCSwitch rf = RCSwitch();
void setup() {
Serial.begin(115200);
rf.enableTransmit(radio);
rf.setProtocol(1);
if (setupNetwork()) {
// Setup fauxmo
Serial.println("Adding Devices");
fauxmo.setPort(80);
fauxmo.enable(true);
// Add devices here
fauxmo.addDevice("RCDevice");
// Set a callback function for when a device is found
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if (strcmp(device_name, "RCDevice") == 0) {
if (state) {
Serial.println("RCDevice On");
RCDeviceOn();
}
else {
Serial.println("RCDevice Off");
RCDeviceOff();
}
}
});
}
}
void loop() {
fauxmo.handle();
server.handleClient();
}
boolean setupNetwork() {
// Let us connect to WiFi
WiFi.mode(WIFI_STA);
String ssid = "<Wifi-Network-Name>";
String password = "<Wifi-Network-Password>";
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".......");
Serial.println("WiFi Connected....IP Address:");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
routeServer();
server.enableCORS(true);
server.begin();
Serial.println("HTTP server live");
return true;
}
//Server Routing
void routeServer () {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"),
F("ESP8266 Controller Basement"));
});
server.on("/rcDeviceOn", HTTP_GET, RCDeviceOn);
server.on("/rcDeviceOff", HTTP_GET, RCDeviceOff);
server.on("/rcDeviceStop", HTTP_GET, RCDeviceStop);
server.onNotFound(handleNotFound);
}
void handleNotFound()
{
if (server.method() == HTTP_OPTIONS)
{
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(204);
}
else
{
server.send(404, "text/plain", "");
}
}
//RCDevice Commands
void RCDeviceOn() {
server.send(200, "text/plain", "RCDevice On");
rf.send(6582737, 24);
delay(300);
rf.send(6582737, 24);
}
void RCDeviceOff() {
server.send(200, "text/plain", "RCDevice Off");
rf.send(6582740, 24);
delay(300);
rf.send(6582740, 24);
}
void RCDeviceStop() {
server.send(200, "text/plain", "RCDevice Stop");
rf.send(6582738, 24);
}