A new Wi-Fi Shield to connect your Arduino to the Internet

By on February 11, 2014
Pin It


Since now on, it will be easy to provide your Arduino with Internet connectivity by using this shield. The shield sports a TCP/IP stack manager, in order to free up the Arduino from some basic tasks. It’s also essential to equip the board with a library, that communicates with the TCP/IP manager and makes it easier to program the Arduino and to let  it communicate with other computers via the Internet.


The WiFi shield


Despite the proliferation of hardware to connect Arduino with the web, and especially despite YUN, we considered useful to design and propose a new WiFi shield for Arduino, which replaces the one already presented.

This time we indeed added an interface processor  that manages the TCP/IP protocol: a definitely too challenging task for Arduino. Anyway a network interface card becomes really useful if it’s driven by software which simplifies as much as possible its use, especially in a context of reduced hardware resources such as with Arduino. For this reason we supplied the card with a Arduino library, that is easier than that for more complex hardware such as that of YUN.

The hardware is essentially based on a radio element (MRF24WB0MA) and a MCW1001A processor by Microchip. The power supply is 3.3V (using a voltage regulator from the 5V of Arduino) but the processor also bears TTL levels. The radio element has also an antenna wired on its base. The processor is connected to the radio element via SPI lines (SDO, SDI, SCK) and other control lines such as CS, INT, RST, HIBERNATE, WP.

The board is then connected to the host (Arduino) with a two-wires serial interface in TTL logic (RX, TX). This forced us to use a software simulated serial port with a customized version of the SoftwareSerial library. The customization was necessary because of the two stop bits mode, but also to adjust the optimum speed and make the use of the library transparent. In fact, this version of the SoftwareSerial is included in the library. But this mode of connection precludes the possibility of using the standard SoftwareSerial library for other uses in the same sketch.


Simulated serial port could be connected to different Arduino pins. By two jumpers you can choose whether to use D2 or D11 for RX (default D2) and D3 or D10 for TX (default D3). In case you decide to change the default values, you must edit the file MWIFI.h and change the following lines:

#define RXPIN 2

#define TXPIN 3

Hence, chosen pins, along with the D7 pin used to reset the WiFi card, cannot be used for other purposes. Anyway  the MCW1001A processor provides additional digital pins. The first four are connected to as many LEDs, while the other three are connected  to as many connectors along with power (5V or 3V) and GND. In fact, these three pins (GPIO5, GPIO 6, GPIO7) can also manage levels of 5V with a current of about 25 mA maximum (in/out). The GPIO pins can be activated by the library functions, but the first two LEDs are reserved to the library to indicate booting and network connection.

As mentioned, the MCW1001A processor manages the TCP/IP stack, running the basic protocol by using its internal RAM for transmission and reception buffers, as well as for storing various parameters. Anyway the processor, not having a non-volatile memory available, must be reconfigured each time you reboot it.

In any case, the low-level management is handled by the library that provides a set of functions to open network sockets or manage the HTTP protocol.




The library covers both Server and Client functionalities. It can be used when we want to give to Arduino the task of server, so that it reacts and responds to messages from another computer. Or the library can be used to give to Arduino the ability to connect on its own initiative to a server computer to send, for example, sensor measurements. The connection is established in TCP/IP via entities named network sockets that correspond to the concept of the socket to which connect a communication cable. So there is a network socket for each of the two computers that want to talk. A network socket is created as part of a port number. The port numbers are application sub-addresses in an IP address. Numbers less than 1000 are reserved for services publicly recognized as the Mail, FTP, Web (port 80) etc..



The MWiFi library

Download the Library for WiFi shield 

The MWiFi library should be positioned (after decompression) in the “libraries” folder of the Arduino IDE, like other libraries. It must be used including the file  <MwiFi.h> in the sketches. At this point it should be instantiated as an object and can be used. The first function to recall is begin() which is used to initialize the board:


#include <MWiFi.h>
void setup()

The start-up of the board  determines the lighting of the first LED. Now try to connect the Arduino to the network using an access point that could be that of the home WiFi router, for example with name (SSID)

WIFI.ConnSetOpen (“your-SSID”);

in case the line is not-protected, or:

WIFI.ConnSetWPA (“your-SSID”, “your-pwd”);

in case the line has WPA protection with password “your-pwd“.

Previous functions are used to prepare the card to connection. But the real connection is given  by calling the function:


If the connection is made, the second LED will light. Be careful though, because in case of a protected network, the connection may take also more than half a minute, as the controller of the card must encode the key using the password. The current version of the library provides an automatic reset of Arduino in case the connection is lost. Even errors detected by the controller cause an automatic reset. In this way, a blockage is avoided and the system can be “unattended”.

For your convenience, direct functions connection have also been added: these prepare the connection and perform it in a single step. Also the ability to generate the numeric key from the password, in order to then use it instead of the password, has been added. In fact the access with password, needing every time to process the numeric key, may take up to a minute, while the key access is very fast.

The router assigns a dynamic IP address to the Arduino, because this is the default behavior of the board. But, if you want, you can impose a fixed address. The address assigned may be required by a function of the library.

At this point choose to let Arduino act as server. First, call the function openServerTCP(), which creates the listener on a certain port (for example 5000), and then put in loop the receiving of a possible link request:

int ssocket=WIFI.openServerTCP(5000);

void loop ()


int csocket=WIFI.pollingAccept(ssocket);


The integer variables ssocket and csocket are references (handle) respectively to the server and connection network socket  to any computer that wants to make the link. The function pollingAccept() returns a number less than 255 if the connection has been requested, or 255 if there is no incoming connection request.

If the connection is established you will be able to send or receive messages referring to this csocket. For example, to receive a record, that is a string ending with a line-feed, you can use the function:


A “null-terminated string” will be returned but without the line-feed. In this case, the library uses a default buffer of 81 characters (but its length can be modified in its define). So you do not need to provide it. However, there are other possibilities. If you want to respond you can use the function:


The variable named “answer” corresponds to a char buffer. But it must be a “null terminated string”. NOTE: when you use a “null-terminated string” means that you do not have to provide the length of the useful characters in the array, because the function automatically calculates it, being based on the null terminating character. Most of the functions  manage and produce this kind of string, not to be confused with the String object also present in the language reference of Arduino.

In this simple way you have established and used a Wi-Fi connection with a remote computer. In the library, among the examples given, there’s one of a server called CommandServer that allows to control  Arduino by using a telnet program on the remote computer. To simplify the testing, a Java program that works as telnet has been added.

If you want to make Arduino act as a “client” that connects to a server, the situation is even simpler, because you only have to create a connection network socket:

int csocket=WIFI.openSockTCP(“”,5000);

And if csocket is valid (less than 255), it means that the connection with the computer, at the address on port 5000, has been established. At this point you can use the reading and writing functions previously seen. Among the examples, there’s one (SendData) that connects to a remote server to send at regular intervals readings of sensors. Obviously a server program on the remote computer is needed. To ease testing, along with the library, a Java program that receives the data and downloads them on a file by adding a time-stamp (to overcome the lack of an RTC-real-time clock on Arduino)  was provided.

In addition to these base features, the library has all the necessary functions to define various parameters such as: the masquerading address of the network (default, any Gateway address, the reading of the board’s MAC etc.. In particular, there are functions to detect the access-points present and visible in the environment. For example, to detect all the networks present:

int nn=WIFI.scanNets()

The nn, integer, variable will contain the number of the detected networks. While the function:


will return the characteristics (as records) of the i-th detected network. Finally, for convenience, a function that returns the name of the more powerful (in terms of signal) unsecured network has been provided.

With the library documentation all the features available will be described. The library contains a help and is documented in the code files (in particular the.h files).

But the library is not only about connection and network socket management. It includes a derived class (and therefore specialized) that handles the HTTP protocol: the Web protocol.

The HTTP protocol is a protocol that contemplates request and response, always. Both request and response let travel in the network packets composed by some headers and out-and-out data (such as HTML pages, images, videos or even only text).




To free up the user from this whole issue, the Http library creatres these packages using the PROGMEM mode, i.e. the possibility to put constants and texts in the flash area. NOTE: The HTTP protocol is a text protocol: it only uses characters. The use of flash memory for the texts saves the small ram of Arduino.


The HTTP library

Being a subclass of MWiFi, the Http library inherits all the features of MWiFi, but if you want to use the new functions must include the file HTTPlib.h (instead of MwiFi.h) and instantiate an HTTP object:



Regarding the connection with an access point, and the management of network sockets everything is as described before (maybe now we will choose the port 80). Anyway also this time we have to decide whether to make Arduino act as a server (this time Web Server) or as a client accessing a web server application (such as Tomcat, GlassFish, JBoss, PHP etc.. .)

Suppose you want to create a Web Server to be queried with any browser. To make Arduino operate as Web Server you have to prepare the resources that it can provide. That is the response html pages. These pages will be stored in PROGMEM areas for the reasons mentioned above. For example:

prog_char pageIndex [] PROGMEM =

“<html> <head>”

“<title> Index </


Now you must link these buffers in memory with the names of the resources to be invoked via the browser. Resources names are the local path part of the URL (or URI), i.e. the file name into a complete URL (eg: http://www.my.com/mypage.html ). In this minimal context, the resources to be invoked will be identified only by the name, with no extension. So it comes to associate a page stored with its corresponding Internet name (eg “/ index”).

Actually this page need to be sent, hence you must connect the resource name with a function which will send it. To make the mechanism as automatic as possible, a structure or, better a tipedef named Webres, has been prepared. This structure is formed by the union of two fields: the resource name and the function name (which in C, corresponds to an address). It is, therefore, to form so many name-function pairs to be passed to the function getRequest(),which will launch the correct function (call-back function), or send a standard response “Not Found” message, in case the name does not match up with any of those predefined.

This code shows the example of the construction of an array of 8 Webres structures and its insertion in the getRequest() call.

WEBRES rs[8]=












void loop()




Every second field of the structures corresponds to the call-back function that getRequest () will launch. The call-back function you will send  the buffer corresponding to the selected page and its prototype expected to be of void type (ie it returns nothing) and has a single argument,  a pointer to a null-terminated string supplied by the caller (see below):

void pindex(char *query)




Summarising:  placing getRequest() in the loop, it will take care of the entire management of the request. It will detect the used  mode, GET or POST, behaving accordingly (the data are contained in a different way) and will launch the function corresponding to the request (or a message “Not Found”).

The call-back function pindex() described above, however, does not but to send in response a static HTML page, that is defined in a fixed way. Arduino Web Server so defined it is not very useful, because it was presumed that it can be used to read values ​​from sensors or switch outputs. To achieve this, the html response page must be built at the moment containing the values ​​that you want to read. That is a dynamic page. But it would be too expensive to build, within the call-back functions, the entire page. To simplify the task it was provided to define the page “one-off” as a static page, but being able to create tags inside (labels placeholders) in the position that you want to complete by the moment. For this purpose there is an alternative to sendResponse(), that is the sendDynResponse()function, which tracks down and replaces the tags and sends the page. The replacement is done sequentially going to run an array of strings arranged at the moment. Hence: the first tag encountered is replaced with the first string of the array, and so on. The tag used is the ‘@’ character. Only one should be used regardless of the length of the string that will replace it.

In the example, three tags will be replaced by three strings that represent the values ​​of three digital inputs.

prog_char pagerdigital[] PROGMEM=



“<td><div align=’center’>@</div></td>”

“<td><div align=’center’>@</div></td>”

“<td><div align=’center’>@</div></td></tr>”



void rdigital(char *query)


 char *val[3];

 if(digitalRead(4)) val[0]=ON;else val[0]=OFF;

 if(digitalRead(5)) val[1]=ON;else val[1]=OFF;

 if(digitalRead(12))val[2]=ON;else val[2]=OFF;



You must pass to the function sendDynResponse() the array of strings and its size. To make Arduino act following commands launched from browser (for example, by the form buttons), you must read the data sent by the Request along with the name of the resource. The situation is different if the request came in the form of GET instead of POST (the two cornerstone methods of the http protocol). It is, in any case, about using that argument passed to the call-back function by getRequest().

In the first case, the data are represented by name-value pairs that identify a parameter. The parameters are appended to the name of the resource in a format that encodes spaces and special characters, which can be named query-string. The query-string is always provided (even if zero-length) to the call-back function (it’s part of its prototype). Then we can use the getParameter() function to retrieve the value (always a string) of the parameter with a  certain name.


void pwmset(char *query)


 char *pwmval;                                      


 if (pwmval!=NULL)

  {int pv;sscanf(pwmval,”%d”,&pv);



 if (pwmval!=NULL)

   {int pv;sscanf(pwmval,”%d”,&pv);





In the second case, instead, the query-string contains values ​​that may be in the query-string format (as generally the forms do) or in a whatsoever format. However, you must consider that the buffer which contains the query-string is provided by the library and has a length of 64 characters (but you can redefine it using “define” on HTTPlib.h). The data in excess are lost.

In the examples there is a complete Web Server that allows to read analog values ​​and digital values, enable and disable digital output and then adjust two PWM outputs. The sketch is very compact (half consists of html pages in PROGMEM) thanks to the automation produced by functions getRequest() and sendDynResponse().




If, instead, you want to use Arduino as a client of  a Web Application Server (or a more simple CGI), you will use the functions sendRequest() and getResponse(). SendRequest is actually composed by two separate functions depending on which mode you want to use: GET or POST.

If you use SendRequestGET() it will provide both the name of the resource and parameters in a single query-string. If you  use sendRequestPOST() it will separately provide the name of the resource and the data placed in a buffer of null-terminated string kind , in this way:





Or this:

sprintf(rec,”%d %d %d %d”,an1,an2,d1,d2);


In case of using sendRequestGET(), the query-string was formed with the help of the function addParameter(). First time initializing the query-string with the name of the resource (null), and then with the name-value pairs for the individual parameters.

Function getResponse(), is used to retrieve the response from the server. This can also be formed by a number of data in various formats: from HTML page to data in XML, JSON or CSV (comma separated values).

If the data cannot be contained in a single buffer, you can call the function getNextResponseBuffer() in a loop until it returns 0.

This is an example of elemental connection to receive and dispatch records.


#include <MwiFi.h>










if (!OpenCom)


if(socket<255) OpenCom=true;

if (OpenCom)







Sketch example




R1: 10 kohm (0805)
R2: 4,7 kohm (0805)
R3: 100 kohm (0805)
R4: 10 kohm (0805)
R5: 1 Mohm (0805)
R6: –
R7: –
R8: 1 kohm (0805)
R9: 1,5 kohm (0805)
R10: 4,7 kohm (0805)
R11: 10 kohm (0805)
R12: 330 ohm (0805)
R13: 330 ohm (0805)
R14: 330 ohm (0805)
R15: 330 ohm (0805)

C1: 100 nF (0805)
C2: 220 µF 6,3 VL (D)
C3: 22 pF (0805)
C4: 22 pF (0805)
C5: 100 nF (0805)
C6: 100 nF (0805)
C7: 10 µF 35 VL (B)
C8: 10 µF 35 VL (B)
C9: 100 nF (0805)
C10: 220 µF 6,3 VL (D)

U2: MCW1001A
U3: TC1262-3.3 (SOT-223)

T1: BC817

Q1: 8 MHz (HCX-7SB)

RST: switch

LD1: LED (0805)
LD2: LED (0805)
LD3: LED (0805)
LD4: LED (0805)

The Store

This WiFi shield for Arduino is available in our store


About Boris Landoni

Boris Landoni is the technical manager of Open-Electronics.org. Skilled in the GSM field, embraces the Open Source philosophy and its projects are available to the community.


  1. Pingback: A new Wi-Fi Shield to connect your Arduino to t...

Leave a Reply