Skip to content

Commit be2ec89

Browse files
committed
write in C++ for better IDE support
1 parent 3bbe29a commit be2ec89

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

.gitignore

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

src/main.ino renamed to src/main.cpp

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@version 3.0
66
*/
77

8+
#include <Arduino.h>
89
#include <dht11.h>
910
#include <LiquidCrystal.h>
1011
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson (use v6.xx)
@@ -21,6 +22,49 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
2122
// ESP RX => Uno Pin 3
2223
SoftwareSerial wifi(2, 3);
2324

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+
2468
void setup() {
2569
Serial.begin(9600);
2670
wifi.begin(9600);
@@ -84,41 +128,4 @@ void loop() {
84128
sendDataToWiFi(preparedData, 1000, DEBUG);
85129

86130
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;
124131
}

0 commit comments

Comments
 (0)