Skip to content

Commit ac62eed

Browse files
authored
Merge pull request #1 from MecaHumArduino/wifi
Wifi
2 parents b262d30 + c12570e commit ac62eed

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
.vscode/.browse.c_cpp.db*
33
.vscode/c_cpp_properties.json
44
.vscode/launch.json
5-
.vscode/ipch
5+
.vscode/ipch

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ board = uno
1414
framework = arduino
1515
lib_extra_dirs = ~/Documents/Arduino/libraries
1616
monitor_speed = 9600
17+
serial_port = /dev/cu.usbserial-A50285BI

src/main.ino

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#include <dht11.h>
22
#include <LiquidCrystal.h>
3+
#include <SoftwareSerial.h>
34

4-
#define DHT11PIN 3
5+
#define DEBUG true
6+
#define DHT11PIN 4
57
dht11 sensor;
68

79
// initialize the library with the numbers of the interface pins
810
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
911

12+
// ESP TX => Uno Pin 2
13+
// ESP RX => Uno Pin 3
14+
SoftwareSerial wifi(2, 3);
15+
1016
void setup() {
1117
Serial.begin(9600);
18+
wifi.begin(9600);
1219

1320
// set up the LCD's number of columns and rows
1421
lcd.begin(16, 2);
@@ -29,26 +36,70 @@ void setup() {
2936

3037
void loop() {
3138

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+
3255
// 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;
3459

3560
// display Hum on the LCD screen
3661
lcd.setCursor(0, 0);
3762
lcd.print("Humidity (%): ");
38-
lcd.print((float)sensor.humidity, 2);
63+
lcd.print(humidity, 2);
3964

4065
// display Temp on the LCD screen
4166
lcd.setCursor(0, 1);
4267
lcd.print("Temp (C): ");
43-
lcd.print((float)sensor.temperature, 2);
68+
lcd.print(temperature, 2);
4469

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 + "\"} ";
4871

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);
5282

5383
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;
54105
}

0 commit comments

Comments
 (0)