#include<WiFi.h>
#include<ThingSpeak.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2236553;
const char * myWriteAPIKey = "Y8ST265ZG0GGZVTN";
int statusCode;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
pinMode(12, OUTPUT);
pinMode(14, INPUT);
pinMode(27, OUTPUT);
}
void loop() {
//wifi connection setup
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.");
}
digitalWrite(12, HIGH);
delay(100); // this speeds up the simulation
digitalWrite(12, LOW);
float a=pulseIn(14,HIGH);
long d = (a*0.034) / 2;
Serial.println(d);// put your main code here, to run repeatedly:
ThingSpeak.setField(1, d);
if(d<200)
{
digitalWrite(27, HIGH);
ThingSpeak.setField(1, d);
}
else
{
digitalWrite(27, LOW);
ThingSpeak.setField(2, 0);
}
statusCode = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode == 200) { //successful writing code
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem Writing data. HTTP error code :" +
String(statusCode));
}
delay(15000);
}