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>
#define ADAFRUIT_CC3000_IRQ 3 #define ADAFRUIT_CC3000_VBAT 8 #define ADAFRUIT_CC3000_CS 10
int speed_motor1 = 6; int speed_motor2 = 5; int direction_motor1 = 7; int direction_motor2 = 4;
#define WLAN_SSID "yourNetwork" // cannot be longer than 32 characters! #define WLAN_PASS "yourPassword" #define WLAN_SECURITY WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);
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:
restServer.begin(); Serial.println(F("Listening for connections..."));
And enable the watchdog, so our sketch restarts automatically if there is any problem:
wdt_enable(WDTO_4S);
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(); rest.handle(client); wdt_reset(); // Check connection if(!cc3000.checkConnected()){while(1){}} wdt_reset();
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) { send_motor_command(speed_motor1,direction_motor1,100,1); send_motor_command(speed_motor2,direction_motor2,100,1); 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 digitalWrite(direction_pin,dir); }
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 192.168.1.103, like in my case. You can now disconnect the USB cable from the robot. Then, go a web browser and type:
http://192.168.1.103/id
You should get the same answer as before, meaning the aREST API is working via WiFi. Now, just type:
http://192.168.1.103/forward
You should see the robot going forward at full speed. To stop it, just do the same with the stop command.
Text editor powered by tinymce.