#include <WiFi.h>
#include "DHTesp.h"
WiFiClient client;
DHTesp dhtSensor;
String thingSpeakAddress = "GOM1EL0T98DESWEE";
String tsfield1Name;
String request_string;
String apiKey = "GOM1EL0T98DESWEE";
const int DHT_PIN = 15;
const int LDR_PIN = 36;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup()
{
Serial.begin(115200);
WiFi.disconnect();
delay(2000);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("START");
WiFi.begin("Wokwi-GUEST", "");
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("Connected");
Serial.println("Your IP is :");
Serial.println((WiFi.localIP()));
}
void kirimThingspeak(){
if (client.connect("api.thingspeak.com", 80)){
Serial.println("I\'m Connected To The Service");
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float LDR_val = analogRead(LDR_PIN);
float voltage = LDR_val / 1024.*5;
float resistance = 2000*voltage/(1 - voltage / 5);
float lux = pow(RL10*1e3*pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println("");
request_string = thingSpeakAddress;
request_string += "&field1=";
request_string += data.temperature;
request_string += "&field2=";
request_string += data.humidity;
request_string += "&field3=";
request_string += lux;
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(request_string.length());
client.print("\n\n");
client.print(request_string);
}
else {
Serial.println("Not Connected To The Service");
}
}
void loop()
{
kirimThingspeak();
delay(10000);
}