#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
//SDA 21, SCL22
const int DHT_PIN = 15; // DHT22 sensor GPIO Pin
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFI Password
const int myChannelNumber = 2973878 ; // ThingSpeak channel number
const char* myApiKey = "TP7Z4X6M4QD6IIPB"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
float suhu;
//Menuliskan alamat I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
//deklarasi waktu untuk reconnect wifi
unsigned long previousMillis = 0;
const unsigned long interval = 10000; // 10 detik
// Create an instance of the DHTesp library
DHTesp dhtSensor;
// Create a WiFi client object
WiFiClient client;
void setup() {
// Initialize the serial communication at a baud rate of 115200
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Initialize the DHT22 sensor
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); // Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected"); // Print a message if WiFi is not connected
}
Serial.println("Wifi connected !"); // Print a message if WiFi is connected
Serial.println("Local IP: " + String(WiFi.localIP())); // Print the local IP address
WiFi.mode(WIFI_STA); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
}
void reconnect() { //buat fungsi untuk reconnect wifi secara otomatis
unsigned long currentMillis = millis();
// Jika tidak terkoneksi WiFi dan sudah lewat interval
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
Serial.print(millis());
Serial.println(" Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
previousMillis = currentMillis;
}
}
void loop() {
// Read temperature and humidity from the DHT22 sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
suhu = data.temperature;
//print data ke serial monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C"); // Print the temperature value with 2 decimal places
Serial.println("Humidity: " + String(data.humidity, 1) + "%"); // Print the humidity value with 1 decimal place
//print suhu ke LCD
lcd.setCursor(0,0);
lcd.print("Temp :");
lcd.setCursor(7,0);
lcd.print(suhu);
lcd.setCursor(13,0);
lcd.print((char)223);
lcd.setCursor(14,0);
lcd.print("C");
//print kelembaban ke LCD
lcd.setCursor(0,1);
lcd.print("Humi : ");
lcd.setCursor(7,1);
lcd.print(data.humidity);
lcd.setCursor(12,1);
lcd.print(" %");
// Set the value of field 1 in the ThingSpeak channel to the temperature
ThingSpeak.setField(1, suhu);
// Set the value of field 2 in the ThingSpeak channel to the humidity
ThingSpeak.setField(2, data.humidity);
// Write the data to the ThingSpeak channel
int status = ThingSpeak.writeFields(myChannelNumber,myApiKey);
if(status == 200){
Serial.println("Data pushed successfully"); // Print a message if the data was successfully pushed to ThingSpeak
}
else{
Serial.println("Push error" + String(status)); // Print an error message with the HTTP status code if there was an error pushing the data
}
Serial.println("---"); // Print a separator line
delay(5000); // Delay for 5 seconds
}