Running fortune in a Python virtualenv

I learned on reddit the other day about the sudo “insults” capability where it throws shade at you when you mistype the password. I configured it everywhere I could, but I wanted more, so I came across cowsay and fortune.

$ fortune | cowsay
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Needless to say… wow.

Continue reading Running fortune in a Python virtualenv

Getting IP camera motion events into Home Assistant to trigger things

Oh this is exciting! I’ve been trying to figure out how to get motion events from my IP camera into my home-assistant instance running on my Raspberry Pi, and I just did a successful test! It works! Hooray. Briefly, I set up an email server on the Pi, have the camera email the Pi, have the email server trigger a script which parses the email for key words and sends MQTT signals as appropriate, at which point the home-assistant MQTT client sees them and triggers automations (like blinking a light to scare people off). Here’s how I did it.

Camera-based motion sensors in home-assistant

Continue reading Getting IP camera motion events into Home Assistant to trigger things

Adding IR send and receive capabilities to my home-assistant based smart home

Or, how to control your A/C from your phone

You may have seen my earlier post about my new home automation setup. Well I just upgraded it! I just finished testing out a new infrared remote control send and receive capability and it is pure awesome. Here’s what I can do now from within home-assistant:

  • Turn on and off my new DeLonghi portable air conditioner from Costco, and set the temperature to whatever
  • Turn on and off the TV and press any buttons from its remote control
  • Turn on and off the stereo and virtually press any buttons from its remote control (which I never even had!)
  • Use an old random blueray remote control to set scenes, turn lights on and off, etc. (basically do anything under home-assistant’s control)

This post is about how I did it on my Raspberry Pi.

Update: A video!

Annotated setup with IR sensors

Continue reading Adding IR send and receive capabilities to my home-assistant based smart home

Using an Aeotech recessed door sensor in home-assistant

I went over to my friend’s house last night to help get home-assistant on a Raspberry Pi working for his z-wave door sensors and siren. The Ecotech sensor showed up fine but the Aeotech recessed one (Recessed Door Sensor Gen5 , ZW089-A) did not show up by default. After poking around in the logs for a while, we realized that it was sending BASIC_SET command classes instead of BinarySensor commands. Apparently (I thought I had fixed this already), home-assistant doesn’t recognize the BASIC_SET commands just yet. But looking at the config file for this device, we discovered that config item 121 changes which kind of report this sensor sends:
Continue reading Using an Aeotech recessed door sensor in home-assistant

Determine your location from relative FM radio signal strengths

In this post, we’ll take a brief measurement of regular old FM radio stations and try to determine where we are. It’s like a GPS but with local FM transmitters instead of satellites. I did this just for fun. I wonder if it could be used for indoor location and stuff? It is nowhere near as accurate as GPS. But whatever.

calibrated_result1Reading FM radio signal power

The first step is to get a reading of all the nearby radio stations. I used gnuradio and a HackRF One software defined radio. A simple flow-chart that takes the FFT and dumps it to a file is all I needed. I had to throttle the I/O or else my computer would freeze. I used 16M sample rate to have as wide a bandwidth as possible.

The gnuradio flow-chart used to gather data
The gnuradio flow-chart used to gather data

The file sink saves the FFT results in 4-byte integers and just has one 1024-length vector after the other. In a few seconds, I had a 50 MB of data. I did all the post-processing in an external Python script.

Continue reading Determine your location from relative FM radio signal strengths

Home automation with Z-Wave, Home-Assistant, Aeon Multisensor, HUE lights, and a Raspberry Pi 2

I’ve always dreamed of having a “smart-home.” There’s just something cool about being able to flip switches and read sensors and have a program turn a light on when you open the door, but only if it’s dark. This post is about home automation.

UPDATE: I made a video demoing everything:

Continue reading Home automation with Z-Wave, Home-Assistant, Aeon Multisensor, HUE lights, and a Raspberry Pi 2

Sending and receiving Morse code with a laser and a Raspberry Pi

Sending and receiving text with Morse code light pulses across the room (or to your neighbor) is a fun and cheap project you can do on a Raspberry Pi or Arduino or any other microcontroller.  This post explains how I did it, and how you can do it too.

Laser blinking
It’s working!


The hardware is simple and cheap. Here’s my parts list:

  • Raspberry Pi B+ as the controller. This does the sending, receiving, and signal processing.
  • Photoresistor – Just a little guy that has variable resistance based on how much light is hitting it
  • A 220 Ohm resistor – to make a voltage divider with the photoresistor for reading the input signal
  • MCP3008 10-bit Analog-to-Digital Converter (ADC) – Since the RPi doesn’t come with an ADC, this is required for converting analog voltage from the photoresistor into a signal I can process on the RPi.
  • Laser module – to transmit with laser light. I got one from for like 3 bucks.
  • Breadboard, wires

You can learn how to use the ADC at this Adafruit tutorial. I decided to talk to the ADC with the RPi’s hardware SPI interface, which I had already enabled. I wanted to be able to go very fast. (You can alternatively do SPI off of GPIO ports with software, if you prefer.) The laser just hooks directly between a GPIO and +5V. Here’s the layout:

Wiring diagram
The wiring diagram

And what it actually looks like:

The actual laserComm hardware.
The actual laserComm hardware.

Continue reading Sending and receiving Morse code with a laser and a Raspberry Pi

Data acquisition and live-plotting over the network with a Raspberry PI and Python

Sensors are fun! I wanted to keep an eye on the temperature of one of my home physics projects and realized I needed a thermocouple and a convenient way to read it. This post is about how I got one and wrote a Python program to send the data from a Raspberry PI over the network to my laptop, which plots the results in real time.

Thermocouple in fridge
Thermocouple in fridge

I bought a K-type thermocouple for several dollars that ranges from -100°C to 500°C. The Seebeck effect that makes it work only gives microvolts per degree so I needed a good amplifier for it. And since I wanted to read it on a Raspberry PI (which only has digital inputs), I’d need an analog-to-digital converter (ADC). Conveniently, the MAX31855 does amplification and ADC and is like $15, so I got one. Sweet. There’s even a Python library for it that makes it easy to access. Double-sweet.

The MAX31855 thermocouple amp and ADC
The MAX31855 thermocouple amp and ADC

Continue reading Data acquisition and live-plotting over the network with a Raspberry PI and Python

Reading out a Crookes radiometer (light-mill) with Python and OpenCV

Since I’m on a late 1800s physics kick, I’ve been staring at my Crookes radiometer a lot.

My Crookes radiometer, or light-mill
My Crookes radiometer, or light-mill

You might remember these from the hands-on museum. Today, these are just novelty items, but back in the 1800’s, there were actually scientific instruments that could quantitatively read out the amount of incoming radiation (like visible or infrared light). They were generally read out using a calibrated spinning disk with slots in it. It’s like when you have a fan spinning in a dark room, and you adjust a strobe light until it looks like the fan isn’t spinning. Then, if you know how fast the strobe is blinking, you know how fast the object is spinning. This is called a stroboscope.

Well, I don’t have a stroboscope. But I do have a digital camera and a computer. As it turns out, I can read the Crookes radiometer using that!

Continue reading Reading out a Crookes radiometer (light-mill) with Python and OpenCV

Remote ham radio operation through a Raspberry Pi

This post is about how I set up remote operation on my ham radio through a Wifi network, over a VPN, and around the world using a Raspberry Pi, like this:


This could be useful for people who travel a lot and want to be able to use their rig while away. Or for people who want to set up their radio and antenna far away from where they actually live (e.g. apartment dwellers who have friends or parents who live in the boonies and wouldn’t mind you erecting an antenna). If you want to skip the VPN and just control your ham shack from your kitchen, that’s even easier.

I did this all with an ICOM-7100 all-mode radio, a Raspberry Pi B+, a Buffalo DD-WRT router/VPN server, and a laptop running Ubuntu 15.04. This post focuses on the radio control and networked audio but not on setting up the VPN.

Continue reading Remote ham radio operation through a Raspberry Pi

You can use open-source Linux for most stuff these days

Linux is an open-source operating system that you can download and install on your computer to complement (or even replace) your current operating system (Windows or MacOS). It started out when some guy wrote some code and gave it to the world community for free. It has become huge, and now provides the engine of Android phones and tablets, runs about 40% of the servers that make up the internet, and powers 97% of the world’s supercomputers. It’s a stunning success in the social open-source experiment.

For home use, it is just now ditching a reputation of being only for nerds, scientists, and hobbyists like me, with lots of headaches getting software and hardware working nicely with it. Really, I’ve been using it at home on my laptop since 2007 and it’s evolved to a point where I think it’s totally usable for most people. It also has a focus on privacy, security, and stability. Since the code is open (everyone can see and change how and why it works), there’s less mystery about what anyone is doing with your computer and more control to get it to do what you want. It was the speed and low-resource requirements that brought me to Linux originally, and it’s the quality and open philosophy that has made me a real proponent.

My desktop
My freshly-updated Ubuntu 15.10 desktop

Continue reading You can use open-source Linux for most stuff these days

Import precedence for different types of python extensions

I was doing some speed tests with cython in Windows and started wondering what the import precedence was between py, pyc, and pyd files (not to mention pyo). So I did a simple little test with two files, and just had a print statement in it and test2 just imported test1 (so it would create the pyc). I compiled with cython into test.pyd and then changed to print something else.

Conclusion: The pyd file was always imported if it was present. If it wasn’t there, then the new print statement from the new was printed out. I’m sure this is documented somewhere but I’m not sure where. The conclusion holds if there are “optimized” pyo files present as well.

how to fix: ctrl-c closes ipython within console2

Just FYI, I was having a problem with ipython 0.12 running within Console, where pressing Ctrl-C to cancel an operation in a Python would actually just close the whole ipython window! It was horrible, since before I reinstalled everything it wouldn’t do that. Anyway, there’s an email chain talking about this here, but it’s irrelevant to this particular problem, since I’m using mainline (non-dev repo) versions and don’t know what an ETS is.

Thankfully, there’s a very simple solution (assuming you made my same mistake). Rather than setting the console tab to C:python27scriptsipython.exe, instead set it to C:python27python.exe and Ctrl+C will give a KeyboardInterrupt and leave you in ipython, just like the good old days!

Building a python extension with Trilinos 10.10 on 64-bit Windows 7

Continuing the journey, the folks at work upgraded me to 64-bit Windows 7 and the Trilinos folks updated to 10.10. Needless to say, I had to rebuild. I can’t definitively say which of these steps is necessary but I can tell you that if you follow them, you can compile a C++ python extension that uses many aspects of Trilinos on this system. And not just a few packages from Trilinos either. In fact, I’m currently using all of the following:

  • Epetra
  • AztecOO
  • Anasazi
  • Teuchos
  • epetraext
  • Amesos

To do this, first run the trilinos cmake system without shared libraries. Then build. This builds things like epetra.lib. Now, go back and turn on shared libraries and build again, this time with shared libraries turned on. This builds the dlls, which I believe are required to build a python extension. If one of the ones you needs fails, just go in to Visual Studio 2008 and open up the Trilinos solution. Right click the project. Change from a static library to a dynamic librray. Change Use of MFC to “Use MFC if a Shared DLL.” Change Use of ATL to “Dynamic Link to ATL.” The rebuild the package. The DLL should be created.

Build trilinos shared library
VS 2008 options to build shared libraries

Once all the static and shared libraries are built, I didn’t even mess with environmental variables (being a grad student and all) so I just copied them all into the build directory of my Python extension. On import, it worked just fine. Amazing!

I have yet to try linking to MSMPI and running large reactor problems on Microsoft HPC but that will be coming someday.

favorite programming language jokes

These started coming up at work and people kept chiming in new ones. Here’s a list. Sorry in advance for the offensive one.

  • What’s a pirate’s favorite programming language?
  • What’s a miner’s favorite programming language?
  • What’s a hispanic’s favorite programming language?
  • What’s a diver’s favorite programming language?
  • What’s a mongoose’s favorite programming language?
  • What’s a Seattleite’s favorite programming language?
  • What’s the army’s favorite scripting language?
  • What’s the little mermaid’s favorite scripting language?
  • What’s Beethoven’s favorite programming language?
  • What’s a Frenchman’s favorite programming language?

Hilarious. Answers below.

Continue reading favorite programming language jokes

building a python extension with trilinos on linux and windows

Continuing my python extension project, I’ve now successfully built a toy python 2.7 extension in C++ that uses the Epetra package from Trilinos 10.8. I’ve built it on Ubuntu 11.10 (with gcc) and on Windows XP (with Visual Studio 2008) using distutils. It’s a bit of a hack, but it works and is a path forward for me.

I started with Linux, because I figured it would be easier. I learned a few things along the way. First, you have to have built Trilinos with shared libraries enabled. This is required for how Python handles things, I guess, and is made clear under the heading “Shared Libraries” in this file. The PyTrilinos package (which is effectively a professional set of python-trilinos extensions, btw. I needed a custom one!) needs them as well. So run your cmake-gui and check the shared libraries button. Besides the libepetra.a in the build folder, you’ll now also find a In Windows, you’ll get >500 errors if you compile all of trilinos with shared libraries, but Epetra at least worked.
Continue reading building a python extension with trilinos on linux and windows

my experience compiling against Trilinos on Ubuntu

I’m just embarking on a historic journey to learn a bit about the Trilinos math library.  To get started, I’ve gone through the hands-on tutorial a bit and after a few tries, I’ve gotten something compiled.

Trilinos 10.4 is available in the repositories for Ubuntu (11.10 confirmed). But Trilinos 10.8 is out now, so I had to build my own. Their instructions with CMAKE were quite good and the build process didn’t have too many hiccups. I found that the cmake gui is the way to go. Make sure you enable Didasko (the tutorial) in the cmake gui. When you do this, a few more Didasko options show up so you have to go enable those too.
Continue reading my experience compiling against Trilinos on Ubuntu

Installing ERANOS 2.1 on a 64-bit linux workstation

I just built ERANOS 2.1 reactor analysis suite from France on a 64-bit linux workstation with Red Hat 7. It wasn’t that bad, as the CEA has done a great job at automating the installation process. I’ll briefly discuss how things went.

First of all, don’t cd into the UTILS folder and run Install. You have to do as it says and run UTILS/Install if it doesn’t work the first time. Also as it says, it’s smart to copy all the library disks over before loading the code DVD.
Continue reading Installing ERANOS 2.1 on a 64-bit linux workstation

ipython on windows

iPython is a very nice way to fiddle around with Python interactively. Most beginners just use IDLE for this, since it comes with Python. But as you get to plotting stuff, you’ll find that IDLE just doesn’t cut it (it freezes every time you plot anything). So you try the regular python command line (on the black screen) but get frustrated that you can’t change directories or use tabbed completion or anything. You hear about ipython and try to use it but it’s not very obvious. Well all you have to do is make a shortcut and you’ll be off and running.By the way, if you had started on Linux in the first place, you wouldn’t have to deal with all this! Anyway, on Windows,

0. Make sure you have Python installed with matplotlib and numpy modules.

1. Get ipython from install it from the download page for your version of python

2. Make a shortcut to python.exe and then right click it and hit properties to change where it points to to:

3. Double click the shortcut.

Yay. Now you have ipython on windows with matplotlib interactive capabilities! I suggest having your text editor open in one window and this ipython window in the other. Edit your code, save it, and then type “run” to run it in the interpreter.

A nice screenshot of an interactive matplotlib session on Windows