Skip to content

Instantly share code, notes, and snippets.

@tomotomo tomotomo/main.py
Last active Sep 7, 2019

Embed
What would you like to do?
Control Philips Hue with Makeblock Halocode
# generated by mBlock5 for HaloCode
# codes make you happy
import event, halo
import urequests
# initialize variables
wifi_ready = 0
on = 0
ENDPOINT = "http://xx.xx.xx.xx/api/H1lG5096CkoA023qmfo************/groups/n/action"
@event.start
def on_start():
global wifi_ready, on
on = 0
wifi_ready = 0
halo.led.show_all(208, 2, 27)
halo.wifi.start(ssid = '{SSID}', password = '{PASSWORD}', mode = halo.wifi.WLAN_MODE_STA)
while not halo.wifi.is_connected():
pass
wifi_ready = 1
# Notify Wi-Fi connection successful
halo.led.show_all(26, 208, 1)
time.sleep(5)
halo.led.off_all()
# =========================
# DELETE ME
# This is a code for debug
# =========================
# halo.cloud_message.start('{TOPIC}')
# @event.button_pressed
# def on_button_pressed():
# global wifi_ready, on
# res = urequests.get(ENDPOINT)
# halo.cloud_message.broadcast('message', res)
@event.button_pressed
def on_button_pressed():
global wifi_ready, on
# Check Wi-Fi connection
if wifi_ready == 1:
# Toggle hue on/off
if on == 0:
# Notify sending request Hue on
halo.led.show_all(200, 200, 200)
data = '{"on":true}'
res = urequests.put(ENDPOINT, data=data)
on = 1
else:
# Notify sending request Hue off
halo.led.show_all(10, 10, 50)
data = '{"on":false}'
res = urequests.put(ENDPOINT, data=data)
on = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.