#include <WiFi.h>
//#include <ThingSpeak.h>
const char apiKey[] = "5V6YO5M7DJFR3SOD"; // write your "Write API key"
const char* ssid = "Wokwi-GUEST"; // write your "wifi name"
const char* password = ""; // write your "wifi password"
//const char* ssid = "mugil";
//const char* password = "12345678";
#define ir_car1 5
#define ir_car2 6
#define ir_car3 7
#define ir_car4 8
const char* server = "api.thingspeak.com";
long myChannelNumber = 2457760;
int S1=0, S2=0, S3=0, S4=0 ;
String p1;
String p2;
String p3;
String p4;
int avail= 4;
int slot = 4;
int valSensor = 4;
WiFiClient client;
void setup()
{
Serial.begin(9600);
Serial.println("Serial Begin");
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
//ThingSpeak.begin(client);
}
void loop() {
valSensor = getSensorData();
Serial.println(valSensor);
//ThingSpeak.writeField(myChannelNumber, 1, Temp, apiKey);
//ThingSpeak.writeField(myChannelNumber, 2, Hum, apiKey);
//Serial.println("Waiting");
delay(1500);
}
void Read_Sensor(){
S1=0, S2=0, S3=0, S4=0;
if(digitalRead(ir_car1) == 0){
S1=1;
}
if(digitalRead(ir_car2) == 0){
S2=1;
}
if(digitalRead(ir_car3) == 0){
S3=1;
}
if(digitalRead(ir_car4) == 0){
S4=1;
}
}
int getSensorData(){
Read_Sensor();
String p1="";
String p2="";
String p3="";
String p4="";
if(S1==1)
{
Serial.print(" S1:Fill ");
p1="1";
}
else
{
Serial.print(" S1:Empty");
p1="0";
}
if(S2==1)
{
Serial.print(" S2:Fill ");
p2="1";
}
else
{
Serial.print(" S2:Empty");
p2="0";
}
if(S3==1)
{
Serial.print(" S3:Fill ");
p3="1";
}
else
{
Serial.print(" S3:Empty");
p3="0";
}
if(S4==1)
{
Serial.print(" S4:Fill ");
p4="1";
}
else
{
Serial.print(" S4:Empty");
p4="0";
}
int total = S1+S2+S3+S4;
slot = avail-total;
String ans="1"+p1+p2+p3+p4;
return slot;
}