Question about udp.beginPacket

Hello,

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.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>




WiFiUDP Udp;

const char* ssid     = "Pi_AP";
const char* password = "*****";
IPAddress ip(192, 168, 42, 1);


#define rxPin 2
#define txPin 3
double val[10];
double x;
double y;

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(115200);
  
  delay(10);

  Serial.println();
  Serial.println();
  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() {

  if (mySerial.available()>0){
    int val1=mySerial.read();
    if (val1==66) {
      for (int i=1; i<=10; i++)
      {
        val[i]=mySerial.read();
      }
      //Serial.println("Valeur du X:");
      if (val[2]==0){
        x=(val[3]*256+val[4])/100;
      }
      else if (val[2]==1) {
        x=-(val[3]*256+val[4])/100;
      }
      Serial.print(x);
      Serial.print(",");
     
      //Serial.println("Valeur du Y:");
      if (val[5]==0){
        y=(val[6]*256+val[7])/100;
      }
      else if (val[5]==1) {      
        y=-(val[6]*256+val[7])/100;
      }
      Serial.println(y);
      //Serial.println("deg");
      delay(1000);
      Udp.beginPacket(ip,54345);
      Udp.print(x);
      //Udp.print(",");
      //Udp.println(y);
      Udp.endPacket();

      delay(1000);     

            
    }

 }

}
  if (mySerial.available()>0){
    int val1=mySerial.read();
    if (val1==66) {
      for (int i=1; i<=10; i++)
      {
        val[i]=mySerial.read();
      }

A byte arrived. If it is 66, it is NOT OK to read the next 10 bytes that may not have arrived yet.

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.

      if(Udp.beginPacket(ip,54345) == 1) {
        Udp.print(x);
        if(Udp.endPacket() == 1) Serial.println("endPacket ok");
        else Serial.println("endPacket fail");
      }
      else Serial.println("beginPacket fail");

I presume the esp8266 uses a library similar to this:

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.

At the begining, the connection seems ok

Connecting to Pi_AP
....
WiFi connected
IP address: 
192.168.42.15

The RPi is assigned 192.168.42.1/255.255.255.0.

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.

I've tested it whith a DHT sensor and it worked well. It's just a python script which waits for packets and stocks data in a csv.

Here is the working code for the DHT sensor :

#include <WiFiUdp.h>

#include <Adafruit_Sensor.h>

#include <DHT_U.h>

#include <DHT.h>



#include <ESP8266WiFi.h>
WiFiUDP Udp;

const char* ssid     = "Pi_AP";
const char* password = "*****";
IPAddress ip(192, 168, 42, 1);


#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT11     // DHT 11 

DHT_Unified dht(DHTPIN, DHTTYPE);


void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  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());
  dht.begin();

}


void loop() {
  delay(60000); //ttes les minutes
  
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
      Udp.beginPacket(ip,54345);
      Udp.write("pas de donnee");
      Udp.endPacket();
  }
  else {
      Udp.beginPacket(ip,54345);
      Udp.print(event.temperature);
      Udp.endPacket();
      
  }

  
}

There is not much difference.

And you are certain the RPi's Python script is working ok? How did you test it?

edit: I used Netcat to test mine.

I've tested it with the DHT-sensor's script and by sending random packets with my phone.

Maybe a problem with the ESP8266 library then. I don't have one here to test.

Yes, maybe there is an interference between the reception of data from the sensor and the packet send.