Things have been pretty busy for me so far this year and frankly I haven’t had too much time for writing blog posts. But, I do want to share with you some of the exciting things I have been working on…
A few weekends ago, I spent a number of hours building my Internet of Things Thermal Printer. This project doesn’t require a ton of know-how, but does require a little soldering and programming. The instructions provided by Adafruit Learning System are top-notch and it’s hard to go wrong.
For those not familiar with the project, it is based on a Raspberry Pi computer and using a WiFi connection to communicate with the outside world. The Raspberry Pi, allows you to pull the weather, today’s date and time, and other useful things such as retrieving the quote of the day, or a stock quote, using your WiFi connection and the internet. What is nice about this unit is that it’s totally wireless and will work from almost anywhere in your house…
Assembly instructions from Adafruit:
AdaFruit Internet of Things Printer for RaspberryPi
Besides getting the basic printer up and running, I wanted to implement some ideas that others have developed to make their printer more useful.
One was a blog post by Wade Wagner demonstrating how he used the Twilio cloud service to allow his family to send SMS messages directly to his printer. Another project of Wade’s was to pull the school lunch menu each day from his kids school’s website and print it out each morning on the printer….
You can read more of this from Wade’s blog post below:
Send a Text Message to Your IoT Thermal Printer
I’m a hobbyist .NET developer and at the time, I wanted to get my printer up-and-running and have a working solution; rather than spend a lot of time re-developing something on another platform. For now, I’ll leave this part to you, if you’re so challenged…
Since the Raspberry Pi is a Debian Linux distribution, the preferred language of choice appears to be Python. Most of the scripts and libraries from Adafruit are Python scripts. So this seems to be the natural choice…
Now Wade didn’t elaborate on the details to getting the Python, Heroku, and RabbitMQ setup for first-time users. Since all this was totally new to me, there was a bit of a learning curve… But I persevered. I’m hoping my instructions below will guide you, should you choose to implement similar functionality on your printer as well…
Most of my time, outside of soldering and putting the printer together, was software related and trying to figure out what I needed to do to get Python to work on Heroku.
Since I’m a Windows guy, the first thing you must do is install Python on Windows. If you’re using Linux, I’m sure there are lots of tutorials out there explaining this…
The next step is creating you a login account on Heroku and installing the Heroku Toolbelt for your platform.
Next is to following the below instructions on configuring Python and the Flask microframework.
Next you will want to install the Twilio module for Python as explained below for accessing Twilio using REST calls.
Next you will want to install the Pika library which will give you RabbitMQ support from Python.
At this point, you probably want to go to CloudAMQP and create you a Lemer account and create you a message queue. Maybe browse out to it to ensure you can get to everything properly…
Next, you should go to Heroku account and create a new application.
Then you should go to Twilio and create a phone number and under the Messaging section, point the “Request URL” to your Heroku application using POST method.
And this point, you should have copied your AMQP URL from CloudAMQP into your Python web service script that you will be hosting on Heroku. You can refer to Wade’s website for his code.
Note that the AMQP URL contains your user name and application key. It’s a very long alphanumeric URL. If you type this in manually, you must ensure you copy it correctly or you will get strange errors or things simply won’t work. I spent a number of hours fixing typos since my copy/paste between Windows and Putty wasn’t working within Nano.
You may want to simply use Notepad or something on Windows and then install SCP or SFTP so that you can drag-and-drop your files directly from your Windows computer to your Raspberry Pi filesystem. This would have saved me a lot of time…
Initially, during testing, I verified I could send information to my Heroku web service successfully, to ensure at least that part was working properly. I used Fiddler for this. Below you can see where I simply passed parameters of “From” and “Body” to the web service and verified I got a 200 response back and the expected text. I spent a number of hours troubleshooting this and later found out the “From” and “FROM” made a difference for Python!!! So watch your case…
Here I’m calling the web service on Heroku using Fiddler and a GET, passing parameters in the URL. The Python script from within the web service will parse out these parameters and put a message in the queue.
At this point, I have a working web service. Debugging Python on the server seemed to be painful and I had to put a lot of print statements all over to see where it was failing… I’m sure there is an easier way, but this worked for me…
Next, you will want to edit the amqp.py script from Wade’s website and copy it over to your Raspberry Pi. You will need your AMQP URL again, so ensure you paste it in correctly. Again, I would highly recommend using SCP to copy the file over to your Raspberry Pi.
You may want to simply run the amqp.py script directly from your /etc/rc.local. This is what I did.
I learned that if you press the power button on the printer for 1-2 secs and release, it will cause the main.py script to make a request out to Yahoo and print the current weather conditions.
If you hold the button down for 4-5 secs, it will actually gracefully shutdown the Raspberry Pi. If you want to restart, you seem to have to unplug it from power and plug it back in…
Now after all got all of this working, I quickly ran into a problem when I had my wife attempt to send SMS messages to the printer. It worked fine from my phone…
When the wife sent a message, it would not print on the printer and instead displayed the below error message on the console:
“UnicodeEncodeError: ‘ascii’ codex can’t encode character u’\xc4’ in position 14: ordinal not in range(120)
It turned out that my wife has the Finnish keyboard enabled on her Windows Phone… which uses unicode characters for all the non-ASCII related characters. Python can’t handle this and I had to resort to making some minor changes to the code.
Here I’m simply replacing any Unicode characters with a space so that it will still print. I’m using wraptext module to allow text to wrap to next line rather than get cutoff.
for line in txtbody:
You can get more information here:
Python Unicode HOWTO
Now you may also want to add quote of the day or stock capabilities to your printer. I found the below scripts pretty useful and straightforward to use:
qotd.py from github
ystockquote.py from github
In conclusion, my kids absolutely love this printer. They enjoy checking the weather each morning and seeing what messages the wife has sent to them each day when they get home from school. It was a fun project to put together and I learned a number of new technologies that I never would have used otherwise in the process….
In fact, I’m already using the skills I learned from Python to work on another Raspberry Pi project related to the CurrentCost Envi system that I have blogged about in the past. I’ll post more about this in a future post.
Until next time…