kivy, the hero of iot gui developers

Kivy – An open-source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

The Internet of Things (IoT) is the likely future of gadgets and devices that you’ll have in your homes and cars (if you don’t already) as well as technology that you wear. As a minimal criteria, these things use the cloud to gather and store information. In a way, even smartphones fall into this category if you think about it. Amazon’s Echo device is a good example.

For many of us who self-identify as “makers”, we use small computers with similar capabilities and we create these type of gadgets. Often when we’re coding software in this space, the Python language is the usual choice for the task.

Until now, we’ve not had many options for displaying graphical menus on such tiny screens other than the full “Desktop” GUI of some Linux-like operating system which didn’t really work, to be honest.

Introducing Kivy

A relatively new technology is the Kivy library. Imagine being able to describe the many screens you’d find in an application, whether it’s a smartphone or the touchscreen of a printer or even a watch. Then Kivy takes care of the rest for you, rendering those screens using a graphics engine behind-the-scenes. It even manages clicks and other gestures, getting these to fire off portions of your code.

Kivy comes equipped with an impressive collection of pre-defined screen widgets as well as the ability to create your own custom types. And you get all this for the low, low price of free (unlike its $5K+/year—priced competitor Qt).

I’ve had the pleasure of working on an almost daily basis with Kivy over the last two months and I must say that I’m still just as fond of it now as the day I originally learned of it.

If you’re a coder and you know Python, I would suggest that you add Kivy to your toolbelt. You’ll find that it’s easy to use and worth the effort you put into it.

meetup tonight

I’ll be giving another lightning talk this evening downtown at the San Diego JS meetup. I get to talk about the Autonomous Tank project and the code behind that.

SDJS

Since I no longer have a corporate-sponsored license of Microsoft PowerPoint, I had to improvise for my overhead slideshow this time. So I did what most coders would do in this scenario: I coded something for the task.

Screen Shot 2018-10-02 at 2.45.50 PM

autonomous tank

I managed to snag some great track data today at the venue. It was necessary to write a service to take snapshots every second while I manually drove around the track a few times.

With data in hand now at home, I was able to do some data processing now with the images from my own webcam.

A New Perspective

I thought I’d compensate for the lines-of-perspective effect so that the trending portion of the software could have accurate data. Since Jimp didn’t have a skew function and since its convolute() method didn’t work as expected with the right matrix for this, I ended up writing my own prototype which now works as shown below.

Screen Shot 2018-09-22 at 9.34.22 PM

and one button to rule them all

The project from yesterday and today is something called a “dash button”, an IFTTT or an IoT button. Push the button and some activity gets invoked (usually, remotely). Amazon’s take on this is for you to be a consumer, press the button and something gets ordered.

dash

My own take on this is to add a big red button (BRB) as a remote panic switch for the 3D printer. Press it, magic happens and the print job is paused. It’s useful when something bad starts to happen and you need to make it stop quickly.

IMG_0034

There’s not a lot of room inside the printed plastic for this. Whatever electronics it uses, it will need to be small enough to be self-contained.

IMG_0049

I’m currently accomplishing this with a nifty Adafruit Huzzah ESP8266 board, a charging module and a 3.7V battery. I’m using the Arduino software to “flash” (upload) code to the tiny processor as well as a full directory of files to support the webserver which runs when it’s in configuration mode.

By strapping a pair of header pin connections and pushing the reset button, it now boots up in wi-fi hotspot mode and serves up a configuration website. Submitting to the form then re-configures the device and resets it again.

Booting up now in the standard mode, it then connects to the local wi-fi and attempts to then connect to the URL that you’ve given it. Once it does all this (perhaps ten seconds’  worth of activity), it promptly goes to sleep. Press the BRB again, it wakes up and goes through its routine again.

If you think about it, it’s now a reconfigurable dash button and much more useful than those one-trick-ponies as provided by Amazon.

Repository

glacier

Earlier today, I created a Raspberry Pi Trezor (cold wallet) for cryptocurrency using that cool Adafruit 1.3″ OLED bonnet.

PiTrezor

It seems to reasonably work from a fork of the original code. It presents itself to the Trezor Bridge software and to your workstation as a slave USB device. I suppose you could think of the entire thing as a smart USB thumb drive, if you will.

The code image is smaller than you’d normally expect (50MB). I’ll get in there and take it apart later from a software standpoint.

Notes:

  • The interface is beautiful on the small screen with attractive fonts and functional animations.
  • I don’t love websites which only work with a single browser. In this case, it’s Google Chrome of course and it was necessary to install that for Trezor to work at all.
  • The standard screen assumes that the buttons are positioned below it, not so for the Adafruit hat. So you just have to guess that the furthest button equals the right-most button and it all works out.
  • The GPIO pin layout of the OLED bonnet is different from the native Trezor device so of course, the bootloader upgrade routine doesn’t work as expected. It will be necessary to recompile and reload the image in order for this to work. I’ll have to review all that to see how it affects me to I don’t love anything in the cryptospace which can’t keep up to the current state of the art.
  • Having the micro USB cable sticking out of the side of the Pi just seems awkward so I’ll work up a serial connection to the GPIO pins with a USB plug tail and incorporate all this into a slick-looking case.
  • I don’t think I enjoy the website interface for selecting other cryptocurrencies. I think I like the KeepKey version of this better, to be honest.
  • Although Trezor suggests they’re compatible with other currencies, they seem to only be able to do Ethereum via a third-party. The hand-off to that third-party provider was about as ugly as it gets and I aborted. You shouldn’t have to create multiple accounts to simple store a wallet.
  • Unless I gain more confidence with all this, I won’t be putting any money in the wallet but it’s an interesting exercise.

adafruit oled display

Other than importing gadgets, Adafruit sometimes also designs them. Today’s review is for their cool 1.3″ OLED display for the Raspberry Pi computer. It appears to fit exactly the top of the Raspberry Pi Zero WH (the one with the included header).

IMG_0041

At $22.50, the hat is about twice the cost of the computer itself. The 128×64 display is enough for most projects, I’d guess.

I’m thinking that I’ll use this one to develop a cold wallet for cryptocurrency.

The hat (“bonnet”) includes a joystick with four positions and a pushbutton as well as two stand-alone pushbuttons on the right. It comes with good support on the Python side of things. If you’re like me and prefer Node JS, then you’ll be doing a lot of research on this one.

The interface is I2C. The device identifier is SSD1306. The example software includes text- and image-based Python and a bit of an animation of text.

In case you’re interested, I’ve written a tutorial for getting this setup.

over the mount’n

Okay, that was a terrible pun. I thought I’d share something today which is pretty cool although it gets a little technical at the end. You can actually edit the files on a tiny microSD card like you might have in some device like a 3D printer or similar. And you don’t have to have it running (like in your product/device) in order to do so.

Smart Things (IoT)

We live in a world of ever-smarter things. There’s a new term, the Internet of Things (IoT) which basically means that lots of the products we’re using now not only have little computers inside, but they also use the Internet in some way.

Tiny Hard Drives

Given the diminutive size of some of these new products, they often use the microSD card as an actual hard drive. These days, they include an entire operating system which boots up the product’s computer and provides a variety of functionality.

microsd

The Bleeding Edge

For some of us, we like to install the very latest version of software for something like this. Those updates sometimes come in the form of an entire image file for the microSD card in this case.

Instructions for editing files on an image file itself

Okay, so I have downloaded an image for a Raspberry Pi computer and the version name is Jessie. The name of the operating system itself which runs on this computer is called Raspbian, btw. The image’s filename is then raspbian-jessie.img, easy enough.

I’m running these commands on a Linux-compatible computer:


$ file raspbian-jessie.img

raspbian-jessie.img: DOS/MBR boot sector; partition 1 : ID=0xc, start-CHS (0x0,130,3), end-CHS (0x8,138,2), startsector 8192, 129024 sectors; partition 2 : ID=0x3, start-CHS (0x8,138,3), end-CHS (0x213,120,37), startsector 137216, 8400896 sectors

So we’re interested in only one number here, that value after startsector for partition 2, namely 137216. Multiply that by 512 to get 70254592 which we use in the next command:

$ sudo mount raspbian-jessie.img -o offset=70254592 /mnt

This means, essentially, “open up the file indicated (at an offset of 7 million or so characters into that file) and show me everything that’s in it and put that in the /mnt folder area”.

So now, you can actually edit the image’s computer name in the /mnt/etc/hostname file, for example. Assuming we have done so, we now unmount the file:

$ sudo umount /mnt

And you’ve managed to edit its internal files in place! The typical activity next is to burn that image to another microSD, put it into your smart refrigerator’s computer or whatever and boot it up.

Conclusion

Okay, so you’re not as excited as I am but this is a giant leap forward in workflow for me. Since I maintain several of these image files, this is pretty cool stuff.

Imagine if you were trying to build a supercomputer with a hundred individual (small) computers and it were necessary to then build 100 different image files each with their own setup. This would be so not fun if you had to do this manually. Using this new method, you could script all this and then run it.  By the end of some lengthy process, you would have all hundred different image files as produced by this method. Huzzah…, right?

blinking the raspi’s built-in LED

I’ve just added a repository of some JavaScript code to take over and exercise the built-in activity LED on a Raspberry Pi Zero W (and presumably other models). It’s called gpiozero-toggle-led and it’s a pretty simple interface with installation instructions and some sample code. It works with the underlying js-gpiozero JavaScript port of the popular original Python code. This would be an excellent way of simply demonstrating GPIO without any additional wiring, components, breadboards, extra power supplies or electrical knowledge (like finding a 330-ohm resistor using its color bands).

zero-wireless

Note that the “zero” in the title of the repository and in js-gpiozero does not refer to the Raspberry Pi Zero but to the original gpiozero Python library.

This should remove some of the guess work when attempting to use the relatively-new library since their documentation examples at the moment are taking a back seat to their code port from the more-extensive Python offering.

This approach can easily be modified to instead exercise external LEDs (as soldered or otherwise attached to the header pin locations seen below).  Note that you’ll use “BCM numbering” for APIs such as this one. For external LEDs, you would need to connect it inline with a resistor from a selected pin to one of the grounds for this to work with correct orientation of the LED’s anode/cathode, of course.

raspberry-pi-pinout

If you’re trying to use this with a Raspberry Pi of a different model, you’ll likely want to adjust the JavaScript slightly as seen below.

/routes/index.js:

// Existing code, for a Raspberry Pi Zero
var ledActivity = new LED(47, false);
// For Raspberry Pi 3, for example
var ledActivity = new LED(47);

And that’s it. Since the Raspberry Pi Zero assumes an opposite value for true/false than the bigger models, it’s necessary to configure this in the device constructor to make things work as expected. Since BCM pin 47 is the activity light on the board itself, this will allow you to control it.