0
votes

I'm doing a telemetry application using Azure IoT Hub, Azure IoT SDK in Python and a raspberry pi with temperature and humidity sensors.

Humidity + Temperature sensors => Rasperry Pi => Azure IoT Hub

In my first implementation thanks azure examples, I used one loop that collect data from the temperature sensor and the humidity sensor, and send them to Azure IoT Hub in the same time every 60 second.

>>> 1 Loop every 60s = Collect data & send data of temperature and humidity

Now I would like to send them with different frequencies, I mean : One loop will collect the data of the temperature sensor and send it to Azure IoT Hub every 60 seconds; Whereas a second loop will collect the data of the humidity sensor and send it to Azure IoT Hub every 600 seconds.

>>> 1 Loop every 60s= Collect data & send data of temperature
>>> 2 Loop every 600s= Collect data & send data of humidity 

I think the tool I need is multi-threading, but I don't understand which library or structure I have to implement in my case.

Here is the code provided by Azure, including one loop that handles temperature and humidity at the same time. Reading the data and sending to Azure every 60 seconds.

import random
import time
import sys

# Using the Python Device SDK for IoT Hub:
from iothub_client import IoTHubClient, IoTHubClientError, 
IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, 
IoTHubError, DeviceMethodReturnValue

# The device connection string to authenticate the device with your IoT hub.
CONNECTION_STRING = "{Your IoT hub device connection string}"

# Using the MQTT protocol.
PROTOCOL = IoTHubTransportProvider.MQTT
MESSAGE_TIMEOUT = 10000

# Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}"

def send_confirmation_callback(message, result, user_context):
    print ( "IoT Hub responded to message with status: %s" % (result) )

def iothub_client_init():
    # Create an IoT Hub client
   client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
   return client

def iothub_client_telemetry_sample_run():

    try:
        client = iothub_client_init()
        print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )

   #******************LOOP*******************************    
   while True:
            # Build the message with simulated telemetry values.
            temperature = TEMPERATURE + (random.random() * 15)
            humidity = HUMIDITY + (random.random() * 20)
            msg_txt_formatted = MSG_TXT % (temperature, humidity)
            message = IoTHubMessage(msg_txt_formatted)

            # Send the message.
            print( "Sending message: %s" % message.get_string() )
            client.send_event_async(message, send_confirmation_callback, None)
            time.sleep(60)

    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )

if __name__ == '__main__':
    print ( "IoT Hub Quickstart #1 - Simulated device" )
    print ( "Press Ctrl-C to exit" )
    iothub_client_telemetry_sample_run()

I would like to use the same structure of functions, including two loops that handles temperature and humidity, one every 60s and one every 600s.

while True:
    # Build the message with simulated telemetry values.
    temperature = TEMPERATURE + (random.random() * 15)
    msg_txt_formatted1 = MSG_TXT1 % (temperature)
    message1 = IoTHubMessage(msg_txt_formatted1)
    # Send the message.
    print( "Sending message: %s" % message1.get_string() )
    client.send_event_async(message1, send_confirmation_callback, None)
    time.sleep(60)

while True:
    # Build the message with simulated telemetry values.
    humidity = HUMIDITY + (random.random() * 20)
    msg_txt_formatted2 = MSG_TXT2 % (humidity)
    message2 = IoTHubMessage(msg_txt_formatted2)
    # Send the message.
    print( "Sending message: %s" % message2.get_string() )
    client.send_event_async(message2, send_confirmation_callback, None)
    time.sleep(600)

How can I do that? How to call those loops with multi-threading or another method?

2

2 Answers

0
votes

It may be simpler to do something like

while True:
    loop_b()
    for _ in range(10):
        loop_a()
        time.sleep(60)

or even

while True:
    time.sleep(1)
    now = time.time()
    if now % 60 == 0:
        loop_a()
    if now % 600 == 0:
        loop_b()

But if you really want to use threads, then:

import threading

class LoopAThread(threading.Thread):
    def run(self):
        loop_a()
class LoopBThread(threading.Thread):
    def run(self):
        loop_b()
...

thread_a = LoopAThread()
thread_b = LoopBThread()
thread_a.start()
thread_b.start()
thread_a.join()
thread_b.join()

0
votes

Here are two competing approaches to consider

  1. Don't bother with threads at all. Just have one loop that sleeps every 60 seconds like you have now. Keep track of the last time you sent humidity data. If 600 seconds has passed, then send it. Otherwise, skip it and go to sleep for 60 seconds. Something like this:

    from datetime import datetime, timedelta
    
    def iothub_client_telemetry_sample_run():
        last_humidity_run = None
        humidity_period = timedelta(seconds=600)
        client = iothub_client_init()
        while True:
            now = datetime.now()
            send_temperature_data(client)
    
            if not last_humidity_run or now - last_humidity_run >= humidity_period:
                send_humidity_data(client)
                last_humidity_run = now
    
            time.sleep(60)
    
  2. Rename iothub_client_telemetry_sample_run to temperature_thread_func or something like it. Create a separate function that looks just like it for humidity. Spawn two threads from the main function of your program. Set them to daemon mode so they shutdown when the user exits

    from threading import Thread
    
    def temperature_thread_func():
        client = iothub_client_init()
        while True:
            send_temperature_data(client)
            time.sleep(60)
    
    def humidity_thread_func():
        client = iothub_client_init()
        while True:
            send_humidity_data(client)
            time.sleep(600)
    
    if __name__ == '__main__':
        temp_thread = Thread(target=temperature_thread_func)
        temp_thread.daemon = True
    
        humidity_thread = Thread(target=humidity_thread_func)
        humidity_thread.daemon = True
    
        input('Polling for data. Press a key to exit')
    

Notes:

  • If you decide to use threads, consider using an event to terminate them cleanly.
  • time.sleep is not a precise way to keep time. You might need a different timing mechanism if the samples need to be taken at precise moments.