/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include "DHTesp.h"
#include <WiFi.h>
#include<HTTPClient.h>
// const char *ssid = "yuren";
// const char *pass = "yourent";
// const char* server = "api.thingspeak.com";
const int DHT_PIN = 15;
const int LED_R = 23;
const int LED_G = 22;
const int LDR_AO = 34;
char *ssid = "Wokwi-GUEST";
char *pass = "";
String serverName = "https://api.thingspeak.com/update?api_key=BJO6XTO91B3QPNCY";
DHTesp dhtSensor;
// WiFi.setup(STA);
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass, 6);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay((400));
}
Serial.println(("WiFi Connected"));
// Serial.println(WiFi.localIP()); //DAPETIN IP ISP
}
void SendData(int temp, int hum){
HTTPClient http; // init http client
// definisi url
String url = serverName + "&field1=" + temp + "&field2=" + hum;
Serial.println("url: " + String(url));
// send HTTP request
http.begin(url.c_str()); //untuk enter
// cek status data
int httpResponseCode = http.GET(); //404notfound
//
if( httpResponseCode > 0){
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
}
else{
Serial.print("Error Code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float t = data.temperature;
float h = data.humidity;
int LDR = analogRead(34);
// Serial.println("Temp: " + String(t, 2) + "°C");
// Serial.println("Humidity: " + String(h, 1) + "%");
// Serial.println("Cahaya: " + String(LDR) + " lux");
// Serial.println("---");
// delay(100); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
// if( (t > 30) && (LDR < 2000)){
// digitalWrite(23, HIGH);
// digitalWrite(22, LOW);
// }
// else {
// digitalWrite(22, HIGH);
// digitalWrite(23, LOW);
// }
SendData(t,h);
}