ESP 8266 & 32. UDP 1 Transmitter, 2 Receivers

Hello all,

I've been trying for a while to manage two other ESPs with a Nunchuk controller with an ESP8266.
I've collected and pasted a code for the Nunchuk ESP. I manage to drive an ESP32. that works.
Only now I am trying to control 2nd ESP8266 where 2 leds are controlled.

The intention is that I use the Nunchuck joystick to control an existing device that has an ESP32 and an IP 192.168.2.150 port 8000.
And control the 2nd ESP, LEDs with the buttons (Z&C) of the Nunchuk
-The Nunchuk has an ESP8266 with the IP 192.168.2.145 port 8000.
Which has a code with I2C and the "NintendoExtensionCtrl.h" library
-the first receiver is an existing device with an ESP32 with ip 192.186.2.150 port 8000, (which works with what I'm trying).
-the second receiver is a device with an ESP8266 with ip 192.186.2.140 port 8000.
where two LEDs can be turned on and off with the Nunchuk's "Z and C" buttons.

My problem is that the 2nd ESP8266 with the leds is not working.
In the serial monitor nothing happens until I press a button.
Then I get to see everything that the Nunchuk broadcasts, so also what needs to be sent to the other ESP32.
Because all data is sent to the 2nd ESP8266, I am unable to distill the piece of data (A29999 and V09999) for turning the LEDs on or off.
With my brief knowledge of code I have tried everything and googled a lot. The answer may well have passed me by, but unfortunately I don't see it.

I've been stuck with this problem for weeks now. who wants to help me further on my way.
Later when the LEDs work on the 2nd receiver I want to build more code around it. But that's for later concern.

Transmitter code Nunchuk ESP8266.

// Wii Nunchuck I2C address 0x53
//---------OSR----------
//V0 is vled pin 18
//A2 is lled pin 19
//-------deze IP--------
//192.168.2.150 for ESP32 Receiver1
//192.168.2.145 for ESP8266 Nunchuck
//192.168.2.140 for ESP8266 Receiver2

#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h> 
#include <NintendoExtensionCtrl.h>


//#define SDA 2 // SDA IS GROEN ESP8266 Def PIN 2
//#define SCL 1 // SCL IS GEEL  ESP8266 Def PIN 1
//#define SDA 0 // SDA ESP-01s PIN 0
//#define SCL 2 // SCL ESP-01s PIN 2
#define SDA 2   // SDA ESP8266 TTGO Oled 2
#define SCL 14  // SCL ESP8266 TTGO Oled 14

Nunchuk nchuk;

int Linear = 10;

const char* LLEDOn = " V09999";
const char* LLEDOff = " V00";
const char* VLEDOn = " A29999";
const char* VLEDOff = " A20";

const char* IPadress1 = "192.168.2.10";    //PC Test
//const char* IPadress1 = "192.168.2.150";     //ESP32 Receiver1
const char* IPadress2 = "192.168.2.140";     //ESP8266 Receiver2

const char* ssid = "SIDD";
const char* password = "Code";

IPAddress staticIP(192,168,2,145); // ESP8266 Nunchuck
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
IPAddress dns1(8,8,8,8);
IPAddress dns2(8,8,4,4);
unsigned int localPort = 8000;

WiFiUDP Udp;

void setup()
{
  Serial.begin(115200);
  Wire.pins(SDA,SCL);
  
  nchuk.begin();
  while (!nchuk.connect())
    {
    Serial.println("Nunchuk not detected!");
    delay(100);
    }
    
  WiFi.config(staticIP, gateway, subnet, dns1, dns2);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
  Serial.print("IP address: ");
  Serial.print(WiFi.localIP());
  Serial.print(", UDP port: ");
  Serial.println(localPort);
  delay(1000);
  Udp.begin(localPort);

}

void loop(){
   
  boolean success = nchuk.update();
  boolean zButton = nchuk.buttonZ();
  boolean cButton = nchuk.buttonC();

  int joyY = map(nchuk.joyY(), 32, 228, 1, 10);   // Up, Down
  int joyX = map(nchuk.joyX(), 33, 226, 1, 9999); // org nintendo L, R
  //int joyX = map(nchuk.joyX(), 0, 255, 1, 9999);  // imitation L, R

  //Udp.beginPacket(IPadress2, localPort);
  Udp.beginPacket(IPadress1, localPort);
    if (joyY <= 1){
      int (Linear = 0);
      
//------------PC------------

      Serial.print("L0");
      Serial.print(Linear);
      Serial.print(" R0");
      Serial.print(joyX);
        if (cButton){
          Serial.print(LLEDOn);
        } else {
          Serial.print(LLEDOff);
        }
        if (zButton){
          Serial.print(VLEDOn);
        } else {
          Serial.print(VLEDOff);
        }
      Serial.println();
      
//----------IPadress1-----------

      Udp.print("L0");
      Udp.print(Linear);
      Udp.print(" R0");
      Udp.print(joyX);
        if (cButton){
          Udp.beginPacket(IPadress2, localPort);
          Udp.print(LLEDOn);
        } else {
          Udp.beginPacket(IPadress2, localPort);
          Udp.print(LLEDOff);
        }
        if (zButton){
          Udp.beginPacket(IPadress1, localPort);
          Udp.print(VLEDOn);
        } else {
          Udp.beginPacket(IPadress1, localPort);
          Udp.print(VLEDOff);
        }
      Udp.println();
      Udp.endPacket();
    } else {
          
      for (Linear = 10; Linear <= 99; Linear += joyY){
        
//------------PC------------

        Serial.print("L0");
        Serial.print(Linear);
        Serial.print(" R0");
        Serial.print(joyX);
          if (cButton){
            Serial.print(LLEDOn);
          } else {
            Serial.print(LLEDOff);
          }
           if (zButton){
            Serial.print(VLEDOn);
          } else {
            Serial.print(VLEDOff);
          }
          Serial.println();
          
//----------IPadress1-----------

        Udp.print("L0");
        Udp.print(Linear);
        Udp.print(" R0");
        Udp.print(joyX);
          if (cButton){
            Udp.beginPacket(IPadress2, localPort);
            Udp.print(LLEDOn);
          } else {
            Udp.beginPacket(IPadress2, localPort);
            Udp.print(LLEDOff);
          }
          if (zButton){
            Udp.beginPacket(IPadress1, localPort);
            Udp.print(VLEDOn);
          } else {
            Udp.beginPacket(IPadress1, localPort);
            Udp.print(VLEDOff);
          }
        Udp.println();
        Udp.endPacket();
        delay(25);
      }  
    }
  Udp.beginPacket(IPadress1, localPort);
    if (joyY <= 1){
    int (Linear = 0);
    
//------------PC------------

      Serial.print("L0");
      Serial.print(Linear);
      Serial.print(" R0");
      Serial.print(joyX);
        if (cButton){
          Serial.print(LLEDOn);
        } else {
          Serial.print(LLEDOff);
        }
        if (zButton){
          Serial.print(VLEDOn);
        } else {
          Serial.print(VLEDOff);
        }
      Serial.println();
      
//----------IPadress1-----------

      Udp.print("L0");
      Udp.print(Linear);
      Udp.print(" R0");
      Udp.print(joyX);
        if (cButton){
          Udp.beginPacket(IPadress2, localPort);
          Udp.print(LLEDOn);
        } else {
          Udp.beginPacket(IPadress2, localPort);
          Udp.print(LLEDOff);
        }
        if (zButton){
          Udp.beginPacket(IPadress1, localPort);
          Udp.print(VLEDOn);
        } else {
          Udp.beginPacket(IPadress1, localPort);
          Udp.print(VLEDOff);
        }
      Udp.println();
      Udp.endPacket();
    } else {
            
      for (Linear = 99; Linear >= 10; Linear -= joyY){
        
//------------PC------------

        Serial.print("L0");
        Serial.print(Linear);
        Serial.print(" R0");
        Serial.print(joyX);
          if (cButton){
            Serial.print(LLEDOn);
          } else {
            Serial.print(LLEDOff);
          }
          if (zButton){
            Serial.print(VLEDOn);
          } else {
            Serial.print(VLEDOff);
          }
        Serial.println();
        
//----------IPadress1-----------

        Udp.print("L0");
        Udp.print(Linear);
        Udp.print(" R0");
        Udp.print(joyX);
          if (cButton){
            Udp.beginPacket(IPadress2, localPort);
            Udp.print(LLEDOn);
          } else {
            Udp.beginPacket(IPadress2, localPort);
            Udp.print(LLEDOff);
          }
          if (zButton){
            Udp.beginPacket(IPadress1, localPort);
            Udp.print(VLEDOn);
          } else {
            Udp.beginPacket(IPadress1, localPort);
            Udp.print(VLEDOff);
          }
        Udp.println();
        Udp.endPacket();
        delay(25);
      }
    } 
  }

Receive code 2 LEDs ESP8266

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

int VLED = 16;
int LLED = 2;

const char* ssid = "SIDD";
const char* password = "CODE";

IPAddress staticIP(192,168,2,140);
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
IPAddress dns1(8,8,8,8);
IPAddress dns2(8,8,4,4);

const char* LLEDOn = " V09999";
//const char* LLEDOff = " V00";
const char* VLEDOn = " A29999";
//const char* VLEDOff = " A20";

unsigned int localPort = 8000;
char incomingPacket[255];

WiFiUDP Udp;

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

  pinMode(VLED, OUTPUT);
  pinMode(LLED, OUTPUT);
  
  //digitalWrite(VLED, HIGH);
  //digitalWrite(LLED, HIGH);

  Serial.println();
  Serial.printf("Connecting to %s ", ssid);
  WiFi.config(staticIP, gateway, subnet, dns1, dns2);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Serial.println(" connected");
  Serial.print("IP address: ");
  Serial.print(WiFi.localIP());
  Serial.print(", UDP port: ");
  Serial.println(localPort);
  delay(1000);
  Udp.begin(localPort);
}


void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize){
    int len = Udp.read(incomingPacket, 255);
    if (len > 0){
      incomingPacket[len] = 0;
    }
    
  //Serial.print(incomingPacket);
  Serial.print (Udp.parsePacket());

  //if ((incomingPacket, 255) == VLEDOn){ // NIET, NIET UP
  //if (incomingPacket == VLEDOn) { // NIET, WEL UP
  //if ((incomingPacket) == VLEDOn) { // NIET, WEL UP
  if ((incomingPacket) == "V09999") {
  //if (incomingPacket, 255 == "V09999") {
  //if ((incomingPacket, 255) == "V09999") {
  //if (Udp.parsePacket() == VLEDOn){
  //if (Udp.parsePacket() == "V09999") {
  //if (Udp.parsePacket() <= 0) { // WERKT GAAT NIE UIT
  //if (Udp.parsePacket() == 0) { // WERKT GAAT NIE UIT
    Serial.print("VledOn");
    digitalWrite(VLED, HIGH);
  } else {
    Serial.print("VledOff");
    digitalWrite(VLED, LOW);
  }
  //if ((incomingPacket, 255) = LLEDOn){
    //Serial.print("LledOn");
    //digitalWrite(LLED, HIGH);
  //} else {
    //Serial.print("LledOff");
    //digitalWrite(LLED, LOW);
  //}
      
  }

}

Thanks everyone for reading my post

You must not nest the construction of UDP packets to different nodes! Construct one packet, send it by calling Udp.endPacket(), then start the next by calling Udp.beginPacket().

Thanks for your response pylon.
I also tried that with the same result. That's why I left it out after the "IPadress2"

it may be worth using UDP multicast sending the same datagram to all devices and each device sorts out what part of the overall message is applies to it

Thanks for your comment Horace
It is indeed interesting to explore that.
I did try with a broadcast IP, but there were many hiccups in sending the data.
But I also don't know how to distil the necessary data from the whole data.
That's why I thought it might be easier to send the data I need directly to the IP that's processing it.
But it's definitely worth investigating.
Thanks for the input

it the protocol data meessage lengths are fixed it is fairly easy, e.g. on recipt of a frame receiver 1 uses first two bytes, receiver 2 the next six bytes, receiver 3 the next four bytes, etc
it is when the message lengths vary you have problems requiring a more complex protocol

1 Like

I didn't know that, Horace. It makes sense what you write.
Do you have an example?

I am assuming fixed positions for data in a structure
say we receive this data over a serial line

10,0,100,97,116,97,0,0,55,2,0,0,0,213,93,71,0,0,0,0

I am on a PC using gcc and I know it consists of

  1. short integer 2 bytes
  2. char array 6 bytes
  3. integer 4 bytes
  4. float 4 bytes

I can extract the values using memcpy() so

// extract data from a byte array
// assuming short 2 bytes, int 4 bytes, float 4 bytes

int main(void) {
   unsigned char data2[]={10,0,100,97,116,97,0,0,55,2,0,0,0,213,93,71,0,0,0,0};
   for(int i=0;i< 20; i++)
     printf("%d,", data2[i]);
   // data structure received is data2
   short i2;
   memcpy(&i2, &data2, 2);
   printf("\ni2=%d\n", i2);
   char ch2[20];
   memcpy(&ch2, &data2[2], 6);
   printf("ch2=%s\n", ch2);
   int j2;
   memcpy(&j2, &data2[8], 4);
   printf("j2=%d\n", j2);
   float x2;
   memcpy(&x2, &data2[12], 4);
   printf("x2=%f\n", x2);
}

and a run gives

10,0,100,97,116,97,0,0,55,2,0,0,0,213,93,71,0,0,0,0,
i2=10
ch2=data
j2=567
x2=56789.000000

values are correct

if it was know the data was in a particular structure it could be extracted so (assuming bytes are packed to a word - two byte - boundary)

// extract data from a byte array into a structure
// assuming short 2 bytes, int 4 bytes, float 4 bytes

struct Data {
   short i;
   char ch[6];
   int j;
   float x;
}  data;

int main(void) {
   unsigned char data2[]={10,0,100,97,116,97,0,0,55,2,0,0,0,213,93,71,0,0,0,0};
   for(int i=0;i< 20; i++)
     printf("%d,", data2[i]);
   memcpy(&data, data2, sizeof(data));
   printf("\ni=%d\n", data.i);
   printf("ch=%s\n", data.ch);
   printf("j=%d\n", data.j);
   printf("x=%f\n", data.x);
}

a run gives

10,0,100,97,116,97,0,0,55,2,0,0,0,213,93,71,0,0,0,0,
i=10
ch=data
j=567
x=56789.000000

howver, transmitting binary data between machines can have problems
notes:
if this was transmitted to an Arduino there would be a problem as int is typically 2 bytes
in such as case j2 would have to be defined as long int which is 4 bytes
I am assuming the endianness of the transmitter and receiver is the same otherwise the bytes would have to be exchanged

get the offsets, positions, data size, etc wrong and one ends up with rubbish

1 Like

Thank you Horace. I need to study this carefully

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