Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vivekanandRdhakane/8a32475326a5022a9ca39aa7ffac3e1f to your computer and use it in GitHub Desktop.
Save vivekanandRdhakane/8a32475326a5022a9ca39aa7ffac3e1f to your computer and use it in GitHub Desktop.
//Author: Vivekanand Dhakane
//Updated on: 28/4/2020
#include <ESP8266WiFi.h>
#define trigPin 0 //trig Pin to GPIO0
#define echoPin 2 //Echo Pin to GPIO2
int count;
////////////////
char ssid[] = "ShriRamNiwas"; // SSID of your AP
char pass[] = "password"; // password of your AP
IPAddress server(192, 168, 4, 15); // IP address of the AP
WiFiClient client;
////////////////
int Total_dist;
double present_time, diff, last_time;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
////////////////////
WiFi.mode(WIFI_STA); // starting ESP in Station Point Mode
WiFi.begin(ssid, pass); // connects to the WiFi AP
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
}
void loop() {
float duration, distance;
last_time = present_time;
present_time = millis();
diff = present_time - last_time;
//Send pulse on trig Pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
//wait for echo
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
//digitalWrite(control, LOW);
if (distance >= 200 || distance <= 0) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println("cm");
Serial.print(duration);
Serial.println("s");
//sending Message to server
client.connect(server, 80);
client.print("Tk" + (String)distance + "\r"); //sending message format "Tk65\r"
client.flush();
client.stop();
}
delay(10000); //Delay of 10 seconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment