#include "DHT.h"
#include <WiFi.h>
#include "ThingSpeak.h"
#include <HTTPClient.h>
#include <DHTesp.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define LEDM 18
#define LEDH 5
#define light 34
char *ssid = "Wokwi-GUEST";
char *pass = "";
const int myChannelNumber = 2290074;
const char* myApiKey = "36UOIEU57JRT9K7C";
String servername ="https://api.thingspeak.com/update?api_key=36UOIEU57JRT9K7C";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello, ESP32!");
dht.begin();
pinMode(LEDM, OUTPUT);
pinMode(LEDH, OUTPUT);
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED){
Serial.print("_");
delay(222);
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
delay(500); // this speeds up the simulation
float t = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" C");
if (t > 30) {
digitalWrite(LEDM, HIGH);
digitalWrite(LEDH, LOW);
}
else{
digitalWrite(LEDM, LOW);
digitalWrite(LEDH, HIGH);
}
delay(1000);
int analogValue = analogRead(light);
Serial.print("LDR: ");
Serial.println(analogValue);
tone(2, 262, 250);
SendData(t, analogValue);
}
void SendData(int temp, int analogValue){
HTTPClient http; // init http client
// definisi url
String url = servername + "&field1=" + temp + "&field2=" + analogValue ;
Serial.println("url: " + String(url));
// send HTTP request
http.begin(url.c_str());
// cek status data
int httpResponseCode = http.GET();
//
if( httpResponseCode > 0){
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
}
else{
Serial.print("Error Code: ");
Serial.println(httpResponseCode);
}
http.end();
}