Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active July 28, 2016 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlynorama/9317806 to your computer and use it in GitHub Desktop.
Save carlynorama/9317806 to your computer and use it in GitHub Desktop.
This stand alone sketch checks the WiFi functionality of an Arduino board with the WiFi shield or an Intel Galileo board.
/*
// This code is to test WiFi function on Arduino Boards
// It was complied from various examples which can be found:
//
// http://arduino.cc/en/Tutorial/ConnectNoEncryption
// http://arduino.cc/en/Tutorial/ConnectWithWPA
// http://arduino.cc/en/Tutorial/WiFiWebClientRepeating
// http://arduino.cc/en/Reference/WiFiClientConnect
//
// Carlyn Maw 2014
// www.carlynorama.com
// This code is released to the public domain
*/
#include <WiFi.h>
char ssid[] = "CHANGE_TO_YOUR_NETWORK_NAME"; // your network SSID (name)
char pass[] = "CHANGE_TO_YOUR_NETWORK_PASSWORD"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// ------------------------------------------------------------- setup()
void setup() {
//Delay start so serial communication and sketch updating
//dont intrfere with each other on the Intel Galileo board
delay(5000);
//Intialize serial
Serial.begin(9600);
//Both of the below functions connect to the wifi card
//and verify that a connection to the local network
//has been made.
wifiNotEncrypted();
//wifiEncrypted();
connectToTestPage();
}
// --------------------------------------------------------- END setup()
// -------------------------------------------------------------- loop()
void loop() {
// put your main code here, to run repeatedly:
}
// ---------------------------------------------------------- END loop()
// -------------------------------------------------- wifiNotEncrypted()
void wifiNotEncrypted() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to open SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network\n");
printCurrentNet();
printWifiData();
}
// ---------------------------------------------- END wifiNotEncrypted()
//------------------------------------------------------ wifiEncrypted()
void wifiEncrypted() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network\n");
printCurrentNet();
printWifiData();
}
//-------------------------------------------------- END wifiEncrypted()
//------------------------------------------------------ printWifiData()
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
//-------------------------------------------------- END printWifiData()
//---------------------------------------------------- printCurrentNet()
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println();
}
//------------------------------------------------ END printCurrentNet()
//-------------------------------------------------- connectToTestPage()
void connectToTestPage() {
char server[] = "www.arduino.cc";
WiFiClient client;
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
}
//---------------------------------------------- END connectToTestPage()
//---------------------------------------------------- connectToGoogle()
void connectToGoogle() {
char server[] = "www.google.com";
WiFiClient client;
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
}
//------------------------------------------------ END connectToGoogle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment