-->
Page 1 of 4

WIFI UDP no longer receives data after data is sent

PostPosted: Sat Apr 04, 2015 3:16 pm
by robertnash30
I'm not entirely sure that this is a bug but I can't see any other explanation for the behavior. I am using the following code on which is connected to a python script which essentially acts as a terminal-esque way of sending and receiving data to the ESP. The code below works and I can have the light set on or off. The problem is that, after a message has been sent using sendMessage, the ESP will no longer receive packets. I can see from the serial output that the loop is still running and if I remove the call to sendMessage, it will continue turning the light on and off merrily. I wonder if I'm just missing something?

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiClient client;
WiFiUDP port;

char packetBuffer[255];
unsigned int localPort = 9999;
IPAddress serverIP(192, 168, 1, 117);


void setup() {
  Serial.begin(9600);
  WiFi.begin("******", "********");
  port.begin(9999);
  pinMode(2, OUTPUT);
  digitalWrite(2,LOW);
}

void loop() {
  int packetSize = port.parsePacket();
  Serial.println(packetSize);
  if (packetSize) {
    int len = port.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len] = 0;
    Serial.println(packetBuffer);
    handleMessage(String(packetBuffer));
  }
  delay(500);
}

void handleMessage(String data) {
  if(data=="on"){
    digitalWrite(2,HIGH);
    Serial.println("ON");
    sendMessage("light on");
  }
  if(data=="off"){
    digitalWrite(2,LOW);
    Serial.write("OFF");
    sendMessage("light off");
  }
}

void sendMessage(char data[]) {
  port.beginPacket(serverIP,localPort);
  port.write(data);
  port.endPacket();
}

Re: WIFI UDP no longer receives data after data is sent

PostPosted: Sun Apr 05, 2015 5:03 am
by gerardwr
robertnash30 wrote:I'm not entirely sure that this is a bug but I can't see any other explanation for the behavior.


+1
In other post I showed working examples for an UDP Server and another one for UDP Client.
But when I extend these examples to combination the UDP Client and Server in 1 sketch I get the same problems you get.

I also adapted this code, but to no avail.
http://arduino.cc/en/Tutorial/WiFiSendReceiveUDPString

Perhaps IGRR can have a look when he's got the time.

Re: WIFI UDP no longer receives data after data is sent

PostPosted: Sun Apr 05, 2015 5:15 am
by robertnash30
From what I can see, it looks like the call to beginPacket in WiFiUDP.cpp resets everything when it calls: _ctx->unref();
In order to try and get around this I have tried calling stop and then begin again after a transmission to restart the listening but to no avail.

Code: Select allint WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
    ip_addr_t addr;
    addr.addr = ip;

    if (_ctx)
        _ctx->unref();
    _ctx = new UdpContext;
    return (_ctx->connect(addr, port)) ? 1 : 0;
}

Re: WIFI UDP no longer receives data after data is sent

PostPosted: Mon Apr 06, 2015 10:44 am
by robertnash30
For posterity, this issue has been solved on the GitHub page. Simply use a different instance of WiFiUDP for sending data.

i.e (plagiarized from mvdbro on GitHub)

Code: Select allvoid sendMessage(char data[]) {
WiFiUDP port2;
port2.beginPacket(serverIP,localPort);
port2.write(data);
port2.endPacket();
}