Prototype wrapup #26
Last time I just made a one-off prototype, but this set of prototypes is once again a series of parts leading up to a project. The post for that project is coming soon, but in short it involves real-time button and display synced to a webpage. This led to a particularly interesting set of problems because I basically wrote code for 4 different environments: Arduino, ESP, web server, and web client. This was on a pretty short time frame, so most of the code is pretty rough and ready. Still, although it involved a lot of different moving parts, each individual part wasn't too complex. Perfect for prototypes!
This was the Arduino code necessary to drive the display, as well as both send and receive updates over serial. One neat trick I ended up using here was JSON for communication, and ignoring anything that didn't begin with a left curly (as a JSON object does). This meant I could send debug information and data on the same serial port and, indeed, debug it even while the serial port was being used for communication between the Arduino and ESP. I also, for some reason, wrote it in a way that would allow me to have any number of displays and buttons.
For this part, getting the Arduino to talk to the web server, I basically just wanted a serial-to-web-request proxy. I used NodeMCU and wrote a custom Lua script on top. This wasn't strictly necessary, and in some ways it may have been easier to just use Arduino on both ends (The Arduino platform can build for the ESP chip). But Lua is fun, and I figured this way I wouldn't have to swap back and forth between Arduino serial targets, which is always a huge pain. Plus being event-driven was probably the best API here, because multiple requests could be coming in over the wire at once, though in practical terms all I did was drop them anyway.
This is the code for the actual web client and server. Ye olde bog standard Coffeescript plus DOM manipulation, nothing fancy because fanciness wasn't really required. Everything just used polling, so it's not really real time, though I had in the back of my head that maybe I should be using websockets. I knew I'd need to have polling as a fallback regardless, and I thought maybe polling would cache better, so I figured that might be enough. Both the graphics and the websockets ended up coming later, but that's for next week's prototypes.