Skip to content

Commit fddd719

Browse files
authored
Merge pull request #49 from agdl/WiFiSleep
WiFi sleep
2 parents 462cf70 + ac9ec14 commit fddd719

File tree

4 files changed

+199
-1
lines changed

4 files changed

+199
-1
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
3+
This example makes an http request every 10s
4+
and puts in sleep mode WiFi in the meanwhile
5+
6+
Circuit:
7+
WiFi shield 101 attached to an Arduino/Genuino board
8+
or Arduino/Genuino MKR1000 board
9+
10+
https://www.arduino.cc/en/Tutorial/Wifi101WiFiSleepingWebClient
11+
12+
created 18 Mar 2016
13+
by Arturo Guadalupi
14+
*/
15+
16+
#include <SPI.h>
17+
#include <WiFi101.h>
18+
19+
char ssid[] = "yourNetwork"; // your network SSID (name)
20+
char pass[] = "yourPassword"; // your network password
21+
int status = WL_IDLE_STATUS; // the Wifi radio's status
22+
23+
WiFiSSLClient client;
24+
25+
// server address:
26+
char server[] = "www.arduino.cc";
27+
28+
void setup() {
29+
//Initialize Serial and wait for port to open:
30+
Serial.begin(9600);
31+
32+
// check for the presence of the shield:
33+
if (WiFi.status() == WL_NO_SHIELD) {
34+
Serial.println("WiFi shield not present");
35+
// don't continue:
36+
while (true);
37+
}
38+
// attempt to connect to WiFi network:
39+
40+
while ( status != WL_CONNECTED) {
41+
Serial.print("Attempting to connect to WPA SSID: ");
42+
Serial.println(ssid);
43+
// Connect to WPA/WPA2 network:
44+
status = WiFi.begin(ssid, pass);
45+
46+
// wait 10 seconds for connection:
47+
delay(10000);
48+
}
49+
50+
// you're connected now, so print out the data:
51+
Serial.print("You're connected to the network");
52+
printCurrentNet();
53+
printWifiData();
54+
55+
Serial.println("Sleeping...");
56+
WiFi.sleepFor(10000); //Sleep for 10s
57+
}
58+
59+
void loop() {
60+
if (WiFi.isAwake()) {
61+
Serial.println("Awake!");
62+
httpRequest();
63+
listenToClient();
64+
WiFi.sleepFor(10000); //Sleep for 10s
65+
Serial.println("Sleeping");
66+
}
67+
}
68+
69+
void printWifiData() {
70+
// print your WiFi shield's IP address:
71+
IPAddress ip = WiFi.localIP();
72+
Serial.print("IP Address: ");
73+
Serial.println(ip);
74+
75+
// print your MAC address:
76+
byte mac[6];
77+
WiFi.macAddress(mac);
78+
Serial.print("MAC address: ");
79+
Serial.print(mac[5], HEX);
80+
Serial.print(": ");
81+
Serial.print(mac[4], HEX);
82+
Serial.print(": ");
83+
Serial.print(mac[3], HEX);
84+
Serial.print(": ");
85+
Serial.print(mac[2], HEX);
86+
Serial.print(": ");
87+
Serial.print(mac[1], HEX);
88+
Serial.print(": ");
89+
Serial.println(mac[0], HEX);
90+
91+
}
92+
93+
void printCurrentNet() {
94+
// print the SSID of the network you're attached to:
95+
Serial.print("SSID: ");
96+
Serial.println(WiFi.SSID());
97+
98+
// print the MAC address of the router you're attached to:
99+
byte bssid[6];
100+
WiFi.BSSID(bssid);
101+
Serial.print("BSSID: ");
102+
Serial.print(bssid[5], HEX);
103+
Serial.print(": ");
104+
Serial.print(bssid[4], HEX);
105+
Serial.print(": ");
106+
Serial.print(bssid[3], HEX);
107+
Serial.print(": ");
108+
Serial.print(bssid[2], HEX);
109+
Serial.print(": ");
110+
Serial.print(bssid[1], HEX);
111+
Serial.print(": ");
112+
Serial.println(bssid[0], HEX);
113+
114+
// print the received signal strength:
115+
long rssi = WiFi.RSSI();
116+
Serial.print("signal strength (RSSI): ");
117+
Serial.println(rssi);
118+
119+
// print the encryption type:
120+
byte encryption = WiFi.encryptionType();
121+
Serial.print("Encryption Type: ");
122+
Serial.println(encryption, HEX);
123+
Serial.println();
124+
}
125+
126+
void httpRequest() {
127+
128+
if (client.connect(server, 443)) {
129+
// Make a HTTP request:
130+
client.println("GET /asciilogo.txt HTTP/1.1");
131+
client.println("Host: arduino.cc");
132+
client.println("Connection: close");
133+
client.println();
134+
}
135+
else {
136+
Serial.println("connection failed");
137+
}
138+
}
139+
140+
void listenToClient()
141+
{
142+
unsigned long startTime = millis();
143+
bool received = false;
144+
145+
while ((millis() - startTime < 10000) && !received) { //try to listen for 10 seconds
146+
while (client.available()) {
147+
received = true;
148+
char c = client.read();
149+
Serial.write(c);
150+
}
151+
}
152+
client.stop();
153+
Serial.println();
154+
}
155+

keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ WiFiServer KEYWORD2
4444
WiFiSSLClient KEYWORD2
4545
WiFiMDNSResponder KEYWORD2
4646

47+
lowPowerMode KEYWORD2
48+
maxLowPowerMode KEYWORD2
49+
noLowPowerMode KEYWORD2
50+
sleepFor KEYWORD2
51+
isAwake KEYWORD2
52+
4753
#######################################
4854
# Constants (LITERAL1)
4955
#######################################

src/WiFi.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ WiFiClass::WiFiClass()
177177
_mode = WL_RESET_MODE;
178178
_status = WL_NO_SHIELD;
179179
_init = 0;
180+
_wakeUpMillis = 0;
180181
}
181182

182183
void WiFiClass::setPins(int8_t cs, int8_t irq, int8_t rst, int8_t en)
@@ -776,6 +777,33 @@ void WiFiClass::refresh(void)
776777
m2m_wifi_handle_events(NULL);
777778
}
778779

780+
void WiFiClass::lowPowerMode(void)
781+
{
782+
m2m_wifi_set_sleep_mode(M2M_PS_H_AUTOMATIC, true);
783+
}
784+
785+
void WiFiClass::maxLowPowerMode(void)
786+
{
787+
m2m_wifi_set_sleep_mode(M2M_PS_DEEP_AUTOMATIC, true);
788+
}
789+
790+
void WiFiClass::noLowPowerMode(void)
791+
{
792+
m2m_wifi_set_sleep_mode(M2M_NO_PS, false);
793+
}
794+
795+
void WiFiClass::sleepFor(uint32_t thisTime)
796+
{
797+
m2m_wifi_set_sleep_mode(M2M_PS_MANUAL, false);
798+
_wakeUpMillis = millis() + thisTime + 150; //150 ms must elapse for complete awakening
799+
m2m_wifi_request_sleep(thisTime);
800+
}
801+
802+
bool WiFiClass::isAwake(void)
803+
{
804+
return (millis() > _wakeUpMillis);
805+
}
806+
779807
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl)
780808
{
781809
IPAddress ip;

src/WiFi101.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,21 @@ class WiFiClass
154154
uint8_t ping(const char* hostname, uint8_t ttl = 128);
155155
uint8_t ping(const String &hostname, uint8_t ttl = 128);
156156
uint8_t ping(IPAddress host, uint8_t ttl = 128);
157-
157+
158158
void refresh(void);
159159

160+
void lowPowerMode(void);
161+
void maxLowPowerMode(void);
162+
void noLowPowerMode(void);
163+
void sleepFor(uint32_t thisTime);
164+
bool isAwake(void);
165+
160166
private:
161167
int _init;
162168
char _version[9];
169+
170+
uint32_t _wakeUpMillis;
171+
163172
uint8_t startConnect(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo);
164173
uint8_t startAP(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo, uint8_t channel);
165174
};

0 commit comments

Comments
 (0)