UDP Incorrect first byte received

Hi everyone, hope you're having a good day.

I am using an ESP32 as a client to send ADC data to a server on my PC.
Currently I am filling up a 2D buffer to send as a large packet (to maximise throughput):

When I receive the data on my PC the first byte is in the wrong order.

#include <WiFi.h>
#include <WiFiUdp.h>

/* WiFi network name and password */
//const char * ssid = "ABD";
//const char * pwd = "$S1mul4t0r$";

const char * ssid = "WiFi Guest_";
const char * pwd = "";

// IP address to send UDP data to.
// it can be ip address of the server or
// a network broadcast address
// here is broadcast address
const char * udpAddress = "172.24.29.139";
const int udpPort = 1234;

#define numOfCh 8
#define numOfTs 32

unsigned long adcSample = 0b11111111111111111111111111111110;
//uint8_t adcSample = 0b11111110;

long buff[numOfCh][numOfTs];
int channel = 0;
int timestep = 0;

//create UDP instance
WiFiUDP udp;

void setup() {
  Serial.begin(115200);
  
  //Connect to the WiFi network
  WiFi.begin(ssid, pwd);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //This initializes udp and transfer buffer
  udp.begin(udpPort);
}

void loop() {

  buff[channel][timestep] = adcSample;
  channel = (channel + 1) % numOfCh;
  if (channel == 0)
  {
    timestep = (timestep + 1) % numOfTs;
    if (timestep == 0)
    {
      udp.beginPacket(udpAddress, udpPort);
      udp.write((uint8_t*)buff, sizeof(buff));
      udp.endPacket();
    }
  }

}

//for(int i =0; i<numOfCh;i++){
//      for(int j = 0; j<numOfTs; j++){
//        Serial.print(buff[i][j],BIN);
//        Serial.print(" ");
//      }
//      Serial.println();
//      }
//      Serial.println("-------------");

On the receiving end I am getting the packets coming in as: b'\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\ ...
which is equivalent to:
11111110111111111111111111111111111111101111111111111111111111111111111...

But I expect to receive:
b'\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\xff\xff\xfe\ ...
equivalent to:
11111111111111111111111111111110111111111111111111111111111111111111110...

Hopefully that's clear. Any ideas on how to fix this would be amazing!

P.S I have tried using pointers but WiFiUDP didn't want to print them.

It would appear that one of the machines is little-endien (LSB first) and the other is big-endien (MSB first) so you will have to reverse the order. Probably easier to do it on the PC side.

It would be more instructive if you used hex notation and a value that was not mostly 1's to verify

unsigned long adcSample = 0x12345678;

and then see what you receive.

Thanks blh64 you are right! For some reason that didn't occur to me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.