#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "bryan";
const char* password = "bryannnn";
// LINE Notify token (replace this with your LINE Notify access token)
const String LINE_NOTIFY_TOKEN = "IJ3zhFQ4CSL18ycriiGBYthx3tIoS7cJaz1jjFPcPXh";
// Create DHT sensor object
DHTesp dhtSensor; // Corrected this line
// Setup Wi-Fi
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Setup DHT sensor
dhtSensor.setup(13, DHTesp::DHT22); // Connect the DHT22 sensor to pin 13
}
void loop() {
// Read sensor data
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Print temperature and humidity to Serial Monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
// Send data to LINE Notify
sendToLineNotify(data.temperature, data.humidity);
// Wait 1 second before next reading
delay(1000);
}
// Function to send data to LINE Notify
void sendToLineNotify(float temperature, float humidity) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + LINE_NOTIFY_TOKEN);
String message = "Temperature: " + String(temperature, 2) + "°C\n" +
"Humidity: " + String(humidity, 1) + "%";
String body = "message=" + message;
int httpCode = http.POST(body);
if (httpCode == HTTP_CODE_OK) {
String response = http.getString();
Serial.println("LINE Notify response: " + response);
} else {
Serial.println("Error sending message: " + String(httpCode));
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}