Sumit Kumar
Published © MIT

Tuya IoT Cloud Smart Weather Lamp

Have your own weather-based colour changing lamp with Tuya cloud API

BeginnerFull instructions provided4 hours758

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
MATRIX Creator
MATRIX Labs MATRIX Creator
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Tuya Cloud and IoT Platform
Tuya Developer

Story

Read more

Schematics

Required hardware

Code

weather.py

Python
from matrix_lite import led
import weatherColorCode as color
from tuya_iot import TuyaOpenAPI
import json
import math
from urllib.request import urlopen
from time import sleep

def getIP():
    url = 'http://ipinfo.io/json'
    response = urlopen(url)
    data = json.load(response)
    IP = data['ip']
    return IP

# Cloud project authorization info
ACCESS_ID = ''
ACCESS_KEY = ''

# Select an endpoint base on your project availability zone
ENDPOINT = "https://openapi.tuyain.com"

# Project configuration
USERNAME = ''  # email address or phone number
PASSWORD = ''

DEVICE_ID = 'your device ID'

# Initialization of tuya openapi
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)

def weatherColorStatus(condition_num):
    if condition_num == 120:
        led.set(color.SUNNY_120)
        return True
    if condition_num == 146:
        led.set(color.CLEAR_146)
        return True
    if condition_num == 139:
        led.set(color.LIGHT_RAIN_139)
        return True
    if condition_num == 132:
        led.set(color.OVERCAST_132)
        return True
    if condition_num == 141:
        led.set(color.MODERATE_RAIN_141)
        return True
    if condition_num == 101:
        led.set(color.HEAVY_RAIN_101)
        return True
    if condition_num == 140:
        led.set(color.HAZE_140)
        return True
    if condition_num == 121:
        led.set(color.FOG_121)
        return True
    if condition_num == 109:
        led.set(color.DUST_109)
        return True
    if condition_num == 103:
        led.set(color.SANDSTORM_103)
        return True
    if condition_num == 105:
        led.set(color.SNOW_105)
        return True
    else:
        led.set('black')
        return False
    
def getLocation(IP):
    location = openapi.get(f'/v1.0/iot-03/locations/ip?ip={IP}')
    print(location)
    location = location['result']
    latitude, longitude = location['latitude'], location['longitude']
    return latitude, longitude

def getWeather(latitude, longitude):
    weather_url = f'/v2.0/iot-03/weather/current?lat={latitude}&lon={longitude}'
    weather = openapi.get(weather_url)
    print(weather)
    condition_num = int(weather['result']['current_weather']['condition_num'])
    air_quality = weather['result']['air_quality']['aqi']
    
    if weatherColorStatus(condition_num):
        print("Device colour set to mode: ", condition_num)
    
while True:
    IP = getIP()
    latitude, longitude = getLocation(IP)
    getWeather(latitude, longitude)
    sleep(5)

weatherColorCode.py

Python
SUNNY_120 = "yellogreen"
CLEAR_146 = "peru"
OVERCAST_132 = "darkcyan"

LIGHT_RAIN_139 = "cornflowerblue"
MODERATE_RAIN_141 = "lightblue"
HEAVY_RAIN_101 = "blue"

HAZE_140 = "orchid"
FOG_121 = "darkseagreen"
DUST_109 = "saddlebrown"
SANDSTORM_103 = "chocolate"

SNOW_105 = "snow"

Credits

Sumit Kumar

Sumit Kumar

32 projects • 96 followers
19 y/o. My daily routine involves dealing with electronics, code, distributed storage and cloud APIs.

Comments