Commit 1425ecb7 authored by nouman.ashraf's avatar nouman.ashraf

Initial commit

parents
#include<SPI.h>
#include <WiFiNINA.h>
#include "ThingSpeak.h"
unsigned long ChannelNo = 1210180;
const char * WriteAPIKey = "122GT3DOPUPHMJVU";
WiFiClient client;
const int Trig_Pin = 11;
const int Echo_Pin = 13;
void setup() {
pinMode(Trig_Pin,OUTPUT);
pinMode(Echo_Pin,INPUT);
char ssid[] = "EE-Hub-Eqp9";
char pass[] = "task-dub-ABACK";
int status = WL_IDLE_STATUS;
Serial.begin(9600);
status = WiFi.begin(ssid, pass);
Serial.println("Connecting to network...");
if(status != WL_CONNECTED) {
Serial.println("Unable to connect to Wifi");
while(true);
}
else{
Serial.print("Connection successful!");
Serial.print("The IP Address is: ");
Serial.println(WiFi.localIP());
delay(10000);
}
ThingSpeak.begin(client);
}
void loop() {
// put your main code here, to run repeatedly:
long car, distanceCm, inches;
digitalWrite(Trig_Pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_Pin, HIGH);
delayMicroseconds(10);
digitalWrite(Trig_Pin, LOW);
car = pulseIn(Echo_Pin,HIGH);
distanceCm = car / 30/ 2 ;
inches = car / 11.81 / 2;
if (distanceCm <= 0)
{
Serial.println("Out of range");
}
else
{
Serial.print(inches);
Serial.print("inch: ");
Serial.print(distanceCm);
Serial.print("cm");
Serial.println();
}
delay(1000);
Serial.println("Posting " + String(distanceCm, 2) + " to ThingSpeak");
ThingSpeak.setField(2, String(distanceCm,2));
ThingSpeak.writeFields(ChannelNo,WriteAPIKey);
delay(30000);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment