Skip to content

Simple tool to update the color of a LIFX bulb based on the carbon intensity of your region using data from the electricityMap, built with Python, Javascript and Flask.

Notifications You must be signed in to change notification settings

ericmassip/enlighten-me

Repository files navigation

EnlightenMe

(Disclaimer: This tweet from Olivier Baumann from Tomorrow inspired me to start this project)

A simple tool that turns a smart light bulb greener when the sun shines or the wind blows (amongst others) in your region, and redder when they don't. Information about the carbon intensity of your region is given by the electricityMap. This tool informs us about the right moment to charge or use our electrified devices via a smart light bulb.

You can see a demo of it in action below. On the left side of the screen, there is the bulb, in the middle the EnlightenMe app and on the right, the electricityMap. (Click on the image to play it on Youtube)

Watch the video

Architecture

Web App

For now, the web app can only be used with a LIFX light bulb. The web app architecture has basically two elements:

  • A web server built in Python with Flask containerized in a Docker image. This server is in charge of making API calls to both the LIFX API and the CO2 Signal API. The LIFX API allows us to easily update the status of our lightbulbs with HTTP requests. The CO2 Signal API lets us extract information about the carbon intensity (in gCO2eq/kWh) of certain regions in real-time from the electricityMap.

  • A super lightweight web page built with plain HTML, CSS and Javascript. This web page makes requests iteratively every 5 minutes to the web server to extract the data from the electricityMap, process it and make another request to update the bulb color. This interaction enables us to follow the evolution in real-time of the carbon intensity of the region we're interested in. It also permits to choose amongst all the zones existing in the electricityMap. Furthermore, it lets us choose which bulb/s to update with the carbon intensity information.

EnlightenMe time evolution

Standalone App

For now, the standalone application can be used with a LIFX or a Xiaomi Yeelight light bulb.

The standalone application is a lightweight version of EnlightenMe written all in Python to run from the command line. This version does not need a browser to run, so basically its purpose is to allow for a quick deployment on a local computer or cloud instance on the background. The behavior is exactly the same as the web app, the only difference is that you must specify the zone or country code via the command line.

EnlightenMe standalone

Run it yourself!

Clone the repository:

git clone https://github.com/ericmassip/enlighten-me.git

The bulb

First of all, you'll need a smart light bulb. For now, you have two options:

  • Your first option is a LIFX light bulb, they are a bit pricey, but their API is really good and they work very well. You can order them online here.

  • Your second option is a Xiaomi Yeelight light bulb, they are half the price of a LIFX. You can buy them from Amazon. Although, with this type of bulb you won't be able to use the web app yet, just the standalone version.

If you have a different type of smart bulb, don't worry, you can find guidelines of how to modify the current code to accept your bulb's specifications below.

Get the API keys

  • You can generate a LIFX API token to connect to your bulbs here. You'll need your LIFX account settings to ask for a token.

  • You can generate a free (non-commercial use only) API key to get real-time data from the electricityMap here.

Edit the env.py file and place your API keys in there.

BULB_API_TOKEN = '<YOUR-BULB-TOKEN-HERE>'
CO2_SIGNAL_TOKEN = '<YOUR-CO2-SIGNAL-API-KEY-HERE>'
  • In case you're using a Xiaomi Yeelight, you won't need an API key to make HTTP requests. You'll just need to find the IP of your bulb while connected to your local Wi-Fi network. There is an awesome python package that handles this bulb IP discovery, plus all other interactions with the bulb, called Yeelight Python. You can read the docs here, it is very simple to find the IP. Once you have it, place it in the env.py file.
YEELIGHT_BULB_IP = '<YOUR-YEELIGHT-BULB-IP-HERE>'

Run the web app

  1. If you have a LIFX light bulb, go to step 2. If you don't, modify the url and headers of the HTTP request called in the method set_bulb_state inside the app.py file according to the specifications of your bulb API.

  2. Build the Docker image with docker build -t enlighten-me:latest .

  3. Run the Flask app locally with docker run --rm -p 5000:5000 enlighten-me python app.py

  4. Open the index.html file on your browser.

Run the standalone app

  1. If you have a LIFX or Xiaomi Yeelight light bulb, go to step 4 directly. If you don't, follow these steps:

  2. Create a new subclass of Bulb. See LIFXBulb.py for an example.

  3. Implement the methods update_bulb_state and _get_bulb_data to match your bulb specifications.

  4. Edit the method update_bulb in the standalone.py file to call the LIFX, Xiaomi Yeelight or the subclass you just created. For the first two bulb options, you just have to uncomment the one you're interested in.

  5. Build the Docker image with docker build -t enlighten-me:latest .

  6. Specify the zone code and run the standalone app on your preferred instance:

docker run --rm -it enlighten-me python standalone.py <zone-code>

...or on detached mode:

docker run -d enlighten-me python standalone.py <zone-code>

Contribute

If you play around with the code creating a new Bulb subclass for your specific bulb type or you add some improvements to the current code, please feel free to submit a PR, it would be greatly appreciated. Thank you!

About

Simple tool to update the color of a LIFX bulb based on the carbon intensity of your region using data from the electricityMap, built with Python, Javascript and Flask.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published