Skip to content

Commit

Permalink
Fix MQTTClient WiFi read timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
robertinant committed Nov 26, 2014
1 parent 5ca414f commit ebd157e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions libraries/MQTTClient/WifiIPStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@ class WifiIPStack
return iface.connect(hostname, port);
}

int read(char* buffer, int len, int timeout)
int read(unsigned char* buffer, int len, int timeout)
{
iface.setTimeout(timeout);
while(!iface.available());
return iface.readBytes(buffer, len);
int interval = 10; // all times are in milliseconds
int total = 0, rc = -1;

if (timeout < 30)
interval = 2;
while (iface.available() < len && total < timeout)
{
delay(interval);
total += interval;
}
if (iface.available() >= len)
rc = iface.readBytes((char*)buffer, len);
return rc;
}


int write(char* buffer, int len, int timeout)
int write(unsigned char* buffer, int len, int timeout)
{
iface.setTimeout(timeout);
return iface.write((uint8_t*)buffer, len);
Expand Down

0 comments on commit ebd157e

Please sign in to comment.