-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX10Controller.ino
308 lines (253 loc) · 7.05 KB
/
X10Controller.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* X-10 Controller Interface
*
* Author: Melissa LeBlanc-Williams
* Website: https://melissagirl.com
*
* Additional Libraries Required to Compile
* U8g2
* MQTT by Joel Gaehwiler
* ESP8266mDNS
* ESP8266WebServer
* ESP8266WiFi
* ESP8266HTTPUpdateServer
*
* X10 FireCracker Library created by BroHogan and updated by Melissa LeBlanc-Williams
*
* Note: After flashing the code once you can remotely access your device by going to http://HOSTNAMEOFDEVICE.local/firmware
* obviously replace HOSTNAMEOFDEVICE with whatever you defined below. The user name and password are also defined below.
*
*/
// X10 FireCracker
#include "X10FireCracker.h"
// OLED
#include <Wire.h>
#include <U8g2lib.h>
// WiFi
#include <ESP8266WiFi.h>
// Update server
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
// MQTT
#include <MQTTClient.h>
#define BIT_DELAY 1
// Initialize the OLED Display
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
/* DO NOT CHANGE ANY SETTINGS ABOVE THIS LINE */
/* Serial Pin Settings */
#define RTS_PIN 14 // DB9 Pin 7
#define DTR_PIN 12 // DB9 Pin 4
/* WIFI Settings */
const char* ssid = "WiFi SSID";
const char* password = "WiFi Password";
/* Web Updater Settings */
// Host Name of Device
const char* host = "X10Controller";
// Path to access firmware update page (Not Neccessary to change)
const char* update_path = "/firmware";
// Username to access the web update page
const char* update_username = "admin";
// Password to access the web update page
const char* update_password = "password";
/* MQTT Settings */
const char* mqttServer = "192.168.11.6";
const char* subscribeTopic = "MySmartHome/utilities/X10Controller";
const char* mqttDeviceID = "X10Controller1";
/* Time in seconds until screen is cleared. Set to 0 to disable. */
int screenClearTimeout = 5;
/* DO NOT CHANGE ANY SETTINGS BELOW THIS LINE */
unsigned long millisSinceLastEvent = 0;
// Web Server
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
WiFiClient espClient;
MQTTClient client;
void setup()
{
X10.init(RTS_PIN, DTR_PIN, BIT_DELAY);
u8g2.begin();
connectWifi();
client.begin(mqttServer, espClient);
client.onMessage(messageReceived);
MDNS.begin(host);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
}
void loop()
{
if (!client.connected()) {
connectMQTT();
}
client.loop();
delay(10);
httpServer.handleClient();
if (screenClearTimeout > 0 && millis() - millisSinceLastEvent > screenClearTimeout * 1000) {
// Clear the screen
startOled();
outputOled();
}
}
void connectWifi() {
delay(10);
startOled();
u8g2.setCursor(0, 35);
u8g2.print("Connecting to ");
u8g2.print(ssid);
outputOled();
// Start WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Animated Connection Message while Connecting
while (WiFi.status() != WL_CONNECTED) {
delay(125);
printMessage("Connecting");
delay(125);
printMessage("Connecting.");
delay(125);
printMessage("Connecting..");
delay(125);
printMessage("Connecting...");
}
// WiFi Info
startOled();
u8g2.setCursor(0, 20);
u8g2.print("WiFi connected");
u8g2.setCursor(0, 35);
u8g2.print("IP address:");
u8g2.setCursor(0, 50);
u8g2.print(WiFi.localIP());
outputOled();
}
void connectMQTT() {
startOled();
u8g2.setCursor(0, 10);
u8g2.print("Attempting MQTT");
u8g2.setCursor(0, 25);
u8g2.print("connection...");
u8g2.setCursor(0, 40);
// Loop until we're reconnected
while (!client.connect(mqttDeviceID)) {
delay(1000);
}
u8g2.print("Connected!");
u8g2.setCursor(0, 55);
u8g2.print("Listening...");
outputOled();
// ... and subscribe
client.subscribe(subscribeTopic);
millisSinceLastEvent = millis();
}
void messageReceived(String &topic, String &payload) {
byte error = 0;
String tokens[3];
int token = 0;
int tokenStart = 0;
int tokenEnd = 0;
HouseCode houseCode;
int deviceId = 0;
CommandCode command;
// Separate the message into tokens
for (int i = 0; i < payload.length(); i++) {
if (payload.charAt(i) == '-') {
tokens[token] = payload.substring(tokenStart, i);
tokenStart = i + 1;
token++;
if (token > sizeof(tokens)) {
break;
}
}
}
// Grab the final token
tokens[token] = payload.substring(tokenStart, payload.length());
if (tokens[0] == "ON") {
command = cmdOn;
} else if (tokens[0] == "OFF") {
command = cmdOff;
} else if (tokens[0] == "BRIGHT") {
command = cmdBright;
} else if (tokens[0] == "DIM") {
command = cmdDim;
} else {
error = 1;
}
char houseLetter = tokens[1].c_str()[0];
if (houseLetter >= 'A' && houseLetter <= 'P') {
houseCode = (HouseCode)(houseLetter - 'A');
} else {
error = 1;
}
if (tokens[2].toInt() >= 1 && tokens[2].toInt() <= 16) {
deviceId = tokens[2].toInt();
} else if (tokens[2] == "ALL" && command == cmdOn) {
command = cmdAllOn;
} else if (tokens[2] == "ALL" && command == cmdOff) {
command = cmdAllOff;
} else if (tokens[2] == "LAMPS" && command == cmdOn) {
command = cmdLampsOn;
} else if (tokens[2] == "LAMPS" && command == cmdOff) {
command = cmdLampsOff;
} else {
error = 1;
}
if (error == 0) {
sendCommand(houseCode, deviceId, command);
} else {
printMessage("Error: Bad Format");
}
millisSinceLastEvent = millis();
}
void startOled() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
}
void outputOled()
{
u8g2.sendBuffer(); // transfer internal memory to the display
}
void printMessage(const char *s)
{
startOled();
u8g2.setCursor(0, 35);
u8g2.print(s); // write something to the internal memory
outputOled();
}
// Send a command and Display command Info on OLED
void sendCommand(HouseCode houseCode, int deviceId, CommandCode command) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.setCursor(30, 10);
u8g2.print("Sending ");
u8g2.setCursor(30, 25);
if (command == cmdOn) {
u8g2.print("ON");
} else if (command == cmdOff) {
u8g2.print("OFF");
} else if (command == cmdBright) {
u8g2.print("BRIGHT");
} else if (command == cmdDim) {
u8g2.print("DIM");
} else if (command == cmdAllOn) {
u8g2.print("ALL ON");
} else if (command == cmdAllOff) {
u8g2.print("ALL OFF");
} else if (command == cmdLampsOn) {
u8g2.print("LAMPS ON");
} else if (command == cmdLampsOff) {
u8g2.print("LAMPS OFF");
} else {
u8g2.print("UNKNOWN");
}
u8g2.setCursor(30, 40);
u8g2.print("House: ");
char houseLetter = 'A' + houseCode;
u8g2.print(houseLetter);
if (deviceId > 0) {
u8g2.setCursor(30, 55);
u8g2.print("Device: ");
u8g2.print(deviceId);
}
u8g2.sendBuffer(); // transfer internal memory to the display
X10.sendCmd(houseCode, deviceId, command);
}