Home  5  Books  5  GBEzine  5  News  5  HelpDesk  5  Register  5  GreenBuilding.co.uk
Not signed in (Sign In)

Categories



Green Building Bible, Fourth Edition
Green Building Bible, fourth edition (both books)
These two books are the perfect starting place to help you get to grips with one of the most vitally important aspects of our society - our homes and living environment.

PLEASE NOTE: A download link for Volume 1 will be sent to you by email and Volume 2 will be sent to you by post as a book.

Buy individually or both books together. Delivery is free!


powered by Surfing Waves




Vanilla 1.0.3 is a product of Lussumo. More Information: Documentation, Community Support.

Welcome to new Forum Visitors
Join the forum now and benefit from discussions with thousands of other green building fans and discounts on Green Building Press publications: Apply now.




    • CommentAuthorEd Davies
    • CommentTimeJan 24th 2013 edited
     
    OK, just the easy one this late in the evening: timestamps.

    You need the datetime module. At the top of the file add “import datetime”. There are three classes in that module, date (just a date, with no time), time (time of day, 00:00 to 23:59) and datetime (date and time combined for a unique instant in history). For a log which will run many days you want datetime. The datetime class has a now() function which gives you the equivalent of the VBA now but for a log which might run for many months I'd suggest using its sibling utcnow so you don't get funny things happening when the clocks change.

    I haven't got a Pi or anything with GPIO so just the interesting bits:

    #!/usr/bin/python

    import datetime

    # ....
    temperature = 42.5 # Dummy

    tfile = open("/home/pi/Desktop/tempdata.txt","w")
    #writes the temperature data to the tempdata/txt file
    tfile.write("%s: Temp C,%s" % (datetime.datetime.utcnow(), temperature) +'\n')
    #closes the tempdata.txt file
    tfile.close()

    Apart from using my own user name for the directory (rather than 'pi') that runs as shown for me and produces:

    2013-01-24 23:18:35.028279: Temp C,42.5

    It's confusing having the datetime module containing a class with the same name. If that's the only class from the module you want to use then you can make it a bit more compact at the point of use. Replace the import statement with:

    from datetime import datetime

    then just use the class name directly:

    tfile.write("%s: Temp C,%s" % (datetime.utcnow(), temperature) +'\n')
    • CommentAuthorEd Davies
    • CommentTimeJan 24th 2013 edited
     
    Posted By: SteamyTeaAnd the final thing for now is how do I force a new line in my tempdata.txt file so that I do not overwrite the data I have in there.

    The problem is not the need for a new line - when you open the file (with the open function) it, by default, replaces the file contents (i.e., it truncates the file first). You want append mode ("a"), rather than write ("w"):

    tfile = open("/home/pi/Desktop/tempdata.txt","a")

    giving:

    2013-01-24 23:28:41.030513: Temp C,42.5
    2013-01-24 23:37:23.421996: Temp C,42.5
    2013-01-24 23:37:35.034155: Temp C,42.5

    While we're on the subject of the mode, when you're writing a text file it's a good idea to specify text mode there as well. On Unix-like systems (Linux, OS X, etc) it doesn't make any difference. On Windows it'll cause the '\n' newlines you write to be written as two characters, carriage return, line feed, as is the standard Windows line end convention.

    tfile = open("/home/pi/Desktop/tempdata.txt","at")
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 25th 2013
     
    Thanks Ed

    Going to be back on it in an hour or so, once I have that working I can then try a loop so that it logs every few seconds. That should be fun.
    • CommentAuthorEd Davies
    • CommentTimeJan 25th 2013
     
    Raspberry Pi thermal imaging. No, not making thermal images with a Pi but thermal images of a Pi:

    http://www.adafruit.com/blog/2013/01/25/raspberry-pi-thermal-images-piday-raspberrypi-raspberry_pi/

    Interesting idea for seeing where electronic boxes and stuff are using energy. Obvious, really, but thermal imaging plumbing, etc, could be helpful, too.
    • CommentAuthorEd Davies
    • CommentTimeJan 25th 2013 edited
     
    Posted By: SteamyTeaIs it possible to put the modprode w1-gpio and modprobe w1-therm commands into the Python script at the very begining?

    Yes, use the subprocess.call function to run an external program. Easier, use subprocess.check_call to run the external program and check its return code is zero (i.e., it worked). In simple cases it just takes an array of strings as the parameter, one string for each “token“ in the command line.
    from subprocess import check_call

    check_call(["modprobe", "w1-gpio"])

    This throws an exception for me because I haven't got that module installed (not having GPIO on my laptop). When I use it with a module I have got it works fine.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 25th 2013 edited
     
    I put my finger on the black squares to see which is the hottest :bigsmile:

    Just spent way to long playing with it, but got the code sort after your help Ed, thanks.

    Also managed to get an LED flashing every 6 seconds as a test of looping. Found out that indenting is not always indenting (4 spaces is better than a tab apparently).

    Next thing to do is combine the temperature reading with a while loop.

    I was so excited that I got a (or is it an) LED working that I videoed it and posted it up on Youtube.
    I also had cheese and crackers for 11sies :cool:

    http://youtu.be/ARvWrpwbuj0

    Must now get on with what I was meant to be doing 2 hours ago (my lit review) but will leave my light flashing.

    Shall look at the subprocess call tonight
    • CommentAuthorEd Davies
    • CommentTimeJan 25th 2013
     
    Posted By: SteamyTeaThe other thing is that when it writes to the tempdata.txt file a terminal window telling me that it all went alright opens up which has to be closed before I can read the file (or reload it). What is that about?

    Sorry, no idea. How are you starting your program?
    • CommentAuthorEd Davies
    • CommentTimeJan 25th 2013
     
    Posted By: SteamyTeaFound out that indenting is not always indenting (4 spaces is better than a tab apparently).

    Either is good but it's best to be consistent - always use tabs or always use spaces. Personally I have my editor set up to expand tabs to spaces as I type them.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 25th 2013 edited
     
    I am starting it from Geany, if I want to start if from the .py file, do I have to compile it first (whatever that does) and do I have to make it executable? Does either of them stop me editing it after?

    Edit:

    Just made the led file 'executable' and the terminal screen does not pop up now but the light keeps flashing :bigsmile:
    •  
      CommentAuthordjh
    • CommentTimeJan 25th 2013
     
    Posted By: Ed Davies
    Posted By: SteamyTeaIs it possible to put the modprode w1-gpio and modprobe w1-therm commands into the Python script at the very begining?

    Yes, use the subprocess.call function to run an external program.

    Err, I don't think this is recommended! For most external programs, yes, but not for the likes of modprobe.

    modprobe can usually only be run by root, and generally the logging script shouldn't be, so it should fail in any case. But modprobe is changing the system configuration and isn't something an application should be doing.

    Steamy, read up about how to use the modprobe.d files to load these kernel modules when your pi starts up.

    http://linux.die.net/lkmpg/x44.html
    http://linux.die.net/man/5/modprobe.conf
    http://linux.die.net/man/8/modprobe
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 25th 2013
     
    I had a little fiddle with it and could not get it to start, so that may be the problem.

    There is a Python for the RPi called RPi.GPIO, I used to it make the led flash. Should I try loading that and setting pin 4 to read instead?
    • CommentAuthorEd Davies
    • CommentTimeJan 25th 2013
     
    Posted By: SteamyTeaI am starting it from Geany, if I want to start if from the .py file, do I have to compile it first (whatever that does)

    No, Python will create the .pyc by itself if it feels like it. You can just run the .py file.

    and do I have to make it executable?

    Yes, if you want to run your program directly as a command. $ chmod a+x myloggingscript.py

    Alternatively, you can just run it with $ python myloggingscript.py.

    Does either of them stop me editing it after?

    No, you can still edit it as long as you have rw access.

    PS, djh is right about it being better to keep the modprobe stuff out of the script. Still, if you need root privileges to access the GPIO pins anyway it's not such a big deal. Do you?
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 25th 2013 edited
     
    As far as I know I do need to be logged in as root.
    At this point in time being root or not does not worry me as I am only playing, maybe later when I want to do some more serious stuff it may become important.

    When I tried putting it in it just said there was an error at line 7. After a bit of playing about it was loading but then getting errors at various lines of the modprobe module. Maybe that is why they developed the RPi.GPIO. Shall have to look it up when I get back later.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 27th 2013
     
    Right
    Found out how to load the w1 (one wire drivers) at start up, just a case of editing the modules file, does not make tightvncserver start, maybe that has a different name.
    Also found out that the RPi.GPIO module does not support 1wire yet but it may later.
    With this subprocess thingy I should be able to look at the w1/devices directory and get the unique ID for each sensor and somehow put that into my scrip as a variable so save having to manually edit the scrip each time I change a sensor. Is that and easy thing to do?
    I also get a fake reading (-0.062) instead of the temperature reading, is this something to do with the CRC from the sensor?
    • CommentAuthorEd Davies
    • CommentTimeJan 27th 2013 edited
     
    Posted By: SteamyTeaWith this subprocess thingy I should be able to look at the w1/devices directory and get the unique ID for each sensor and somehow put that into my scrip as a variable so save having to manually edit the scrip each time I change a sensor. Is that and easy thing to do?

    You could use a call to one of the functions in the subprocess module to run ls and look at its output as per your previous method. That'd be a rather perverse way to do it, though.

    There are various ways of wandering around the file system in Python. Here are examples of two. I don't have a w1 directory in /sys/bus/ so these work higher up the tree from what you'd want.
    >>> import os
    >>> os.listdir('/sys/bus')
    ['platform', 'pci', 'rapidio', 'spi', 'i2c', 'eisa', 'MCA', 'acpi', 'pnp', 'scsi', 'mdio_bus', 'usb', 'serio', 'mmc', 'sdio', 'event_source', 'pci_express', 'isa', 'ssb', 'hid', 'pcmcia']
    >>> import glob
    >>> glob.glob('/sys/bus/*')
    ['/sys/bus/platform', '/sys/bus/pci', '/sys/bus/rapidio', '/sys/bus/spi', '/sys/bus/i2c', '/sys/bus/eisa', '/sys/bus/MCA', '/sys/bus/acpi', '/sys/bus/pnp', '/sys/bus/scsi', '/sys/bus/mdio_bus', '/sys/bus/usb', '/sys/bus/serio', '/sys/bus/mmc', '/sys/bus/sdio', '/sys/bus/event_source', '/sys/bus/pci_express', '/sys/bus/isa', '/sys/bus/ssb', '/sys/bus/hid', '/sys/bus/pcmcia']

    I think with glob you could even do glob.glob("/sys/bus/w1/devices/28-*/w1_slave") to directly get the devices you need to access all of your temperature sensors, ignoring any other 1-wire devices on the bus(es).
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 27th 2013
     
    Ed
    You ever thought of teaching this, or write a book on it.

    I shall have a look at what you have done above and see if I can get it to do some things. I suppose when I have listed the temperature sensors with os.dirlist I can use the line and text split to isolate the directory names and then assign a variable to those directories to be read.

    I have managed to get it to log from 2 sensors now, which is half way to making a temperature comparator, just have to master the IF statement in Python and get it to turn my LED on and off, then I may put a couple more sensors on and stick it up on the home made MVHR in the loft.

    Trip to Maplins during the week I think to see what connectors and stuff they have in there.
    • CommentAuthorEd Davies
    • CommentTimeJan 27th 2013
     
    :-)

    But the books have already been written. The one I have to hand all the time is the O'Reilly book <i>Python in a Nutshell</i> by Alex Martelli. I'd thoroughly recommend it to anybody who wants to learn Python if they already know a few other languages.

    For introductory programming books using Python, I know there are a few around but haven't looked at any so can't say anything useful. However, <i>Python for Kids</i> looks interesting. My 13-year old niece was talking about how some other girls in her class had been learning to program on Raspberry Pis and lamenting that access to computers you can do that sort of thing on is not like it used to be in schools. I just laughed at her. She has a fairly powerful desktop with a gorgeous widescreen monitor, an iPhone and an iPad all of which would be just fine for getting some programming experience. Might get her that book next time I go to visit. Might be a good book for adults, too.

    http://boingboing.net/2012/12/18/python-for-kids-a-playful-int.html
    • CommentAuthorborpin
    • CommentTimeJan 27th 2013
     
    I wish I had the time to fiddle with my Pi......
  1.  
    Splendid piece on Woman's Hour last week, on R4, with a splendidly clued-up girl programming a Pi and some similar basic computer like there was no tomorrow. I think she could teach!
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 27th 2013
     
    Posted By: borpinI wish I had the time to fiddle with my Pi......
    I would say fiddle with mine but would only get you into trouble.

    Posted By: Nick ParsonsSplendid piece on Woman's Hour last week
    Watch my video if your interested in my comments about Woman' Hour:
    http://youtu.be/ARvWrpwbuj0
    • CommentAuthorTriassic
    • CommentTimeJan 31st 2013
     
    If you're struggling with you pie you'll soon be able to ask the kids for help -

    15,000 Raspberry Pis for UK schools – thanks Google!

    Today’s been a bit unlike most Tuesdays at the Raspberry Pi Foundation. Today we’ re the recipients of a very generous grant from Google Giving, which will provide 15,000 Ra spberry Pi Model Bs for schoolkids around the UK.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 31st 2013
     
    Nothing quite like getting the kids to do the work. :wink:
    • CommentAuthorwookey
    • CommentTimeJan 31st 2013
     
    Yeah the 13-year geek-girl was great. More like that please.

    As you've discovered steamy, the correct way to get modules loaded automatically is to put them in /etc/modules/

    (and trying to load them in your python program, whilst possible, is definitely not right).

    If you want to read 1-wire devices easily the simplest way is to install owfs. That will expose your devices over http (owserver) or as a virtual filesystem (owfs). There are also python bindings, so you can easily enumerate, read, configure. But you can of course do your own low-level access via the kernel interface, as you are doing, if you prefer.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJan 31st 2013
     
    What is a python binding, is it some sort of bridge that allows me to read the sensor? Does it need me to use W1-GPIO as as well?

    I intuitively understand writing or reading to a numbered 'pin' as that is almost mechanical, it is all those fiddle % " " , . () and [] I struggle with. Seems to me that they are used randomly, but I am sure they are not.
    • CommentAuthorEd Davies
    • CommentTimeJan 31st 2013
     
    If you've only got a few lines of code accessing the 1-wire bus then I don't see any compelling reason to use owfs. If you later expand it to something more complicated which you don't want to be dependent on the details of Pi GPIO (because you might want to port it to other systems) then owfs would give a useful level of abstraction. It'd probably also help if you wanted to have multiple processes accessing the 1-wire bus or wanted to get at the 1-wire bus across your network.

    Yes, a Python binding is a library (typically written in C) which allows Python code to call some other library. On Ubuntu the Python binding for owlib is in the package python-ow (i.e., sudo apt-get install python-ow, other packages which might be helpful are owserver and ow-shell). Use with import ow. Had a little play but must admit I couldn't figure out how to enumerate the devices on the bus - but maybe that's because I don't any real 1-wire devices to hand so owserver is simulating a fake bus for me (which is kind of it).

    Yes, you'd need to use w1-gpio as well. Owfs does its own access to all the normal serial port and USB 1-wire adapters (and having w1 modules loaded breaks things, IIRC) but for GPIO owfs delegates all the work to w1.

    As I say, I'd ignore owfs for now and concentrate on getting interesting stuff done. But it's worth keeping in mind for later.
    • CommentAuthorEd Davies
    • CommentTimeJan 31st 2013
     
    Should have said, though, that owfs does have the advantage that your code doesn't need to run with root privileges. Again, if you've just got a little script writing a log file on a machine which isn't Internet connected then that's not a big deal. If you're doing something in a web server or something then it needs thinking about.
    • CommentAuthorTriassic
    • CommentTimeFeb 6th 2013
     
    Raspberry Pi Announces $25 Camera Module

    Technical details are blurry. The exact sensor size is unknown, but five megapixels is the likely size according to official comments on RPi forums. Makers won’t be able to change lenses, add physical filters, or even zoom, but the camera module will be capable of HD video capture. Proposed applications for the new module are focused on robotics, home automation, and aerial applications where potential crashes favor low-cost solutions. Priced at $25, it will cost the same as the newly released Model A board — much cheaper and easier to replace than an iPhone or DSLR should an airborne project inadvertently “zoom in” to a crash landing.
    • CommentAuthorTriassic
    • CommentTimeJun 27th 2013
     
    I was wondering how the raspberry pie was going. Anyone got any good home automation projects, ideas or code to share?
    • CommentAuthorSeret
    • CommentTimeJun 27th 2013
     
    Not done anything interesting like that Triassic but I did just break down and buy one to replace my old HTPC. It's absolutely cracking, and cost less than the parts from my old machine will get on Ebay.
    •  
      CommentAuthorSteamyTea
    • CommentTimeJun 27th 2013 edited
     
    I got some code written that allows me to log the CurrentCost data, shall post the image up sometime and put a link to it.

    Right this is the image, use your favourite way to load it onto an SM card after unzipping it.

    It should work as soon as it is switched on as long as the CurrentCost is plugged in with the USB cable

    Normal Pi password

    To stop and start and clear the data:
    service serial stop
    rm /var/log/serial/data.csv
    rm /var/log/serial/hist.csv
    touch /var/log/serial/data.csv
    touch /var/log/serial/hist.csv
    service serial start

    https://www.dropbox.com/s/xhhpr71u87re24f/RPI%20with%20CurrentCost.zip

    And if anyone knows how it all works and how I can get it to work with my RTC instead of the CC device time I would be very grateful
   
The Ecobuilding Buzz
Site Map    |   Home    |   View Cart    |   Pressroom   |   Business   |   Links   
Logout    

© Green Building Press