Skip to content

Instantly share code, notes, and snippets.

@tomotomo
Last active September 7, 2019 09:37
Show Gist options
  • Save tomotomo/0f2c0e8605a6c2fac41f113d5d434c00 to your computer and use it in GitHub Desktop.
Save tomotomo/0f2c0e8605a6c2fac41f113d5d434c00 to your computer and use it in GitHub Desktop.
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