Adding remote control to the Bigtrak.

All the code from this post is on GitHub here.

Hello! I haven’t posted for a long time, but I’m hoping to make up for it by writing a few blogs over the coming weeks. Sorry about that.

Also, if you haven’t already, I’d suggest you take a look at this post before this one so that you know what I’m going on about.

Last time we opened up the Bigtrak, added an Arduino and a motor driver and made sure it all worked. This time we’re going to be writing some code in order to remote control the Bigtrak over WiFi using an ESP-01 WiFi module (which uses the ESP8266 microchip).

Connecting the module

Firstly, we’re going to have to connect the ESP-01 to the Arduino. It should have 8 pins which are labelled in this diagram:

Image result for ESP-01

As you can see, the module has 2 GPIO pins.  This is because it can be used as a programmable microcontroller by itself, but in this case we’re just going to connect it to the Arduino Mega so we aren’t using them.

But wait! The board is labelled as 3.3V and the Arduino Mega is at 5V! Well, that’s what I thought. I used a cheap step down buck converter to connect them but it didn’t work. After reading this I decided to try it with 5V, and it started working. I haven’t had any problems yet.

So here’s how I connected the module the the Arduino:

 Arduino ESP-01
 GND GND
 5V 3.3V
 5V CH_PD (‘en’)
 RX1 (19) TX
 TX1 (18) RX

Remember that this is as well as all the connections to the motor driver from last time.

bt

My Arduino Mega is Chinese, so apologies if the pin numbers are incorrect.

Testing the module

You communicate with the ESP8266 using serial. You can send AT commands, which are listed here.

Firstly, we can test the module. Open the Arduino IDE and start a new program.

Firstly we set up the two serial ports – one to the computer (‘Serial’ )and one to the ESP-01 (‘Serial1’). The default baudrate is usually 115200. We then send ‘AT’ to the module. If everything is good, it should reply ‘OK’. Its reply is then sent to the computer.

We then use the ‘CWMODE’ command to set the module to station mode, allowing it to see and connect to WiFi networks. it should reply ‘OK’ again. The last command we send is ‘CWLAP’, which instructs the module to list available WiFi networks.

In the ‘loop’ section of the code we just send back any more output from the module to the computer. Here’s the whole test program:

ESP-01_test.ino

Upload the program to the Arduino Mega and open the serial monitor. You should see ‘OK’ twice followed by a list of nearby WiFi networks like this:

AT


OK
AT+CWMODE=1


OK
AT+CWLAP

+CWLAP:(4,"TP-LINK_6C5DA4",-88,"64:70:02:6c:5d:a4",1,5,0)
+CWLAP:(3,"BTHub6",-63,"d8:7d:7f:c0:6c:82",6,3,0)
+CWLAP:(4,"BTHub4",-85,"40:f2:01:1b:03:d2",11,-9,0)
+CWLAP:(0,"BTWifi-with-FON",-87,"40:f2:01:1b:03:d5",11,-9,0)
+CWLAP:(0,"BTWifi-X",-84,"40:f2:01:1b:03:d7",11,-9,0)

OK

If it didn’t work, try swapping round the RX and TX cables.

Using the module for remote control

Now for the hard bit. I must admit, I’m no expert at C++ or Arduino, so this code is going to get a bit messy.

First we store which pins the motor driver is connected to, like we did last time.

This is just a function that empties the serial buffer. When the Arduino receives a serial message it stores it in a buffer until it is read by the program. emptySerialBuffer() reads everything in the serial buffer to get rid of it.

This function, as the name suggests, gets the left motor moving at a specific speed. It accepts two arguments: the speed to move at and whether or not it should be in reverse. Look at part 1 if you need a recap on how the motor driver works.

This function does the same but for the right motor.

In the setup code we configure the ESP-01. CWMODE=2 sets it to AP mode, which allows it to broadcast its own WiFi network for devices to connect to. CIPMUX=1 allows multiple devices to connect. CIPSERVER=1,80 creates a webserver on port 80. Finally, CWSAP names the WiFi network ‘BigTrak’ and sets the password to ‘ArduinoMega’. We then empty the serial buffer to remove the chip’s replies to all these commands, which we have no need for.

We then declare all of the variables we’re going to need.

At the start of the loop, we wait for the module to send a serial message, meaning a request has been sent to the webserver.

In order to send the speeds to the Arduino, we are going to have arguments in the URL. For example, something like http://192.168.4.1/?l=150&r=150&ln=0&rn=0. The message the Arduino gets for this from the module would be something like:

+IPD,0,470:GET /?l=150,r=150,ln=0,rn=0 HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

As you can see, it contains our arguments but we need to extract them. We loop through the character the module has sent until we get to the string “/?”, stored in previousChar and newestChar. At this point we call Serial1.parseInt() four times. This reads through the buffer until it finds a number, which it returns. The four numbers are stored in the integer array parsedNumbers. The serial buffer is then emptied in order to get rid of the rest of the message.

If the message contained numbers and hence validConnection is true, we get the speeds from the parsedNumbers array. We then determine whether the right and left motors should be spinning in reverse (a negative speed) from the other two numbers.

We then get the motors moving at the speeds sent to the module using the functions from earlier. CIPCLOSE then closes the module’s connection with the sending device. You can find all the code on gitHub here.

Upload your code to the Arduino Mega. You can test it by connecting to the network ‘BigTrak’ on your computer with the password ‘ArduinoMega’. Open a browser (ideally Chrome) and type in http://192.168.4.1/?l=150&r=150&ln=0&rn=0. The webpage should show an error saying the server sent no information but the BigTrak’s wheels should start spinning. If the wheels don’t start spinning, you may have the wrong IP address. On your phone, download an app such as ‘Fing’, which can list all of the devices connected to a WiFi network. Connect to the BigTrak network and do a scan. You should see your phone, your computer and the ESP-01. Find the ESP-01’s IP address and use that instead: http://%5BIP address]/?l=150&r=150&ln=0&rn=0.

You should be able the change the numbers sent to the Bigtrak to change the speeds the wheels rotate at; the first number is the left motor’s speed, then the right motor, then whether the left motor should be in reverse, and then whether the right should reverse.

Making a controller

It’s fun for a bit to rotate the wheels from the browser, but before long it gets a bit tedious having to type in the numbers and getting error messages. What we need to do is program a controller. I used python and the requests library with tkinter to send speeds to the Bigtrak from a computer.

Firstly we import tkinter and requests, set the IP address of the module and define a global variable we’ll need.

After declaring the functions (which I’ll come back to later) we create a tkinter window named root. selection stores which radio button is selected and links them all together. We then create five radio buttons that tell the Bigtrak to go Forwards, Left, Right, Backwards or stop.

We then set the ‘update’ function to run in 0.2 seconds (which I’ll come back to). After that, we bind the arrow keys and space to the different movement options.

The update function runs every 0.2 seconds. If the current radiobutton selected has changed since last time, it sends a new speed to the bigtrak. 1 is forwards, 2 is left, 3 is right, 4 is backwards and 5 is stop.

The send_speeds function sends the speeds defined in the update function to the Bigtrak. It uses the requests library to send a post request with the arguments in the url.

These five functions are called when the arrow keys or space bar is pressed. They select the relevant radio button. You can get all the code on GitHub here.

When you run the program, the GUI will come up and you will be able to select the control the Bigtrak with your keyboard keys.

bt

Remember that the Bigtrak will continue to move even when you let go of the key and you must press the space bar to stop. Also, the Bigtrak doesn’t work very well on carpets – it tends to struggle to turn.

So now you can remote control your Bigtrak! I hope you enjoyed this tutorial. If you have any problems with anything not working, please comment below and I will try my best to help you.

Get all the code here.

Advertisements