#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.begin(115200);
delay(4000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float suhu = data.temperature;
float kelembaban = data.humidity;
// if (!isnan(suhu) && !isnan(kelembaban)) {
// Serial.println("Temp: " + String(suhu, 2) + "°C");
// Serial.println("Humidity: " + String(kelembaban, 1) + "%");
// }
//Serial.println("---");
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin("https://webminar-iot.smdev-staging.web.id/api/receivers/sensor"); //Specify destination for HTTP request
http.addHeader("appid", "TDH4CT6V");
http.addHeader("appsecret", "IUUXDP3UM18VVLNH");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (!isnan(suhu) && !isnan(kelembaban)) {
String httpRequestData = "suhu=" + String(data.temperature, 2) + "&kelembaban=" + String(data.humidity, 1);
int httpResponseCode = http.POST(httpRequestData);
if(httpResponseCode>0){
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
}else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}else{
Serial.println("Error in WiFi connection");
}
}
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}