Hello !!
i have made a medium project using Wifi on WIOT.
i have a problem when the wifi connexion is lost, all my tasks seems to hang…
after some investiguation, i can see something wrong when calling udp.begin(xx) when the Wifi is being lost.
here my code (slightly modified example to query NTP server)
static time_t _getNTPtime(void)
{
// module returns a unsigned long time valus as secs since Jan 1, 1970
// unix time or 0 if a problem encounted
WiFiUDP udp;
unsigned long epoch=0;
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
//initializes the UDP state
//This initializes the transfer buffer
Serial.println("Sync time with NTP");
udp.begin(WiFi.localIP(), 2390);
_sendNTPpacket(&udp, timeServer, packetBuffer, NTP_PACKET_SIZE); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (udp.parsePacket())
{
Serial.println("udp packet received");
// We've received a packet, read the data from it
if (udp.read(packetBuffer, NTP_PACKET_SIZE)==NTP_PACKET_SIZE) // read the packet into the buffer
{
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
epoch = secsSince1900 - seventyYears;
}
else
{
Serial.println("udp packet not complete");
}
}
// not calling ntp time frequently, stop releases resources
udp.stop();
return epoch ;
}
i have enabled the verbose mode for RPCWiFi and added some more trace inside udp.begin fonction…
Sync time with NTP
[E][WiFiUdp.cpp:42] begin(): before stop
[E][WiFiUdp.cpp:44] begin(): after stop
[D][WiFiGeneric.cpp:383] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:407] _eventCallback(): Reason: 0 - MAX
Disconnected from WIFI access point
WiFi lost connection. Reason: 0
As you could see, the wifi disconnected event is rised after i begin to deal with udp.
and the task trying to call udp.begin is blocked after the 1st instruction (stop()
The instruction that is probably waiting infinitly something seems to be "socket’
uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
log_e("before stop");
stop();
log_e("after stop");
server_port = port;
tx_buffer = new char[1460];
if(!tx_buffer){
log_e("could not create tx buffer: %d", errno);
return 0;
}
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
log_e("could not create socket: %d", errno);
return 0;
}
log_e("after socket");
int yes = 1;
if (setsockopt(udp_server,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
log_e("could not set socket option: %d", errno);
stop();
return 0;
}
log_e("after setsockopt");
struct sockaddr_in addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(server_port);
addr.sin_addr.s_addr = (in_addr_t)address;
if(bind(udp_server , (struct sockaddr*)&addr, sizeof(addr)) == -1){
log_e("could not bind socket: %d", errno);
stop();
return 0;
}
log_e("after bind");
fcntl(udp_server, F_SETFL, O_NONBLOCK);
return 1;
}
it seems WiFi.status() can also be blocking… in this case
hereunder, 1 task use HTTPClient for a request on a server… (and never returns)
1 other task try to start a NTP sync…but WiFi.status() never returns also…
all tasks using rpcWifi are now blocked infinitly…
Read measurements
[V][HTTPClient.cpp:236] beginInternal(): url: http://someip/somerequest.php
[D][HTTPClient.cpp:277] beginInternal(): host: someip port: 80 url: /somerequest.php
[D][HTTPClient.cpp:563] sendRequest(): request type: ‘POST’ redirCount: 0
[D][WiFiGeneric.cpp:383] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:407] _eventCallback(): Reason: 0 - MAX
Disconnected from WIFI access point
WiFi lost connection. Reason: 0
Sync time from NTP
the associated code:
Serial.println("Sync time from NTP");
if (WiFi.status() == WL_CONNECTED)
{
time_t ntpdt=_getNTPtime();
the _getNTPtime() function is the one at the begining of this post…
It seems there is a kind of deadlock situation somewhere in rpcWifi…
is someone can help ??
oh, i forgot, i’m using FreeRTOS and plateform.io, memory & stack seems to be good enough…
Eric.