1
1
#include < dht11.h>
2
2
#include < LiquidCrystal.h>
3
+ #include < SoftwareSerial.h>
3
4
4
- #define DHT11PIN 3
5
+ #define DEBUG true
6
+ #define DHT11PIN 4
5
7
dht11 sensor;
6
8
7
9
// initialize the library with the numbers of the interface pins
8
10
LiquidCrystal lcd (7 , 8 , 9 , 10 , 11 , 12 );
9
11
12
+ // ESP TX => Uno Pin 2
13
+ // ESP RX => Uno Pin 3
14
+ SoftwareSerial wifi (2 , 3 );
15
+
10
16
void setup () {
11
17
Serial.begin (9600 );
18
+ wifi.begin (9600 );
12
19
13
20
// set up the LCD's number of columns and rows
14
21
lcd.begin (16 , 2 );
@@ -29,26 +36,70 @@ void setup() {
29
36
30
37
void loop () {
31
38
39
+ Serial.print (" buffer: " );
40
+ if (wifi.available ()) {
41
+ String espBuf;
42
+ long int time = millis ();
43
+
44
+ while ( (time+1000 ) > millis ()) {
45
+ while (wifi.available ()) {
46
+ // The esp has data so display its output to the serial window
47
+ char c = wifi.read (); // read the next character.
48
+ espBuf += c;
49
+ }
50
+ }
51
+ Serial.print (espBuf);
52
+ }
53
+ Serial.println (" endbuffer" );
54
+
32
55
// read from the digital pin
33
- int check = sensor.read (DHT11PIN);
56
+ int check = sensor.read (DHT11PIN);
57
+ float temperature = (float )sensor.temperature ;
58
+ float humidity = (float )sensor.humidity ;
34
59
35
60
// display Hum on the LCD screen
36
61
lcd.setCursor (0 , 0 );
37
62
lcd.print (" Humidity (%): " );
38
- lcd.print (( float )sensor. humidity , 2 );
63
+ lcd.print (humidity, 2 );
39
64
40
65
// display Temp on the LCD screen
41
66
lcd.setCursor (0 , 1 );
42
67
lcd.print (" Temp (C): " );
43
- lcd.print (( float )sensor. temperature , 2 );
68
+ lcd.print (temperature, 2 );
44
69
45
- // print Hum on the serial monitor
46
- Serial.print (" Humidity (%): " );
47
- Serial.println ((float )sensor.humidity , 2 );
70
+ // const char DATA_TO_SEND[] = "{\"Humidity\":\"" + (String)humidity + "\", \"Temperature\":\"" + (String)temperature + "\"} ";
48
71
49
- // print Temp on the serial monitor
50
- Serial.print (" Temperature (C): " );
51
- Serial.println ((float )sensor.temperature , 2 );
72
+
73
+ String sensorData = " {\" Humidity\" :\" " ;
74
+ sensorData += (String)humidity;
75
+ sensorData += " \" , \" Temperature\" :\" " ;
76
+ sensorData += (String)temperature;
77
+ sensorData += " \" }" ;
78
+ sensorData += " \r\n " ;
79
+
80
+ Serial.println (sensorData);
81
+ sendDataToWiFi (sensorData, 1000 , DEBUG);
52
82
53
83
delay (2000 ); // take measurements every 2 sec
84
+ }
85
+
86
+ String sendDataToWiFi (String command, const int timeout, boolean debug)
87
+ {
88
+ String response = " " ;
89
+
90
+ wifi.print (command); // send the read character to the esp8266
91
+
92
+ long int time = millis ();
93
+
94
+ while ((time+timeout) > millis ()) {
95
+ while (wifi.available ()) {
96
+ // The esp has data so display its output to the serial window
97
+ char c = wifi.read (); // read the next character.
98
+ response+=c;
99
+ }
100
+ }
101
+
102
+ Serial.print (response);
103
+
104
+ return response;
54
105
}
0 commit comments