IoT Thermal Printer, Twilio, Heroku, RabbitMQ, and Python

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…  

IoTA 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

Parts:
Pi-Printer-Parts

Finished product:
Pi-Printer

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…

iot_printer_image

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…

Installing Python on Windows

The next step is creating you a login account on Heroku and installing the Heroku Toolbelt for your platform.

Heroku Toolbelt

Next is to following the below instructions on configuring Python and the Flask microframework.

Getting Started with Python on Heroku

Next you will want to install the Twilio module for Python as explained below for accessing Twilio using REST calls.

twilio-python

Next you will want to install the Pika library which will give you RabbitMQ support from Python.

Introduction to Pika

Pika

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…

RabbitMQ

Next, you should go to Heroku account and create a new application.

heroku_dashboard

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.

twilio-phone

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.Fiddler1

After hitting Execute, here is the response:Fiddler2

 

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)

UnicodeEncodeError

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.

txtbody=textwrap.wrap(body.encode(‘ascii’,'replace’),32)
for line in txtbody:
  printer.println(line)

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…

Posted in Projects, Python, Raspberry Pi | Leave a comment

Merry Christmas and Happy New Year!

As I close out this blog for this year, 2013, I just wanted to take a few moments and wish all my dear readers and friends a very Merry Christmas and Happy New Year!

For me, 2013 has been a very busy year with a lot of good times and a number of frustrating times…  Luckily for me, no real bad times that I can remember…  

I’m looking forward to see what 2014 brings me and my family –

Until next time!

image

Posted in Uncategorized | Leave a comment

Using a Timestamp string from .NET’s System.DateTime

Today I had a need to produce a timestamp string for creating log files.   You can easily convert a System.DateTime object into a string based on current date and time by using the ToString() method of the DateTime object.   For example:

DateTime d = DateTime.Now;
String s = d.ToString(“yyyyMMdd-HHmmss.ffff”);

If today is 08/06/2013 10:15:33.345AM, it will create the string value of “20130806-101533.345 when the above code is executed.

Hope this helps!

Posted in .NET | Leave a comment

HP LaserJet P3005X – Memory Test Failure Replace DIMM 0

I have an HP LaserJet P3005X that I have been using for a number of years now.  It was a hand-me-down and until recently has worked flawlessly.

HP-P3005X

The other day, I was trying to print and noticed the printer wasn’t working.  After a few power-offs and resets, it would always seem to fail at the POST and hang at various steps.

On one of the failed reset attempts, I noticed an error on the LED that said “Memory test failure, replace DIMM 0”, or something another.

A quick search on Google, I found that one of the common issues with this error is the need to replace the “Formatter Board”.   The part # is Q7848-60003.

Buying a new replacement board seemed too expensive for home use… like over $350.  However, I checked out e-bay and found a used board for only $70.   I figured what the heck and figured if it works… great and I just saved a boat-load of money from having to buy another printer.  If not, I’ll just be out $70.   

Installing the replacement board was relatively simple.

  1. First, turn off your printer and unplug it from the back.
  2. From the right-side of the printer, slide the side-panel towards the back of the printer.  It will then pop off.
  3. Using your fingers, swing the medal door outward to open the circuit board compartment.   You can lift up on the door and the hinges will come off to allow you to remove it.  You will need to do this in order to get to the corner screws in the back.
  4. Carefully remove the three wires from the top left of the board.   The bigger wire, you will need to lift up from the bottom the white notch in order for it to release.
  5. Using a Phillips screw-driver, remove the five screws located in the corners and near the middle of the board.
  6. Then unplug your network cable.
  7. The board should basically come out without any issues.
  8. Insert new board, and reverse this process to put everything back together.
  9. Finally, plug your printer back in and turn it on.  Hopefully it will complete the POST process without any issues.   If it doesn’t work, double check all your cable connections and ensure your memory card is seated firmly and making a good connection.

Here is the picture of the side-pane and medal door open:

HP-P3005X-Open

Here is a picture of the Q7848-60003 Reformatter Board:

Q7848-60003-Formatter-Board

Now that I have everything working again, I’m hoping this printer will last me a few more years…

Hope this helps!

Posted in Repair, Troubleshooting | Leave a comment

GoodTimes GT-ORB1 Bluetooth Sound System

I’m a geek.

I’m always buying gadgets and have previous blog posts to prove it and my bad spending habits!

Verizon Home Monitoring and Control

Filtrete 3M-50 Wi-Fi Thermostat

CurrentCost Envi and Google PowerMeter

Using lynda.com and pluralsight.com with AppleTV

As such, I’m always looking for gadgets to make my life easier or to give me information that I may not have otherwise…  Isn’t that what geeks do anyway?

The family and I recently purchased an in-ground pool for our home.   Since construction is finally complete, I have been enjoying my days in the pool.

I have an iPod Touch and have attempted to use an old iHome speaker system to listen to my music or podcasts near the pool (like .NET Rocks, RunAs Radio, etc.)   Dragging out the iHome device and an extension cord to the deck each time I want to use it is a big hassle for me.   Plus I don’t like the idea of mixing electricity with water…. if you know what I mean…

I even have a cool SoundFreaq Bluetooth wireless device that I frequently use in the house… but this too is no better for me outside…

Lucky for me, I ran into the GoodTimes GT-ORB1 Wireless Bluetooth Floating Sound System for Pools, Ponds, or other…

Orb1                   Orb2

I ordered mine from Amazon for $119.

This little device works great and has pretty good sound.  I’ve used it about 4-5 times already for hours without any issues.   The kids even love it because I can play their music when they are playing in the pool as well…

It takes six AA batteries for the Orb.   And another two AA batteries for the docking station.  

This device allows your Bluetooth enabled phone or device to pair directly to the Orb.  If for some reason your device isn’t Bluetooth enabled, it comes with an external docking station that allows you to plug your device directly into it using an RCA cable.   The docking station will then pair to the Orb via Bluetooth.   I’m not using the docking station at this time.

I’ve only had a few cases where the device loses its connection to the Orb, even though its only a few feet away.   It will beep for a sec, then auto-reconnect.  I’m guessing its simply because its up against the pool wall and being blocked slightly…

I’m even using the Orb device for dual purposes.  For example, I frequently place the Orb on the table next to the pool while sitting in the chair to listen to my music and podcast.    And since I bring the Orb device in the house when done, it sits nicely in the kitchen where I frequently use it for listening as well while cooking food or cleaning…

I will forewarn you that my first device came broken from Amazon.   Even though it was packaged fairly well, it must have been dropped in the original box because the Orb was split in half and the screw mounts broken.    I promptly returned it for another…

Now go order your Orb device now!

Posted in Reviews | Leave a comment

CurrentCost Envi and Windows 8 Device Driver Support

In October 2010, I wrote a post about using a great little device from CurrentCost called Envi along with Google’s PowerMeter which allowed you to monitor your home energy consumption.  

CurrentCost Envi and Google PowerMeter

Around June 2011, Google announced that it was retiring its Google PowerMeter on 09/16/2011.

Google Retires PowerMeter

Sadly, since that time, I have been pretty much dead in the water, as I never really found a good third party replacement for Google’s PowerMeter in the USA.   In fact, I have upgraded my PC a few times and never bothered to transfer the programs and configuration setup from one machine to another.   To make matters worse, upgrading to Windows 8 in October 2012 essentially killed the ENVI product for me due to lack of Windows 8 driver support.

The product support hasn’t really been updated in years and evidently the Prolific drivers used by the ENVI only support Windows XP, Vista, and Windows 7 for both x86 and x64.  And Mac O/S.   If you Google search around trying to find updated drivers, you will find many complaints and issues using Windows 8, but unfortunately no solutions.

The cable used is an RJ45 to USB cable by Prolific.

Envi-OriginalSerialCableRJ45

Today I thought I’d attempt to revisit this driver issue again and ran across a replacement cable on eBay that costs $24.99.   I figured that if I didn’t buy this cable, I may as well throw my ENVI away; as I wasn’t planning on running Win7 just for this.

The replacement cable is a USB-to-UART bridge that has a virtual COM port.   It uses the SiLabs CP2102 chipset and appears to support an array of Windows…  Windows 8, 7, Server 2008, Vista, Server 2003, XP, and Windows 2000.   Linux and Mac are also supported.

Envi-NewSerialCable

When the cable arrived, I simply installed the drivers that were included on the DVD then plugged in the new cable.  Within a few seconds the device was recognized and installed using COM3.

Note below the “Silicon Labs CP210x USB to UART Bridge (COM3).

Envi-DeviceManager

After installation, I re-installed the ENVI C2 Terminal software (or you can use PUTTY).  I selected COM3, ENVI (CC128 – 57600) and instantly began seeing raw data in the form of XML come across the serial port.

Envi-C2Terminal

This new cable worked nicely and I’m surprised CurrentCost doesn’t provide some other alternatives to using their outdated cable and drivers.  

Now I can get back to finding my old Windows service that captured this raw data and inserted into MySQL for historical purposes or simply use the plug-in to send the data to Pachube.    Either way, I’m happy to bring life back to this little monitoring device.

ENVI

Hope this helps!

Posted in Reviews, Troubleshooting, Windows 8 | Leave a comment

How to Determine IIS 7 AppPool’s ID

In a prior post, I documented how to obtain the AppPool IDs for W3WP.EXEs under IIS6.  Unfortunately, this procedure doesn’t work under IIS 7, but there is another easy way to figure this out.

1) Launch Task Manager

2) Select “Processes” tab, go to “View” and select “Select Columns…”

3) You will need to select “PID (Process Identifier)”.  then click “OK” button.

TaskManager

4) Scroll down until you find “W3WP.EXE”.   You will have one of these for each AppPool.

5) Go to an elevated DOS Prompt, CD to “C:\Windows\System32\Inetsrv folder

6) Enter APPCMD LIST WP.   You will get the following output:

CmdPrompt

7) Go back to Task Manager and find the PID that corresponds with the AppPool name that you created.

TaskManager2

Hope this helps!

Posted in Debugging, IIS | Leave a comment

Cannot Connect to Default Administrative Share C$ on Windows 2008 R2

The other day I had the need to browse out to one of my Windows 2008 R2 servers using one of the default administrative shares, such as C$.  

Trying to browse out using a UNC path of \\SERVER1\C$\FOLDER1 and I received an error “Access Denied”.

Trying to map a drive to \\SERVER1\C$ also returned an “Access Denied”.

After a little research, I learned that UAC (User Account Control) is set up to not allow access to the default shares remotely.

There is even a Microsoft KB article on this here.

To fix this issue and enable, simply launch RegEdit and set the following key:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system\ LocalAccountTokenFilterPolicy

0 = build filtered token (Remote UAC enabled)
1 = build elevated token (Remote UAC disabled)

By setting the DWORD entry to 1, you will be able to access the administrative shares since the remote logon token will not be filtered.

LocalACcountTokenFilterPolicy

Hope this helps!

Posted in Troubleshooting, Windows 2008 | 2 Comments

Http Error 503. Service Unavailable

Today I ran into an interesting problem with troubleshooting one of two Windows 2008 R2 load balanced services running IIS 7.

We have one ASP.NET 2.0 web service application running on each of the servers.   I could successfully browse out to the .asmx file without any issue.  However, when trying to pull up a static html file from the default c:\inetpub\wwwroot folder, I would get the error you see below:

503ServiceUnavailable

I checked folder permissions, etc., and did not find anything wrong.   This seemed like a no-brainer configuration – considering it was a simple static html file, right?  C’mon!

Since this was working on the first server, I was almost tempted to clone/reinstall the server until I discovered that the default website was set to use .NET 4.0.   The default AppPool was “ASP.NET v4.0 DefaultAppPool”.

See screen below:

AppPool

When I went to look at all my AppPools, I noticed that the “ASP.NET v4.0 DefaultAppPool” was not running.   I right-clicked and selected “Start” and then re-tested and the static html page came up.

AppPools

The ASP.NET web service that I tested earlier that worked is running under a different AppPool which explains why it worked; since it was already running.

This solution may not resolve every 503 error, but it sure did mine!

Hope this helps.

Posted in IIS, Troubleshooting | Leave a comment

DB2 – SQL1013N The database alias name or database name could not be found SQL0843N

I ran into an issue today when trying to restore a DB2 backup using DB2’s Control Center GUI application that failed during the restore but left the database name in the tree-view. 

Trying to DROP or REMOVE or even access the database would return access errors that the database didn’t exist or wasn’t in the system database.   Yet it was clearly showing in the tree-view.

Rebooting DB2 and whatnot did not help.

The solution for me was to jump to a DB2 CLI window and enter the following (under Windows):

  • db2 get dbm cfg | find “DFTDBPATH”
  • db2 catalog db USERDB1 on DFTDBPATH
  • db2 terminate
  • db2 drop db USERDB1

I then went back to Control Center, refreshed the tree-view, and my database was gone.

Hope this helps!

Posted in DB2, Troubleshooting | Leave a comment