Building a browser-programmable robot in two days

Eliot Lim
Hackers at Cambridge
5 min readFeb 4, 2018

--

Summary — Using technology to make something cool is a great way to try something new. I built a browser-programmable robot, solving problems and finding solutions along the way. In the process, I learnt a lot about the platforms I used and pitfalls to avoid in future inventions. It turns out hardware hacks are pretty difficult, but it is also immensely rewarding to see your electronics and code coming to life!

I recently had the privilege of joining Hackers at Cambridge, and found myself in an excellent environment to do a side-project. One of the ideas that leapt into mind was to build a robot. But not just any robot: One that could be programmed using just a web browser over a Wi-Fi connection, yet extensible enough to perform more complex tasks in the future.

The proliferation of smartphones has placed a computer in everyone’s pocket. By making the robot programmable using a web-browser, such a platform would be accessible to everyone. In particular, one could learn procedural programming concepts and see their code take immediate, visible effect. The real hurdle in achieving this lies in finding a suitable micro-controller: one that is robust, powerful and flexible, and yet low-cost.

The ESP32 makes a good candidate for the “brains” of this project — it possesses Wi-Fi, BLE, and a host of peripherals — and has 520KiB of SRAM, which is a luxury for someone coming from an ATMega or ESP8266.

I started this project over the weekend, and ran into a multitude of problems. Hardware hacks are difficult to pull off in a short amount of time, since electronics and materials are difficult to acquire at short notice, meaning planning ahead is all the more important.

Meet the Hardware

Left: Motor Driver | Right: 5V Boost Converter

The ESP32’s Wi-Fi radio consumes a lot of power during transmission, so a stable power source is needed to prevent brown-outs. Since the on-board batteries need to provide power to both the motors and the ESP32, it is possible that the chip might reset when the motors are switched on.

I solved this by using two boost converters which draw power from the main batteries. They provide a stable ~5V output, which is supplied to the low-dropout AMS1117 3.3V regulator of the ESP32 development board.

Left: Soldered motor terminals | Right: Testing the ability to move

Some assembly was required, and obsessing over the details has therapeutic value. Putting the hardware together was as simple as connecting a few jumper wires between the outputs of the ESP32, the connections of the motor driver breakout, and the motors themselves.

Things don’t always work on the first try, and it turns out that 4 × AA Nickel-Metal Hydride (Ni-MH) rechargeable batteries do not provide sufficient current capacity to simultaneously start all four motors from a standstill. A pleasant walk to a nearby electronics retailer solved that problem, and 8 × AA alkaline batteries work well when placed in 4S2P configuration (4 cells in series per set, 2 sets in parallel).

Firmware and programming interface

While hacking the robot together, my previous attempt of a browser-programmable robot came to mind.

An earlier iteration of this project made use of the NodeMCU firmware, running on an ESP8266, an earlier product of Espressif and sibling of the ESP32. The robot experienced resource constraints — single-threaded execution, 96KiB of RAM — and making the robot programmable in the Lua scripting language required a rather inelegant ‘hack’. Execution of the user’s script would have to take place line by line, with a special function to emulate sleep. This ‘sleep’ function, when called, would set a flag — handing back execution to the NodeMCU core — and schedule a timer to continue execution of the script.

For this project, I chose to run MicroPython on the ESP32. It provides many useful facilities: including threading, sockets, IO, and a plethora of libraries to make one’s life easier. Not to mention the fact that Python is a popular and beginner-friendly language.

Python scripts can be loaded and run on the robot’s processor

The ESP32 creates a Wi-Fi hotspot on boot and starts MicroWebSrv, a lightweight web server that hosts the homepage and the bootstrap UI dependencies to make it look prettier ❤

A simple interface invites you to begin programming. The form data is submitted to a route handler on the robot that writes the script to disk and executes it, which makes the robot move. More advanced and complex commands can be written and saved as libraries, so that they can be imported by other scripts. One could also connect sensors and servomotors so that the robot can respond to external stimulation autonomously.

There are a few rough edges that could use some more engineering effort, like ensuring that the garbage collector doesn’t stop the web server, and providing error messages nicely when script execution encounters errors.

Does this constitute a hack? Why should I hack?

Hacking is really about making something do a task or function that it wasn’t meant to do, and making something cool. I’m sure that the ESP32 was never intended to run a web server and interpret data from an HTML form as code.

There’s no better way to complete your learning than to apply it to something fun and interesting. You might come up with an idea that makes others pause to think, smile, and makes them equally curious.

--

--