diff --git a/.gitignore b/.gitignore index 69107a2..4de822f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .vscode/.browse.c_cpp.db* .vscode/c_cpp_properties.json .vscode/launch.json -.vscode/ipch \ No newline at end of file +.vscode/ipch +.DS_Store diff --git a/src/main.ino b/src/main.cpp similarity index 90% rename from src/main.ino rename to src/main.cpp index 374c0fb..a258ac2 100644 --- a/src/main.ino +++ b/src/main.cpp @@ -5,6 +5,7 @@ @version 3.0 */ +#include #include #include #include //https://github.com/bblanchon/ArduinoJson (use v6.xx) @@ -21,6 +22,49 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // ESP RX => Uno Pin 3 SoftwareSerial wifi(2, 3); +// declaring custom function to follow C++ validation rules +String prepareDataForWiFi(float humidity, float temperature); +String sendDataToWiFi(String command, const int timeout, boolean debug); +String sendDataToWiFi(String command, const int timeout, boolean debug); + + +String prepareDataForWiFi(float humidity, float temperature) +{ + + StaticJsonDocument<200> doc; + + doc["humidity"] = (String)humidity; + doc["temperature"] = (String)temperature; + + char jsonBuffer[512]; + serializeJson(doc, jsonBuffer); + + return jsonBuffer; +} + +String sendDataToWiFi(String command, const int timeout, boolean debug) +{ + String response = ""; + + wifi.print(command); // send the read character to the esp8266 + + long int time = millis(); + + while((time+timeout) > millis()) { + while(wifi.available()) { + // The esp has data so display its output to the serial window + char c = wifi.read(); // read the next character. + response+=c; + } + } + + if (debug) { + Serial.print(response); + } + + return response; +} + void setup() { Serial.begin(9600); wifi.begin(9600); @@ -84,41 +128,4 @@ void loop() { sendDataToWiFi(preparedData, 1000, DEBUG); delay(2000); // take measurements every 2 sec -} - -String prepareDataForWiFi(float humidity, float temperature) -{ - - StaticJsonDocument<200> doc; - - doc["humidity"] = (String)humidity; - doc["temperature"] = (String)temperature; - - char jsonBuffer[512]; - serializeJson(doc, jsonBuffer); - - return jsonBuffer; -} - -String sendDataToWiFi(String command, const int timeout, boolean debug) -{ - String response = ""; - - wifi.print(command); // send the read character to the esp8266 - - long int time = millis(); - - while((time+timeout) > millis()) { - while(wifi.available()) { - // The esp has data so display its output to the serial window - char c = wifi.read(); // read the next character. - response+=c; - } - } - - if (debug) { - Serial.print(response); - } - - return response; } \ No newline at end of file