Configuring Your Mobile Robot

We now need to configure the ESP8266 WiFi chip on our robot so it can receive commands via WiFi. For that, we are going to use the aREST framework, which is a very convenient way to make the ESP8266 be completely controllable via WiFi. 

For this section, you will need the latest version of the Arduino IDE, along with the following libraries:

You can find more information about how to install an Arduino library by following this guide:

https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use/arduino-libraries

Let's now have a look at the code for this project. It stars by including the required libraries:

Download: file
#include "ESP8266WiFi.h"
#include <aREST.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

After that, we create an instance of the Adafruit_MotorShield, and also create instances for the two motors:

Download: file
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
 
// And connect 2 DC motors to port M3 & M4 !
Adafruit_DCMotor *L_MOTOR = AFMS.getMotor(4);
Adafruit_DCMotor *R_MOTOR = AFMS.getMotor(3);

We also create an instance of the aREST library:

Download: file
aREST rest = aREST();

You will need to enter your WiFi network name & password inside the sketch:

Download: file
const char* ssid = "wifi-name";
const char* password = "wifi-pass";

We also declare a set of functions to control the robots:

Download: file
int stop(String message);
int forward(String message);
int right(String message);
int left(String message);
int backward(String message);

Inside the setup() function of the sketch, we initialise the Adafruit_MotorShield library:

Download: file
AFMS.begin();

We also expose all the control functions to the aREST API, so we can call them via WiFi:

Download: file
rest.function("stop", stop);
rest.function("forward", forward);
rest.function("left", left);
rest.function("right", right);
rest.function("backward", backward);

We also connect the ESP8266 WiFi chip to your local WiFi network:

Download: file
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
 
// Start the server
server.begin();
Serial.println("Server started");
  
// Print the IP address
Serial.println(WiFi.localIP());

Inside the loop() function of the sketch, we process incoming connections with aREST:

Download: file
// Handle REST calls
WiFiClient client = server.available();
if (!client) {
  return;
}
while(!client.available()){
  delay(1);
}
rest.handle(client);

Let's now have a look at the implementation of the functions used to control the robot. For example, this is the function used to make the robot move forward:

Download: file
int forward(String command) {
  
  // Stop
  L_MOTOR->setSpeed(200);
  L_MOTOR->run( FORWARD );
 
  R_MOTOR->setSpeed(200);
  R_MOTOR->run( FORWARD );
  
}

In a similar fashion, here the implementation of the function to make the robot turn right:

Download: file
int right(String command) {
  
  // Stop
  L_MOTOR->setSpeed(100);
  L_MOTOR->run( FORWARD );
 
  R_MOTOR->setSpeed(100);
  R_MOTOR->run( BACKWARD );
  
}

Note that you can find the complete code inside the GitHub repository of the project:

https://github.com/openhardwarerobots/esp8266-robot

It's now finally time to configure the robot! First, grab all the code from the GitHub repository of the project, and modify it with your own WiFi name and password. Then, upload the code to the ESP8266 feather board. Once that's done, open the Serial monitor, you should see the IP address of the board:

Make sure you copy and paste this IP somewhere, you'll need it in the next section where we'll configure the interface to control the robot.

This guide was first published on Jul 29, 2016. It was last updated on Jul 29, 2016.
This page (Configuring Your Mobile Robot) was last updated on Aug 12, 2020.