-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlight-duino.ino
277 lines (230 loc) · 7.57 KB
/
light-duino.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
/*
* light-duino v2
* MQTT <-> DMX controller with hw switches, based on ESP8266
* See attached Readme.md for details
*
* This is based on the work of Jorgen (aka Juergen Skrotzky, [email protected]), buy him a beer. ;-)
* Rest if not noted otherwise by Peter Froehlich, [email protected] - Munich Maker Lab e.V. (January 2016)
*
* Published under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
* You'll find a copy of the licence text in this repo.
*/
/*
* ToDo:
* - external switch to set all lights to off
* - make OTA work
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <PubSubClient.h> //https://github.com/Imroy/pubsubclient
#include <ESPDMX.h> //https://github.com/Rickgg/ESP-Dmx
// for WiFiManager:
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include "config.h"
#include "helpers.h"
#include "mqtt.h"
#include "dmx.h"
#include "eeprom.h"
#include "switches.h"
#include <Ticker.h>
Ticker DMXupdater;
/*
* MQTT methods
*/
// callback method to handle received mqtt messages
void mqttMessageReceived(const MQTT::Publish& pub) {
// handle message arrived
mqttTopic = pub.topic();
// OTA: "<topic>/<id>/ota", bin file to flash
if (mqttTopic == String(strTopicPrefixID + "ota")) {
//very cool, but kinda dangerous?? disabled for now...
return;
uint32_t startTime = millis();
uint32_t size = pub.payload_len();
if (size == 0)
return;
DEBUG_PRINT("Receiving OTA of ");
DEBUG_PRINT(size);
DEBUG_PRINTLN(" bytes...");
Serial.setDebugOutput(true);
if (ESP.updateSketch(*pub.payload_stream(), size, true, false)) {
pub.payload_stream()->stop();
DEBUG_PRINTLN("Clearing retained message.");
mqttClient.publish(MQTT::Publish(pub.topic(), "").set_retain());
mqttClient.disconnect();
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
ESP.restart();
delay(10000);
}
} else {
mqttPayload = pub.payload_string() + "0"; // trailing 0 needed for wonky but fast field separation later.
mqttNewMessage = true;
DEBUG_PRINTLN("receive MQTT: topic='" + mqttTopic + "', message='" + mqttPayload + "'");
}
}
bool initializeMQTT() {
if (WiFi.status() == WL_CONNECTED) {
if (!mqttClient.connected()) {
if (connectMQTT(mqtt_user, mqtt_pass, mqtt_host)) {
publishMQTTMessage(strTopicPrefixID + "controller", strMac + "," + strIPAddr, true);
// bind callback function to handle incoming mqtt messages
mqttClient.set_callback(mqttMessageReceived);
// define what to listen to
subscribeMQTTTopic(strTopicPrefixID + "set");
return true;
}
} else {
return true;
}
}
return false;
}
void configModeCallback () {
digitalWrite(BUILTIN_LED, LOW);
}
void processMQTTMessage() {
// reset flag
mqttNewMessage = false;
// DMX 'set' Topic: "DMX/<device_id>/set"
// payload syntax: multiple comma-sep-fields consisting of <dmx_id>:<brightness value (0-255) or 1000 to switch between on/dimmed and off>
// e.g.: 1:0,2:128,3:255,4:x
if (mqttTopic == String(strTopicPrefixID + "set")) {
char input[mqttPayload.length()]; // +1 neccessary for \0??
mqttPayload.toCharArray(input, mqttPayload.length());
// Read each command pair
char* command = strtok(input, ",");
while (command != 0) {
// Split the command in two values
char* separator = strchr(command, ':');
if (separator != 0) {
// Actually split the string in 2: replace ':' with 0
*separator = 0;
int dmxID = atoi(command);
if (dmxID <= intMaxChannel) {
++separator;
int dmxValue = atoi(separator);
// 255 is the maximum DMX understands. higher values are errors or special triggers
if (dmxValue == 1000) {
toggleChannel(dmxID);
continue;
}
if (dmxValue > 255) {
dmxValue = 255;
}
//prepare dmx output:
channelValue(dmxID, dmxValue);
}
}
// Find the next command in input string
command = strtok(0, ",");
}
//apply changes to dmx
dmxApplyChanges();
}
}
void setupWifi() {
if (useWifiManager) {
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
wifiManager.setAPConfig(IPAddress(10,0,0,1), IPAddress(10,0,0,1), IPAddress(255,255,255,0));
//and goes into a blocking loop awaiting configuration
String strAPName = String(mqtt_client_id) + String("-") + String(ESP.getChipId());
if(!wifiManager.autoConnect(strAPName.c_str())) {
DEBUG_PRINTLN("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
} else {
long startConnectionAttempt = millis();
DEBUG_PRINTLN("Starting WiFi");
// use static wifi config
WiFi.begin(ssid, password);
// Wait for connection
while(WiFi.status() != WL_CONNECTED or millis() - startConnectionAttempt > wifiTimeout) {
delay(100);
DEBUG_PRINT(".");
}
}
DEBUG_PRINTLN("WiFi connected!");
DEBUG_PRINTLN("IP address: ");
IPAddress local = WiFi.localIP();
strIPAddr = String(local[0]) + "." + String(local[1]) + "." + String(local[2]) + "." + String(local[3]);
DEBUG_PRINTLN(strIPAddr);
}
/*
* ==================
* S E T U P
* ==================
*/
void setup() {
//debug Led on ESP
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH); // means led is off.
// initial serial port
if(_debug)
Serial.begin(9600);
DEBUG_PRINTLN("");
DEBUG_PRINT("MQTT <-> DMX (");
DEBUG_PRINT(ESP.getChipId());
DEBUG_PRINTLN(")");
DEBUG_PRINTLN(__TIMESTAMP__);
DEBUG_PRINT("Sketch size: ");
DEBUG_PRINTLN(ESP.getSketchSize());
DEBUG_PRINT("Free size: ");
DEBUG_PRINTLN(ESP.getFreeSketchSpace());
// Mac address
WiFi.macAddress(mac);
byteToHexString(strMac, mac, 6, ":");
DEBUG_PRINTLN("MAC: ");
DEBUG_PRINTLN(strMac);
// define topics
strTopicPrefix = strTopic + "/";
strTopicPrefixID = strTopicPrefix + strDeviceID + "/";
// init DMX
setupDmx();
// init EEPROM and set initial state
setupEEPROM();
// setup switches
setupSwitches();
// setup WiFi
setupWifi();
// check for connection
initializeMQTT();
// Update DMX every 0.01 seconds
DMXupdater.attach(0.01, updateDMX);
}
/*
* ==================
* L O O P
* ==================
*/
void loop() {
// check for connection and process MQTT
if (processMQTTLoop()) {
// MQTT connection is alive
// process new mqtt messages
if (mqttNewMessage)
processMQTTMessage();
} else {
// No MQTT Connection, reinitialize
DEBUG_PRINTLN("Reinitializing MQTT connection");
initializeMQTT();
}
// check switches
if (switchesEnabled)
checkSwitches();
// save dmx state
saveDMXState();
}