JSON_MQTT_Markus_Schreibtisch_ESP32_Forum.ino


1
#include <WiFi.h>
2
#include <PubSubClient.h>
3
#include <ArduinoJson.h>
4
5
6
const char* ssid = "";
7
const char* password = "";
8
const char* mqttServer = "192.168.0.24";
9
const int mqttPort = 1883;
10
11
const char* on_cmd = "ON";
12
const char* off_cmd = "OFF";
13
const int BUFFER_SIZE = 300;
14
int count = 0;
15
int firstStart = 0;
16
String topicStr = "NULL";
17
byte brightness[6] = {255,255,255,255,255,255};
18
bool stateOn[6] = {false,false, false, false,false, false};
19
20
int LED1 = 17;
21
int LED2 = 18;
22
int LED3 = 19;
23
int LED4 = 21;
24
int LED5 = 22;
25
int LED6 = 23;
26
27
28
const int freq = 5000;
29
const int ledChannel1 = 0;
30
const int ledChannel2 = 1;
31
const int ledChannel3 = 2;
32
const int ledChannel4 = 3;
33
const int ledChannel5 = 4;
34
const int ledChannel6 = 5;
35
const int resolution = 8;
36
37
WiFiClient espClient;
38
PubSubClient client(espClient);
39
40
void setup() 
41
{
42
  Serial.begin(115200);
43
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
44
  WiFi.setHostname("ESP32_Markus_Schreibtisch");
45
  WiFi.begin(ssid, password);
46
  while (WiFi.status() != WL_CONNECTED) 
47
  {
48
    Serial.println(".");
49
    delay(500);
50
  }
51
52
  Serial.println("Connected to the WiFi network");
53
  Serial.println("IP Adress: ");
54
  Serial.println(WiFi.localIP());
55
  Serial.begin(115200);
56
  
57
  pinMode(LED1, OUTPUT);
58
  pinMode(LED2, OUTPUT);
59
  pinMode(LED3, OUTPUT);
60
  pinMode(LED4, OUTPUT);
61
  pinMode(LED5, OUTPUT);
62
  pinMode(LED6, OUTPUT);
63
64
  ledcSetup(ledChannel1, freq, resolution);
65
  ledcSetup(ledChannel2, freq, resolution);
66
  ledcSetup(ledChannel3, freq, resolution);
67
  ledcSetup(ledChannel4, freq, resolution);
68
  ledcSetup(ledChannel5, freq, resolution);
69
  ledcSetup(ledChannel6, freq, resolution);
70
71
  ledcAttachPin(LED1, ledChannel1);
72
  ledcAttachPin(LED2, ledChannel2);
73
  ledcAttachPin(LED3, ledChannel3);
74
  ledcAttachPin(LED4, ledChannel4);
75
  ledcAttachPin(LED5, ledChannel5);
76
  ledcAttachPin(LED6, ledChannel6);
77
  
78
  delay(10);
79
  
80
  client.setServer(mqttServer, mqttPort);
81
  client.setCallback(callback);
82
  reconnect();
83
  client.subscribe("/JSON/Markus_Schreibtisch1/");
84
  client.subscribe("/JSON/Markus_Schreibtisch2/");
85
  client.subscribe("/JSON/Markus_Schreibtisch3/");
86
  client.subscribe("/JSON/Markus_Schreibtisch4/");
87
  client.subscribe("/JSON/Markus_Schreibtisch5/");
88
  client.subscribe("/JSON/Markus_Schreibtisch6/");
89
  Serial.println("Connected to MQTT");
90
91
}
92
93
void alloff()
94
{
95
  for(int i = 0; i < 6; i++)
96
  {
97
    stateOn[i] = false;
98
  }
99
  count = 0;
100
  setColor();
101
  sendState("/JSON/Markus_Schreibtisch1/Confirm/");
102
  count = 1;
103
  setColor();
104
  sendState("/JSON/Markus_Schreibtisch2/Confirm/");
105
  count = 2;
106
  setColor();
107
  sendState("/JSON/Markus_Schreibtisch3/Confirm/");
108
  count = 3;
109
  setColor();
110
  sendState("/JSON/Markus_Schreibtisch4/Confirm/");
111
  count = 4;
112
  setColor();
113
  sendState("/JSON/Markus_Schreibtisch5/Confirm/");
114
  count = 5;
115
  setColor();
116
  sendState("/JSON/Markus_Schreibtisch6/Confirm/");
117
}
118
119
void setup_wifi() 
120
{
121
122
  delay(10);
123
  Serial.println();
124
  Serial.print("Connecting to ");
125
  Serial.println(ssid);
126
127
  WiFi.mode(WIFI_STA);
128
129
  //WiFi.setHostname("ESP32_Markus");
130
  Serial.println(WiFi.macAddress());
131
  WiFi.begin(ssid, password);
132
133
  while (WiFi.status() != WL_CONNECTED) 
134
  {
135
    delay(1000);
136
    Serial.print(".");
137
  }
138
139
  Serial.println("");
140
  Serial.println("WiFi connected");
141
  Serial.println("IP address: ");
142
  Serial.println(WiFi.localIP());
143
}
144
145
void callback(char* topic, byte* payload, unsigned int length)
146
{
147
  Serial.print("Message arrived [");
148
  Serial.print(topic);
149
  Serial.print("] \n");
150
151
  topicStr = topic;
152
153
  char message[length + 1];
154
  for (int i = 0; i < length; i++) 
155
  {
156
    message[i] = (char)payload[i];
157
  }
158
  message[length] = '\0';
159
160
  if (topicStr == "/JSON/Markus_Schreibtisch1/")
161
  {
162
    count = 0;
163
    if (!processJson(message)) 
164
    {
165
      return;
166
    } 
167
    setColor();
168
    sendState("/JSON/Markus_Schreibtisch1/Confirm/");
169
  }
170
  else if (topicStr == "/JSON/Markus_Schreibtisch2/")
171
  {
172
    count = 1;
173
    if (!processJson(message)) 
174
    {
175
      return;
176
    } 
177
    setColor();
178
    sendState("/JSON/Markus_Schreibtisch2/Confirm/");
179
  }
180
  else if (topicStr == "/JSON/Markus_Schreibtisch3/")
181
  {
182
    count = 2;
183
    if (!processJson(message)) 
184
    {
185
      return;
186
    } 
187
    setColor();
188
    sendState("/JSON/Markus_Schreibtisch3/Confirm/");;
189
  }
190
  else if (topicStr == "/JSON/Markus_Schreibtisch4/")
191
  {
192
    count = 3;
193
    if (!processJson(message)) 
194
    {
195
      return;
196
    } 
197
    setColor();
198
    sendState("/JSON/Markus_Schreibtisch4/Confirm/");
199
  }
200
  else if (topicStr == "/JSON/Markus_Schreibtisch5/")
201
  {
202
    count = 4;
203
    if (!processJson(message)) 
204
    {
205
      return;
206
    } 
207
    setColor();
208
    sendState("/JSON/Markus_Schreibtisch5/Confirm/");
209
  }
210
  else if (topicStr == "/JSON/Markus_Schreibtisch6/")
211
  {
212
    count = 5;
213
    if (!processJson(message)) 
214
    {
215
      return;
216
    } 
217
    setColor();
218
    sendState("/JSON/Markus_Schreibtisch6/Confirm/");;
219
  }
220
}
221
222
bool processJson(char* message) 
223
{
224
 StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
225
226
  JsonObject& root = jsonBuffer.parseObject(message);
227
228
  if (!root.success()) 
229
  {
230
    Serial.println("parseObject() failed");
231
    return false;
232
  }
233
234
  if (root.containsKey("state")) 
235
  {
236
    if (strcmp(root["state"], on_cmd) == 0) 
237
    {
238
      stateOn[count] = true;
239
    }
240
    else if (strcmp(root["state"], off_cmd) == 0) 
241
    {
242
      stateOn[count] = false;
243
    }
244
  }
245
246
    if (root.containsKey("brightness")) 
247
    {
248
      brightness[count] = root["brightness"];
249
    }
250
251
  return true;
252
}
253
254
void sendState(char* topicStr) 
255
{
256
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
257
258
  JsonObject& root = jsonBuffer.createObject();
259
260
  root["state"] = (stateOn[count]) ? on_cmd : off_cmd;
261
262
  root["brightness"] = brightness[count];
263
264
  char buffer[root.measureLength() + 1];
265
  root.printTo(buffer, sizeof(buffer));
266
267
  //Serial.println(buffer);
268
  client.publish(topicStr, buffer, true);
269
}
270
271
void setColor() 
272
{
273
 if(count == 0)
274
  {   
275
    if(stateOn[count] == false)
276
    {
277
      ledcWrite(ledChannel1, 0);
278
    }
279
    else
280
    {
281
      ledcWrite(ledChannel1, brightness[count]);
282
    }
283
  }
284
  else if(count == 1)
285
  {
286
    if(stateOn[count] == false)
287
    {
288
      ledcWrite(ledChannel2, 0);
289
    }
290
    else
291
    {
292
      ledcWrite(ledChannel2, brightness[count]);
293
    }    
294
  }
295
  else if(count == 2)
296
  {
297
    if(stateOn[count] == false)
298
    {
299
      ledcWrite(ledChannel3, 0);
300
    }
301
    else
302
    {
303
      ledcWrite(ledChannel3, brightness[count]);
304
    }    
305
  }
306
    else if(count == 3)
307
  {
308
    if(stateOn[count] == false)
309
    {
310
      ledcWrite(ledChannel4, 0);
311
    }
312
    else
313
    {
314
      ledcWrite(ledChannel4, brightness[count]);
315
    }    
316
  }
317
  else if(count == 4)
318
  {
319
    if(stateOn[count] == false)
320
    {
321
      ledcWrite(ledChannel5, 0);
322
    }
323
    else
324
    {
325
      ledcWrite(ledChannel5, brightness[count]);
326
    }    
327
  }
328
    else if(count == 5)
329
  {
330
    if(stateOn[count] == false)
331
    {
332
      ledcWrite(ledChannel6, 0);
333
    }
334
    else
335
    {
336
      ledcWrite(ledChannel6, brightness[count]);
337
    }    
338
  }
339
340
}
341
342
void reconnect() 
343
{
344
  while (!client.connected()) 
345
  {
346
    Serial.print("Attempting MQTT connection...");
347
    // Attempt to connect
348
    if (client.connect("Markus_Schreibtisch")) 
349
    {
350
      Serial.println("connected_Markus_Schreibtisch");
351
      //setColor(0, 0, 0);
352
      //sendState();
353
      client.subscribe("/JSON/Markus_Schreibtisch1/");
354
      client.subscribe("/JSON/Markus_Schreibtisch2/");
355
      client.subscribe("/JSON/Markus_Schreibtisch3/");
356
      client.subscribe("/JSON/Markus_Schreibtisch4/");
357
      client.subscribe("/JSON/Markus_Schreibtisch5/");
358
      client.subscribe("/JSON/Markus_Schreibtisch6/");
359
    } 
360
    else 
361
    {
362
      Serial.print("failed, rc=");
363
      Serial.print(client.state());
364
      Serial.println(" try again in 5 seconds");
365
      // Wait 3 seconds before retrying
366
      delay(3000);
367
    }
368
  }
369
}
370
void reconnectoL() 
371
{
372
    Serial.print("Attempting MQTT connection...");
373
    // Attempt to connect
374
    if (client.connect("Markus_Schreibtisch")) 
375
    {
376
      Serial.println("connected_Markus_Schreibtisch");
377
      //setColor(0, 0, 0);
378
      //sendState();
379
      client.subscribe("/JSON/Markus_Schreibtisch1/");
380
      client.subscribe("/JSON/Markus_Schreibtisch2/");
381
      client.subscribe("/JSON/Markus_Schreibtisch3/");
382
      client.subscribe("/JSON/Markus_Schreibtisch4/");
383
      client.subscribe("/JSON/Markus_Schreibtisch5/");
384
      client.subscribe("/JSON/Markus_Schreibtisch6/");
385
    } 
386
    else 
387
    {
388
      Serial.print("failed, rc=");
389
      Serial.print(client.state());
390
      Serial.println(" try again in 10 seconds");
391
      // Wait 10 seconds before retrying
392
      delay(10000);
393
    }
394
}
395
void loop() 
396
{
397
  if (WiFi.status() != WL_CONNECTED ) 
398
  { 
399
    Serial.println("setup_wifi\n");
400
    alloff();
401
    firstStart = 0;
402
    setup_wifi();
403
  }
404
  else if (!client.connected()) 
405
  {
406
    Serial.println("reconnect\n");
407
    alloff();
408
    firstStart = 0;
409
    reconnectoL();
410
  }
411
  else
412
  {
413
    if(firstStart < 50)
414
    {
415
      firstStart++;
416
    }
417
    else if(firstStart == 50)
418
    {
419
      alloff();
420
      firstStart++;
421
      Serial.println("alloff"); 
422
    }
423
    client.loop();
424
  }
425
   delay(10);
426
}