Saturday, February 20, 2016

Getting Started with the Arduino WiFi Shield 101

After verifying the Arduino Zero was functional (previous blog entry), the WiFi Shield 101 was connected (via stacking headers) and tested out.  Again, there is not too much to be added to the basic documentation on the Arduino site, this just documents the links for later:

Arduino WiFi Shield 101
Arduino WiFi Shield 101 Getting Started
Arduino WiFi 101 Software Library


The shield requires a software library, which is first downloaded via the Library Manager (Sketch | Include Library | Manage Libraries...):


After installing the library, the sample code for Scan for available networks was compiled/uploaded and successfully listed my local network.  I then combined a few sample code snippets into the following, and was able to connect to the local network with WPA2:

 #include <SPI.h>  
 #include <WiFi101.h>  
   
 char ssid[] = "mynetwork";   // your network SSID (name)   
 char pass[] = "mypassword";  // your network password  
 int status = WL_IDLE_STATUS;   // the Wifi radio's status  
   
 void setup()   
 {  
  // initialize serial:  
  Serial.begin(9600);  
   
  if (WiFi.status() == WL_NO_SHIELD)  
  {  
   Serial.println("WiFi shield not present");  
   // don't continue:  
   while (true);  
  }  
    
  // attempt to connect using WPA2 encryption:  
  Serial.println("");  
  Serial.println("Attempting to connect to WPA network...");  
  status = WiFi.begin(ssid, pass);  
   
  if (status != WL_CONNECTED)  
  {   
   // if you're not connected, stop here:  
   Serial.println("Couldn't get a wifi connection");  
   while(true);  
  }   
  else  
  {  
   // if you are connected, print out info about the connection:  
   Serial.println("Connected to network");  
   printWifiStatus();  
  }  
 }  
   
 void loop()  
 {  
  // do nothing  
 }  
   
 void printWifiStatus()   
 {  
  // print the SSID of the network you're attached to:  
  Serial.print("SSID: ");  
  Serial.println(WiFi.SSID());  
   
  // print your WiFi shield's IP address:  
  IPAddress ip = WiFi.localIP();  
  Serial.print("IP Address: ");  
  Serial.println(ip);  
   
  // print your gateway address:  
  IPAddress gateway = WiFi.gatewayIP();  
  Serial.print("Gateway: ");  
  Serial.println(gateway);  
    
  // print the received signal strength:  
  long rssi = WiFi.RSSI();  
  Serial.print("signal strength (RSSI):");  
  Serial.print(rssi);  
  Serial.println(" dBm");  
 }  
   

The sketch worked as designed, and the Arduino was visible on my routers list of connected devices.  Unfortunately I can find no way to set the device name for the WiFi shield, but that is a minor complaint when so much functionality worked "out of the box".


Next up will be to work with the onboard real-time clock (RTC), set the RTC automatically via an NTP call, and perform some basics to serve a webpage on the local network.



No comments:

Post a Comment