* Adding content

* Update en.json

* Update README.md

* Update TRANSLATIONS.md

* Adding lesson tempolates

* Fixing code files with each others code in
This commit is contained in:
Jim Bennett 2021-06-01 17:38:04 -07:00 коммит произвёл GitHub
Родитель 05bcf93a0e
Коммит 5adefbc97e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 10 добавлений и 8 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -6,7 +6,8 @@ import paho.mqtt.client as mqtt
id = '<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

Просмотреть файл

@ -6,8 +6,7 @@ import paho.mqtt.client as mqtt
id = '<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