From 91467c3d4ec9f2e3a2e0874e80d44574147ecd1e Mon Sep 17 00:00:00 2001 From: Jim Bennett Date: Tue, 1 Jun 2021 17:37:13 -0700 Subject: [PATCH] Fixing code files with each others code in --- 1-getting-started/lessons/4-connect-internet/README.md | 2 ++ .../4-connect-internet/code-commands/server/app.py | 8 +++++++- .../lessons/4-connect-internet/code-server/server/app.py | 8 +------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/1-getting-started/lessons/4-connect-internet/README.md b/1-getting-started/lessons/4-connect-internet/README.md index e5e61322..b6cd3e68 100644 --- a/1-getting-started/lessons/4-connect-internet/README.md +++ b/1-getting-started/lessons/4-connect-internet/README.md @@ -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. +> 💁 You can find this code in the [code-commands/server](code-commands/server) folder. + ### 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. diff --git a/1-getting-started/lessons/4-connect-internet/code-commands/server/app.py b/1-getting-started/lessons/4-connect-internet/code-commands/server/app.py index e6b46379..01b6a0f2 100644 --- a/1-getting-started/lessons/4-connect-internet/code-commands/server/app.py +++ b/1-getting-started/lessons/4-connect-internet/code-commands/server/app.py @@ -6,7 +6,8 @@ import paho.mqtt.client as mqtt id = '' 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.connect('test.mosquitto.org') @@ -17,6 +18,11 @@ def handle_telemetry(client, userdata, message): payload = json.loads(message.payload.decode()) 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.on_message = handle_telemetry diff --git a/1-getting-started/lessons/4-connect-internet/code-server/server/app.py b/1-getting-started/lessons/4-connect-internet/code-server/server/app.py index 01b6a0f2..e6b46379 100644 --- a/1-getting-started/lessons/4-connect-internet/code-server/server/app.py +++ b/1-getting-started/lessons/4-connect-internet/code-server/server/app.py @@ -6,8 +6,7 @@ import paho.mqtt.client as mqtt id = '' 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.connect('test.mosquitto.org') @@ -18,11 +17,6 @@ def handle_telemetry(client, userdata, message): payload = json.loads(message.payload.decode()) 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.on_message = handle_telemetry