I am running ESP8266 together with a HC-SR04 to measure distances using Ultrasonic sounds.
All seems to work fine and I am able to get the distance in the Serial Monitor window.
Is it possible to send this info over WiFi assuming I am using the same wifi connection for both ESP8266 and the PC/Mobile on which I am getting the readings? (i.e. using same network not over the internet)
@GolamMostafa & @Juraj
thanks for your input very interesting! instead of using a second ESP, will it be possible to use a PC or mobile to act like an emulator?
EDIT: I think i missed the part that the ESP can be configured as a Server with an access point. Will try it out!
in the example i showed, the datagram is sent every 10 seconds..
sdata,idata,fdata is the data collected and sent.. seq and crc are used to ensure proper communications..
you could mod the sketch collect data populate sdata,idata,fdata and send it off..
just to see if it would work..
udp is not a guaranteed delivery protocol, there's is no delivery notification..
that's what the seq var keeps track of, if it varies usually means packet loss..
keeps the packets as small as possible and you can send pretty fast if you need too..
generally works quite well, the sketch also uses .255 which is a broadcast address, meaning goes to all in the same lan, can have multiple receivers..
thanks again, managed to make it work. full code below:
// Import required libraries
#include <ESP8266WiFi.h>
#include "ESPAsyncWebSrv.h"
// Set your access point network credentials
const char* ssid = "Ultrasonic_Meter";
const char* password = "123456789";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
const int LED = D4;
const int trigPin = D5;
const int echoPin = D6;
long duration;
float distance;
String readDist() {
return String(distance) + "m";
}
void setup() {
pinMode(LED, OUTPUT); // Sets the LED as an Output
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
Serial.println();
// Setting the ESP as an access point
Serial.print("Setting AP (Access Point)…");
Serial.println();
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/distance", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send_P(200, "text/plain", readDist().c_str());
});
server.begin(); // Start Server
}
void loop() {
digitalWrite(D4, HIGH); //turn on LED
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= (duration*0.034/2)/100;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("m");
delay(1500);
digitalWrite(LED, LOW); //turn off LED
delay(500);
}