void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
#include <ESP8266WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include "DHT.h"
#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2);
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
DHT dht(D3, DHT11);
#define LED D5
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
lcd.begin();
lcd.backlight();
pinMode(LED, OUTPUT);
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
float suhu = dht.readTemperature();
float kelembaban = dht.readHumidity();
if (suhu > 29){
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
// set the fields with the values
ThingSpeak.setField(1, suhu);
ThingSpeak.setField(2, kelembaban);
int x = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if (x == 200){
Serial.println("Channel update succesful.");
Serial.print("suhu: ");
Serial.println(suhu);
Serial.print("kelembaban: ");
Serial.println(kelembaban);
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(suhu);
lcd.print( (char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(kelembaban);
lcd.print("%");
delay(500);
}
else {Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000); // Wait 20 seconds to update the channel again
}