#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#define DHTPIN 15
#define DHTTYPE DHT22
#define kipas 13
const char* ssid ="Wokwi-GUEST";
const char* password = "";
String serverName = "https://api.thingspeak.com/update?api_key=CD9FPCRBP444AZMX&field1=";
String serverName2 = "https://api.thingspeak.com/channels/2655340/feeds/last.json?api_key=C9USCXAWYKYFAJ0Y";
DHT dhtku (DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dhtku.begin();
WiFi.begin (ssid,password);
while(WiFi.status() != WL_CONNECTED)
{
Serial.println("z");
delay (500);
//Serial.println("Sedang Connect Bro");
}
Serial.println("wifi sdh konek dg IP");
Serial.println(WiFi.localIP());
}
void loop() {
float suhu,klbb;
suhu = dhtku.readTemperature();
klbb = dhtku.readHumidity();
Serial.print(suhu);
Serial.println("C");
Serial.print(klbb);
Serial.println("%");
// Menulis ke Thingspeak
if (WiFi.status()==WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName + String (suhu) + "&field2=" + String (klbb);
http.begin (serverPath);
int httpResponseCode = http.GET ();
if (httpResponseCode>0);
{
Serial.print("HTTP Response Code");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("wifi Disconnected");
}
// Membaca Thingspeak
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 Error Bro!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject [keys[4]];
Serial.print("Ini valuenya");
Serial.println(value);
byte datalampu = atoi(value);
if (datalampu==100)
{
digitalWrite (kipas,HIGH);
}
else
{
digitalWrite (kipas,LOW);
}
}
http.end();
}
else
{
Serial.println("WIFI Disconnected");
}
delay (4000);
}