Introduction: Raspberry Pi Weather Depicting Coat Hanger

“Imitation is the sincerest form of flattery that mediocrity can pay to greatness” - Oscar Wilde.

I think this essentially means that people who aren’t very bright copy other things. With that in mind the idea for this project came from this great project.

In my version of this project I planned to take JSON information from an open weather API and display the current weather by lighting up old Polaroids depicting different weather scenes. I also wanted to set up a web server as a ‘health-checker’ and to display the weather information. The brains behind the project would be a raspberry pi zero.

As an example, if the weather outside was snowy or below freezing then something like the snowy image above of the Snowdon Mountain Railway would be displayed.

Step 1: PARTS

Electronics

  • Raspberry Pi Zero, 5V power supply for Raspberry Pi, 8GB micro SD Card, USB Wi-Fi adapter, USB OTG cable and mini HDMI to HDMI Adapter. There's a great kit from Adafruit which you can get here.
  • Mouse, keyboard, HDMI cable and monitor for initial setup
  • 5V LED Strip. This prevents the need for any external power supply.
  • Assorted connector wires

Hardware

    • Wood for the 'coat hanger'
    • Framing wood for the Polaroid light boxes
    • Polaroids depicting weather scenes
    • Wood screws
    • Wood varnish
    • Wood glue
    • A selection of hooks

    Tools

    • Soldering iron and solder
    • Drill
    • Sandpaper
    • Paint brush for varnishing
    • Junior Hacksaw
    • Assorted screwdrivers

    Step 2: Raspberry Pi Setup

    There’s lots of information out there in the ether on how to setup up your Raspberry Pi for remote access so I won't repeat it here. The great things about having the remote access is that you can easily work and update the project once it's in place.

    To initially configure the raspberry pi you’ll need a mouse, keyboard, HDMI cable and monitor. (along with a mini HDMI to HDMI Adapter and USB OTG connector as the Pi Zero comes with micro USB and micro HDMI connectors) Once it’s connected to your wifi you can have full headless access from your laptop using PuTTY for simple command line access or TightVNC for the full-blow GUI desktop experience.

    Step 3: Build

    The wood we had cut to size as one of the big hardware stores and so the hardest part of it all was lugging it home. Once we had the various hooks we laid everything out to see where it would go. The next step was to sand the wood down to make sure there were no rough edges and then to varnish it.

    Once the varnish had dried the hooks and others bits and pieces can be screwed in using wood screws. We found what looks like a leather part off a saddle which we also fixed on. I thought it could come in handy if I ever decided to start a neckerchief collection and wanted something to hang them on.

    For each spot where a Polaroid picture would go drill a hole through the wood in the middle for the wiring. Each Polaroid then needs a frame to act as a light box which can be made using thin wood in a box shape. These were simply cut to size using a hacksaw and fixed with wood glue, varnished and then glued onto the wooden plank.

    The LED strips come with an adhesive backing and can just be attached onto either side of the frame and then wired up to the raspberry pi (see below). The Polaroids could then be glued to the top of the wood. We decided to keep the white frames they came in.

    There would be another step here for creating the temperature and humidity dials which I haven't go around to. Also if the plan is to fix the coat hanger to the wall there would be a few more steps required but as we planned to rest it on the top of a large mirror we avoided this.

    Once everything is fixed, glued or otherwise nailed on it’s time to program the badger.

    Step 4: Electronics

    The 5V LED strips can be cut along the copper contacts into individual LEDs. To fit the frames we cut them into segments that were two LEDs long. There's no need for any resistors as they already come with them. Cut away the plastic from the end of the strip to expose the copper contact. Heat the copper contact with the soldering iron before applying solder and then soldering the exposed wire to the contact. I would recommend using a small gauge wire here to prevent the contact breaking and it will also allow the wire to be better hidden within the frame.

    Each of the -ve wires can be soldered together to make a common ground which is then connected to the -ve pin on the Raspberry Pi and the +ve wires can then be attached to the relevant GPIO pin on the Raspberry Pi as shown in the hand drawn diagram.

    Step 5: WiringPi

    Tagline: WiringPi is a GPIO library written in C for the BCM2835 used in the Raspberry Pi.

    How to install: http://wiringpi.com/download-and-install/

    Although WiringPi is a C library it includes a command-line utility which can be used to program and setup the GPIO pins and allows for the control of the pins via shell scripts.

    Once installed use the gpio utility to test if the WiringPi library has been installed using the command line.

    gpio -v //this gives version information

    gpio readall //this will print the pinout information

    We can test our first Polaroid image at wPi pin 0 (GPIO 17) by first setting pin 0 as an output and then setting the pin to a logic 1. You can then turn it off by setting it back to 0.

    gpio mode 0 out

    gpio write 0 1

    gpio write 0 0

    To use the GPIO pins instead of the wPi pins use the -g flag.

    ggio -g mode 17 out

    gpio -g write 17 1

    gpio -g write 17 0

    In this way we can use the command line to quickly test each LED is working and each Polaroid is being lit up correctly.

    I also used this great instructable to create a test interface during the project so I could easily test the status of the different LEDs during the build using a great simple web interface. There’s a lot of great information in there.

    Now we need a way to light them up based on the weather.

    Step 6: Web Server

    In order to interact with the weather monitor and get information from the open weather API we need a web server. We don’t need the full traditional LAMP stack and can do away with the MySQL portion and just LAP away. The Raspberry Pi will give us the Linux and so we need to download the Apache server and PHP portion. As an aside you could get the MySQL component to store the weather data to display in any way you wish but I didn’t explore this route for this project.

    //install HTTP server and php

    sudo apt-get update

    sudo apt-get install apache2 php5 libapache2-mod-php5

    The root of the folder of your server will be something like /var/www/html. If you navigate to your Raspberry Pi IP address from another device on the network you should see the default page that comes with the installation and we are good to go.

    Step 7: JSON

    OpenWeatherMap comes with an API that allows you to access current weather data in JSON, XML and HTML formats. To get this information you make a simple API call. This is a free service and only requires that you sign up to get you own API key. A range of parameters can be used as part of the API call. To get the current weather information you make the following call with your own API key:

    http://api.openweathermap.org/data/2.5/weather?zip={zipcode},{city}&units=metric&APPID={API}

    This returns something like this: { "weather":[ { "id":800, "main":"Clear", "description":"clear sky", "icon":"01d" } ], "base":"stations", "main":{ "temp":1.68, "pressure":1022, "humidity":64, "temp_min":1, "temp_max":3 } }

    If JSON had a tagline... "JSON is a lightweight data-interchange format. It is easy for humans to read and write."

    JSON is essentially just a data interchange standard. Although not the only factor, with the rise of APIs, JSON is increasingly becoming the data interchange standard of choice and although you can grab the information in XML it made more sense to go with JSON.

    If you're ever feeling low and in need of a bit of schadenfreude just remember that the very first JSON message used by its designer Douglas Crockford failed due to the use of the reserved keyword do.

    var firstJSON = {to:"session",do:"test","message":"Hello World"}; // Syntax Error in ECMA 3

    If you really keen the full description of JSON is described in RFC 4627 but a few basic syntax rules include:

    1. Data is in name/value pairs.

    2. Data is separated by commas.

    3. Curly braces hold objects.

    4. Square brackets hold arrays

    Step 8: PHP Script

    The script below will initially set all the outputs to 0 before scrolling through the different images and lightening them up at one second intervals. It then grabs the current weather information from the API, applies some very basic logic to decided which Polaroid weather representations to light up before calling a system call using the wiringPi library. The logic is very basic but you get the idea. We can then use cron to run this script at regular intervals (see below).

    //set all pins to off

    for ($x = 0; $x <= 6; $x++) {

    System("gpio mode $x out");

    System("gpio write $x 0");

    }

    //scroll through pins

    for($x = 0; $x <=6; $x++) {

    System("gpio write $x 1");

    sleep(1);

    System("gpio write $x 0");

    }

    //get the current weather information

    $jsonurl = "http://api.openweathermap.org/data/2.5/weatherq=Toronto&units=metric&APPID=";

    $json = file_get_contents($jsonurl);

    //Takes on a JSON encoded string and turns it into a PHP variable.

    $weather = json_decode($json);

    $station = $weather->name;

    $wind = $weather->wind->speed;

    $tempC = $weather->main->temp;

    $tempmin = $weather->main->temp_min;

    $tempmax = $weather->main->temp_max;

    $pressureC = $weather->main->pressure;

    $descriptionC = $weather->weather[0]->description;

    //Check to see if description is rain and light pin 4

    if (strpos($descriptionC,'rain')!== false) { System("gpio write 4 1"); }

    //Check to see if description is cloud and light pin 0

    if (strpos($descriptionC,'clouds')!== false) { System("gpio write 0 1"); }

    if ($tempC >= 10 && $tempC < 20) { System("gpio write 2 1"); }

    //Check to see if temperature is > 20 degC light pin 3

    if ($tempC > 20) { System("gpio write 3 1"); }

    //Check to see if temperature is between 0 and 10 and light pin 1

    if ($tempC >= 0 && $tempC < 10) { System("gpio write 1 1"); }

    //Check to see if description is like snow and light pin 6

    if (strpos($descriptionC,'snow') !== false) { System("gpio write 6 1"); } elseif ($tempC < 0) { System("gpio write 6 1"); } else { }

    //if the description is clear sky then light pin 5

    if ($descriptionC == 'clear sky') { System("gpio write 5 1"); }

    ?>

    Step 9: CRON

    As the weather is constantly changing we need to be able to execute the php script periodically to refresh the weather information. For this we can use cron. My php script was in a file called scroll.php.

    //Check the following command works on the command line to make sure your script is working

    /usr/bin/php /var/www/html/scroll.php

    //Make the php file executable

    chomd +x /var/www/html/scroll.php

    //update crontab

    crontab -e

    //run the script every minute to test it is working

    * * * * * /usr/bin/php /var/www/html/scroll.php

    //this can later be change to every 5 minutes or so or to whatever interval you need

    5 * * * * /usr/bin/php /var/www/html/scroll.php

    Step 10: Improvements

    In terms of the design the wires and LED strips are somewhat visible in a few of the Polaroid frames so this design could be improved. The logic behind which Polaroids are displayed based on the weather information could also be improved. In addition to this there’s a whole load of other information which can be gathered from the API and used here. Information could be gathered about how the weather is going to develop over the day and if for example rain is expected at any point in the day you could have an RGB LED light up red. We could also display the current temperature using a servo and a temperature dial and a shed load of other things.

    LED Contest

    Participated in the
    LED Contest