In this section, we are going to build the Arduino sketch that we will use to control the switch via WiFi.

To do so, we are going to use the aREST library that implements a REST API for Arduino. This way, we can have an easy access to the pins of the Arduino board, and also to the variable in which the power measurement is stored.

Note that only the most important parts of the code are detailed here, please get to the complete sketch on GitHub to have the complete code that you can upload to the Arduino board.

It starts by including all the required libraries:

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <CC3000_MDNS.h>
#include <aREST.h>

And declaring the pin on which the powerswitch module is connected:

const int relay_pin = 8;
Then, we create the instance of the aREST object that we will use to handle the requests coming via the WiFi connection:
aREST rest = aREST();
We also need to define on which port we want to the WiFi chip to listen to. For convenience, we will use the port 80, so you can directly command your Arduino board from a web browser:
#define LISTEN_PORT  80
We also create an instance of the CC3000 server:
Adafruit_CC3000_Server restServer(LISTEN_PORT);
We also need to create an instance of the MDNS server, so we can send commands to the Arduino board without having to type the board IP address to access it:
MDNSResponder mdns;
After that, we declare the relay pin as an output:
We can also set a name and an ID for the device, that will be returned at each call of the board via the aREST library:
After this step, we set a name for the Arduino board on the network, for example “arduino”. This means that the board will be accessible by the name arduino.local on your local network:
if (!mdns.begin("arduino", cc3000)) {
Finally, still in the setup() function we start the CC3000 server and wait for incoming connections:
Serial.println(F("Listening for connections..."));

In the loop() function of the sketch, we update the MDNS server:


And process any incoming connection using the aREST library:

Adafruit_CC3000_ClientRef client = restServer.available();

Note that you can find all the code for this part inside the GitHub repository of the project:

It’s now time to test this sketch on our project. Download the code from the GitHub repository, and make sure that you modify the name and the password of the WiFi network on which the WiFi chip will connect to. Then, upload the code to the Arduino board, and open the Serial monitor. This is what you should see:

Listening for connections...

Now, close the Serial monitor, and open your web browser. You can now make direct calls to the REST API running on the board to control the pins of the Arduino board. For example, to turn the switch on, just type:

You should hear the relay switching, the device connected to the project should turn on and you should have a confirmation message inside your browser:

To switch the device off again, just type:


Note that these commands will work from any devices connect to the same local network as the Arduino board. For example, you can use your smartphone to control your Arduino board with the same commands.

If it doesn’t work, the first thing to do is to use the IP address of the board in place of the arduino.local name. You can get the IP address of the board by looking at the messages displayed on the Serial monitor when starting the project:

This guide was first published on Sep 08, 2014. It was last updated on Sep 08, 2014.

This page (Remote Control) was last updated on Jun 25, 2014.

Text editor powered by tinymce.