Created
May 25, 2023 01:18
-
-
Save almcarvalho/766e5a8ae2ea89836874ece6ba738da2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClient.h> | |
#include <Arduino_JSON.h> | |
const char* ssid = "REPLACE_WITH_YOUR_SSID"; | |
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; | |
//Your Domain name with URL path or IP address with path | |
const char* serverName = ""https://fit2sell.herokuapp.com/rafael""; | |
// the following variables are unsigned longs because the time, measured in | |
// milliseconds, will quickly become a bigger number than can be stored in an int. | |
unsigned long lastTime = 0; | |
// Timer set to 10 minutes (600000) | |
//unsigned long timerDelay = 600000; | |
// Set timer to 5 seconds (5000) | |
unsigned long timerDelay = 5000; | |
String sensorReadings; | |
float sensorReadingsArr[3]; | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.println("Connecting"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to WiFi network with IP Address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading."); | |
} | |
void loop() { | |
// Send an HTTP POST request depending on timerDelay | |
if ((millis() - lastTime) > timerDelay) { | |
//Check WiFi connection status | |
if(WiFi.status()== WL_CONNECTED){ | |
sensorReadings = httpGETRequest(serverName); | |
Serial.println(sensorReadings); | |
JSONVar myObject = JSON.parse(sensorReadings); | |
// JSON.typeof(jsonVar) can be used to get the type of the var | |
if (JSON.typeof(myObject) == "undefined") { | |
Serial.println("Parsing input failed!"); | |
return; | |
} | |
Serial.print("JSON object = "); | |
Serial.println(myObject); | |
// myObject.keys() can be used to get an array of all the keys in the object | |
JSONVar keys = myObject.keys(); | |
for (int i = 0; i < keys.length(); i++) { | |
JSONVar value = myObject[keys[i]]; | |
Serial.print(keys[i]); | |
Serial.print(" = "); | |
Serial.println(value); | |
sensorReadingsArr[i] = double(value); | |
} | |
Serial.print("1 = "); | |
Serial.println(sensorReadingsArr[0]); | |
Serial.print("2 = "); | |
Serial.println(sensorReadingsArr[1]); | |
Serial.print("3 = "); | |
Serial.println(sensorReadingsArr[2]); | |
} | |
else { | |
Serial.println("WiFi Disconnected"); | |
} | |
lastTime = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment