Automatic ambient light controller 
Tags: raspberrypi, python, opensource, showdev
Published: 2022-02-22 06:18:50

_[Originally appeared on Medium ](https://s1ndhu.medium.com/lets-light-it-up-720685e28ac3)_

![My workstation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxjqb8307q0vy1fyjkr7.jpg)

Not so surprisingly this problem was staring at my face all along. Sitting right next to french windows has its pros and cons. Although the balcony view is great, the fluctuating light coming from outside the window really annoys me. I end up straining my eyes looking a bright screen in a dim lit environment for hours. I forget to turn on the lamp near my desk when it’s too dark, sigh.. if only Google Home could read my mind!

This seemed like an easy enough problem I could fix using my Raspberry Pi 4.

Introducing ⭐️ **SIRIUS** ⭐️ (named after the brightest star) — the ambient light controller and picture-taker 📸.

The purpose of Sirius is to adjust ambient lighting. Sirius controls lighting by periodically taking a photo and calculating levels of brightness in the image. Then the lamp near my desk is turned on if the ambient brightness drops. The lamp is also turned off when the brightness hits the max threshold. Why is this important to me, you ask… because I prefer working in a well lit (mostly bright) environment.

I started off using our existing lamp which was connected to the outlet via a smart plug. But then I realized that I wanted to also try the same approach with a dimmable lamp. 

I will go over the implementation for both a regular lamp and dimmable lamp to control ambient lighting. My favorite was the dimmable lamp, but the regular lamp is much simpler to adjust.

**Components used:**
- Raspberry Pi 4
- [Arducam 5MP mini camera](https://www.amazon.com/Arducam-Megapixels-Sensor-OV5647-Raspberry/dp/B012V1HEP4?ref_=ast_sto_dp)
- [TP-Link Kasa smart plug](https://www.bestbuy.com/site/tp-link-kasa-smart-wi-fi-plug-mini-with-homekit-white/6472520.p?skuId=6472520)
- [Mi LED desk lamp](https://www.amazon.com/Flicker-free-Eye-caring-Dimmable-Version-Warranty/dp/B0772KLVS4)


![Arducam 5MP mini camera with instructions
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r19khma0ppw1vxws6m1q.jpg)

**🔆 STEP 1: Connecting Camera to RPi 🔆**

- The camera port in the RPi is located between HDMI and audio port. The black tab on the port should be pulled up before inserting the ribbon.

- Black tab needs to be pulled up before inserting the ribbon.
- The camera ribbon that comes with the camera should be inserted into the camera port. Take extra caution here because the ribbon and the port are delicate and can be easily damaged.
⚠️ NOTE: while pushing the camera ribbon into the port, make sure that the silver connectors are facing the HDMI port and the blue side should be facing outwards.
Now push the tab to securely connect the camera to RPi

![Camera connected to RPi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aujaj1xr1h9389veald3.jpg)

- Enable the camera by navigating to Main Menu → Preferences → Raspberry Pi Configuration → Interfaces → Camera → Select Enabled
- Update OS if necessary — sudo apt update and sudo apt full-upgrade
- Let the fun begin! 🎬

I started by taking pictures using raspistill. 💃🏻

`raspistill -vf -hf -o test.jpg` — this command gave me useable pictures. 🎊

I did not care much about the quality of the image, because my focus (pun intended 😎) was only on measuring the brightness. `raspistill` and `libcamera` have awesome features to enhance picture quality. Refer to their [documentation](https://www.raspberrypi.com/documentation/accessories/camera.html).

**🔆 STEP 2: Measuring ambient brightness 🔆**
I tried two approaches and ended up using the latter, as it gave me higher precision which I needed for adjusting brightness of the lamp, more on that later.

**Option 1:** Using [Python Image Library’s (PIL)](https://pillow.readthedocs.io/en/stable/index.html) ImageStat modules for extracting statistics of an image.

```
def brightness(image):
   img = Image.open(image).convert('L')
   stat = ImageStat.Stat(img)
   print(stat.mean)
   return stat.mean[0]
```

`convert(‘L’)` — returns 8-bit pixels, black and white copy of the original image. The mean stat of the B&W image is then used to get the average pixel brightness and therefore a numerical value representing the brightness in a picture.

**Option 2:** Using LAB color space to extract the luminous channel in an image. I used [OpenCV](https://pypi.org/project/opencv-python/) (Open Source Computer Vision Library), a popular library used for image processing.

`pip3 install opencv-python`

In the following snippet of code, `COLOR_BGR2LAB` — converts image to RGB image to LAB color scheme.
After conversion, the image is represented in the following three dimensional format,

L* — perceptual lightness (0 = black and 100 = white)
a* — represents value between red and green (negative = green and positive = red)
b* — represents value between blue and yellow (negative = blue and positive = yellow)

```
def LABColorSpace (image):
    img = cv2.imread(image)
    # Convert to LAB format
    L, A, B = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2LAB))
    # Normalize the result
    L = L/np.max(L)
    return np.mean(L)
```

**🔆 STEP 3: Controlling a regular lamp using a smart plug 🔆**

I installed [python-kasa](https://python-kasa.readthedocs.io/en/latest/#python-kasa), a Python library to control any device connected to the outlet using the TP-Link SmartPlug.

`$ pip3 install python-kasa`

`kasa` command Lists devices connected using TP-Link SmartPlug and their IP address.

Once we have the IP address of the lamp, it’s straightforward to control the device. After a lot of fine tuning, I arrived at this range (0.37–0.55) by taking into consideration my personal lighting preference, the location of my work desk and lamp’s brightness.

```
regularLamp = SmartPlug('<ipAddr>')
print('outsideBrightness: ', outsideBrightness)
if (outsideBrightness < 0.37) :
    await regularLamp.turn_on()
elif (outsideBrightness > 0.55):
    await regularLamp.turn_off()
await regularLamp.update()
print('light.is_on: ', regularLamp.is_on)
```

Once the lamp is turned on, it is not turned off until the brightness index increased to over 0.55 and not turned on until it drops to 0.37.

[And it works!!!](https://www.youtube.com/watch?v=MoEhNJus0aU&t=2s) 🤩

**🔆 STEP 4: Controlling a dimmable lamp 🔆**

First, I installed the necessary library to access and operate the Mi LED lamp — [python-miio](https://python-miio.readthedocs.io/en/latest/index.html)

`pip3 install python-miio`

The device IP address and token are needed to communicate with it. The lamp can be discovered by running the following command —

`miiocli discover`

If you are having any trouble obtaining the token, other ways to get the token are listed [here](https://python-miio.readthedocs.io/en/latest/discovery.html#device-discovery).

With the IP address and token we can control the device. Initially the lamp is turned on with brightness set to 20. Then the brightness is adjusted based on ambient light.

```
dimmableLamp = Yeelight('<ipAddr>', '<token>')
    if not dimmableLamp.status().is_on and outsideBrightness < 0.53:
        dimmableLamp.set_brightness(20)
        dimmableLamp.toggle()
    print('outsideBrightness: ', outsideBrightness)
    if (outsideBrightness > 0.37 and outsideBrightness < 0.5):
        dimmableLamp.set_brightness(50)
    elif (outsideBrightness > 0.5 and outsideBrightness < 0.53):
        dimmableLamp.set_brightness(30)
    elif (outsideBrightness > 0.53):
        dimmableLamp.toggle()
    print('lamp.brightness: ', dimmableLamp.status().brightness)
```

[Lamp in action!](https://www.youtube.com/watch?v=aMYFJkPsiKk) 🎉

These videos have been sped up and I altered lighting in the room by opening/closing the blinds to showcase the changing lamp states. In reality I don’t often adjust the blinds, so the transition between lamp states would be less frequent.

You can also replace the camera with a Photoresistor because it indicates the presence/absence of light. There are several implementations that use these light-dependent resistors (LDR) as brightness sensors. So all you would need is a breadboard, a photoresistor and a smart home device to try a simpler implementation.

The coding implementation can be found on my GitHub — https://github.com/sindhunaydu/Sirius.

Cheers to the open source community and their amazing contributions which have made this project possible. 🍻 🙌🏽
This was a fun one to work on, Happy New Year! Have a well lit year ahead! 🥳 🔥