Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
At work, but not for production apps
I don’t use WebAssembly but expect to when the technology matures
I have no plans to use WebAssembly
No plans and I get mad whenever I see the buzzword
Edge Computing / Software Development / Tech Culture

Off-The-Shelf Hacker: Super Simple App For The Yard Sensor

Mar 16th, 2019 6:00am by
Featued image for: Off-The-Shelf Hacker: Super Simple App For The Yard Sensor

We’ve worked on the passive infrared (PIR) yard sensor for a while now, mostly on the hardware and software side. Last week we discussed firmware for the NodeMCU WiFi module that allows the device to send and receive MQTT messages to a broker and switch the relay on or off. In addition to automated “incursion” logging (when somebody walks by the sensor), I need an easy way to turn the outside yard floodlights on and off from my Linux notebook. It would be fun to operate the lights from other devices, too.

MQTT massively simplifies how we develop machine-to-machine communication processes. Once you get your clients (the yard sensor) and the broker (my Linux notebook) set up, making things happen is simply a matter of sending messages back and forth, to do your bidding.

Today, we’ll look at a super simple panel-based application, written in Processing. This language mirrors the code we write for Arduinos and has a ton of libraries for all manner of programming needs. We’ll use one of the libraries specifically for graphical user interfaces, like buttons and sliders. Processing applications are portable between Linux notebooks and Raspberry Pi computers.

Let’s get started.

First Things First

All MQTT setups require a broker. The broker coordinates the MQTT messages. Think of it as a message server. You publish a message to a specific topic, on the broker and then any MQTT client that is subscribed to that topic gets the message, as soon as it is sent. You can have a bunch of clients subscribed to the same topic and as soon as a new message is published, it will appear at each client.

For this quick prototype, I just used an existing installation of Mosquitto on my Ubuntu 18.10 version of Linux for the broker. A while back I also set up a Mosquitto MQTT broker on a CHIP computer and called it the “Mosquitto Under Glass” project. Sad to say, the CHIP stopped working recently. The company went belly up too. Go figure.

I still need a dedicated MQTT broker to handle all my current “just out of prototype” projects, so will outline building one on a spare Raspberry Pi, in an upcoming article. Maybe I’ll replace the CHIP with a newer Raspberry Pi. I can call it the “Raspberry Mosquitto, Under Glass” project.

If you know what you’re doing, use apt-get for a quick and dirty setup on any common Linux system.

We could also use an online MQTT broker, although for now, I’ll just keep all my messages on my local network. Mosquitto has a test broker, Adafruit and Hive all have online brokers. There are others.

Build a Simple Panel-Based App

We’ve used Processing for lots of jobs in the past. It’s a great language for casual programming. Most recently, Processing code helped me make my robotic skull, Hedley talk and move his jaw.

Start Processing and install the ControlP5 library. Under the “Sketch” tab, click the “import library” menu item and select “Add Library” from the sub-menu. Type “control p5” into the search box and hit enter. Click the “install button”. You may have to load the MQTT publish/subscribe library, as well.

I used a combination of examples to cobble together this prototype. Find examples by clicking “File”, then “Examples” on the Processing IDE. Look down in “Contributed Libraries” under “ControlP5.” The one I used was “ControlP5button,” I also used the “publish/subscribe” example under the “MQTT” heading, in “Contributed Libraries.”

The code is pretty bare-bones. It hooks up to the MQTT broker on my Linux notebook using the IP address. Adjust your code to point to your broker IP address. I used the “M1” ID to indicate the device generating the data. Other yard sensors will have similar IDs like “M2” or “M3”. You could just as easily, name your devices “south-east-sensor,” “north-east-sensor,” and so on.

Next, the buttons are initialized and kind of sit in the background waiting for a push. When one of the buttons are pushed the code jumps down to the “on,” “off,” or “alloff” function and publishes an appropriate message to “inTopic” topic on the MQTT broker.

Make sure your Linux notebook is on the network and the yard sensor is plugged in and running. Start the program by clicking on the “Run” arrow. A new panel will appear with “on,” “off,” and “alloff” buttons. Click the “on” button to energize the relay. Click “off” to turn it off.

Simple Yard Light Application On The Linux Desktop

You can also run it as a standalone program. Go to “File,” then “Export Application” and select “Linux” as the platform. I like to use the “Embed Java for Linux (64-bit)” option to head off any problems with external Java library issues. The Processing IDE will ask you where you want to save the file. Pick a spot and then you can just run the shell script under the “application.linux64” directory. In my case the shell script was named nodemcu_mqtt_gui2. The directory for the program was /home/rob//sketchbook3/nodemcu_mqtt_gui2/application.linux64.

At the command line switch to your application.linuxXX directory and run the following.

The panel will pop up on the desktop and you can switch the relay on and off using the buttons. Note that your MQTT broker may be on a different IP address than the one I’m using. Use “ifconfig” to get your local IP address.

Running the program in a terminal also lets you see when the PIR sensor picks up a warm body by printing “new message: M1” to the screen.

What’s Next?

The Processing program, to control the yard light sensor relay is very basic. There is really no security and it only turns on a single relay.

There’s a lot of potential for improvement. I’ll probably use this program as the basis for a more sophisticated control for the NodeMcu sprinkler project. That one sports eight relays.

What new features would you like to see?

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.