Skip to content

Commit 6d9d865

Browse files
committed
Added WiFi methods to sleep
1 parent 5da947c commit 6d9d865

File tree

4 files changed

+200
-1
lines changed

4 files changed

+200
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ WiFiServer KEYWORD2
4444
WiFiSSLClient KEYWORD2
4545
WiFiMDNSResponder KEYWORD2
4646

47+
lowPowerMode KEYWORD2
48+
noLowPowerMode KEYWORD2
49+
sleepFor KEYWORD2
50+
isAwake KEYWORD2
51+
4752
#######################################
4853
# Constants (LITERAL1)
4954
#######################################

src/WiFi.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,37 @@ void WiFiClass::refresh(void)
771771
m2m_wifi_handle_events(NULL);
772772
}
773773

774+
void WiFiClass::lowPowerMode(void)
775+
{
776+
m2m_wifi_set_sleep_mode(M2M_PS_DEEP_AUTOMATIC, true);
777+
}
778+
779+
void WiFiClass::noLowPowerMode(void)
780+
{
781+
m2m_wifi_set_sleep_mode(M2M_NO_PS, false);
782+
}
783+
784+
void WiFiClass::sleepFor(uint32_t thisTime)
785+
{
786+
m2m_wifi_set_sleep_mode(M2M_PS_MANUAL, false);
787+
_sleepTime = thisTime + 150; //150 ms must elapse for complete awakening
788+
_startSleepTime = millis();
789+
m2m_wifi_request_sleep(thisTime);
790+
}
791+
792+
bool WiFiClass::isAwake(void)
793+
{
794+
if (_sleepTime == 0)
795+
return true;
796+
797+
if (millis() - _startSleepTime > _sleepTime) {
798+
_sleepTime = 0;
799+
return true;
800+
}
801+
else
802+
return false;
803+
}
804+
774805
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl)
775806
{
776807
IPAddress ip;

src/WiFi101.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,20 @@ class WiFiClass
152152
uint8_t ping(const char* hostname, uint8_t ttl = 128);
153153
uint8_t ping(const String &hostname, uint8_t ttl = 128);
154154
uint8_t ping(IPAddress host, uint8_t ttl = 128);
155-
155+
156156
void refresh(void);
157157

158+
void lowPowerMode(void);
159+
void noLowPowerMode(void);
160+
void sleepFor(uint32_t thisTime);
161+
bool isAwake(void);
162+
158163
private:
159164
int _init;
160165
char _version[9];
166+
167+
unsigned long _startSleepTime, _sleepTime;
168+
161169
uint8_t startConnect(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo);
162170
uint8_t startAP(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo, uint8_t channel);
163171
};

0 commit comments

Comments
 (0)