So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By obts000
#86228 I'm really stumped on this. I am trying to get two modules to act together to send some distance sensor informaton from one to the other. I had implemented that by setting up one module as an AP named "Scott" hosting a webserver and the other module as a DHCP client of the first module and running webclient software. It all worked but the response time was a little iffy with delays of 2 to 3 second for everything to work.

I decided to convert it to use UDP and dumped all the webserver and web client stuff. I cannot get directly copied sample code to run. There are no errors, but the receiver never seems to detect any packets. I am trying to do some testing now with one module connected to my home wifi as a client. That seems to work fine and I can ping it from my PC.

Now I noticed that the old access point I had defined comes online whenever I power up the module, even though there is nothing about that in the new code. It's like there is still some old code on the module that is running in addition to what I am trying to do. I have tried holding the flash button down while writing the new code, but that didn't do anything different. Any ideas? The code I am currently running is some test code I downloaded and is listed below.

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

const char* ssid = "midway";
const char* password = "scottperie4780";

WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back


void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("Connecting");
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");

Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}


void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);

// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
}
}
User avatar
By obts000
#86240 Thanks. That did get rid of the persistant AP, but still no luck getting the UDP packets to register. I have tried various code samples, and my own, and can't seem to get any of them to work. Any other tricks I'm missing? I saw some post about putting in AT commands, but as I understand it, that would apply to running the NodeMCU firmware, not Arduino IDE code.