import time
from azure.iot.device import IoTHubDeviceClient
CONNECTION_STRING = "<your device connection string>"
MSG_TXT = '{{"lockStatus": {0}, "accessAttempts": {1}}}'
def send_message(lock_status, access_attempts):
try:
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
print("Sending data to Azure IoT Hub:")
while True:
message = MSG_TXT.format(lock_status, access_attempts)
client.send_message(message)
print("Message sent: ", message)
time.sleep(5) # Delay between sending messages
except KeyboardInterrupt:
print("IoT Hub data transmission stopped.")
if __name__ == '__main__':
lock_status = True # Example lock status
access_attempts = 10 # Example access attempts
send_message(lock_status, access_attempts)