Week 12

Looking for a JS hello-world type example to get started, since I’ve had such a hard time with the other electronics weeks.

Started from Node’s SerialPort, found a recommendation for something called Johnny Five that looks promising, trying to get it set up at a basic level on Arduino board before I try anything with AVR programming.

ran npm install johnny-five here, it installed something, didn’t work the first time, upgraded to current LTS version of node, actually did something.

Created helloworld.js with code from hello world, no idea how to run it at first. Found in basic johnny five initialization instructions that I needed to run node helloworld.js for anything to happen. Now when I do, the program errors out.

Now paying attention to section that I hoped I could safely ignore, “Setup your board” on J5 instructions, which has several parts that make no sense to me. Sent along to some troubleshooting instructions, which assume that I can run arduino from terminal, which I cannot do. which arduino returns nothing, but I have to arduino --install-library or something.

Okay, maybe I can keep ignoring the instructions that include the nonexistent arduino terminal command. I tried the sketch with the LED plugged in and it flashes twice and then stops. If I remove it and put it back in, it fails again. (the led). Okay, it wasn’t removing the led and putting it back, it just fails after a few seconds, even if I do nothing. So now I have to figure out what reality includes a command line application for arduino and figure out how to get there.

okay, somehow running sudo apt update and then sudo apt install arduino arduino-core got me an arduino from the command line, let’s see what happens when I try to follow those troubleshooting directions…

Wow so far this is really annoying. It doesn’t feel like the terminal command is acknowledging anything after arduino because it’s just starting up as though it’s freshly installed (asking me to locate a sketch directory), and now complaining about my AVR library folder name, then starting normally to a sketch window. Great. Maybe if I fix its problems it will pay attention to me.

No, not even a little bit. It just starts the arduino IDE again, which also has the LED on the board flashing twice again.

I’m just trying the next thing to see if it’s at all possible, and no, still I have wildly unexpected behavior from attempting to follow very simple directions. arduino --board "arduino:avr:mega" --upload ./path/to/firmware/firmware.ino, substituting my path to firmware for the placeholder path, opens the sketch, but does not try to upload it, and also does not set the board to my Mega. setting the board to mega and trying from there also fails, because “utility/SerialFirmata.h: No such file or directory”, which is fairly normal, since the command that’s supposed to install those libraries is just completely ignored. I confirmed this by cursing at arduino through the command line with some flags, which it also ignored.

Okay, now we’re cooking with chicken. Fuck those old instructions, the even older ones were better. I checked previous versions of the J5 troubleshooting guide to see when arduino was available for terminal, and found them uploading firmware from a sketch. This time I actually found the Firmata firmware in the example sketches and uploaded it. Now my LED is happily blinking away as expected, so I think I can use J5 to control it. Now to see if the documentation says anything about AVR programming…

Okay, so J5 IO plugins rely on Firmata, which only has well-known and well-documented support for Arduino and Spark.io, and I don’t know enough about these systems to even begin to try to make my own implementation, although the page indicates that it’s theoretically possible. I’m not going to be able to use this on an attiny board, but I’m interested in how it might work on an Arduino board.

I just noticed that the tx and rx lights blink before every state change of my LED, which seems… bad? Or at leas possibly inefficient eventually? Like, adding overhead to every command might be bad? Well, in this case, Node is acting like a server, and just telling the arduino what to do over serial, so I guess that’s okay?

Part of making my peace with using a commercial board for this project is this post from the initial developer of node-serialport. Development these days looks to be being undertaken by someone else, but I found the idea of having short, easy to read scripts for quick prototyping and making cool stuff quite compelling, even if I’ll have to reconstruct my toolchain completely at some point.

So now I’m making it my business to figure out how to use Node. I normally use python for things, but I’m used to using frameworks like Django for web stuff, and I’d rather take the opportunity to figure out what’s going on with server-side JS, which I still find a bit baffling.

The first thing I did was to take the hello world web server from node’s getting started guide and copy that code above my other hello world for using Johnny-Five. It still works when running node helloworld.js, so that’s something.

I looked at the other guides from nodejs.org, and didn’t see anything like what I wanted, which was something on making a button that talks to the server process. I don’t care about learning Node at a deep level now, I just want a surface-level understanding that lets me do one thing. So I’m googling “node js make button”…

I skimmed over a couple of preview results before landing on this Quora question: What is the simplest way to make an HTML button communicate with a Node.js server?, which is exactly what I want. The first answer looks promising, but I took a look at the second one to see if it looked cooler. They started talking about AJAX requests and the step numbers kept incrementing, so I’m goin to try the first one.

That technically worked, in that I clicked a button and got to an page powered by Node, but it didn’t convey any information, and I don’t know how to have a server listen on multiple endpoints or render different things, so I think I need a little more node knowledge. I’ve had good luck with books before, so I looked up “node js” in HOLLIS (Harvard library catalog). “Practical Node.js” looks promising…

I’m skipping around to what looks most helpful, starting with “express.js”, which seems like it’ll help with templating and responses.

I got bored and/or annoyed at how much I’d have to learn about and then not care about, so I’m looking at doing dumb things with vanilla node js in a single file so that I can get something done. I’m just slowly picking apart this hello world example. So far, I’ve figured out a few things that seem useful. One is that my page doesn’t currently care what path the url is pointing at, it’ll render the same thing at localhost:3000/literally-anything unless I tell it not to. Second is that there is a request object that has the base url just hanging out.

Those two things are enough for some basic interaction based on what the url is. I could get fancier with it and use “/” characters to separate different kinds of instructions, but I’m not going to do that, because once I start, it’s probably a better idea to just use a proper system like express, and for a toy project, I don’t want to. So instead, I’m going to say that the url “/on” will turn my LED on, and “/off” will turn it off. That won’t actually change how the page renders, but it will change the value of req.url, which I can interact with. The last thing to do is to hook it up to my board. To do that, I had to figure out how to access the variable that defines the LED and makes it blink in the hello world example from Johnny Five. The board variable is all I have access to, but fortunately there seems to be a console I can access while running this app, so I can take a look at what it has inside it. I saw a reference to an LED with the pin I set in the registered key of this variable, so I tried running board.registered[0].on(), to keep the LED on. It didn’t, but it did make it flicker a little, so I suspect that led.blink() sends on and off signals that override my one measly on signal. To test, I took out the line that made the LED blink, and viola, my board.registered[0].on() worked.

That’s the setup for the final version of helloworld.js, which has my LED turning on and off on command. Success!

Next steps involve finding an appropriate callback function to toggle the LEDs properly, instead of relying on the board already being initialized, as well as styling the interface, which should really involve a templating language.