I encounter a problem with my code. The goal is to send data from a sensor through wifi to a raspberry pi. I use an ESP8266 with arduino code. The protocol used is UDP. I started with a DHT sensor and it worked. Now, I make tests with an other sensor and my raspberry doesn't receive packets with my code. Measurements are well displayed on my serial monitor but the packet send part doesn't work.
I don't understand what is happening.
Thanks. Surely there are improvements to do with this part but my major worry is to have the data sent. I have the values I need in the serial monitor but the udp packet are not sent even if I write some chars in it.
Udp functions normally return values. The Github library functions beginPacket and endPacket return 1 on success and 0 on fail. Add some error checking like below.
Thank you.
Yes esp8266 uses this library. I've modified my script with your code, it returns "endPacket ok".
When I put the "udp packet send" part at the begining of the void loop, before the condition "if (mySerial.available()>0)" I receive data with my raspberry pi once. When I put it wherever else, I don't get any packets.
Normally that indicates the next device en route to the destination (not the destination device if not localnet) took the packet.
What IP is displayed for the ESP8266 in the setup function? Does it have the same first three octets as this IP? BTW, this looks like a router IP (gateway).
IPAddress ip(192, 168, 42, 1);
edit: The "first three octets" are the 192.168.42 part of that IP. Does the wifi device show the same first three numbers?
The raspberry pi is configured as a wifi access point so the IP is ok. The ESP8266 send data when I remove the sensor data acquisition. Is it possible that there is a problem with the definition of the software serial with a baudrate of 9600 and the ESP8266 that uses a 115200 baudrate ? I don't know much about those parameters.
It appears the serial stuff is working ok. It would be garbled or non-responsive if failing. Does the esp8266 show a 1292.168.42.x IP during startup? Is the RPi assigned 192.168.42.1/255.255.255.0?
edit: I use a RPi as a UDP "server" and it works fine, so if your RPi code is correct, you should be receiving the packets.
Good. Then the RPi and Arduino are on the same localnet, indicating the RPi is receiving your packets. I suspect it is your code on the RPi that is failing.