Smart Dashboard System using ESP32 Terminal
Smart Dashboard System using ESP32 microcontrollers, designed to monitor and control environmental parameters in indoor spaces. The system comprises two ESP32 boards: one for sensing data and another for displaying the readings. The sensing microcontroller has various sensors, including the DHT11 for temperature and humidity, MQ135 for air quality, and MQ9 for CO2 levels. Additionally, a buzzer is used to signal changes and assist in light control. Data from these sensors is transmitted over Wi-Fi via the MQTT protocol to a cloud service, where it is processed and shared with the display module.

Project Overview
Smart Dashboard System for Indoor Environmental Monitoring Using ESP32
Project Overview
This project presents a smart dashboard system for real-time indoor environmental monitoring, utilizing dual ESP32 microcontrollers. The system continuously tracks key indoor conditions including temperature, humidity, air quality, and gas levels, displaying data on a dedicated graphical interface.
System Architecture and Operation
The system is implemented using a distributed two-module design:
Sensing Module
Components:
DHT11 sensor for temperature and humidity measurement
MQ135 sensor for air quality assessment
MQ9 sensor for CO₂ and gas level detection
Buzzer for environmental change alerts and feedback
Function: Collects environmental data and transmits it wirelessly via Wi-Fi using the MQTT protocol to a cloud-based service.
Display Module
Components:
ESP32 with integrated SPI display (CrowPanel ESP32 Terminal, 3.5-inch) or OLED display
Function: Receives processed sensor data from the cloud service and presents it through a custom-designed graphical user interface, updated in real time.
Hardware Requirements
ESP32 Development Module (×2)
CrowPanel ESP32 Terminal (3.5-inch SPI display) or OLED Display
DHT11 Temperature and Humidity Sensor
MQ135 Air Quality Sensor
MQ9 Gas Sensor
Buzzer
5V Power Supply or USB Adapter
Software and Development Tools
Arduino IDE for firmware development and programming
SquareLine Studio for designing and implementing the graphical user interface
MQTT Protocol for wireless data transmission
Project Objective
To develop a reliable, wireless monitoring system that provides comprehensive indoor environmental tracking for applications in smart homes, offices, and laboratories, emphasizing user-friendly data visualization.
Key Features
Distributed architecture with dedicated sensing and display modules
Real-time wireless data transmission using MQTT protocol
Comprehensive environmental parameter monitoring (temperature, humidity, air quality, gas levels)
Visual and auditory alerting mechanisms
Customizable graphical user interface for enhanced user experience
This system demonstrates the integration of embedded sensing, wireless communication, and graphical display technologies to create a practical solution for indoor environmental awareness and management.
Gallery

Circuit diagram

Sensor board
Code Examples
1#include <ArduinoJson.h> //JSON library
2
3// WIFI and MQTT library
4#include <PubSubClient.h>
5#include <WiFiClientSecure.h>
6#include <WiFiManager.h>
7
8// OLED
9#include <Wire.h>
10#include "SSD1306.h"
11
12// Temperature and Humidity Sensor
13#include "DHT.h"
14#define DHT11_PIN 33
15
16DHT dht11(DHT11_PIN, DHT11);
17
18// set i2c pins for OLED
19#define I2C_SDA 25
20#define I2C_SCL 26
21HW_I2C I2C_2 = I2C_TWO;
22// HW_I2C I2C_2 = I2C_ONE;
23OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64;
24SSD1306 display(0x3c, I2C_SDA, I2C_SCL, g, I2C_2);
25
26// bitmap image refer: https://github.com/ThingPulse/esp8266-oled-ssd1306/issues/53
27const uint8_t bitmap_Logo[] PROGMEM = {
28 0x00,
29 0x00,
30 0x00,
31 0x00,
32 0x00,
33 0x00,
34 0x00,
35 0x00,
36 0x00,
37 0x00,
38 0x00,
39 0x00,
40 0x00,
41 0x00,
42 0x00,
43 0x00,
44 0x00,
45 0x00,
46 0x00,
47 0x00,
48 0x00,
49 0x00,
50 0x00,
51 0x00,
52 0x00,
53 0x00,
54 0x00,
55 0x00,
56 0x00,
57 0x00,
58 0x00,
59 0x00,
60 0x00,
61 0x00,
62 0x00,
63 0x00,
64 0x00,
65 0x00,
66 0x00,
67 0x00,
68 0x00,
69 0x00,
70 0x00,
71 0x00,
72 0x00,
73 0x00,
74 0x00,
75 0x00,
76 0x00,
77 0x00,
78 0x00,
79 0x00,
80 0x00,
81 0x00,
82 0x00,
83 0x00,
84 0x00,
85 0x00,
86 0x00,
87 0x00,
88 0x00,
89 0x00,
90 0x00,
91 0x00,
92 0x00,
93 0x00,
94 0x00,
95 0x00,
96 0x00,
97 0x00,
98 0x00,
99 0x00,
100 0x00,
101 0x00,
102 0x00,
103 0x00,
104 0x00,
105 0x00,
106 0x00,
107 0x00,
108 0x00,
109 0x00,
110 0x00,
111 0x00,
112 0x00,
113 0x00,
114 0x00,
115 0x00,
116 0x00,
117 0x00,
118 0x00,
119 0x30,
120 0x00,
121 0x00,
122 0x00,
123 0x00,
124 0x00,
125 0x00,
126 0x00,
127 0x78,
128 0x00,
129 0x00,
130 0x00,
131 0x00,
132 0x00,
133 0x00,
134 0x00,
135 0xFC,
136 0x00,
137 0x00,
138 0x00,
139 0x00,
140 0x00,
141 0x00,
142 0x00,
143 0x7E,
144 0x00,
145 0x00,
146 0x00,
147 0x00,
148 0x00,
149 0x00,
150 0x00,
151 0x3F,
152 0xFF,
153 0xFF,
154 0x1F,
155 0x00,
156 0x00,
157 0x00,
158 0x80,
159 0x1F,
160 0xFF,
161 0xFF,
162 0x1F,
163 0x00,
164 0x00,
165 0x00,
166 0x80,
167 0x8F,
168 0xFF,
169 0xFF,
170 0x3F,
171 0x00,
172 0x00,
173 0x00,
174 0xC0,
175 0xCF,
176 0x03,
177 0x1F,
178 0x3C,
179 0x00,
180 0x00,
181 0x00,
182 0xE0,
183 0xE7,
184 0x01,
185 0x06,
186 0x3C,
187 0x00,
188 0x00,
189 0x00,
190 0xF0,
191 0xF3,
192 0x00,
193 0x06,
194 0x3C,
195 0x00,
196 0x00,
197 0x00,
198 0xF8,
199 0x79,
200 0x38,
201 0x06,
202 0x3C,
203 0x00,
204 0x00,
205 0x00,
206 0xFC,
207 0x3C,
208 0x3E,
209 0x06,
210 0x3C,
211 0x00,
212 0x00,
213 0x00,
214 0x7E,
215 0x3E,
216 0xFC,
217 0x07,
218 0x3C,
219 0x00,
220 0x00,
221 0x00,
222 0x3E,
223 0x1F,
224 0xFC,
225 0x03,
226 0x3C,
227 0x00,
228 0x00,
229 0x00,
230 0x1F,
231 0x0F,
232 0xFC,
233 0x01,
234 0x3C,
235 0x00,
236 0x00,
237 0x80,
238 0x8F,
239 0x07,
240 0xF8,
241 0x01,
242 0x3C,
243 0x00,
244 0x00,
245 0xC0,
246 0xC7,
247 0x03,
248 0xF8,
249 0x01,
250 0x3C,
251 0x00,
252 0x00,
253 0xE0,
254 0xE7,
255 0x01,
256 0xF8,
257 0x00,
258 0x3C,
259 0x00,
260 0x00,
261 0xF0,
262 0xF3,
263 0x00,
264 0xF0,
265 0x00,
266 0x3C,
267 0x00,
268 0x00,
269 0xF8,
270 0x79,
271 0x00,
272 0xF0,
273 0x01,
274 0x3C,
275 0x00,
276 0x00,
277 0xF8,
278 0x78,
279 0x00,
280 0xF0,
281 0x00,
282 0x3C,
283 0x00,
284 0x00,
285 0xF8,
286 0x79,
287 0x00,
288 0x20,
289 0x00,
290 0x3C,
291 0x00,
292 0x00,
293 0xF0,
294 0xF1,
295 0x00,
296 0x00,
297 0x00,
298 0x3C,
299 0x00,
300 0x00,
301 0xE0,
302 0xE3,
303 0x01,
304 0x00,
305 0x00,
306 0x3C,
307 0x00,
308 0x00,
309 0xC0,
310 0xE7,
311 0x03,
312 0x00,
313 0x00,
314 0x3C,
315 0x00,
316 0x00,
317 0xC0,
318 0xCF,
319 0x03,
320 0x00,
321 0x60,
322 0x3C,
323 0x00,
324 0x00,
325 0x80,
326 0x9F,
327 0x07,
328 0x00,
329 0xF0,
330 0x3C,
331 0x00,
332 0x00,
333 0x00,
334 0x3F,
335 0x0F,
336 0x00,
337 0xF0,
338 0x3C,
339 0x00,
340 0x00,
341 0x00,
342 0x7E,
343 0x1E,
344 0x00,
345 0xF0,
346 0x3C,
347 0x00,
348 0x00,
349 0x00,
350 0x7C,
351 0x3C,
352 0x00,
353 0xF0,
354 0x3C,
355 0x00,
356 0x00,
357 0x00,
358 0xF8,
359 0x78,
360 0x00,
361 0xF0,
362 0x3C,
363 0x00,
364 0x00,
365 0x00,
366 0xF0,
367 0xF9,
368 0x00,
369 0xF0,
370 0x3C,
371 0x00,
372 0x00,
373 0x00,
374 0xF0,
375 0xF3,
376 0x00,
377 0xF0,
378 0x3C,
379 0x00,
380 0x00,
381 0x00,
382 0xF8,
383 0xE7,
384 0x01,
385 0xF0,
386 0x3C,
387 0x00,
388 0x00,
389 0x00,
390 0xFC,
391 0xCF,
392 0x03,
393 0xF8,
394 0x3D,
395 0x00,
396 0x00,
397 0x00,
398 0xFC,
399 0x8F,
400 0xFF,
401 0xFF,
402 0x3F,
403 0x00,
404 0x00,
405 0x00,
406 0xFC,
407 0x1F,
408 0xFF,
409 0xFF,
410 0x3F,
411 0x00,
412 0x00,
413 0x00,
414 0xF8,
415 0x3F,
416 0xFF,
417 0xFF,
418 0x1F,
419 0x00,
420 0x00,
421 0x00,
422 0x00,
423 0x7E,
424 0x00,
425 0x00,
426 0x00,
427 0x00,
428 0x00,
429 0x00,
430 0x00,
431 0xFC,
432 0x00,
433 0x00,
434 0x00,
435 0x00,
436 0x00,
437 0x00,
438 0x00,
439 0x78,
440 0x00,
441 0x00,
442 0x00,
443 0x00,
444 0x00,
445 0x00,
446 0x00,
447 0x30,
448 0x00,
449 0x00,
450 0x00,
451 0x00,
452 0x00,
453 0x00,
454 0x00,
455 0x00,
456 0x00,
457 0x00,
458 0x00,
459 0x00,
460 0x00,
461 0x00,
462 0x00,
463 0x00,
464 0x00,
465 0x00,
466 0x00,
467 0x00,
468 0x00,
469 0x00,
470 0x00,
471 0x00,
472 0x00,
473 0x00,
474 0x00,
475 0x00,
476 0x00,
477 0x00,
478 0x00,
479 0x00,
480 0x00,
481 0x00,
482 0x00,
483 0x00,
484 0x00,
485 0x00,
486 0x00,
487 0x00,
488 0x00,
489 0x00,
490 0x00,
491 0x00,
492 0x00,
493 0x00,
494 0x00,
495 0x00,
496 0x00,
497 0x00,
498 0x00,
499 0x00,
500 0x00,
501 0x00,
502 0x00,
503 0x00,
504 0x00,
505 0x00,
506 0x00,
507 0x00,
508 0x00,
509 0x00,
510 0x00,
511 0x00,
512 0x00,
513 0x00,
514 0x00,
515 0x00,
516 0x00,
517 0x00,
518 0x00,
519 0x00,
520 0x00,
521 0x00,
522 0x00,
523 0x00,
524 0x00,
525 0x00,
526 0x00,
527 0x00,
528 0x00,
529 0x00,
530 0x00,
531 0x00,
532 0x00,
533 0x00,
534 0x00,
535 0x00,
536 0x00,
537 0x00,
538 0x00,
539 0x00,
540};
541
542// Gas sensor
543const int mq135 = 34;
544const int mq9 = 35;
545
546// buzzers
547const int buzzer = 14;
548
549// relay for fan
550const int relay_fan = 13;
551// relay for humidifier
552// const int relay_humidifier = 12;
553
554// WIFI button
555const int wifi_btn = 4;
556const int wifi_led = 18;
557
558//---- WiFi settings
559// const char* ssid = "bruh";
560// const char* password = "megabruh";
561
562// WIFI MANAGER
563const char *ssid_AP = "ESP32HomeAP";
564
565//---- HiveMQ Cloud Broker settings
566const char *mqtt_server = ""; // replace with your HiveMQ Cluster URL
567const char *mqtt_username = ""; // replace with your Username
568const char *mqtt_password = ""; // replace with your Password
569const int mqtt_port = 8883;
570
571WiFiClientSecure espClient;
572PubSubClient client(espClient);
573
574bool isOnline = false;
575
576unsigned long lastMsg = 0;
577unsigned long prevTime = 0;
578
579const char *temp_humi_topic = "temp_humi";
580const char *gas_topic = "gas_sensor";
581
582const char *buzzer_topic = "light1";
583
584// HiveMQ Cloud Let's Encrypt CA certificate
585static const char *root_ca PROGMEM = R"EOF(
586-----BEGIN CERTIFICATE-----
587MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
588TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
589cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
590WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
591ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
592MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
593h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
5940TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
595A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
596T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
597B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
598B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
599KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn
600OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn
601jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw
602qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI
603rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV
604HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq
605hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL
606ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ
6073BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK
608NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
609ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
610TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
611jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
612oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
6134RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
614mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
615emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
616-----END CERTIFICATE-----
617)EOF";
618
619// set wifi credentials
620void OnDemandWifi()
621{
622
623 display.clear();
624 display.drawString(0, 0, "Welcome to WiFi Manager");
625 display.drawString(0, 10, "1.Connect to WIFI: ");
626 display.drawString(0, 20, ssid_AP);
627 display.drawString(0, 30, "2.Open Your Browser at: ");
628 display.drawString(0, 40, "192.168.4.1");
629 display.drawString(0, 50, "3.Enter WIFI credentials: ");
630 display.display();
631
632 WiFiManager wm;
633
634 // reset settings - for testing
635 // wm.resetSettings();
636
637 int timeout = 120; // 120 seconds to run for ie 2 min
638 // set configportal timeout
639 // 2 min to connect to given wifi
640 wm.setConfigPortalTimeout(timeout);
641
642 if (!wm.autoConnect("HomeSafetyAP", "password"))
643 {
644 Serial.println("failed to connect and hit timeout");
645 display.clear();
646 display.drawString(0, 0, "Failed to Connect");
647 display.drawString(0, 20, "Offline mode");
648 display.display();
649 delay(3000);
650 isOnline = false;
651 delay(5000);
652 }
653 else
654 {
655 // connected to the WiFi
656 Serial.println("connected to wifi");
657 isOnline = true;
658 digitalWrite(wifi_led, HIGH);
659
660 Serial.println("");
661 Serial.println("WiFi connected");
662 display.clear();
663 display.drawString(0, 0, "WiFi connected");
664 display.display();
665
666 Serial.print("ESP32-CAM IP Address: ");
667 Serial.println(WiFi.localIP());
668 display.drawString(0, 10, "ESP32-CAM IP Address: ");
669 display.drawString(0, 20, String(WiFi.localIP()));
670 display.display();
671 delay(2000);
672
673 display.clear();
674 display.drawString(0, 0, "Connecting to MQTT Server: ");
675 display.display();
676
677 espClient.setCACert(root_ca);
678 client.setServer(mqtt_server, mqtt_port);
679 client.setCallback(callback);
680 }
681}
682
683void callback(char *topic, byte *payload, unsigned int length)
684{
685 Serial.print("Message arrived: ");
686 String recievedMsg = "";
687 for (int i = 0; i < length; i++)
688 {
689 recievedMsg += (char)payload[i];
690 }
691 if (strcmp(topic, buzzer_topic) == 0)
692 {
693 if (recievedMsg == "on")
694 {
695 digitalWrite(buzzer, HIGH);
696 }
697 else if (recievedMsg == "off")
698 {
699 digitalWrite(buzzer, LOW);
700 }
701 }
702 Serial.println(recievedMsg);
703}
704
705void reconnect()
706{
707 // Loop until we’re reconnected
708 int attempt = 0;
709 int max_attempt = 10;
710 while (!client.connected())
711 {
712 Serial.print("Attempting MQTT connection… ");
713 String clientId = "ESP32Client4563274";
714 attempt++;
715
716 // Attempt to connect
717 if (client.connect(clientId.c_str(), mqtt_username, mqtt_password))
718 {
719 Serial.println("connected!");
720 isOnline = true;
721 digitalWrite(wifi_led, HIGH);
722 display.drawString(0, 20, "MQTT connected");
723 display.display();
724
725 client.subscribe(buzzer_topic);
726 // client.subscribe("testTopic2");
727 }
728 else
729 {
730 Serial.print("failed, rc = ");
731 Serial.print(client.state());
732 Serial.println(" try again in 1 seconds");
733 isOnline = false;
734 digitalWrite(wifi_led, LOW);
735 display.clear();
736 display.drawString(0, 10, "MQTT Retry: " + String(attempt) + " / " + String(max_attempt));
737 display.drawString(0, 20, "MQTT Failed");
738 display.display();
739 if (attempt >= max_attempt)
740 {
741 break;
742 }
743 // Wait
744 // Wait 1 seconds before retrying
745 delay(1000);
746 }
747 }
748}
749
750float round_to_dp(float in_value, int decimal_place)
751{
752 float multiplier = powf(10.0f, decimal_place);
753 in_value = roundf(in_value * multiplier) / multiplier;
754 return in_value;
755}
756
757void getTempHumiValue()
758{
759 float humi = dht11.readHumidity();
760 float tempC = dht11.readTemperature();
761
762 JsonDocument doc;
763 doc["sensor"] = "dht11";
764 doc["temperature"] = String(tempC);
765 doc["humidity"] = humi;
766
767 char JSONdata[200];
768 serializeJson(doc, JSONdata);
769 Serial.println(JSONdata);
770
771 display.drawString(65, 30, "Temp: " + String(tempC));
772 display.drawString(65, 40, "Humi: " + String(humi));
773
774 // publish only when online (wifi and mqtt is connected)
775 if (isOnline)
776 {
777 client.publish(temp_humi_topic, JSONdata);
778 }
779}
780
781int getMQValues(int sensor)
782{
783 int raw_val[10]; // int array for gas readings
784 int ppm = 0; // int for calculated ppm
785 int sum = 0; // int for averaging
786
787 for (int i = 0; i < 10; i++) // get values 10x over 1 seconds
788 {
789 raw_val[i] = analogRead(sensor);
790 delay(100);
791 }
792
793 for (int i = 0; i < 10; i++) // add samples together
794 {
795 sum += raw_val[i];
796 }
797
798 ppm = sum / 10; // divide samples by 10
799 return ppm;
800}
801
802void getGasValue()
803{
804
805 /*
806 * Atmospheric CO2 Level..............400ppm
807 * Average indoor co2.............350-450ppm
808 * Maxiumum acceptable co2...........1000ppm
809 * Dangerous co2 levels.............>2000ppm
810 */
811
812 int mq135_val = getMQValues(mq135);
813 int mq9_val = getMQValues(mq9);
814
815 JsonDocument doc;
816 doc["sensor"] = "gas sensor";
817 doc["mq135"] = mq135_val;
818 doc["mq9"] = mq9_val;
819
820 char JSONdata[200];
821 serializeJson(doc, JSONdata);
822 Serial.println(JSONdata);
823
824 display.drawString(65, 10, "MQ135: " + String(mq135_val));
825 display.drawString(65, 20, "MQ9: " + String(mq9_val));
826
827 // publish only when online
828 if (isOnline)
829 {
830 client.publish(gas_topic, JSONdata);
831 }
832}
833
834void setup()
835{
836 delay(500);
837 // When opening the Serial Monitor, select 9600 Baud
838 Serial.begin(115200);
839 dht11.begin(); // initialize DHT11 sensor
840
841 Wire1.begin(I2C_SDA, I2C_SCL);
842 display.init(); // INITIALIZE OLED
843
844 // INIT actuators
845 pinMode(buzzer, OUTPUT);
846 pinMode(relay_fan, OUTPUT);
847
848 pinMode(wifi_btn, INPUT_PULLUP);
849 pinMode(wifi_led, OUTPUT);
850
851 display.clear();
852 display.drawString(0, 0, "Starting....");
853 display.display();
854 delay(2000);
855
856 OnDemandWifi();
857}
858
859void loop()
860{
861 if (isOnline)
862 {
863 if (!client.connected())
864 {
865 isOnline = false;
866 reconnect();
867 }
868 client.loop();
869 }
870
871 if (!isOnline)
872 {
873 unsigned long currentTime = millis();
874 if (currentTime - prevTime > (30 * 60 * 1000))
875 { // try to connect to wifi again every 30 min if wifi is lost
876 prevTime = currentTime;
877 OnDemandWifi();
878 }
879 }
880
881 if (digitalRead(wifi_btn) == LOW)
882 {
883
884 OnDemandWifi();
885 }
886 unsigned long now = millis();
887 if (now - lastMsg > 10000)
888 {
889 lastMsg = now;
890
891 display.clear();
892 if (isOnline)
893 {
894 display.drawString(65, 0, "Online Mode");
895 }
896 else
897 {
898 display.drawString(65, 0, "Offline Mode");
899 }
900 // display logo
901 display.drawFastImage(0, 0, 64, 64, bitmap_Logo);
902
903 getTempHumiValue();
904 getGasValue();
905
906 display.display();
907 }
908}