Thursday, April 4, 2013

Raspberry Pi Weather station (in 5 mins)

Here is an example of how small scripts can give you amazing makes. Here's my weather station using the LCD display I mentioned in my previous post: An LCD display for Raspberry Pi.



(Photos updated: April 7, 2013)

It's displaying weather forecast for Cambridge, UK. That's a miserable weather indeed :). As you can guess, the first line is the current condition followed by next two day's forecast which includes high and low temperatures (in Celsius). The last line is the date and time of update.

The awesome bit is you can make this with 20 lines of scripting and in less than 5 mins. Here is how it works:
  1. Grabs the weather data from Yahoo weather feed.
  2. Extracts key weather data from the xml feed, and
  3. Displays the data to the LCD.
The whole thing is done with this shell script (which I'm sure can be further reduced a lot):


Most of the script above is for converting the rss weather feed xml into those few lines to display. It writes the information in a file which is then read and displayed to LCD by a python script. Here's the python script to display the info in green color only:
#!/usr/bin/env python
from pylcdsysinfo import BackgroundColours, TextColours, TextAlignment, TextLines, LCDSysInfo
from time import sleep
d = LCDSysInfo(); d.clear_lines(TextLines.ALL, BackgroundColours.BLACK)
d.dim_when_idle(False);d.set_brightness(127);d.save_brightness(127, 255)
d.set_text_background_colour(BackgroundColours.BLACK)
f = open('yw', 'r'); col = TextColours.GREEN; c = 1;
for line in f:
        d.display_text_on_line(c, line, False, TextAlignment.LEFT, col)
 c = c + 1

That's 20 lines of scripting doing it all! Yet another example of how awesome shell and python scripts are.

You can run this every hour simply by adding a cron job. There is a bit of details involved before you can use this code. You need the python lib for the LCD and also a bit of info on how Yahoo weather feed works: http://developer.yahoo.com/weather/. To get the forecast for your location, you need the WOEID (Google: WOEID finder) and to pass it to the Yahoo weather feed url.

I understand I should provide detailed steps for beginners, perhaps as an instructable. If you want to see the detailed steps, do let me know in the comments :).

36 comments:

  1. Detailed steps would be apreciated. We have holiday cottages and I can see putting one of these in each cottage could be a nifty idea.
    Thanks

    ReplyDelete
  2. That's enough interest to post an instructable :). I hope to post the detailed steps next week. Thanks!

    ReplyDelete
  3. Although it has been used a weather station. I believe we can use it for email or rss feeds, can we not?

    ReplyDelete
    Replies
    1. We definitely can use it to display email or rss feeds.. even share prices :).

      Delete
    2. Nicely done!. I guess I will have to do some research on this. Any advice?

      Delete
    3. For email count, I guess there should be some simple linux script or tool to get count of unread emails using POP/IMAP.
      Rss/xml feeds can be stripped using similar scripts I've shown. Hope that helps.

      Delete
    4. Check out the adafruit.com tutorials, there is some code there that parses an email feed and lights an led when a new email arrives. That should help!

      Delete
    5. Thanks for the info Andy, will check it out.

      Delete
  4. I would love the details as well

    ReplyDelete
  5. Very well done - a really excellent mini project. Going to reblog it at http://www.recantha.co.uk/blog

    ReplyDelete
  6. Thanks for the great project, I would like to an instructable also
    Ross Bell

    ReplyDelete
  7. Thanks so much for the overwhelming interest! I'll write up the instructable and hope to share it this Sunday (April 21st)! :)

    ReplyDelete
    Replies
    1. I apologize, I couldn't make the time it needs to write the details. I hope to do so later this month.

      Delete
  8. I pretty much got the LCD to work as follows:
    Go here https://github.com/walac/pyusb and download files
    Then
    $sudo apt-get install python libusb-1.0
    $ sudo python setup.py install

    Then go here https://github.com/dangardner/pylcdsysinfo
    download files
    I also did this as is in Readme
    "You may need to copy the provided 99-lcdsysinfo.rules file into /etc/udev/rules.d/"

    To test i ran the demo.py and it works.

    I then copied the two scripts above and only get the date to display. So not sure what is missing

    ReplyDelete
    Replies
    1. Ok, I got the info to display but it only scrolls across the display and date is displayed at the end. Obviously I am not proficient in python to figure it out as far. Looking for to the info to make it work correctly.

      Delete
    2. I've uploaded the files here:
      https://code.google.com/p/kontroller/source/browse/#svn%2Ftrunk%2Fweather-station

      Please download and run 'weather.py' and 'weather.sh' files in RPi. You can use 'wget' command to download in RPi. Please put the files in the directory where demo.py is and use:
      sudo ./weather.sh

      The weather.sh will generate 'yw.htm', I've uploaded a sample file in the same location. That xml is stripped into 'yw', please check the content of the 'yw' file if it is not displayed correctly.

      Delete
    3. Very nice...It works great. nicely done and much better than what I came up with. Thank you.

      Delete
    4. Great! Happy to know you liked it :).

      Delete
  9. Could it be done to display the results on an android phone instead of an LCD? I would also like an instruct-able please. Thank you!

    ReplyDelete
    Replies
    1. I can imagine a number of ways.. if I got it right, simplest would be to use some browser/feed viewer to see the link in the phone.

      Delete
  10. Nice. I have just received one of these displays. I'm going to use pylcdsysinfo to display meeting room bookings. I've already worked out how to get the data out of google calendar now this will make a perfect display.

    ReplyDelete
    Replies
    1. Great! Will love to see some photos/links. :)

      Delete
  11. Yeah, I'd love to see an instructible. Once I have this working, I want to put it together with the raspberry pi opensprinkler project.

    ReplyDelete
    Replies
    1. That sounds interesting.. always wanted to use a rpi in the garden.. :)

      Delete
  12. I've also bought this screen and have been busy writing a driver for it so that it can be an automatic facebook picture frame :)

    ReplyDelete
    Replies
    1. Sounds cool! Do share a link to you work!

      Delete
  13. Hello:
    What happened to the instructable you were about publish? Are still considering doing one?
    Thank you Ross Bell.

    ReplyDelete
  14. You asked for it, here it is: http://hajos-kontrapunkte.blogspot.de/2013/05/raspberry-pi-as-rbn-client.html

    That's ham radio related. The R-Pi listens to a Telnet Server, which monitors ham radio stations all over the world. http://www.reversebeacon.net/main.php And the spots will be shown on the LCD.

    Thanks again for the inspiration.
    72 de Hajo

    ReplyDelete
  15. Just wondering if this is Linux only? Trying to start up saying d = LCDSysInfo() tells me

    import usb, time, struct
    ImportError: No module named usb

    and pyusb seems to have different naming. I don't lind using the Linux box, just wondering if using Windows is possible. (Win 7 SP1)

    ReplyDelete
  16. I'd be interested in your instructable. This would be a great first project for me and my Pi!

    ReplyDelete
  17. Where on earth is this instructable?

    And also, would this work directly from a HDMI output from the RPi itself?

    ReplyDelete
  18. Does anyone know how to get the large images to display in Python? I have pictures uploaded from a Windows machine whoch run as a picture frame loop but the Python scripts don't seem to be able to access them. There's a script called image-loop.py but it doesn't seem to work properly.

    ReplyDelete