ESP8266WiFi - Connect Fails.... Sometimes....


i have large, complex 8266 application implements custom mesh-like network tree-like structure of nodes.  there top-level ap+sta node connects tp-link router.  node has several other 8266s, in ap+sta mode, connected it.  each of nodes has number of 8266 sta-onlly nodes connected them.  nodes running exact same firmware, difference being ssids , passwords use, , ip addresses.  hard ips used everywhere except connection tp-link network, , communications between nodes via udp packets sent between directly-connected nodes.

this works great, except getting initial sta connections between 8266s going.  top-level node connects tp-link fine.  others connect if nodes started in correct order.  if reset nodes @ once, or first top-level node, 2 intermediary nodes connect it, lowest-level nodes, well.  but, if reset of intermediary or lowest-level nodes, will, in cases, never re-connect, despite having auto-connect , auto-reconnect enabled.  acts each node allowed connect 1 time, , never again, either resetting ap node trying connect.  what's odd ap works fine other drives.  can connect phone of 8266 aps.  also, every once in while, single message slip through, though connection made split-second, closed again.

another thing: never see status of wl_connected, no matter state of connection - whether connection or down, status() returns state wl_connected.  using bad library? i'm using 1 installed board manager, ide v1.8.4.  see communications in 1 direction.  instance, can send messages ap sta, not other way around.

what trick making these things able disconnect , re-connect?

here code used initialize nodes.  initializeap() used initialize nodes both ap+sta, , initializetimer() used initalize sta-only nodes.  again, works on first try when 8266s reset in correct order, there seems no issue init sequence, or values being passed.  there must i'm missing in initialization.  current running on nodemcus, behavior same running on esp-01s.

code: [select]

boolean timerwifideviceapi::initializeap(timermodes mode,
char *apssid, char *appassword, ipaddress apip, ipaddress gatewayip, uint8_t apchannel,
char *stassid, char *stapassword, ipaddress staip,
uint16_t port)
{
boolean success = false;

consoleport->printf("initializing %s ap network %s on channel %d...\n", devicename, timerapssid, timerapchannel);

while (!success)
{
// set mode
if (!(success = wifi.mode(wifimode_t::wifi_ap_sta)))
continue;

// configure sta
if (!(success = wifi.config(staip, gatewayip, netmask, gatewayip)))
continue;

// configure ap
if (!(success = wifi.softapconfig(timerapip, gatewayip, netmask)))
continue;

if (!(success = wifi.setautoconnect(true)))
continue;

if (!(success = wifi.setautoreconnect(true)))
continue;

// start/connect ap + sta
if (!(success = wifi.begin(stassid, stapassword)))
continue;

if (!(success = wifi.softap(apssid, appassword, apchannel)))
continue;
}

// setup port listener
udp = new wifiudp();
consoleport->printf("udp listener%s ready!\n", udp->begin(udpport) ? "" : " not");
}


void timerwifideviceapi::initializetimer(char *stassid, char *stapassword, ipaddress staip, ipaddress gatewayip)
{
boolean success = false;

// setup wifi client
consoleport->printf("initializing %s timer network %s...\n", devicename, stassid);

while (!success)
{
// set mode
if (!(success = wifi.mode(wifimode_t::wifi_sta)))
continue;

// configure sta
if (!(success = wifi.config(staip, gatewayip, netmask, gatewayip)))
continue;

if (!(success = wifi.setautoconnect(true)))
continue;

if (!(success = wifi.setautoreconnect(true)))
continue;

// connect sta
if (!(success = wifi.begin(stassid, stapassword)))
continue;
}

// setup port listener
udp = new wifiudp();
consoleport->printf("udp listener%s ready!\n", udp->begin(udpport) ? "" : " not");
}


regards,
ray l.

here simplest test case put together, reasons can't understand, not work @ all.  data sent, never received.  thought starting understand these things....

[update] example working - had forgotten put call recvudp() in loop().  leaves question - why not work in big application?  wifi calls in rest of code udp send , receive code, identical what's in example.  yet, code running, not re-connect reliably, or hardly @ all....

just program 2 8266s, 1 programmed ap set 0, other ap set 1.  should sending udp packets each other every 10 seconds, , each should printing packets receives serial port.  aps working, ips, ssids, passwords, channels, etc. correct no data seems pass between them.  why?  doing wrong?


code: [select]

#include <arduino.h>
#include <stdarg.h>
#include <stdio.h>
#include <esp8266wifi.h>
#include <wifiudp.h>


stream *consoleport = &serial;

char* gatewayssid = "tp-link_a47dc2";
char* gatewaypassword = "79517515";
uint16_t udpport = 8000;
wifiudp *udp = null;

#define ap 1

#if ap
#define stassid "qdnetwork0"
#define stapassword "qdpassword0"
#define staip ipaddress(192,168,0,2)
#define gatewayip ipaddress(192,168,0,1)

#define apssid "qdnetwork1"
#define appassword "qdpassword1"
#define apip ipaddress(192,168,1,1)
#define apchannel 6

#define target ipaddress(192,168,0,1)
#else
#define stassid "tp-link_a47dc2"
#define stapassword "79517515"
#define staip ipaddress(10,0,0,50)
#define gatewayip ipaddress(10,0,0,1)

#define apssid "qdnetwork0"
#define appassword "qdpassword0"
#define apip ipaddress(192,168,0,1)
#define apchannel 1

#define target ipaddress(192,168,0,2)
#endif

#define netmask ipaddress(255,255,255,0)

char *statusstrings[] =
{
"wl_idle_status",
"wl_no_ssid_avail",
"wl_scan_completed",
"wl_connected",
"wl_connect_failed",
"wl_connection_lost",
"wl_disconnected",
};

void setup()
{
delay(1000);
((hardwareserial *)consoleport)->begin(115200);
consoleport->println("\n\nnodemcu ready\n\n");

delay(1000);
wifi.begin();

initializeap(apssid, appassword, apip, gatewayip, apchannel, stassid, stapassword, staip);
}


boolean initializeap(char *apssid, char *appassword, ipaddress apip, ipaddress gatewayip, uint8_t apchannel,
char *stassid, char *stapassword, ipaddress staip)
{
boolean success = false;

while (!success)
{
// set mode
if (!(success = wifi.mode(wifimode_t::wifi_ap_sta)))
continue;

// configure sta
if (!(success = wifi.config(staip, gatewayip, netmask, gatewayip)))
continue;

// configure ap
if (!(success = wifi.softapconfig(apip, gatewayip, netmask)))
continue;

if (!(success = wifi.setautoconnect(true)))
continue;

if (!(success = wifi.setautoreconnect(true)))
continue;

// start/connect ap + sta
if (!(success = wifi.softap(apssid, appassword, apchannel)))
continue;

if (!(success = wifi.begin(stassid, stapassword)))
continue;
}

// setup port listener
udp = new wifiudp();
udp->begin(udpport);

ipaddress staip = wifi.localip();
ipaddress apip = wifi.softapip();
consoleport->printf("ap ready, staip=%d.%d.%d.%d, apip=%d.%d.%d.%d\n", staip[0], staip[1], staip[2], staip[3], apip[0], apip[1], apip[2], apip[3]);
delay(500);
}


boolean sendudp(char *s)
{
boolean ret = false;

ipaddress ip = target;
if (ret = udp->beginpacket(ip, udpport))
{
udp->write(s);
ret = udp->endpacket();
}
consoleport->printf("status()=%s, sent %s %d.%d.%d.%d\n", statusstrings[wifi.status()], s, ip[0], ip[1], ip[2], ip[3]);
return ret;
}


void recvudp(void)
{
char packetbuffer[128];
int len;

while (len = udp->parsepacket())
{
// read packet packetbufffer
len = udp->read(packetbuffer, 128);
packetbuffer[len] = '\0';
consoleport->printf("status=%s, received: %s\n", statusstrings[wifi.status()], packetbuffer);
}
}


uint32_t start1 = 0;
uint32_t start2 = 0ul - 5000ul;
void loop()
{
if ((millis() - start1 > 10000) && (!ap))
{
sendudp("hello ap0");
start1 = millis();
}
if ((millis() - start2 > 8000) && (ap))
{
sendudp("hello ap1");
start2 = millis();
}
wifi.status() == wl_connected;

        recvudp();
}


regards,
ray l.


Arduino Forum > Using Arduino > Programming Questions > ESP8266WiFi - Connect Fails.... Sometimes....


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???