5
5
@version 3.0
6
6
*/
7
7
8
+ #include < Arduino.h>
8
9
#include < dht11.h>
9
10
#include < LiquidCrystal.h>
10
11
#include < ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
@@ -21,6 +22,49 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
21
22
// ESP RX => Uno Pin 3
22
23
SoftwareSerial wifi (2 , 3 );
23
24
25
+ // declaring custom function to follow C++ validation rules
26
+ String prepareDataForWiFi (float humidity, float temperature);
27
+ String sendDataToWiFi (String command, const int timeout, boolean debug);
28
+ String sendDataToWiFi (String command, const int timeout, boolean debug);
29
+
30
+
31
+ String prepareDataForWiFi (float humidity, float temperature)
32
+ {
33
+
34
+ StaticJsonDocument<200 > doc;
35
+
36
+ doc[" humidity" ] = (String)humidity;
37
+ doc[" temperature" ] = (String)temperature;
38
+
39
+ char jsonBuffer[512 ];
40
+ serializeJson (doc, jsonBuffer);
41
+
42
+ return jsonBuffer;
43
+ }
44
+
45
+ String sendDataToWiFi (String command, const int timeout, boolean debug)
46
+ {
47
+ String response = " " ;
48
+
49
+ wifi.print (command); // send the read character to the esp8266
50
+
51
+ long int time = millis ();
52
+
53
+ while ((time+timeout) > millis ()) {
54
+ while (wifi.available ()) {
55
+ // The esp has data so display its output to the serial window
56
+ char c = wifi.read (); // read the next character.
57
+ response+=c;
58
+ }
59
+ }
60
+
61
+ if (debug) {
62
+ Serial.print (response);
63
+ }
64
+
65
+ return response;
66
+ }
67
+
24
68
void setup () {
25
69
Serial.begin (9600 );
26
70
wifi.begin (9600 );
@@ -84,41 +128,4 @@ void loop() {
84
128
sendDataToWiFi (preparedData, 1000 , DEBUG);
85
129
86
130
delay (2000 ); // take measurements every 2 sec
87
- }
88
-
89
- String prepareDataForWiFi (float humidity, float temperature)
90
- {
91
-
92
- StaticJsonDocument<200 > doc;
93
-
94
- doc[" humidity" ] = (String)humidity;
95
- doc[" temperature" ] = (String)temperature;
96
-
97
- char jsonBuffer[512 ];
98
- serializeJson (doc, jsonBuffer);
99
-
100
- return jsonBuffer;
101
- }
102
-
103
- String sendDataToWiFi (String command, const int timeout, boolean debug)
104
- {
105
- String response = " " ;
106
-
107
- wifi.print (command); // send the read character to the esp8266
108
-
109
- long int time = millis ();
110
-
111
- while ((time+timeout) > millis ()) {
112
- while (wifi.available ()) {
113
- // The esp has data so display its output to the serial window
114
- char c = wifi.read (); // read the next character.
115
- response+=c;
116
- }
117
- }
118
-
119
- if (debug) {
120
- Serial.print (response);
121
- }
122
-
123
- return response;
124
131
}
0 commit comments