Wifi Library for Arduino

So, with all these tutorials about running Edison on the linux side, I thought it might be nice to post something about Arduino and some extra add-ons for that. This post will be about adding the native wifi capability to your Edison sketches.

First, I’ve created a Wifi library for Arduino. Download the file and unzip it. Place it wherever you keep your Arduino libraries that are associated with Edison. For me, since most sketches work equivalently on Arduino and Edison, I use the same location for both: /Documents/Arduino/libraries/. If you’re using the default Edison Arduino configuration, then your files are in the same place.

Note: You might notice that this is an extremely bare-bones Arduino library. It has a single .h and a single .cpp file, which is all c++ needs to compile and run the library. Most Arduino libraries provide a keywords.txt file out of courtesy. I didn’t because, well, I guess I’m not that courteous? 

Next, open the Edison Arduino IDE. The library will be imported automatically. Let’s walk through the example script so that I can explain the code a little. Just in case you’re unfamiliar, you can get to the example script via one of these routes:

File –> Sketchbook –> libraries –> Wifi –> Examples –> connect_test

File –> Examples –> Wifi –> Examples –> connect_test

Wifi Example Sketch

Wifi Example Sketch

So, let’s step through it a bit at a time.

First, we import the Wifi library with the call:

#include <Wifi.h>

Then, we create the wifi object with

Wifi wifi;

In the setup, we configure our networks first with configure_norm and configure_enterprise.

wifi.configure_norm("Network1","WPA-Pass",1); // 1 for new configuration
wifi.configure_enterprise("EmployeeHotspot","yourhotspotname","yourhotspotpass",0); // 0 to add another network

If you have a standard WPA network configuration where you just select the network and type in the network passcode, then you’re just going to be using configure_norm. This is the vast majority of home and hotspot networks like your local coffee shop. The library is setup so that you can add multiple network configurations to your sketch, so that you can have the same Arduino sketch running in different places with automatic wifi connectivity. The networks will be tried in the order that they are listed in your sketch (so, here “Network1” would have preference over “EmployeeHotspot”), so if you have a preference be sure to order them as such. Finally, the numbers at the end of the configure whether this is your first network or not. So, set 1 for the very first network and 0 for each subsequent one no matter how  many you use.

Obviously, the only thing left to do is initiate the connection. After initiating, I program in a delay of 5 seconds because, ya know, love takes time.

// Initiate the connection

// Give some connection time

So, what can we do with the wifi library? Well, I’ve built in a quick loop with a function to just show what’s possible:

void loop() {

// Test connection by pinging google and returning the results
const char* ping_results = wifi.ping_google();

// put your main code here, to run repeatedly:

// Wait 1 minute before doing it again


This script pings google 3 times (3 is hard-coded in the ping_google script in wifi.cpp) and then displays the results to the Serial monitor. It waits 1 minute in between subsequent ping packets.

This is just a quick example of what can be done so you can work from it. A very handy function in the Edison Arduino IDE is the system() command that comes built in without any library calls. system() is a way to call the unix command line from within the Arduino IDE. So, if you’re hoping to use your Edison to push or post to a rest API, then you can do this simply with the curl function:

system("curl -X POST -d @filename.txt http://example.com/path/to/resource --header 'Content-Type:application/json'");

which I find to be a really straightforward solution.

Please note that, to my knowledge, the straight Arduino IDE does not have a built-in system() function, so this won’t be portable back to an Arduino should you switch. There’s a similar capability with Arduino Yun (since it runs linux) with process:

Process p;
// This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then
// sends the result to the grep command to look for a line containing the word
// "Signal:" the result is passed to this sketch:
p.runShellCommand("curl -X POST -d @filename.txt http://example.com/path/to/resource --header 'Content-Type:application/json'");

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *