-->
Page 1 of 2

Module running old code in addtion to current after reload?

PostPosted: Sat Mar 28, 2020 4:09 pm
by obts000
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();
}
}

Re: Module running old code in addtion to current after relo

PostPosted: Sun Mar 29, 2020 4:09 am
by schufti
try putting
WiFi.mode(WIFI_STA);
before
WiFi.begin()

Re: Module running old code in addtion to current after relo

PostPosted: Sun Mar 29, 2020 8:15 am
by obts000
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.

Re: Module running old code in addtion to current after relo

PostPosted: Sun Mar 29, 2020 9:27 am
by obts000
Never mind my post. I got it to work with my original code, configuring the module as an AP. Not sure what changed, and I never got any of the sample code to work, but I am good to go.