{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rickymoorhouse.uk/blog/2013/2013-01-11-measure-sunlight-with-raspberry-pi",
  "path": "/blog/2013/2013-01-11-measure-sunlight-with-raspberry-pi",
  "publishedAt": "2013-01-11T20:54:54.000Z",
  "site": "at://did:plc:r53zv4vpzeihop3aliwyejlu/site.standard.publication/3mos5q3a7jf2w",
  "textContent": "For Christmas the girls gave me a Raspberry Pi, and for my first project I decided to try recording how bright the sunlight at our window is using an old Webcam.\n\n[](/images/raspberrypi.jpg)\n\nThe basic idea is:\n\t\n  1.  Capture a photo\n\n\t\n  2. Analyse the brightness of the photo\n\n\t\n  3. Log it (and eventually publish to cosm)\n\nSo first of all getting the webcam set up - My webcam is a Logitech Quickcam Express, which proved to work nicely with the Raspberry Pi, after plugging it in, it showed up straight away in the output to lsusb:\n\nricky@pi ~ $ lsusb\nBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub\nBus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.\nBus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.\nBus 001 Device 004: ID 046d:0840 Logitech, Inc. QuickCam Express\n\nTo get the photo from the webcam I used fswebcam which was simple to install (sudo apt-get install fswebcam) and use:\n\nfswebcam --no-banner -d /dev/video0 webcam.jpg\n\nThe no-banner removes the default date and time at the bottom of the image, /dev/video0 is where the webcam appeared and webcam.jpg is the file to save the image to.\n\nI found a python function to calculate the brightness of the image from StackExchange so put it all together and here is the python script I'm using:\n\n    \n    <code class=\"language-python\">\n    #!/usr/bin/python\n    import Image\n    import ImageStat\n    import math\n    import os\n    import datetime\n    os.system(\"fswebcam --no-banner --scale 50x50 -d /dev/video0 webcam.jpg\")\n    im = Image.open(\"webcam.jpg\")\n    stat = ImageStat.Stat(im)\n    r,g,b = stat.mean\n    brightness = math.sqrt(0.241(r2) + 0.691(g*2) + 0.068(b**2))\n    dt = datetime.datetime.now().strftime(\"%Y%m%d-%H:%M:%S\")\n    data = '%s,%sn' % (dt, brightness)\n    open(\"brightness.csv\", 'a').write(data)\n    </code>\n\nIt could be tidied up quite a bit and I'm sure there's a way to capture the image within Python without having to write it to disk first as well. My first days readings taken every 10 minutes look something like this:\n//",
  "title": "Measure sunlight with Raspberry Pi"
}