Skip to content

Commit

Permalink
write in C++ for better IDE support
Browse files Browse the repository at this point in the history
  • Loading branch information
isbkch committed Sep 25, 2020
1 parent 3bbe29a commit be2ec89
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/ipch
.DS_Store
81 changes: 44 additions & 37 deletions src/main.ino → src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@version 3.0
*/

#include <Arduino.h>
#include <dht11.h>
#include <LiquidCrystal.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson (use v6.xx)
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

0 comments on commit be2ec89

Please sign in to comment.