Let's now see the Arduino sketch for this project. You will need to have the Arduino IDE installed on your computer, as well as the Adafruit CC3000 library and the aREST library. To install the library, simply clone the Git repository or extract the files in your Arduino /libraries folder.

The sketch basically connects the CC3000 WiFi chip to your WiFi network, creates a web server on the Arduino board, and then start listening for incoming connections. When it gets the commands, it applies them directly to the two motors to move the robot accordingly. The main parts of the sketch are explained below, and you can find the complete code on the GitHub repository of the project.

The sketch starts by importing the required libraries:

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <aREST.h>
#include <avr/wdt.h>
After that, we have to declare the pins for the CC3000 breakout board:
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  8
#define ADAFRUIT_CC3000_CS    10
And the pins corresponding to your motor shield (that's where you need to enter your own values depending on the motor shield you are using):
int speed_motor1 = 6;  
int speed_motor2 = 5;
int direction_motor1 = 7;
int direction_motor2 = 4;
We can now set the parameters that depends on your WiFi network. You will need to modify these lines of code with your WiFi network configuration:
#define WLAN_SSID       "yourNetwork"        // cannot be longer than 32 characters!
#define WLAN_PASS       "yourPassword"
We also need to create the CC3000 instance:
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,

We then create a server on our Arduino, that will accept the incoming connections coming from the network:

// The port to listen for incoming TCP connections 
#define LISTEN_PORT           80
// Server instance
Adafruit_CC3000_Server restServer(LISTEN_PORT);

Now, in the setup() function of the sketch, there is a large number of functions to connect to the WiFi network that we won’t detail here. We also start the WiFi server:

Serial.println(F("Listening for connections..."));

And enable the watchdog, so our sketch restarts automatically if there is any problem:


In the loop() function, we accept incoming connections and process them, and check if we are still connected to the network:

// Measure distance
distance = measure_distance(distance_sensor);
// Handle REST calls
Adafruit_CC3000_ClientRef client = restServer.available();
// Check connection

Let’s now have a look at one of the functions we will use to control the robot. Let’s take the one that makes the robot goes forward:

int forward(String command) {
  return 1;

We can see that we simply call the function send_motor_command twice, once per motor. This function is also defined in the sketch:

void send_motor_command(int speed_pin, int direction_pin, int pwm, boolean dir)
  analogWrite(speed_pin,pwm); // Set PWM control, 0 for stop, and 255 for maximum speed

It’s now time to test the sketch. Make sure you modified it with your own WiFi parameters, and upload it to the robot. Open the Serial monitor, and check that the connection details are correctly printed, and note the IP address of your board.

Let’s it was, like in my case. You can now disconnect the USB cable from the robot. Then, go a web browser and type:

You should get the same answer as before, meaning the aREST API is working via WiFi. Now, just type:

You should see the robot going forward at full speed. To stop it, just do the same with the stop command.

This guide was first published on Nov 19, 2013. It was last updated on Nov 19, 2013.

This page (Arduino sketch) was last updated on May 05, 2021.

Text editor powered by tinymce.