Fixing code files with each others code in

pull/48/head
Jim Bennett 4 years ago
parent 7eb4abe7ec
commit 91467c3d4e

@ -392,6 +392,8 @@ The next step for our Internet controlled nightlight is for the server code to s
> 💁 The telemetry and commands are being sent on a single topic each. This means telemetry from multiple devices will appear on the same telemetry topic, and commands to multiple devices will appear on the same commands topic. If you wanted to send a command to a specific device, you could use multiple topics, named with a unique device id, such as `/commands/device1`, `/commands/device2`. That way a device can listen on messages just meant for that one device. > 💁 The telemetry and commands are being sent on a single topic each. This means telemetry from multiple devices will appear on the same telemetry topic, and commands to multiple devices will appear on the same commands topic. If you wanted to send a command to a specific device, you could use multiple topics, named with a unique device id, such as `/commands/device1`, `/commands/device2`. That way a device can listen on messages just meant for that one device.
> 💁 You can find this code in the [code-commands/server](code-commands/server) folder.
### Handle commands on the IoT device ### Handle commands on the IoT device
Now that commands are being sent from the server, you cna now add code to the IoT device to handle them and control the LED. Now that commands are being sent from the server, you cna now add code to the IoT device to handle them and control the LED.

@ -6,7 +6,8 @@ import paho.mqtt.client as mqtt
id = '<ID>' id = '<ID>'
client_telemetry_topic = id + '/telemetry' client_telemetry_topic = id + '/telemetry'
client_name = id + '_nightlight_server' server_command_topic = id + '/commands'
client_name = id + 'nightlight_server'
mqtt_client = mqtt.Client(client_name) mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org') mqtt_client.connect('test.mosquitto.org')
@ -17,6 +18,11 @@ def handle_telemetry(client, userdata, message):
payload = json.loads(message.payload.decode()) payload = json.loads(message.payload.decode())
print("Message received:", payload) print("Message received:", payload)
command = { 'led_on' : payload['light'] < 200 }
print("Sending message:", command)
client.publish(server_command_topic, json.dumps(command))
mqtt_client.subscribe(client_telemetry_topic) mqtt_client.subscribe(client_telemetry_topic)
mqtt_client.on_message = handle_telemetry mqtt_client.on_message = handle_telemetry

@ -6,8 +6,7 @@ import paho.mqtt.client as mqtt
id = '<ID>' id = '<ID>'
client_telemetry_topic = id + '/telemetry' client_telemetry_topic = id + '/telemetry'
server_command_topic = id + '/commands' client_name = id + '_nightlight_server'
client_name = id + 'nightlight_server'
mqtt_client = mqtt.Client(client_name) mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org') mqtt_client.connect('test.mosquitto.org')
@ -18,11 +17,6 @@ def handle_telemetry(client, userdata, message):
payload = json.loads(message.payload.decode()) payload = json.loads(message.payload.decode())
print("Message received:", payload) print("Message received:", payload)
command = { 'led_on' : payload['light'] < 200 }
print("Sending message:", command)
client.publish(server_command_topic, json.dumps(command))
mqtt_client.subscribe(client_telemetry_topic) mqtt_client.subscribe(client_telemetry_topic)
mqtt_client.on_message = handle_telemetry mqtt_client.on_message = handle_telemetry

Loading…
Cancel
Save