-
Notifications
You must be signed in to change notification settings - Fork 1
/
wemos-lolin-mqtt-subscriber.ino
340 lines (291 loc) · 8.85 KB
/
wemos-lolin-mqtt-subscriber.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <Wire.h>
#include <SSD1306.h>
#include "RobotoFonts.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <string.h>
#include <ArduinoJson.h>
#include <DebounceEvent.h>
/////////////////////////////////////////
// CHANGE TO MATCH YOUR CONFIGURATION //
/////////////////////////////////////////
#define WIFI_SSID "my_wifi"
#define WIFI_PASS "my_password"
#define BROKER "192.168.0.1"
#define BROKER_PORT 1883
#define MQTT_USER "my_mqtt_user"
#define MQTT_PASS "my_mqtt_password"
#define CLIENT_ID "esp32_display_0" // has to be unique on your broker
#define TOPIC "display/wemos/0"
#define ALIGN_TOPIC TOPIC "/align_right"
#define SHOULD_FLIP_SCREEN false // flips the screen vertically
#define INVERT_COLORS false // if true: white background with black text
#define BUTTON_PIN 0 // comment out if you don't have a button
// #define SENSOR_PIN 15 // uncomment if you have a motion sensor
#ifdef SENSOR_PIN
#define SECONDS_TO_TURN_OFF 60
#endif
/////////////////////////////////////////
// Only change this if you have a diferent display size
#define OLED_HEIGHT 64
#define OLED_WIDTH 128
// Bumped the packat size so it supports larger messages.
// If you are sending huge messages and your display stops updating,
// you can try to increase it
#define MQTT_PACKET_SIZE 1024
#define JSON_BUFFER_SIZE MQTT_PACKET_SIZE
#ifdef BUTTON_PIN
void buttonTriggered(uint8_t pin, uint8_t event, uint8_t count, uint16_t length);
DebounceEvent buttonEvent = DebounceEvent(BUTTON_PIN, buttonTriggered, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP);
#endif
struct DisplayData {
const char* line1;
const char* line2;
const char* line3;
int line1Size;
int line2Size;
int line3Size;
};
SSD1306 display(0x3c, 5, 4);
WiFiClient wclient;
PubSubClient clientMQTT(wclient);
// Functions
void motionSensorTriggered();
void reconnect();
DisplayData displayData = {"--", "--", "--", 10, 16, 24};
volatile boolean shouldUpdateUI = false;
volatile boolean sensorIsOff = true;
volatile boolean align_right = false;
volatile int currentPage = 0;
volatile unsigned long motionSensorLastTriggeredInMicros;
DynamicJsonDocument jsonObject(JSON_BUFFER_SIZE);
void callback(char* topic, byte* payload, unsigned int length) {
// handle message
Serial.println("\n\ncallback called!");
Serial.print("length: ");
Serial.println(length);
String topicStr = String(topic);
Serial.print("Message arrived [");
Serial.print(topicStr);
Serial.print("] ");
Serial.println();
char charPayload[length+1];
Serial.print("payload: ");
for (int i = 0; i < length; i++) {
charPayload[i] = (char)payload[i];
Serial.print(charPayload[i]);
}
charPayload[length] = '\0';
if (topicStr == ALIGN_TOPIC) {
Serial.println("parsing alignment!");
String payloadStr = String(charPayload);
align_right = payloadStr == "true";
Serial.print("Align Right: ");
Serial.print(align_right);
Serial.print(" vs ");
Serial.println(payloadStr);
shouldUpdateUI = true;
return;
}
if (topicStr == TOPIC) {
Serial.println("deserializing Json!");
DeserializationError error = deserializeJson(jsonObject, (const char*) charPayload);
if (error) {
Serial.println("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
Serial.println("success!");
shouldUpdateUI = true;
return;
}
}
void setup() {
Serial.begin(115200);
delay(10);
display.init();
if (SHOULD_FLIP_SCREEN) {
display.flipScreenVertically();
}
if (INVERT_COLORS) {
display.invertDisplay();
}
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(Roboto_Condensed_16);
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.println();
Serial.println();
display.clear();
display.drawString(0, 0, "Waiting for WiFi...");
display.display();
Serial.print("Waiting for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
display.clear();
display.drawString(0, 0, "WiFi connected");
display.drawString(0, 16, "IP address: ");
display.drawString(0, 32, WiFi.localIP().toString());
display.display();
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(3000);
// set MQTT broker and connect
clientMQTT.setServer(BROKER, BROKER_PORT);
clientMQTT.setCallback(callback);
clientMQTT.setBufferSize(MQTT_PACKET_SIZE);
display.clear();
display.drawString(0, 0, "Connecting to broker...");
display.display();
reconnect();
display.drawString(0, 16, "Connected!");
display.drawString(0, 32, "Waiting for messages...");
display.display();
Serial.println("Waiting for messages...");
#ifdef SENSOR_PIN
pinMode(SENSOR_PIN, INPUT);
attachInterrupt(SENSOR_PIN, motionSensorTriggered, CHANGE);
motionSensorLastTriggeredInMicros = micros();
#endif
}
void loop() {
if (!clientMQTT.connected()) {
reconnect();
}
clientMQTT.loop();
#ifdef BUTTON_PIN
buttonEvent.loop();
#endif
#ifdef SENSOR_PIN
long sensorTriggerAgo = (long)(micros() - motionSensorLastTriggeredInMicros);
if (sensorIsOff && sensorTriggerAgo > (SECONDS_TO_TURN_OFF * 1000000)) {
display.displayOff();
return;
}
#endif
display.displayOn();
if (shouldUpdateUI) {
Serial.println("\n\nUpdating UI");
serializeJson(jsonObject, Serial);
shouldUpdateUI = false;
parseJsonForCurrentPage();
int freeSpace = OLED_HEIGHT - (displayData.line1Size + displayData.line2Size + displayData.line3Size);
if (freeSpace < 0) {
//this means that the last line will be out of the display
freeSpace = 0;
}
int line2Y = displayData.line1Size + freeSpace / 2;
int line3Y = displayData.line2Size + line2Y + freeSpace / 2;
display.clear();
int x = 0;
if (align_right) {
display.setTextAlignment(TEXT_ALIGN_RIGHT);
x = OLED_WIDTH;
} else {
display.setTextAlignment(TEXT_ALIGN_LEFT);
x = 0;
}
display.setFont(getFontForSize(displayData.line1Size));
display.drawString(x, 0, displayData.line1);
Serial.println(displayData.line1);
display.setFont(getFontForSize(displayData.line2Size));
display.drawString(x, line2Y, displayData.line2);
Serial.println(displayData.line2);
display.setFont(getFontForSize(displayData.line3Size));
display.drawString(x, line3Y, displayData.line3);
Serial.println(displayData.line3);
display.display();
}
}
void parseJsonForCurrentPage() {
JsonObject page = jsonObject["pages"][currentPage];
const char* line1 = page["1"]["text"];
const char* line2 = page["2"]["text"];
const char* line3 = page["3"]["text"];
int line1Size = parseSize(page["1"]["size"]);
int line2Size = parseSize(page["2"]["size"]);
int line3Size = parseSize(page["3"]["size"]);
displayData = {line1, line2, line3, line1Size, line2Size, line3Size};
}
int lastPageNumer() {
JsonArray pages = jsonObject["pages"];
return pages.size();
}
int parseSize(int intendedSize) {
switch (intendedSize) {
case 10:
return 10;
case 24:
return 24;
default:
return 16;
}
}
const unsigned char* getFontForSize(int fontSize) {
switch (fontSize) {
case 10:
return Roboto_Condensed_10;
case 24:
return Roboto_Condensed_24;
default:
return Roboto_Condensed_16;
}
}
#ifdef BUTTON_PIN
void buttonTriggered(uint8_t pin, uint8_t event, uint8_t count, uint16_t length) {
if (event == EVENT_PRESSED) {
currentPage++;
if (currentPage >= lastPageNumer()) {
currentPage = 0;
}
// we should also reset the motion timer, since you are clicking the buttonEvent
motionSensorLastTriggeredInMicros = micros();
shouldUpdateUI = true;
}
}
#endif
#ifdef SENSOR_PIN
void motionSensorTriggered() {
Serial.println("Sensor Triggered!");
Serial.println("Sensor state: ");
Serial.println(digitalRead(SENSOR_PIN));
if (digitalRead(SENSOR_PIN) == HIGH) {
sensorIsOff = false;
shouldUpdateUI = true;
} else {
sensorIsOff = true;
Serial.println("Updating Sensor timestamp");
motionSensorLastTriggeredInMicros = micros();
shouldUpdateUI = true;
}
}
#endif
void reconnect() {
// Loop until we're reconnected
while (!clientMQTT.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
clientMQTT.setCallback(callback);
if (clientMQTT.connect(CLIENT_ID, MQTT_USER, MQTT_PASS)) {
Serial.println("connected");
// subscribe to the topic
clientMQTT.subscribe(TOPIC, 1);
Serial.print("subscribed to ");
Serial.println(TOPIC);
clientMQTT.subscribe(ALIGN_TOPIC, 1);
Serial.print("subscribed to ");
Serial.println(ALIGN_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(clientMQTT.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}