Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Boss lady on 6/14/2024 in #iot-cloud
how can I send data from a smart door lock to Azure IoT Hub?
Firstly you need to create an account on there official site install necessary libraries then your good to go, your code should look like 👇
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)
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)
@Boss lady
3 replies