No announcement yet.

Light sensor device for a spare RPI

  • Filter
  • Time
  • Show
Clear All
new posts

    Light sensor device for a spare RPI

    There have been a few threads recently about light sensors, particularly ones which can cover the full range of dark to daylight as most light sensors built in to motion sensors saturate very quickly and can't distinguish bright sun from cloud (and typically aren't mounted where you would want to measure the sun anyway).

    A couple of years ago I built a simple daylight monitor based on a BH1750 sensor and an old RPI. The hardware part followed the instructions here:

    Using the BH1750FVI I2C Digital Light Sensor - Raspberry Pi Spy (

    My light sensor is installed in a sealed plastic dome (half of one of the balls from a toy vending machine) and mounted on my roof, with the PI in the loft underneath connected to my network by WiFi. It gives values from zero to about 55000, with good distinction of levels (dusk is about 100, proper daylight about 1000, bright but cloudy up to 30000 and sunny above that).

    On the PI you need to install Python 3 and PIP:

    sudo apt update
    sudo apt install python3​
    sudo apt install python3-pip
    Then install the smbus package:

    pip3 install smbus
    My first software exposed a simple web server ( ) on port 8000 with two endpoints - /luminance which is just the current luminance as a number, and /follow which uses a small javascript to show updating values.

    (Just put this onto the PI and run it from the command line, then access it through a browser at http://(IP)/luminance or http://(IP)/follow - it will report HTTP requests).

    An event/script on my HomeSeer polled the /luminance endpoint every minute to get the value and store it in a device.

    My improved approach using MQTT and the mcsMQTT plugin, so luminance changes are pushed instead of having to poll. This script does that ( ) and also requires the Paho MQTT library (pip3 install paho-mqtt). It sends updates based on either significant change or every thirty seconds; you'll probably need to change the MQTT target (I have Avahi running on my Homeseer RPI with the name homeseer.local, you may not but can just use the IP address) and the topic (mine is rpi3/luminance).

    Once you have this running you should schedule it as a systemd service - here's an appropriate definition: luminance-server-mqtt.service.txt

    The MQTT version is very reliable, it happily reconnects after power, wifi and homeseer outages.

    Note: files have extension .txt added because the forum doesn't allow .py or .service extentions



      Thanks, I didn't see Petes most recent post! Would be interested to see it to compare, also as I'm thinking of moving the same sensor from the RPI to a Wemos D1 Mini Lite which is smaller and uses less power.