/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
}
void send_data_to_api(float celsius, float humidity, float kelvin) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
return;
}
HTTPClient http;
String url = "https://patcharaphonapi.aimaccount.com/upload";
url += "?celsius=" + String(celsius);
url += "&fahrenheit=" + String(humidity);
url += "&kelvin=" + String(kelvin);
//https://patcharaphonapi.aimaccount.com/upload?celsius=25.5&fahrenheit=77.9&kelvin=298.7
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Server Response: " + payload);
} else {
Serial.println("HTTP Error: " + String(httpCode));
}
http.end();
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// ตรวจสอบค่าที่อ่านได้
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("Failed to read from DHT sensor!");
delay(2000);
return;
}
// คำนวณหน่วยอุณหภูมิ
float fahrenheit = (data.temperature * 9.0/5.0) + 32;
float kelvin = data.temperature + 273.15;
// แสดงผลบน Serial Monitor
Serial.println("Temperature: " + String(data.temperature, 1) + "°C");
Serial.println("Fahrenheit: " + String(fahrenheit, 1) + "°F");
Serial.println("Kelvin: " + String(kelvin, 1) + "K");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("----------------------------");
// ส่งข้อมูลไปยัง API
send_data_to_api(data.temperature, data.humidity);
delay(30000); // หน่วงเวลา 30 วินาที
}