#include <WiFi.h>
#include "DHT.h"
#include "ThingSpeak.h"
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = "";
WiFiClient client;
unsigned long myChannelNumber = 2414950;
const char * myWriteAPIKey = "OYREA4WEUXJRINPJ";
float temp=0;
float hum=0;
int pot=0;
#define DHTPIN 33
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void send_thinkspeak(){
Wifi_Connect();
ThingSpeak.setField(1,String(temp));
ThingSpeak.setField(2,String(hum));
ThingSpeak.setField(3,String(pot));
int x= ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
// int x = ThingSpeak.writeField(myChannelNumber, 10.1, 20.1,30.1, myWriteAPIKey);
//int y = ThingSpeak.writeField(myChannelNumber, 2, 20.1, myWriteAPIKey);
Serial.println("the value of x");
Serial.println(x);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
void read_sensors(){
//hum = analogRead(A5);
hum = dht.readHumidity();
Serial.println("Humidity :");
Serial.println(hum);
// Read temperature as Celsius (the default)
//temp = analogRead(A5);
temp = dht.readTemperature();
Serial.println("Temperature in celsius :");
Serial.println(temp);
pot = analogRead(A5);
Serial.println("Pot value :");
Serial.println(pot);
}
void Wifi_Connect() {
Serial.print("Attempting to connect");
// put your main code here, to run repeatedly:
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Initialize serial
pinMode(A5,INPUT);
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); //
analogReadResolution(9);
Wifi_Connect();
}
void loop() {
read_sensors();
send_thinkspeak();
delay(10000);
}