You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
921 B
39 lines
921 B
4 years ago
|
import time
|
||
|
from grove.grove_light_sensor_v1_2 import GroveLightSensor
|
||
|
from grove.grove_led import GroveLed
|
||
|
import json
|
||
|
import paho.mqtt.client as mqtt
|
||
|
|
||
|
light_sensor = GroveLightSensor(0)
|
||
|
led = GroveLed(5)
|
||
|
|
||
|
id = '<ID>'
|
||
|
|
||
|
client_telemetry_topic = id + '/telemetry'
|
||
|
server_command_topic = id + '/commands'
|
||
|
client_name = id + 'nightlight_client'
|
||
|
|
||
|
mqtt_client = mqtt.Client(client_name)
|
||
|
mqtt_client.connect('test.mosquitto.org')
|
||
|
|
||
|
mqtt_client.loop_start()
|
||
|
|
||
|
def handle_command(client, userdata, message):
|
||
|
payload = json.loads(message.payload.decode())
|
||
|
print("Message received:", payload)
|
||
|
|
||
|
if payload['led_on']:
|
||
|
led.on()
|
||
|
else:
|
||
|
led.off()
|
||
|
|
||
|
mqtt_client.subscribe(server_command_topic)
|
||
|
mqtt_client.on_message = handle_command
|
||
|
|
||
|
while True:
|
||
|
light = light_sensor.light
|
||
|
print('Light level:', light)
|
||
|
|
||
|
mqtt_client.publish(client_telemetry_topic, json.dumps({'light' : light}))
|
||
|
|
||
|
time.sleep(5)
|