//const char* serverName = "http://www.ioty.pl/JSON.php";
//const char* serverName = "http://www.iot.atthost24.pl/IoT_DevGET.php";
//String serverName = "http://www.ioty.pl/JSON.php";
//sensorReadings[10] ='{"DEV":"bfd7be67fa52a9cde4hnpa","TS":"1700509501","PV":"19.50","SP":"19.50","STATE":"1"}';
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-open-weather-map-thingspeak-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Your Domain name with URL path or IP address with path
String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd435";
// Example:
//String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd435";
// Replace with your country code and city
String city = "Porto";
String countryCode = "PT";
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 10000;
String jsonBuffer;
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 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
}
void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()!= WL_CONNECTED){
//String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
String serverPath = "http://www.iot.atthost24.pl/IoT_DevGET.php";
// jsonBuffer = httpGETRequest(serverPath.c_str());
jsonBuffer ='[{"DEV":"bfd7be67fa52a9cde4hnpa","TS":"1700655002","PV":"19.00","SP":"19.50","STATE":"1"},{"DEV":"bf02ae968c75ce66b3xd9j","TS":"1700655002","PV":"20.50","SP":"21.00","STATE":"1"},{"DEV":"bffd97c53aa063d72au0lw","TS":"1700655002","PV":"19.00","SP":"20.50","STATE":"1"},{"DEV":"bf3123d5faf951a6619h6y","TS":"1700655002","PV":"20.50","SP":"21.50","STATE":"1"},{"DEV":"bfbf248dea74604ca4ild0","TS":"1700655002","PV":"21.00","SP":"21.00","STATE":"1"},{"DEV":"bf85c1b70c6fb02341nfp4","TS":"1700655002","PV":"20.00","SP":"21.50","STATE":"1"},{"DEV":"bfc636be39b86bdc04f93k","TS":"1700655001","PV":"20.00","SP":"20.50","STATE":"1"},{"DEV":"bf70c0f4f1e78d58e5vory","TS":"1700655001","PV":"20.00","SP":"21.00","STATE":"1"},{"DEV":"bfcf8726cf9cd7f7d2qgn7","TS":"1700655001","PV":"20.50","SP":"19.00","STATE":"1"},{"DEV":"bf6d158eda2f5c62c2eu3x","TS":"1700655001","PV":"20.00","SP":"21.00","STATE":"1"},{"DEV":"bf013794a0aaba46c8p3xa","TS":"1700655001","PV":"19.50","SP":"19.00","STATE":"1"},{"DEV":"bf97311bdcdeff8379egrg","TS":"1700655001","PV":"20.50","SP":"19.00","STATE":"1"},{"DEV":"bf780a0671340585f0zb4l","TS":"1700655001","PV":"20.00","SP":"21.00","STATE":"1"}]';
//Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
delay(10000);
// 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);
Serial.print("Object1: ");
Serial.println(myObject[0]);
/*
Serial.print("Pressure: ");
Serial.println(myObject["main"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["main"]["humidity"]);
Serial.print("Wind Speed: ");
Serial.println(myObject["wind"]["speed"]);
*/
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}