Skip to content

Flask application on a SBC to communicate via smart light bulbs in reaction to API events.

License

Notifications You must be signed in to change notification settings

its-leofisher/going-light

Repository files navigation

Overview

❗ This software is in alpha so please be EXTRA cautious about exposing endpoints to public. At minimum use Ngrok. We will be providing better instructions soon.

Welcome! Going Light is meant to be used as a status indicator through light, receiving a message with a keyword from a service and triggering a Smart Bulb to notify you...one less message to read and dashboard/screen to context switch to.

The meanings of the lights can be changed to represent different events and statuses. Essentially, the project is used to facilitate communication through light. This means that the devices need to have the ability to change colors and be dimmable.

Brief History

This projects initial purpose was to indicate stages of code deployments by triggering different light bulb colors through a Kasa TP-Link SmartBulb. The deployment system sent status updates to a Slack channel and a custom slack app was set up to relay the request to an always on Raspberry Pi 2 W running a copy of "Going Light". The Application then maps the message/status to a pre-defined color and performs an API call to the registered lightbulb causing it to display the color for a certain duration.

Tech Stack

Created with Flask on a Raspberry Pi 2 W (Or any SBC with Wifi connectivity) to control a TP-Link Smart Bulb via Python Kasa library. Ngrok is used to make the pi available publicly to receive incoming API requests. The application is meant to be running 24/7 so the Pi 2 W was my choice due to the low power consumption when idle and under load.

Default status/emotions and light color mapping:

  • "In Progress" triggers a blinking Yellow Light for 25 seconds
  • "Fail" triggers a Red Light for 30 seconds
  • "success" triggers a Green Light for 35 seconds
  • "green" triggers a solid green light
  • "laughing" triggers a solid gold "FFD700"
  • "on" and "off" will toggle the light
  • View more here

Diagram

image

Prerequisites

  • Raspberry Pi Zero 2 W with Raspbian OS with SSH enabled and connected to local network. Other SBCs or Laptop will do.
  • Python 3 and pip installed
  • Python VirtualEnv
  • tmux to run app and ngrok in background
  • Already configured TP-Link Kasa smart bulb
    • Or a device that can change colors and is dimmable
  • A Slack workspace and app
    • Phase 2: abstract class for other Communication Apps
  • Ngrok
    • Sign up, install and set up a static domain to make it easier
    • It's important to secure your endpoint

Setup Steps

You will be able to select a device connected to your local network. The app will let inform you of any compatible and incompatible devices. Right now it only supports TP-Link Kasa Smart Bulbs. There are plans to support multiple brands.

Step 1: Environment Setup

1.1 Update and Upgrade Raspberry Pi packages

Install latest raspbian on SD card using Raspberry Imager.

sudo apt update
sudo apt upgrade

1.2 Install Tmux, Python and Pip on PI

sudo apt install python3 python3-pip
sudo apt-get install tmux

1.3 Clone Going Light Repo

  • git clone going-light repo: git clone [email protected]:its-leofisher/going-light.git
  • cd going-light

1.4 Create Virtual Env and activate

pip3 install virtualenv
source venv/bin/activate

1.5 Install Project Dependencies

  • pip install -r requirements.txt

Step 2: Run setup script

This script will create necessary files and perform device discovery. Ensure your device is set up and connected to your network prior to running.

python3 setup.py

Step 3: Run Flask Application

3.1 Run Flask Application

gunicorn -w 2 -b 127.0.0.1:8000 app:app

3.2 Start Ngrok

Open a new terminal and start Ngrok to expose your Flask application:

ngrok http 5000

3.3 Test your integration [optional]

Slack

  • Follow steps in here

Example Curl Command

curl -X POST https://your_ngrok_url_here.app/v1/events \
-H "Content-Type: application/json" \
-d '{
  "event": {
    "type": "message",
    "text": "success",
    "channel": "CHANNEL_IDS"
  },
  "type": "event_callback"
}'

Production Steps

When you're ready to leave the application running in the background permanently we will use GUNICORN to serve the app. SSH into the Pi and run these TMUX commands:

TMUX: Start App and NGROK

SSH into Pi. Start App with tmux command below to run app through gunicorn in background

tmux new-session -d -s appsession "source <path_to_project>/venv/bin/activate && cd <path_to_project> && gunicorn -w 2 -b 127.0.0.1:8000 app:app"

Start app server and tunnel via NGROK

Run Ngrok then Detach Automatically

tmux new-session -d -s ngroksession "ngrok http 8000 --domain=YOUR-DOMAIN.ngrok-free.app

It's Ready!

Any events going into v1/events endpoint will be verified and processed!

Color Map Reference

Message in payload: Color Mapping

{
    'fail': ('#FF0000', True, 30),
    'in progress': ('#FFFF00', True, 25),
    'in process': ('#FFFF00', True, 25),
    'processing': ('#FFFF00', True, 25),
    'success': ('#5C214A', False, 35),
    'off': ('', None, None),
    'on': ('', None, None),
    'orange': ('#FFA500', False, None),
    'royal purple': ('#5C214A', False, None),
    'matcha green': ('#94A796', False, None),
    'blue': ('#0000FF', False, None),
    'green': ('#008000', False, None),
    'yellow': ('#FFFF00', False, None),
    'red': ('#FF0000', False, None),
    'purple': ('#800080', False, None),
    'black': ('#000000', False, None),
    'dark green': ('#006400', False, None),
    'dark blue': ('#00008B', False, None),
    'happy': ('#FFFF00', False, None),
    'sad': ('#0000FF', False, None),
    'laughing': ('#FFD700', False, None),
    'fun': ('#FF69B4', False, None)
}

Planned Integrations

  • Client: OctoPrint Plugin
  • Client: Zoom Plugin
  • Client: Going Light Slack App
  • Source: Philips Hue and LED Strips
  • Source: Kasa Switches and LED Strips
  • Source: Homeassistant

Contribute

Open to code contributions, bug reports, and new ideas

To report a bug:

  • Make sure that it can be replicated at least 65% of the time
  • Open a new issue and add as much detail as possible

For New Ideas:

To contribute code:

  • First make a ticket with a description of contribution OR pick up a ticket already open
  • Make a fork of Main branch
  • Create a new branch and start committing
  • When you're ready, make a PR against dev branch
  • Tag @its-leofisher as reviewer and update the ticket for visibility

Remember not all contributions will be accepted, it depends on the scope of the project, there will be some back and forth as with all contributions.

Support Development

About

Flask application on a SBC to communicate via smart light bulbs in reaction to API events.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages