|
| 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 | + |
0 commit comments