[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | This example connects to an unencrypted Wifi network.
|
---|
| 4 | Then it prints the MAC address of the Wifi shield,
|
---|
| 5 | the IP address obtained, and other network details.
|
---|
| 6 |
|
---|
| 7 | Circuit:
|
---|
| 8 | * WiFi shield attached
|
---|
| 9 |
|
---|
| 10 | created 13 July 2010
|
---|
| 11 | by dlf (Metodo2 srl)
|
---|
| 12 | modified 31 May 2012
|
---|
| 13 | by Tom Igoe
|
---|
| 14 | */
|
---|
| 15 | #include "Arduino.h"
|
---|
| 16 | #include <SPI.h>
|
---|
| 17 | #include <WiFi.h>
|
---|
| 18 |
|
---|
| 19 | void printCurrentNet();
|
---|
| 20 | void printWifiData();
|
---|
| 21 | void httpRequest();
|
---|
| 22 |
|
---|
| 23 | char ssid[] = "AVRGUEST"; // your network SSID (name)
|
---|
| 24 | char pass[] = "MicroController"; // your network password
|
---|
| 25 | int status = WL_IDLE_STATUS; // the Wifi radio's status
|
---|
| 26 |
|
---|
| 27 | WiFiClient client;
|
---|
| 28 |
|
---|
| 29 | void setup() {
|
---|
| 30 | WiFi = WiFiClass();
|
---|
| 31 | client = WiFiClient();
|
---|
| 32 |
|
---|
| 33 | delay(500); // Waiting for initialization
|
---|
| 34 |
|
---|
| 35 | // SPI.begin();
|
---|
| 36 | //Initialize Serial5 and wait for port to open:
|
---|
| 37 | Serial5.begin(9600);
|
---|
| 38 | while (!Serial5) {
|
---|
| 39 | ; // wait for Serial5 port to connect. Needed for Leonardo only
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // check for the presence of the shield:
|
---|
| 43 | if (WiFi.status() == WL_NO_SHIELD) {
|
---|
| 44 | Serial5.println("WiFi shield not present");
|
---|
| 45 | // don't continue:
|
---|
| 46 | while(true);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | // attempt to connect to Wifi network:
|
---|
| 50 | while ( status != WL_CONNECTED) {
|
---|
| 51 | Serial5.print("Attempting to connect to WPA SSID: ");
|
---|
| 52 | Serial5.println(ssid);
|
---|
| 53 | // Connect to WPA/WPA2 network:
|
---|
| 54 | status = WiFi.begin(ssid, pass);
|
---|
| 55 |
|
---|
| 56 | // wait 10 seconds for connection:
|
---|
| 57 | delay(10000);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // you're connected now, so print out the data:
|
---|
| 61 | Serial5.print("You're connected to the network");
|
---|
| 62 | printCurrentNet();
|
---|
| 63 | printWifiData();
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void loop() {
|
---|
| 68 | // check the network connection once every 10 seconds:
|
---|
| 69 | delay(10000);
|
---|
| 70 | printCurrentNet();
|
---|
| 71 |
|
---|
| 72 | // Trying to connect to http://hasthelargehadroncolliderdestroyedtheworldyet.com/
|
---|
| 73 | Serial5.println("Trying to connect to : www.hasthelargehadroncolliderdestroyedtheworldyet.com :");
|
---|
| 74 | httpRequest();
|
---|
| 75 | while( client.available() )
|
---|
| 76 | {
|
---|
| 77 | Serial5.print((char)(client.read()));
|
---|
| 78 | }
|
---|
| 79 | Serial5.println("END");
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void printWifiData() {
|
---|
| 83 | // print your WiFi shield's IP address:
|
---|
| 84 | IPAddress ip = WiFi.localIP();
|
---|
| 85 | Serial5.print("IP Address: ");
|
---|
| 86 | Serial5.println(ip);
|
---|
| 87 | Serial5.println(ip);
|
---|
| 88 |
|
---|
| 89 | // print your MAC address:
|
---|
| 90 | byte mac[6];
|
---|
| 91 | WiFi.macAddress(mac);
|
---|
| 92 | Serial5.print("MAC address: ");
|
---|
| 93 | Serial5.print(mac[5],HEX);
|
---|
| 94 | Serial5.print(":");
|
---|
| 95 | Serial5.print(mac[4],HEX);
|
---|
| 96 | Serial5.print(":");
|
---|
| 97 | Serial5.print(mac[3],HEX);
|
---|
| 98 | Serial5.print(":");
|
---|
| 99 | Serial5.print(mac[2],HEX);
|
---|
| 100 | Serial5.print(":");
|
---|
| 101 | Serial5.print(mac[1],HEX);
|
---|
| 102 | Serial5.print(":");
|
---|
| 103 | Serial5.println(mac[0],HEX);
|
---|
| 104 |
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void printCurrentNet() {
|
---|
| 108 | // print the SSID of the network you're attached to:
|
---|
| 109 | Serial5.print("SSID: ");
|
---|
| 110 | Serial5.println(WiFi.SSID());
|
---|
| 111 |
|
---|
| 112 | // print the MAC address of the router you're attached to:
|
---|
| 113 | byte bssid[6];
|
---|
| 114 | WiFi.BSSID(bssid);
|
---|
| 115 | Serial5.print("BSSID: ");
|
---|
| 116 | Serial5.print(bssid[5],HEX);
|
---|
| 117 | Serial5.print(":");
|
---|
| 118 | Serial5.print(bssid[4],HEX);
|
---|
| 119 | Serial5.print(":");
|
---|
| 120 | Serial5.print(bssid[3],HEX);
|
---|
| 121 | Serial5.print(":");
|
---|
| 122 | Serial5.print(bssid[2],HEX);
|
---|
| 123 | Serial5.print(":");
|
---|
| 124 | Serial5.print(bssid[1],HEX);
|
---|
| 125 | Serial5.print(":");
|
---|
| 126 | Serial5.println(bssid[0],HEX);
|
---|
| 127 |
|
---|
| 128 | // print the received signal strength:
|
---|
| 129 | long rssi = WiFi.RSSI();
|
---|
| 130 | Serial5.print("signal strength (RSSI):");
|
---|
| 131 | Serial5.println(rssi);
|
---|
| 132 |
|
---|
| 133 | // print the encryption type:
|
---|
| 134 | byte encryption = WiFi.encryptionType();
|
---|
| 135 | Serial5.print("Encryption Type:");
|
---|
| 136 | Serial5.println(encryption,HEX);
|
---|
| 137 | Serial5.println();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void httpRequest() {
|
---|
| 141 | // close any connection before send a new request.
|
---|
| 142 | // This will free the socket on the WiFi shield
|
---|
| 143 | client.stop();
|
---|
| 144 |
|
---|
| 145 | // if there's a successful connection:
|
---|
| 146 | if (client.connect("www.hasthelargehadroncolliderdestroyedtheworldyet.com", 80)) {
|
---|
| 147 | Serial5.println("connecting...");
|
---|
| 148 | // send the HTTP PUT request:
|
---|
| 149 | client.println("GET / HTTP/1.1");
|
---|
| 150 | client.println("Host: www.hasthelargehadroncolliderdestroyedtheworldyet.com");
|
---|
| 151 | //client.println("User-Agent: ArduinoWiFi/1.1");
|
---|
| 152 | client.println("Connection: close");
|
---|
| 153 | client.println();
|
---|
| 154 |
|
---|
| 155 | if( client.connected() )
|
---|
| 156 | {
|
---|
| 157 | Serial5.println("\tClient connected");
|
---|
| 158 | while( client.available() == 0 )
|
---|
| 159 | {
|
---|
| 160 | //Waiting for data
|
---|
| 161 | if( !client.connected() )
|
---|
| 162 | {
|
---|
| 163 | Serial5.println("\tClient disconnected !");
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
| 170 | Serial5.println("\tClient not connected");
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | }
|
---|
| 174 | else {
|
---|
| 175 | // if you couldn't make a connection:
|
---|
| 176 | Serial5.println("\tconnection failed");
|
---|
| 177 | }
|
---|
| 178 | }
|
---|