#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 15 // ขา DHT22 ต่ออยู่ที่ GPIO15
#define DHTTYPE DHT22 // ใช้เซ็นเซอร์ประเภท DHT22
const char* ssid = "Your_SSID"; // ชื่อ WiFi
const char* password = "Your_PASSWORD"; // รหัสผ่าน WiFi
const char* scriptURL = "YOUR_APPS_SCRIPT_URL"; // URL จาก Apps Script
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
dht.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float temperature = dht.readTemperature(); // อ่านค่าอุณหภูมิ
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
HTTPClient http;
http.begin(scriptURL);
http.addHeader("Content-Type", "application/json");
// สร้าง JSON payload
String jsonPayload = "{\"temperature\":" + String(temperature) + "}";
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("WiFi disconnected");
}
delay(10000); // ส่งข้อมูลทุก 10 วินาที
}