Hey guys and girls,
I have a project where i need to send the data read from a ultrasonic data with a Node MCU (ESP 8266 ESP-12E) over UDP to my PC. First Problem was that i can logg into my Wifi but when i want to send a packet from the PC to the Node MCU it doesnt respond as the script should do. I used this Script:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "";
const char* password = "";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
}
}
I tried to send a packet with Packet sender.
I doublechecked the port, the IP, the ssid and the password.
Then my next problem is that i tried to run my standard script for ultrasonic sensors, which i modified to the Node MCU pins. but this as well didnt worked even over serial port. i used this sketch:
// defines pins numbers
const int trigPin = 2; //D4
const int echoPin = 0; //D3
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// 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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}
I doublechecked the Baudrate and all wires...
The next problem if those are solved is that i dont exactly know how to send the value then. Just like this?
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(distance);
Udp.endPacket();
Please if anyone knows any suggestions, information or how to code it please let me know. I have to finish this until 20th August cause then my Installation will happen and i dont know any other way to solve this task.
Thanks in advance, Mateo