#include <WiFi.h>
#include <ThingSpeak.h>
int statusCode;
const int PIR_SENSOR_OUTPUT_PIN = 15; /* PIR sensor O/P pin */
#define buzzer 19
char ssid[]="Wokwi-GUEST";
char pass[]="";
WiFiClient Client;
unsigned long myChannelNumber= 2311077;
const char * myWriteAPIKey="UMSR1Q64M0JAG976";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(14, OUTPUT);
pinMode(13, INPUT);
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
pinMode(buzzer,OUTPUT);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(Client);
}
void loop() {
int motion = digitalRead(PIR_SENSOR_OUTPUT_PIN);
if(WiFi.status() !=WL_CONNECTED)
{
Serial.print("Attempting to connect");
while(WiFi.status() !=WL_CONNECTED)
{
WiFi.begin(ssid,pass);
Serial.print(".");
delay(1000);
}
Serial.println("\nConected.");
}
digitalWrite(14,HIGH); //water flowing
if( motion == LOW ) //No one to turn off the pipe
{
digitalWrite(buzzer,HIGH); //buzzer alarm to turn off the pipe
Serial.print("Please turn off the pipe\n\n");
delay(1000);
}
else{
Serial.print("Object detected\n\n");
delay(1000);
}
digitalWrite(14,LOW);//water not flowing
if(motion == HIGH) //moment of public
{
digitalWrite(buzzer,LOW); //buzzer does not make alarm
delay(1000);
}
else{
Serial.print("Please turn off the pipe\n\n");
delay(1000);
}
ThingSpeak.setField(1, motion);
statusCode=ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode==200)
{
Serial.println("Channel update successfull.");
}
else
{
delay(1000);
}
}