#include "DHT.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
// Buzzer
#define Buzzer 13
//LDR
#define ldrPin 35
const float gama = 0.7;
const float rl10 = 50;
//DHT22
#define DHTPIN 21
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LED
#define LEDPIN 12
// Wifi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverName = "https://api.thingspeak.com/update?api_key=8V6ZWO2T4OJPMCSD&field1=";
String serverName2 = "https://api.thingspeak.com/channels/2353160/fields/3/last.json?api_key=1SDVZ0T2TWE0MSOM";
String serverName3 = "https://api.thingspeak.com/channels/2353160/fields/5/last.json?api_key=1SDVZ0T2TWE0MSOM";
int nilaiLDR;
void setup() {
pinMode (ldrPin, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(LEDPIN, OUTPUT);
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
float suhu = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(suhu);
Serial.println(F("°C "));
nilaiLDR = analogRead(ldrPin);
Serial.print("nilai LDR = ");
Serial.println(nilaiLDR);
nilaiLDR = map(nilaiLDR, 4095, 0, 1024, 0); //mengubah nilai pembacaan sensor LDR dari nilai ADC arduino menjadi nilai ADC ESP32
float voltase = nilaiLDR / 1024.*5;
float resistansi = 2000 * voltase / (1-voltase/5);
float kecerahan = pow(rl10*1e3*pow(10,gama)/resistansi,(1/gama));
Serial.print("Kecerahan = ");
Serial.println(kecerahan);
// Menulis thingspeak
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName + String(suhu) + "&field2=" + String(humidity)+"&field4="+ String(kecerahan);
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
//Membaca dari thingspeak Buzzer
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName2;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[2]];
Serial.print("keys: ");
Serial.println(keys);
Serial.print("Value: ");
Serial.println(value);
byte databuzzer = atoi(value);
Serial.print("Data Buzzer: ");
Serial.println(databuzzer);
if(databuzzer==1)
{
tone(Buzzer, 250);
Serial.println("bunyi");
}
else
{
noTone(Buzzer);
Serial.println("off");
}
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
//Membaca dari thingspeak LED
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName3;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[2]];
Serial.print("keys: ");
Serial.println(keys);
Serial.print("Value: ");
Serial.println(value);
byte dataled = atoi(value);
Serial.print("Data Buzzer: ");
Serial.println(dataled);
if(dataled==1)
{
digitalWrite(LEDPIN, HIGH);
Serial.println("led nyala");
}
else
{
digitalWrite(LEDPIN, LOW);
Serial.println("led mati");
}
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
delay (500);
// delay(1000);
}