How to connect a web page to Arduino
You want a robot to move when someone clicks a button on a RESTful website, and the robot reboots every time you click the web page. Here’s how I fixed it.
The problem setup
Your robot motors connect with wires to motor drivers
connected with wires to an Arduino
connected with USB serial to a Raspberry Pi
Connected via (s)http to your phone
That all sounds good but…
Devil in the details
- The USB serial connection is a bit like a dedicated phone line. One “feature” of Arduinos is that they reset when someone calls.
- RESTful web systems are not persistent: on every page load the server will pick up the phone, do something, hang up, and then send the page contents.
That means every time someone clicks on your web page the robot will reboot. Suppose your robot is mixing drinks and orders are arriving faster than the drinks can be made. The Arduino will be half finished making drink A when an order for drink B will arrive. The Arduino will reset and then try to make drink B without finishing drink A. CRASH!
Partial fix
One partial solution is to store the web actions in a database. A background job on the raspberry Pi periodically checks the database for new commands. if the Arduino isn’t busy and there are new commands, it sends the oldest new command first. This is great to prevent flooding the Arduino …and it doesn’t prevent the original problem from reappearing when the call drops. And it will! Sometimes hardware serial connection close accidentally. This is a rare and real problem. The first time it happens might be weeks from now, after you sold your drink robot to a friend and it crashed at her party. It’s the reason most 3D printers still use an SD card in 2016: they solved the serial “dropped call” problem with portable physical media. What is this, 1985?
Story time
I had the solution right in front of me for a year before I *realized* what I was looking at.
I had tried to build a drink mixing robot about 2 years ago and got stuck on this very problem. I abandoned the project and moved on to making a vending machine accept Bitcoin.
The hardware to connect an Arduino to a vending machine has been made open source by the fine men and women at NottingHack, the Nottingham Hackspace. Their design says that the hardware serial – the dedicated phone line – has to connect to the vending machine or the call won’t work.
Without a phone line, I couldn’t use my normal methods to debug the Arduino code. My PC couldn’t call the Arduino and watch it as it was running because the phone line was being used by the vending machine. I needed another way…
The Last Piece of the Puzzle
Arduino has a library called SoftwareSerial. With SoftwareSerial you can connect a second phone to an Arduino and it doesn’t reset anyone. So with TWO Arduinos you can solve the reboot problem:
Pi / PC > hardware serial > Arduino 2 > SoftwareSerial > Arduino 1 > hardware serial > robot
#2 passes messages between #1 and the Pi. Now when someone clicks on a web page the Pi picks up the phone and sends a message to Arduino #1 without the reboot problem.
Final thought
As I write this in 2016, this method can also save you money. Two Genuine Arduino UNOS ($21.62*2=$43.24) and a latest model Pi (+$5=$48.24) is less than one Arduino Yun ($56.20).
Need more detail? Let me know in the comments if you’d like to see code samples or a schematic of Pi > Arduino 2 > Arduino 1.