All tutorials

Tutorial 6 of 6

~10 min

Upload telemetry every 10 seconds (ESP8266 & Python)

Publish temperature, humidity and voltage to the platform over MQTT on a fixed interval — from an ESP8266 sketch or the same loop in Python.

  1. 1

    What you'll build

    A device that connects to WiFi and your MQTT broker, then publishes a JSON reading to devices/<id>/telemetry every 10 seconds. This mirrors the ESP8266_Telemetry_Upload example that ships with the Arduino library — and the identical loop with the iotflow Python package.

    {"token":"dev_…","temperature":28.5,"humidity":65,"voltage":3.7}
    → published to  devices/<DEVICE_ID>/telemetry  every 10 s
  2. 2

    Create the device and copy its token

    Run the Add Device wizard (see Tutorial 1) and choose MQTT. Note the device id and the dev_… token — the token rides inside every telemetry message to authenticate it.

  3. 3

    Find your broker address

    Use the LAN IP of the machine running the Mosquitto broker — not “localhost”, because the ESP8266 is a separate device on your network. Find it with ipconfig (Windows) or ifconfig (macOS/Linux). Port is 1883 by default.

  4. 4

    ESP8266 — flash the sketch

    Install the PubSubClient library (Library Manager), fill in your WiFi, broker IP, device id and token, and flash. The full sketch is clients/arduino/IoTFlow/examples/ESP8266_Telemetry_Upload on GitHub; this is the heart of it.

    const char* MQTT_BROKER  = "192.168.1.50";   // your broker's LAN IP
    const char* DEVICE_ID    = "your-device-id";
    const char* DEVICE_TOKEN = "dev_YOUR_DEVICE_TOKEN";
    
    void loop() {
      if (WiFi.status() != WL_CONNECTED) connectWiFi();
      if (!mqttClient.connected()) connectMQTT();
      mqttClient.loop();
    
      if (millis() - lastPublish >= 10000) {
        lastPublish = millis();
        // TODO: replace with real sensor readings
        publishTelemetry(28.5, 65, 3.7);   // temperature, humidity, voltage
      }
    }
  5. 5

    Raspberry Pi / PC — the same thing in Python

    No ESP8266 handy? The iotflow package does the identical job on anything that runs Python. Install it, fill in the same broker/id/token, and run.

    # pip install "iotflow[mqtt]"
    import time
    from iotflow import IoTFlow
    
    iot = IoTFlow(token="dev_YOUR_DEVICE_TOKEN", device_id="your-device-id",
                  mqtt_host="192.168.1.50", mqtt_port=1883)
    iot.connect()                          # MQTT runs in the background
    
    while True:
        # TODO: replace with real sensor readings
        iot.mqtt_publish(temperature=28.5, humidity=65, voltage=3.7)
        time.sleep(10)
  6. 6

    Watch it arrive, then visualise it

    Open the Dashboard — a new reading lands under Latest Telemetry every 10 seconds. Bind Number, Gauge or Line-chart widgets to the temperature, humidity and voltage metrics (see Tutorial 3) and you have a live sensor feed.