Building a Python Flask Web UI For Raspberry Pi Sure Elec LCD

In an earlier post I outlined how I setup a Sure Electronics LCD screen with my Raspberry Pi 3 using a Python driver. Whilst updating the LCD via command line is immensely useful I decided to build a UI to control the LCD send messages too it. By using a browser based UI I could update the LCD screen from anywhere. Essentially this was a chance to play with a Python web framework and write some code!

BlogFlaskUISureElecLCD2 

I’ve passed the UI’s URL round my family’s devices at home and they now send me messages whilst I’m in my study working/playing.

The end result can be found in my GitHub repo.

As my driver was in Python and I’m enjoying coding in Python at the moment I decided to use a Python Web framework to serve the HTML/JavaScript UI and host RESTful services on the server side to accept LCD commands. After some reading I went with Flask which  seemed perfect for my needs. I could have gone with Django but Flask seemed for appropriate for my needs. For a good comparison see this CodeMentor.io post. For a great tutorial on Flask checkout this series by Miguel Grinberg and this great post by Scotch.io.  

Building the server side web framework was easy and logical in Flask and I was able to get something setup in one file which served my needs. However after reading some Flask best practices I spread my solution out into a more appropriate structure. Flask will seem familiar to web developers with experience of ASP.net MVC, Web API, Node/Express etc. You define routes to handle incoming requests. The key aspects my solution are outlined below. I am running the Flask server directly on my Raspberry Pi and using it to serve the pages and host the services for commanding the LCD screen.

To install Flask (on a Pi) first install Python Pip (a popular Python Package Manager) via “apt-get install python-pip” or “apt-get install python3-pip” (for a Python v3 specific Pip) and then install Flask via “pip install flask”.

Flask comes with a small lightweight development server which runs your app in Dev mode and also auto restarts after code changes. I found this fast and robust enough for my needs. 

Lets check out the main parts of the code:

run.py:  This is the entry point for the app. When run it calls run in the app file and here I have optionally passed IP/Port I want the app to run on which enables the app to be exposed to the internal network so I can connect from other machines on the network. 

from app import app 
if __name__ == '__main__':
    app.run(host="192.100.100.100", port=5000)

app/__inti_.py & config.py: This is the app initialisation code and where it points to the config.py file where config settings can be set for the app.

app/views.py: This is the heart of the app. After importing the relevant python components and instantiating the Smartie LCD driver (from previous post), the routes for the app are defined.

@app.route("/")
def show_homepage():
    return "Home Page!"

@app.route("/lcd")
def show_lcdpage():
    name="Jeff"
    return render_template("lcd.html", name=name)

The route for root will just return the text “Home Page” whereas the route for /lcd will call render_template to return a templated HTML page (lcd.html) and passes any relevant data (e.g. “Jeff” which is irrelevant in this example”). Templates will be covered shortly below.

@app.route("/lcd/clear", methods=["POST","GET"])
def display_clear():
    smartie1.clear_screen()
    return "Success"

@app.route("/lcd/displaymessage", methods=["POST"])
def display_message():
    if not request.json:
        abort(400)
    smartie1.clear_screen()
    smartie1.backlight_on()
    smartie1.write_lines_scroll(request.json['Lines'])
    return "Success" 

Any POST or GET on http://SERVERADDRESS:PORT/lcd/clear will result in the smartie drivers clear screen method being called. A POST to “/lcd/displaymessage” will be validated to ensure that the request contains JSON data and then the data will be passed to the driver for display.

/app/templates/lcd.html: This is the main HTML page that enables the user to type the messages to display.

BlogFlaskUI2

The CSS and JavaScript used by this page is found in the static folder and referenced in the usual way………….

<!-- CSS for our app -->         
 <link rel="stylesheet" href="/static/lcd.css"/>

<!-- JS for our app --> 
<script type="text/javascript" src="/static/lcd.js" charset="utf-8"></script>

So we need to ensure that the flask server returns these static files, but we don’t want to have to define  a specific app.route for each one so instead we use this one in our views.py :

@app.route('/<path:filename>')  
def send_file(filename):  
      return send_from_directory('/static', filename)

This basically states that any requests for a file path are sourced from the /static folder directly. So we can just place any files in the static folder that we want to be served directly (the CSS and JavaScript files in our case).

/app/static/lcd.js:  From this JavaScript code we can consume the services hosted by Flask for our application. It’s using the XMLHttpRequest object to make AJAX requests to the Flask server. The SendCommand function takes callback methods which will be called on success or error.

function SendCommand(url, httpVerb, data, successCallback, errorCallback){
                
  var dataToSend;
  if(data!=null){
      var dataToSend = JSON.stringify(data);          
  }
   var request = new XMLHttpRequest();
  request.open(httpVerb, url, true);
  request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
   request.onload = function() {
      if(this.status >= 200 && this.status < 400){
          // success here
          var returnedData; 
          if (this.response != null){
              successCallback(returnedData, this.status);
          }                                                                        
      }
      else{
          //error returned from server
          errorCallback("Error response returned from server", this.status);
      }
  }
   request.onerror = function() {
          errorCallback("Error contacting server", this.status);
  }

  if (dataToSend != null){
      request.send(dataToSend);
  }
  else{
      request.send();
  }
}

That’s mostly it. Run the app by running the run.py module (e.g in the Python IDLE or terminal) and direct your browser to http://SERVERADDRESS:5000/lcd.

The code for my Python driver and this web app is available on GitHub here https://github.com/RichHewlett/smartie and https://github.com/RichHewlett/LCD-Smartie-Web.

Advertisements

Using a Sure Electronics LCD with the Raspberry Pi using Python

Using a Sure Electronics LCD with the Raspberry Pi using Python

After receiving a new Raspberry Pi 3 at Christmas I quickly set off looking for uses for this wonderful machine, and quickly found myself hooking it up to an LCD display. This post covers the Python driver I used and modified, as well as some other useful resources.

RPi3

Firstly as an owner of the original Raspberry Pi model B I was pleasantly surprised how capable the Raspberry Pi version 3 is. The performance is much improved and the new built in wireless is a superb addition which makes this version of the Pi more  usable and reduces the friction caused by having too many USB devices hanging off it like a mutant spider. The Pi v3 is powerful enough to be used as a basic workstation and I found myself working on it directly (within Raspbian OS) making the coding workflow more enjoyable as a result than the previous models. Also the as VNC Server is now built into the Raspbian OS it makes setting up the Pi as headless very easy. In fact once you have installed Raspbian and setup VNC then you can run it with just the power supply connected which makes it use little space and you can locate it anywhere.

SureElecLCD1Once my Pi3 was up and running I decided to connect an old LCD matrix screen to it. This LCD is Sure Electronics 20×4 screen which I bought years ago to attach to my HomeServer but never got around to it. The LCD has a USB interface and was used on Windows via the LCD Smartie application. A quick google showed that LCDProc is an equivalent tool for using LCD screens with Linux, and I found some useful tutorials (e.g. here & here). You can install LCDProc via “apt-get install lcdproc”.

I only had limited success with LCDProc but soon realised what I really needed was a programmatic way of controlling the LCD as opposed to a general display tool, so after some more googling I found a few variations of python drivers were being used to control similar LCDs, and this “Smartie” driver by Steve Davis worked best for me, which in turn was inspired by this driver. These drivers use the “pyserial” Python module, easily installable via Python PIP package manager:

“apt-get install python-pip” or “apt-get install python3-pip” (for python v3 pip)
followed by “pip install pyserial==3.0”.

RaspberryPiWhilst the smartie driver initially worked I found that it failed to work after a reboot. After some serious head scratching I realised that it would always work after running LCDProc, meaning the initalisation code for LCDProc setup up the LCD successfully for the Python driver to communicate.  After digging into the Sure Electronics manual and digging into the source code for the SureElec driver for LCDProc (here & here) I was able to find the initisiation command codes for the LCD and add this to the smartie python driver. This enabled the Python driver to be used without relying at all on LCDproc or similar software.

 I have also added some new functionality to the driver which includes flashing, wrapping text and scrolling multiple lines etc. Also included is a demo function that runs through the various functions provided by the driver to show what’s possible and to help testing after making any modications. My updated driver can be found on my GitHub repo (https://github.com/RichHewlett/smartie). With this driver i was able to control my LCD backlight, display text and get a temperature reading. The fact that it was writen in Python was a bonus as I find Python a great langage for coding fun projects.

SureElecLCD2

If you have a Sure Electronics LCD you may be able to take this and use it, or modify it as required, although there are many variations of these devices. Once you have connected your LCD and rebooted the machine just check that the LCD is on the right USB port in Linux (e.g. /dev/ttyUSB0) and that the user has permissions on that port. If you have a Sure 16×4 screen then this will probably work by modifying the SCREEN_WIDTH constant to 16. If you have a different LCD Screen then you may need to modify the initalisation codes and command strings that get sent to the LCD by each method. A good source of refercence is to check out the LCDProc source code for their bundled drivers as they support many screens.

In the next post I’ll cover my next step of building Python Web App using Flask to control the LCD screen from a browser.

BigTrak Is Back

imageThe 1980’s iconic “programmable electronic vehicle” BigTrak is back and in the shops now. I always wanted one of these when I was a boy and now I finally get to buy one, and I can also justify the purchase by buying it as a Christmas present for my son. Having boys is great!

Below is the classic 80’s advert in case you’ve forgotten how useful BigTrak is for delivering an apple to your resting dad:

New Intel Core i5 Desktop Build

After my trusty Pentium 4 home build finally bit the dust I’ve invested in a new desktop. Whilst performance was important for this build (as it’s my main desktop) it had to be cost effective. Luckily Intel’s new Core i3/5/7 CPUs are now mainstream and getting excellent reviews making them the ideal choice for this project. Not only are they more powerful Intel Core i5 750and more energy frugal than their predecessors there is plenty of choice in their ranges to suit most budgets. Initially the i3 seemed to offer the best value for my requirements but some shopping around quickly showed that the i5’s are available for only a few pounds more. What’s more the i5 and i7’s come with the ‘Turbo Boost’ feature which looks good in theory and benefits from a snappy name. In the end I decided on the Intel Core i5 750 which gets good reviews (Best Buy Computer Shopper May 2010) and is available at almost i3 prices. It’s a 2.66 GHz Quad Core that includes ‘Turbo Boost’ but doesn’t include the built in graphics chip that many Core ix’s do (not a problem if you’re expecting to include a dedicated Graphics card).

BioStar TH55B HD  I combined the CPU with a BioStar TH55B HD  Motherboard (Best Buy Computer Shopper May 2010) and 4GB of Kingston ValueRAM (2x 2GB). The motherboard comes with four slots for RAM and so two 2GB sticks under Dual Channel configuration should be fine for now and it leaves two extra slots for a future RAM upgrade.

 

For a case I wanted something that looked good and had plenty of space for future expansion OCZ ModXStream Pro 500W and good air flow. The Antec Three Hundred Case is an excellent midi tower case with Antec 300a smart look and some quality features for an excellent price. For power I’ve gone for the OCZ ModXStream Pro 500W which seems to be well made and came with a good selection of quality power cables (and a cable bag?).  I have yet to measure the power consumption of this build but I expect it to be fairly low.

As I’m a big fan of Windows Home Server I tend to centralise my data storage onto the server resulting in no real need for a large capacity hard drive. I couldn’t justify the cost of an SSD drive so for this build I have opted for a new WD Caviar Blue 250GB SATA hard drive and have thrown in my 200GB Seagate Barracuda drive from my old PC as additional storage. I’ve also recycled my DVD-RW and DVD Rom’s from my old PC.

Ok so that’s the good stuff now what’s the ugly duckling in the build? Well as I don’t use my desktop for gaming I have no need for a meaty video card, hence my decision to go with a budget card (the GeForce 210 512MB DDR-2 PCI Express). Performance of this card seems fine for desktop use but I am currently suffering a random glitch whereby the display sometimes seems to duplicate and not refresh correctly. This could be the card or the Nivida Windows 7 64bit drivers. Either way it is annoying and will result in either a hunt for new drivers or a new card. Other than this problem the build has been plain sailing and I would recommend any of these components, especially the i5 which so far as shown to be powerful and running at steady temperatures.

Lastly I took the opportunity from this build to move to 64bit, a move I’ve been quietly resisting for a while. Whilst I needed a 64bit OS to make use of the 4GB of RAM in this build I was nervous at the thought of not being able to source drivers for older peripherals. Whilst many people recommend running 64bit few could give me a good reason that didn’t involve irregular scenarios of needing to register large amounts of memory etc. Also whilst Microsoft insists the hardware vendors now provide 64 bit drivers in order to qualify for the windows logo I suspect that these 64 bit drivers have historically undergone less testing in the wild due to the lower proportion of 64 bit Windows PCs. These days however most PCs sold seem to include 64 bit Windows 7 and so it seems a safe time to take the plunge. In the end my experience has been very positive with my peripherals and software mostly installing fine (I had a few issues with an old Logitech Webcam) and I’m pleased with the 64 bit experience.

‘Windows Home Server’ Build & Setup

I recently setup a new Windows Home Server and this post covers why I chose this operating system and how I setup my server.

Requirements:

My requirement was for an extendable ‘always on’ network attached file storage solution that would allow me to access my files from any machine in the house (and ideally remotely via the Internet when required) whilst providing some fault tolerance data protection. Having all my data in one place makes it easier to manage (less duplication of files across machines) and easier to back-up. This centralisation of data however means being more susceptible to hardware failure (e.g. hard disk failure) and so a solution with either a RAID configuration or something similar was required which ruled out most budget NAS Storage devices. After investigating the options I decided to build a Windows Home Server (WHS). This meets all the requirements above and also adds other neat features such as the extensible Add-In model (a huge bonus for a .Net developer like me).

Buy vs Build:

Having decided on Windows Home Server as the solution the next step was to decide whether to buy or build. There are several very smart WHS devices available from manufacturers like HP and Acer. Whilst these are the easy option they are not the cheapest or the easiest to extend. Also the availability of these devices varies depending on your geographical location. Based on these factors I decided to build.

Build Option:

The fact that WHS has such modest hardware requirements means that building a server is a very economical option. As my server will be ‘always on’ I put power efficiency as a key requirement in my build. To this end I considered the Intel Atom processor found in most ‘NetBooks’. These consume little power and pack enough punch to run WHS comfortably. The Atom CPU comes pre-attached to an Intel motherboard (you can’t buy them separately yet) for under £50. However as I wanted the storage in my server to be extendable and grow over the next few  years I needed the space for at least 4 hard drives but the majority of Intel Atom boards only come with 2 SATA ports. Some boards do exist with four SATA ports but they are hard to source. Another possible Atom drawback is that it may be difficult to source Windows 2003 drivers (required for WHS) for ‘NetBook’ targeted Atom motherboards.

Buy Option:

image24Eventually after some investigations I had a list of parts to build into my shiny new server, but also a few reservations. Firstly would all these components play nicely together and would the build be solid enough to meet my ‘always on’ requirement. After discussions with a colleague he suggested I look for pre-built end of line servers, which is what I did. I quickly found the HP Proliant Ml110 G5 going for £170, bargain. With 1GB RAM, Dual Core Pentium 1.8Ghz CPU, on-board video, Gigabit NIC (Network Interface Card), 160GB HDD, DVD ROM and a multitude of SATA ports it was ideal.

Sure it lacked the power saving benefits of an Atom processor based server but it’s solid Enterprise level build quality more than makes up for it. As the server is designed to run Windows 2003 drivers would also not be a problem.

  BiosInfo

 

For storage I purchased two Western Digital Caviar Green 750GB SATA drives to sit alongside the HP’s 160GB disk. By buying two I can make full use of WHS’s data duplication features to protect my data. Whilst the ‘Green’ branded disks are not as fast as traditional drives they are packed with energy saving features which I value in an ‘always on’ server. 

internals

Hardware:

After much deliberation on whether to use the faster 160GB drive or the larger 750GB drive for the system drive I decided to install a 750GB drive as the system drive, mainly to ensure maximum extendibility. Whichever I installed as the system drive I would be stuck with (without reinstalling the Operating System) and I didn’t want to be limited to the smaller 160GB drive. To make installation of the OS easier I only connected up the first hard drive, and then connected the other two later once the OS was up and running.

 drives

Software Installation:

Once the hardware was sorted I put in the WHS DVD and followed the instructions. The installation went quicker than expected, surprisingly not spending long on performing the ‘Microsoft Updates’. Once installed I logged on to find that WHS didn’t have the right NIC (Network Interface Card) drivers and therefore the NIC hadn’t been installed. This of course explains why I didn’t have to wait for the install to download the updates as it couldn’t get on the web to find them. I installed the NIC drivers from the HP CD and rebooted to find that I could now access the internet via Internet Explorer but neither ‘Windows Update’ nor ‘Product Activation’ would connect. After further investigation (and much head scratching) I found this error in the Windows Event Log:

Type: Error.  Source: W32Time.
Description: Time Provider NtpClient: An error occurred during DNS lookup of the manually configured peer ‘time.windows.com,0x1’. NtpClient will try the DNS lookup again in 15 minutes. The error was: A socket operation was attempted to an unreachable host. (0x80072751)

Checking the System Time revealed I was two years in the past (2007) for some unknown  reason. After correcting the date I could connect to Windows Update fine. After a mammoth 70 updates and a reboot I’m presented with a strict ‘Activate Now’ prompt on logon. I presume that since my WHS install believes it has been installed for two years without activation it thinks it’s time to get serious. After I activate it I ran Windows Update again and this time it installs 5 more updates. Once the OS is stable I connect up the extra hard drives and add them to the storage pool via the WHS Console Server Storage tab.

Clients:

In order to connect to your Client PCs the server and Clients need to be in the same Workgroup making this alignment the next task, along with checking for useful machine names/descriptions. Once all the clients are ready I installed the Client Connector software on each client (all Windows 7 clients) and configure their backup schedules. All clients connected and performed a successful backup first time.

Before copying across all my data onto the Windows Home Server Shared Folders I made sure that ‘Folder Duplication’ was turned off. This was purely to maximise the transfer speed (as WHS didn’t have to perform any duplication during the copy process) but I made sure I turned ‘Folder Duplication’ on for all folders after the data was in place.

Setting up a Printer Server :

Next I wanted to set-up my server as a Print Server ensuring that I could print from any machine without having to turn on the Desktop hosting the printer first. The printer is a basic Lexmark Z615 but there are some unsupported Windows 2003 drivers on the Lexmark site. After trial and error with these though I abandoned them and reverted to the XP drivers which worked ok. I did have to reboot several times though to completely remove the failed printer installed with the Windows 2003 drivers.

An annoying feature of Windows is that it searches the local network for other printers and adds them to the server. I don’t want ‘Print to OneNote’ and ‘XPS Document Printer’ printers on my server but deleting them is pointless as they will reappear. To prevent Windows from performing this auto search you need to turn it off by deselecting the option in: My Computer > Tools > Folder Options > View > “Automatically Search for Network Folders & Printers’.

With my print server setup I attempted to add the printer to my Windows 7 client, but this was to prove difficult too. I couldn’t find an option to specify the correct drivers to use for the Printer and the Vista printers (needed for Windows 7) weren’t installed on my server. In the end I found this blog post  where it explains how to use the Print Manager tool (new to Vista Sp1) to add additional drivers to your print server. This worked perfectly and on the next attempt it downloaded the Vista drivers correctly and installed the printer successfully.

WHS Add Ins:

I intend to install and (time permitting) write plenty of Add Ins for use with WHS as I think that they are an excellent way to add functionality to your server. So far I have installed the Microsoft WHS Toolkit v1.1 and Andreas M’s Advanced Admin Console. I find the Advanced Admin console useful for accessing admin tools via the Console without having to Remote Desktop into the server each time.  Over the next few weeks I hope to review the Power Management Add-Ins and install one to help my server to get a few hours sleep over night when it’s not required, thus saving power and money.

Summary:

So that’s my build story. My home server is up and running and I’m so far very impressed with it. I aim to post some more articles about Windows Home Server over the coming months.

Nokia E71 – An update

In May posted an entry about my new Nokia E71 smart phone. This is a quick update post and I can confirm that the novelty factor has still yet to wane. I’ve found more uses for my E71 than I originally expected. In addition to the applications listed in my original post I have since downloaded these apps:

  • Twibble: If you do that ‘Twitter’ thing then this application is an excellent mobile client that makes it easy to keep up to date on the move, and it works great on the E71.
  • * UPDATE:  I’ve since dumped Twibble for Snaptu. The Twitter client is excellent on Snaptu and you gain from the extra plugins such as RSS readers, FaceBook etc. * 
  • Snakes: A new 3D take on the old Nokia ‘Snake’ game. The link I used for this is no longer valid, but it might still be available on the Nokia site.
  • Top Hits Solitaires: A nice selection of basic card games.
  • Global Race – Raging Thunder: A very smart driving game.
  • Ovi Store: Despite Nokia’s efforts this is no Apple iPhone ‘App Store’ but it’s worth having a look through the list of available applications for your Nokia phone periodically as there are a lot of apps available and many are free.

I listen to lots of podcasts and I’m finding that the E71 makes this so easy through its in-built Podcasting application. It enables you to add podcasts to your regular download list by searching by podcast name or you can just  add the feed URL if you know it. You can then check for updates and download them for playing on the phone manually or automatically. The phones in-built speaker is solid too. It’s loud and clear enough to listen to in the car whilst travelling without the need for an additional FM Transmitter.

I find the Opera Mini browser makes mobile browsing really easy and slick on the E71. It’s very fast and easy to use, adjusting the view of the page to fit neatly on your screen minimising the amount of scrolling required per page.

All in all I’m still enjoying my E71 and am starting to wonder how I managed without it. My first months data transfer amount was a eye watering 1 GB of data which shows the extent to which I’ve been maximising the phones 3G connectivity features.

Nokia E71

image 

I’ve recently purchased a Nokia E71 and I am very impressed with it. If you’re an E71 owner then check out this list of useful links:

These sites provide lots of info on the phone and links to applications you can download. Also check out the Nokia support page where you can download some software that may not have come pre-installed (e.g Global Race, Solitaire).

Recommended applications for your E71:

Wii as a Media Centre

Using your games console as a media player is not just for PlayStation or X-Box owners, its also possibly with the Wii. You can consume all your media (play music, view photos etc) using your television and your Wii.

As long as you have your Wii connected to the Internet, all you need is to download the Internet Channel from the Wii Shop (costs a couple of quid), and use ORB. Install ORB on your PC (where the media lives) and then browse to ORB online from your Wii. You can then browse your PC’s music collection and photos using your Wii.

I found it a little slow (could be my wireless network), but its usable and a great way to maximise your Wii.

Check out http://www.orb.com/wii/