#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);
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(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Ganti "localhost" dengan alamat IP server Laravel atau ganti dengan alamat IP sesuai kebutuhan
String url = "http://localhost:8000/esp/" + String(suhu, 2) + "/" + String(kelembaban, 1);
Serial.print("Sending GET request to: ");
Serial.println(url);
http.begin(url); // Set the URL
int httpResponseCode = http.GET();
if(httpResponseCode == HTTP_CODE_OK){
String response = http.getString();
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.print("Error on sending GET. HTTP Response Code: ");
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)
}