Skip to content

FlamingoLindo/lights

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lights project

What this project does

  • Integrates Twitch chat with Tuya smart bulbs so viewers can change light colors and power state.
  • Uses the Tuya Cloud API for device control and OAuth token management.
demo.mp4

How it works (high level)

  • src/main.rs: bootstraps the application, creates the TwitchClient and TuyaClient, and enters the main receive loop.
mod bulb;
mod clients;
mod settings;
mod token;
mod twitch;

#[tokio::main]
async fn main() {
    let settings = default_settings();

    let mut twitch = TwitchClient::new(&settings);

    let tuya = TuyaClient::new(/* client id, secret, base url, ... */);
    // ... request token, check online devices, and loop receiving events
}
  • src/clients/twitch_client.rs: connects to Twitch EventSub, translates chat events into responses, and delegates bulb actions to the Tuya client.
pub struct TwitchClient {
    api: TwitchEventSubApi,
    last_color_change: Option<Instant>,
}

impl TwitchClient {
    pub fn new(settings: &Settings) -> Self {
        let keys = get_twitch_keys(settings);
        let builder = create_twitch_builder(keys);
        let api = build_twitch_connection(builder);
        twitch_tokens_to_settings();
        Self { api, last_color_change: None }
    }
}
  • src/bulb/color.rs: converts CSS color names to HSV and sends signed colour_data_v2 commands to Tuya devices (HMAC-SHA256 signing per request).
pub async fn bulb_color(
    base_url: &String,
    client: &reqwest::Client,
    client_id: &Option<String>,
    secret: &Option<String>,
    devices_ids: &[String],
    sign_method: &String,
    access_token: &Option<String>,
    color_name: &str,
) {
    // lookup color, convert to HSV, build command body, sign, and POST to Tuya
}

These snippets are small excerpts; see the real implementations in src/main.rs, src/clients/twitch_client.rs, and src/bulb/color.rs.

How to get Tuya keys

  1. Head over to Tuya Developer Platform
  2. Go to the Cloud tab and click on Create Cloud Project image
  3. Fill in with the following information:
    • Project name: your choice;
    • Description: optional;
    • Industry: select Smart Home;
    • Development Method: select Smart Home;
    • Data center: this you have to select the one according to your smart device, in my case its Western America Data Center; image2
  4. Select this options:
    • IoT Core
    • Authorization Token Management
    • Smart home Basic Service
    • Data Dashboard Service
    • [Deprecate] Smart Home Scene Linkage
    • Product Management Service
    • Device Status Notification
    • Smart Home Content Manage image3
  5. In the Overview copy your Access ID/Client ID and Access Secret/Client Secret.
  6. Head over to the Devices tab, and click on Add Device
    • In my case I went with the Add Device with Tuya Spatial App option image4
  7. Copy the id of each device (light bulb) image5

How to get Twitch uya keys

  1. Head on over to Twitch Dev Platform
  2. Click on the + Register your application button: image6
  3. Fill out the form with something similar to this: image7
  4. Then click click on the Manage button
  5. Here you will need to copy your Client ID and request a new Client Secret: image8

About

Twitch Channel Points integration for Tuya smart lights built with Rust. Viewers can change colors, turn lights on/off, and interact with stream lighting in real time through Channel Point rewards

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages