Skip to content

Commit 0f9acee

Browse files
committed
Provisioning mode improvements
- Add new example sketch - Add new status types for provisioning - Use m2m_wifi_get_connection_info to get SSID when connected with default connect
1 parent 41581e2 commit 0f9acee

File tree

4 files changed

+200
-15
lines changed

4 files changed

+200
-15
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
WiFi Web Server
3+
4+
A simple web server that shows the value of the analog input pins.
5+
using a WiFi shield.
6+
7+
This example is written to configure the WiFi settings using provisioning mode.
8+
9+
Circuit:
10+
WiFi shield attached
11+
Analog inputs attached to pins A0 through A5 (optional)
12+
13+
created 13 July 2010
14+
by dlf (Metodo2 srl)
15+
modified 31 May 2012
16+
by Tom Igoe
17+
18+
*/
19+
20+
#include <SPI.h>
21+
#include <WiFi101.h>
22+
23+
const int ledPin = 6; // LED pin for connectivity status indicator
24+
25+
WiFiServer server(80);
26+
27+
void setup() {
28+
//Initialize serial:
29+
Serial.begin(9600);
30+
31+
// check for the presence of the shield:
32+
if (WiFi.status() == WL_NO_SHIELD) {
33+
Serial.println("WiFi shield not present");
34+
// don't continue:
35+
while (true);
36+
}
37+
38+
// configure the LED pin for output mode
39+
pinMode(ledPin, OUTPUT);
40+
41+
// Start in provisioning mode:
42+
// 1) This will try to connect to a previously associated access point.
43+
// 2) If this fails, an access point named "wifi101-XXXX" will be created, where XXXX
44+
// is the last 4 digits of the boards MAC address. Once you are connected to the access point,
45+
// you can configure an SSID and password by visiting http://wifi101/
46+
WiFi.beginProvision();
47+
48+
while (WiFi.status() != WL_CONNECTED) {
49+
// wait while not connected
50+
51+
// blink the led to show an unconnected status
52+
digitalWrite(ledPin, HIGH);
53+
delay(500);
54+
digitalWrite(ledPin, LOW);
55+
delay(500);
56+
}
57+
58+
// connected, make the LED stay on
59+
digitalWrite(ledPin, HIGH);
60+
61+
server.begin();
62+
// you're connected now, so print out the status:
63+
printWifiStatus();
64+
}
65+
66+
67+
void loop() {
68+
// listen for incoming clients
69+
WiFiClient client = server.available();
70+
if (client) {
71+
Serial.println("new client");
72+
// an http request ends with a blank line
73+
boolean currentLineIsBlank = true;
74+
while (client.connected()) {
75+
if (client.available()) {
76+
char c = client.read();
77+
Serial.write(c);
78+
// if you've gotten to the end of the line (received a newline
79+
// character) and the line is blank, the http request has ended,
80+
// so you can send a reply
81+
if (c == '\n' && currentLineIsBlank) {
82+
// send a standard http response header
83+
client.println("HTTP/1.1 200 OK");
84+
client.println("Content-Type: text/html");
85+
client.println("Connection: close"); // the connection will be closed after completion of the response
86+
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
87+
client.println();
88+
client.println("<!DOCTYPE HTML>");
89+
client.println("<html>");
90+
// output the value of each analog input pin
91+
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
92+
int sensorReading = analogRead(analogChannel);
93+
client.print("analog input ");
94+
client.print(analogChannel);
95+
client.print(" is ");
96+
client.print(sensorReading);
97+
client.println("<br />");
98+
}
99+
client.println("</html>");
100+
break;
101+
}
102+
if (c == '\n') {
103+
// you're starting a new line
104+
currentLineIsBlank = true;
105+
}
106+
else if (c != '\r') {
107+
// you've gotten a character on the current line
108+
currentLineIsBlank = false;
109+
}
110+
}
111+
}
112+
// give the web browser time to receive the data
113+
delay(1);
114+
115+
// close the connection:
116+
client.stop();
117+
Serial.println("client disonnected");
118+
}
119+
}
120+
121+
122+
void printWifiStatus() {
123+
// print the SSID of the network you're attached to:
124+
Serial.print("SSID: ");
125+
Serial.println(WiFi.SSID());
126+
127+
// print your WiFi shield's IP address:
128+
IPAddress ip = WiFi.localIP();
129+
Serial.print("IP Address: ");
130+
Serial.println(ip);
131+
132+
// print the received signal strength:
133+
long rssi = WiFi.RSSI();
134+
Serial.print("signal strength (RSSI):");
135+
Serial.print(rssi);
136+
Serial.println(" dBm");
137+
}
138+

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ flush KEYWORD2
2626
stop KEYWORD2
2727
connected KEYWORD2
2828
begin KEYWORD2
29+
beginProvision KEYWORD2
30+
beginOrProvision KEYWORD2
2931
beginMulti KEYWORD2
3032
disconnect KEYWORD2
3133
macAddress KEYWORD2
@@ -37,6 +39,7 @@ BSSID KEYWORD2
3739
APClientMacAddress KEYWORD2
3840
RSSI KEYWORD2
3941
encryptionType KEYWORD2
42+
provisioned KEYWORD2
4043
getResult KEYWORD2
4144
getSocket KEYWORD2
4245
poll KEYWORD2

src/WiFi.cpp

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ extern "C" {
4747
static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
4848
{
4949
switch (u8MsgType) {
50+
case M2M_WIFI_RESP_DEFAULT_CONNECT:
51+
{
52+
tstrM2MDefaultConnResp *pstrDefaultConnResp = (tstrM2MDefaultConnResp *)pvMsg;
53+
if (pstrDefaultConnResp->s8ErrorCode) {
54+
WiFi._status = WL_DISCONNECTED;
55+
}
56+
}
57+
break;
58+
5059
case M2M_WIFI_RESP_CON_STATE_CHANGED:
5160
{
5261
tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg;
@@ -133,8 +142,9 @@ static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
133142
m2m_wifi_connect((char *)pstrProvInfo->au8SSID, strlen((char *)pstrProvInfo->au8SSID),
134143
pstrProvInfo->u8SecType, pstrProvInfo->au8Password, M2M_WIFI_CH_ALL);
135144
} else {
136-
WiFi._status = WL_CONNECT_FAILED;
145+
WiFi._status = WL_PROVISIONING_FAILED;
137146
//SERIAL_PORT_MONITOR.println("wifi_cb: Provision failed.\r\n");
147+
WiFi.beginProvision();
138148
}
139149
}
140150
break;
@@ -173,6 +183,8 @@ static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
173183
}
174184
WiFi._remoteMacAddress = 0;
175185
}
186+
187+
strcpy((char *)WiFi._ssid, pstrConnInfo->acSSID);
176188
}
177189
break;
178190

@@ -327,11 +339,17 @@ uint8_t WiFiClass::begin()
327339
millis() - start < 60000) {
328340
m2m_wifi_handle_events(NULL);
329341
}
342+
343+
memset(_ssid, 0, M2M_MAX_SSID_LEN);
344+
330345
if (!(_status & WL_CONNECTED)) {
331346
_mode = WL_RESET_MODE;
347+
} else {
348+
m2m_wifi_get_connection_info();
349+
350+
m2m_wifi_handle_events(NULL);
332351
}
333352

334-
memset(_ssid, 0, M2M_MAX_SSID_LEN);
335353
return _status;
336354
}
337355

@@ -481,12 +499,32 @@ uint8_t WiFiClass::startAP(const char *ssid, uint8_t u8SecType, const void *pvAu
481499
return _status;
482500
}
483501

484-
uint8_t WiFiClass::beginProvision(char *ssid, char *url)
502+
uint8_t WiFiClass::beginProvision()
503+
{
504+
return beginProvision(1);
505+
}
506+
507+
uint8_t WiFiClass::beginProvision(uint8_t channel)
485508
{
486-
return beginProvision(ssid, url, 1);
509+
// try to connect using begin
510+
if (begin() != WL_CONNECTED) {
511+
// failed, enter provisioning mode
512+
513+
uint8_t mac[6];
514+
char provSsid[13];
515+
516+
// get MAC address for provisioning SSID
517+
macAddress(mac);
518+
sprintf(provSsid, "wifi101-%.2X%2X", mac[1], mac[0]);
519+
520+
// start provisioning mode
521+
startProvision(provSsid, "wifi101", channel);
522+
}
523+
524+
return status();
487525
}
488526

489-
uint8_t WiFiClass::beginProvision(char *ssid, char *url, uint8_t channel)
527+
uint8_t WiFiClass::startProvision(const char *ssid, const char *url, uint8_t channel)
490528
{
491529
tstrM2MAPConfig strM2MAPConfig;
492530

@@ -500,16 +538,16 @@ uint8_t WiFiClass::beginProvision(char *ssid, char *url, uint8_t channel)
500538
strM2MAPConfig.u8ListenChannel = channel;
501539
strM2MAPConfig.u8SecType = M2M_WIFI_SEC_OPEN;
502540
strM2MAPConfig.u8SsidHide = SSID_MODE_VISIBLE;
503-
strM2MAPConfig.au8DHCPServerIP[0] = 0xC0; /* 192 */
504-
strM2MAPConfig.au8DHCPServerIP[1] = 0xA8; /* 168 */
505-
strM2MAPConfig.au8DHCPServerIP[2] = 0x01; /* 1 */
506-
strM2MAPConfig.au8DHCPServerIP[3] = 0x01; /* 1 */
541+
strM2MAPConfig.au8DHCPServerIP[0] = 192;
542+
strM2MAPConfig.au8DHCPServerIP[1] = 168;
543+
strM2MAPConfig.au8DHCPServerIP[2] = 1;
544+
strM2MAPConfig.au8DHCPServerIP[3] = 1;
507545

508-
if (m2m_wifi_start_provision_mode((tstrM2MAPConfig *)&strM2MAPConfig, url, 1) < 0) {
509-
_status = WL_CONNECT_FAILED;
546+
if (m2m_wifi_start_provision_mode((tstrM2MAPConfig *)&strM2MAPConfig, (char*)url, 1) < 0) {
547+
_status = WL_PROVISIONING_FAILED;
510548
return _status;
511549
}
512-
_status = WL_CONNECTED;
550+
_status = WL_PROVISIONING;
513551
_mode = WL_PROV_MODE;
514552

515553
memset(_ssid, 0, M2M_MAX_SSID_LEN);
@@ -527,6 +565,8 @@ uint8_t WiFiClass::beginProvision(char *ssid, char *url, uint8_t channel)
527565

528566
uint32_t WiFiClass::provisioned()
529567
{
568+
m2m_wifi_handle_events(NULL);
569+
530570
if (_mode == WL_STA_MODE) {
531571
return 1;
532572
}

src/WiFi101.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ typedef enum {
4444
WL_DISCONNECTED,
4545
WL_AP_LISTENING,
4646
WL_AP_CONNECTED,
47-
WL_AP_FAILED
47+
WL_AP_FAILED,
48+
WL_PROVISIONING,
49+
WL_PROVISIONING_FAILED
4850
} wl_status_t;
4951

5052
/* Encryption modes */
@@ -119,8 +121,8 @@ class WiFiClass
119121
uint8_t beginAP(const char *ssid, uint8_t key_idx, const char* key);
120122
uint8_t beginAP(const char *ssid, uint8_t key_idx, const char* key, uint8_t channel);
121123

122-
uint8_t beginProvision(char *ssid, char *url);
123-
uint8_t beginProvision(char *ssid, char *url, uint8_t channel);
124+
uint8_t beginProvision();
125+
uint8_t beginProvision(uint8_t channel);
124126

125127
uint32_t provisioned();
126128

@@ -171,6 +173,8 @@ class WiFiClass
171173
uint8_t startConnect(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo);
172174
uint8_t startAP(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo, uint8_t channel);
173175
uint8_t* remoteMacAddress(uint8_t* remoteMacAddress);
176+
177+
uint8_t startProvision(const char *ssid, const char *url, uint8_t channel);
174178
};
175179

176180
extern WiFiClass WiFi;

0 commit comments

Comments
 (0)