#include <WiFi.h>
#include "ThingSpeak.h"
int relay = 15;
int relay2 = 4;
const int DHT_PIN = 15;
const int LED_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2276925;
const char* myApiKey = "IU7OOCU2W8LDPJ2J";
const char* server = "api.thingspeak.com";
int temp= 32;
int sw= 2;
int bs = 0;
int bs2 = 0;
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(relay, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(sw,INPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
bs = digitalRead(sw);
Serial.print("sw status =");
Serial.println(bs);
delay(500);
bs2 = analogRead(temp)/25;
Serial.print("temp status =");
Serial.println(bs2);
delay(500);
ThingSpeak.setField(1,bs);
ThingSpeak.setField(2,bs2);
if(bs == HIGH)
{
digitalWrite(relay,HIGH);
}
else{
digitalWrite(relay,LOW);
}
if(bs2>40)
{
digitalWrite(relay2,HIGH);
}
else{
digitalWrite(relay2,LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Temp: " + String(bs));
Serial.println("Humidity: " + String(bs2) + "oC");
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
}