#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include "DHTesp.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
DHTesp dht;
const char* apiUrl = "https://panel-iot.peroro.web.id/api/temperature";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching " + String(apiUrl) + "... ");
dht.setup(15,DHTesp::DHT22);
}
void loop() {
sendHttpPost();
delay(100);
}
void sendHttpPost() {
HTTPClient http;
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
// Specify the destination URL
http.begin(apiUrl);
JSONVar objTemp;
objTemp["temperature"] = String(temperature);
String json = JSON.stringify(objTemp);
// Set the content type to JSON (adjust accordingly based on your API requirements)
http.addHeader("Content-Type", "application/json");
// Create a JSON payload (adjust this based on your API's expected data format)
String jsonData = json;
// Send the POST request with the JSON data
int httpResponseCode = http.POST(jsonData);
// Check the response
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Print the response payload
String payload = http.getString();
Serial.println(payload);
} else {
Serial.print("HTTP POST request failed, error code: ");
Serial.println(httpResponseCode);
}
// Close the connection
http.end();
}