Skip to content

Commit 566b291

Browse files
committed
exchanging data between the boards
1 parent 69727a9 commit 566b291

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

src/main.ino

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
#include <LiquidCrystal.h>
33
#include <SoftwareSerial.h>
44

5+
#define DEBUG true
56
#define DHT11PIN 4
67
dht11 sensor;
78

89
// initialize the library with the numbers of the interface pins
910
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
10-
SoftwareSerial WiFiBoard(2, 3);
11+
SoftwareSerial wifi(2, 3);
1112

1213
void setup() {
1314
Serial.begin(9600);
14-
WiFiBoard.begin(9600);
15+
wifi.begin(9600);
1516

1617
// set up the LCD's number of columns and rows
1718
lcd.begin(16, 2);
@@ -31,10 +32,22 @@ void setup() {
3132
}
3233

3334
void loop() {
34-
// Serial.println("checking ESP responses");
35-
while (WiFiBoard.available()) {
36-
Serial.println(WiFiBoard.read());
35+
36+
Serial.print("buffer: ");
37+
if (wifi.available()) {
38+
String espBuf;
39+
long int time = millis();
40+
41+
while( (time+1000) > millis()) {
42+
while (wifi.available()) {
43+
// The esp has data so display its output to the serial window
44+
char c = wifi.read(); // read the next character.
45+
espBuf += c;
46+
}
47+
}
48+
Serial.print(espBuf);
3749
}
50+
Serial.println(" endbuffer");
3851

3952
// read from the digital pin
4053
int check = sensor.read(DHT11PIN);
@@ -51,7 +64,39 @@ void loop() {
5164
lcd.print("Temp (C): ");
5265
lcd.print(temperature, 2);
5366

54-
Serial.print("{\"Humidity\":\"" + (String)humidity + "\", \"Temperature\":\"" + (String)temperature + "\"}");
67+
// const char DATA_TO_SEND[] = "{\"Humidity\":\"" + (String)humidity + "\", \"Temperature\":\"" + (String)temperature + "\"} ";
68+
69+
70+
String sensorData = "{\"Humidity\":\"";
71+
sensorData += (String)humidity;
72+
sensorData += "\", \"Temperature\":\"";
73+
sensorData += (String)temperature;
74+
sensorData += "\"}";
75+
sensorData += "\r\n";
76+
77+
Serial.println(sensorData);
78+
sendDataToWiFi(sensorData, 1000, DEBUG);
5579

5680
delay(2000); // take measurements every 2 sec
81+
}
82+
83+
String sendDataToWiFi(String command, const int timeout, boolean debug)
84+
{
85+
String response = "";
86+
87+
wifi.print(command); // send the read character to the esp8266
88+
89+
long int time = millis();
90+
91+
while((time+timeout) > millis()) {
92+
while(wifi.available()) {
93+
// The esp has data so display its output to the serial window
94+
char c = wifi.read(); // read the next character.
95+
response+=c;
96+
}
97+
}
98+
99+
Serial.print(response);
100+
101+
return response;
57102
}

0 commit comments

Comments
 (0)