#include"DHTesp.h"
#include <WiFi.h>
#include <ThingSpeak.h>
const int pin = 15;
DHTesp dhtsensor;
TempAndHumidity data;
const int tri = 13;
const int eco = 12;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
int statusCode;
unsigned long myChannelNumber = 2074394;
const char * myWriteAPIKey = "WVAXRAYERZXE7E07";
float dis;
long dur;
void setup() {
Serial.begin(115200);
dhtsensor.setup(pin,DHTesp::DHT22);
pinMode(tri, OUTPUT);
pinMode(eco, INPUT);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
if(WiFi.status()!= WL_CONNECTED)
{
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid,pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nconnected.");
}
data = dhtsensor.getTempAndHumidity();
Serial.println("humi:" + String(data.humidity,1));
Serial.println("Temp:" + String(data.temperature,2));
dis = getdistanceIn();
Serial.println("distance:"+ String(dis));
Serial.println("_______________________");
ThingSpeak.setField(1,data.humidity);
ThingSpeak.setField(2,data.temperature);
ThingSpeak.setField(3,dis);
statusCode=ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode == 200)
{
Serial.println("Channel Updated");
}
else{
Serial.println("Problem in Updating");
String(statusCode);
}
delay(15000);
}
long getdistanceIn()
{
digitalWrite(tri,HIGH);
delayMicroseconds(2);
digitalWrite(tri,HIGH);
delay(1000);
digitalWrite(tri,LOW);
dur = pulseIn(eco,HIGH);
return((dur*0.034/2)+1);
}