#include<WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
String Lokasi = "";
String suhu = "";
String lembab = "";
unsigned const long interval = 2000;
unsigned long zero = 0;
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED){
delay(100);
Serial.println(".");
}
Serial.println("WiFi Connected!");
Serial.println(WiFi.localIP());
}
void PostCuaca() {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin("https://echo.free.beeceptor.com");
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST
String Data = "{\"Lokasi\":\"" + Lokasi + "\", \"suhu\":\"" + suhu + "\", \"kelembaban\":\"" + lembab + "\"}";
// Send HTTP POST request
int httpResponseCode = http.POST(Data);
if (httpResponseCode > 0) {
String postResponse = http.getString();
Serial.println(httpResponseCode);
Serial.println("Response from server: " + postResponse);
} else {
Serial.print("Error sending POST request");
Serial.println(httpResponseCode);
}
http.end();
}
void loop(){
if(millis()-zero > interval){
HTTPClient http;
http.begin("https://api.bmkg.go.id/publik/prakiraan-cuaca?adm4=35.73.05.1007");
int httpResponCode = http.GET();
Serial.println(httpResponCode);
if(httpResponCode > 0){
String payload = http.getString();
//Serial.print(payload);
JSONVar myObject = JSON.parse(payload);
// Serial.print("JSON object = ");
//Serial.println(myObject);
Lokasi = String((const char*)myObject["lokasi"]["desa"]);
suhu = String((int)myObject["data"][0]["cuaca"][0][0]["t"]);
lembab = String((int)myObject["data"][0]["cuaca"][0][0]["hu"]);
Serial.print("Lokasi: ");
Serial.println(Lokasi);
Serial.println(suhu);
Serial.println(lembab);
PostCuaca();
}else{
Serial.print("error ");
Serial.println(httpResponCode);
}
zero = millis();
}
}