Skip to content

Commit dc0d049

Browse files
maidnlfacchinm
authored andcommitted
added wait on IP address to wait for DHCP
1 parent d2b190a commit dc0d049

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

libraries/WiFiS3/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino

+8-9
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
This code is in the public domain.
1515
*/
1616

17-
#include "WiFi.h"
18-
#include "WiFiClient.h"
19-
#include "IPAddress.h"
20-
#include "MemoryFree.h"
17+
#include "WiFiS3.h"
18+
2119

2220
#include "arduino_secrets.h"
2321
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
@@ -35,7 +33,7 @@ char server[] = "example.org";
3533
//IPAddress server(64,131,82,241);
3634

3735
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
38-
const unsigned long postingInterval = 10L * 2000L; // delay between updates, in milliseconds
36+
const unsigned long postingInterval = 10L * 200L; // delay between updates, in milliseconds
3937

4038
/* -------------------------------------------------------------------------- */
4139
void setup() {
@@ -54,7 +52,7 @@ void setup() {
5452
}
5553

5654
String fv = WiFi.firmwareVersion();
57-
if (fv < WiFi_FIRMWARE_LATEST_VERSION) {
55+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
5856
Serial.println("Please upgrade the firmware");
5957
}
6058

@@ -66,7 +64,7 @@ void setup() {
6664
status = WiFi.begin(ssid, pass);
6765

6866
// wait 10 seconds for connection:
69-
delay(10000);
67+
//delay(10000);
7068
}
7169
// you're connected now, so print out the status:
7270
printWifiStatus();
@@ -82,12 +80,13 @@ void read_request() {
8280
/* actual data reception */
8381
char c = client.read();
8482
/* print data to serial port */
85-
Serial.print(c);
83+
//Serial.print(c);
8684
/* wrap data to 80 columns*/
8785
received_data_num++;
8886
if(received_data_num % 80 == 0) {
89-
Serial.println();
87+
Serial.print('.');
9088
}
89+
9190
}
9291
}
9392

libraries/WiFiS3/src/WiFi.cpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,31 @@ IPAddress CWifi::localIP() {
332332
/* -------------------------------------------------------------------------- */
333333
modem.begin();
334334
string res = "";
335-
if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) {
336-
if(atoi(res.c_str()) == 1) {
337-
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) {
338-
IPAddress local_IP;
339-
local_IP.fromString(res.c_str());
340-
return local_IP;
335+
int attempts = 0;
336+
IPAddress local_IP(0,0,0,0);
337+
338+
do {
339+
delay(100);
340+
if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) {
341+
if(atoi(res.c_str()) == 1) {
342+
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) {
343+
344+
local_IP.fromString(res.c_str());
345+
346+
}
341347
}
342-
}
343-
else if(atoi(res.c_str()) == 2) {
344-
if(modem.write(string(PROMPT(_IPSOFTAP)),res, CMD(_IPSOFTAP))) {
345-
IPAddress local_IP;
346-
local_IP.fromString(res.c_str());
347-
return local_IP;
348+
else if(atoi(res.c_str()) == 2) {
349+
if(modem.write(string(PROMPT(_IPSOFTAP)),res, CMD(_IPSOFTAP))) {
350+
351+
local_IP.fromString(res.c_str());
352+
}
348353
}
349354
}
355+
attempts++;
350356
}
357+
while(local_IP == IPAddress(0,0,0,0) && attempts < 50);
351358

352-
return IPAddress(0,0,0,0);
359+
return local_IP;
353360
}
354361

355362
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)