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.
IoT-For-Beginners/translations/pcm/1-getting-started/lessons/4-connect-internet/single-board-computer-telem...

3.0 KiB

Control your nightlight for Internet - Virtual IoT Hardware and Raspberry Pi

For dis part of di lesson, you go send telemetry wey get light levels from your Raspberry Pi or virtual IoT device go one MQTT broker.

Publish telemetry

Di next step na to create one JSON document wey get telemetry and send am go di MQTT broker.

Task

Publish telemetry go di MQTT broker.

  1. Open di nightlight project for VS Code.

  2. If you dey use virtual IoT device, make sure say di terminal dey run di virtual environment. If na Raspberry Pi you dey use, you no go need virtual environment.

  3. Add dis import for di top of di app.py file:

    import json
    

    Di json library na im dem dey use to encode di telemetry as JSON document.

  4. Add dis one after di client_name declaration:

    client_telemetry_topic = id + '/telemetry'
    

    Di client_telemetry_topic na di MQTT topic wey di device go dey publish di light levels put.

  5. Change wetin dey inside di while True: loop for di end of di file to dis one:

    while True:
        light = light_sensor.light
        telemetry = json.dumps({'light' : light})
    
        print("Sending telemetry ", telemetry)
    
        mqtt_client.publish(client_telemetry_topic, telemetry)
    
        time.sleep(5)
    

    Dis code go package di light level inside JSON document and publish am go di MQTT broker. E go then sleep small to reduce how often e dey send message.

  6. Run di code di same way wey you take run di code for di previous part of di assignment. If you dey use virtual IoT device, make sure say di CounterFit app dey run and di light sensor and LED don dey set for di correct pins.

    (.venv) ➜  nightlight python app.py 
    MQTT connected!
    Sending telemetry  {"light": 0}
    Sending telemetry  {"light": 0}
    

💁 You fit find dis code for di code-telemetry/virtual-device folder or di code-telemetry/pi folder.

😀 You don successfully send telemetry from your device.


Disclaimer:
Dis document don use AI translation service Co-op Translator take translate am. Even though we dey try make e accurate, abeg sabi say automated translations fit get mistake or no dey 100% correct. Di original document for di native language na di main correct source. For important information, e better make una use professional human translation. We no go fit take responsibility for any misunderstanding or wrong interpretation wey fit happen because of dis translation.