#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Server URL
const char* serverName = "http://your-server.com/traffic-data";
// Ultrasonic Sensor Pins
const int trigPin = 5;
const int echoPin = 18;
// Variables for distance measurement
long duration;
int distance;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize Ultrasonic Sensor Pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If a vehicle is detected (distance < threshold)
if (distance < 100) {
sendTrafficData(distance);
}
delay(1000);
}
void sendTrafficData(int distance) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Specify content-type header
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare data to be sent
String httpRequestData = "distance=" + String(distance);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If the response code is greater than 0, the request was successful
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}
#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Server URL
const char* serverName = "http://your-server.com/traffic-data";
// Ultrasonic Sensor Pins
const int trigPin = 5;
const int echoPin = 18;
// Variables for distance measurement
long duration;
int distance;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize Ultrasonic Sensor Pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If a vehicle is detected (distance < threshold)
if (distance < 100) {
sendTrafficData(distance);
}
delay(1000);
}
void sendTrafficData(int distance) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Specify content-type header
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare data to be sent
String httpRequestData = "distance=" + String(distance);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If the response code is greater than 0, the request was successful
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}