Advertisement
Guest User

Send None

a guest
Aug 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. import random
  2. import time
  3. import sys
  4.  
  5. import iothub_client
  6. # pylint: disable=E0611
  7. from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
  8. from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue
  9.  
  10. import board
  11. import busio
  12. from digitalio import DigitalInOut
  13. from adafruit_pn532.i2c import PN532_I2C
  14. reset_pin = DigitalInOut(board.D6)
  15. req_pin = DigitalInOut(board.D12)
  16. i2c = busio.I2C(board.SCL, board.SDA)
  17. pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
  18. pn532.SAM_configuration()
  19.  
  20. # Your IoT hub device connection string:
  21. CONNECTION_STRING = "HostName=mb-iot-hub.azure-devices.net;DeviceId=RaspberryPi;SharedAccessKey=lu3szoJReJwqvAbXgAqopQ4V6TKnXSYkKN0wP6e4BtI="
  22.  
  23. # Using the MQTT protocol.
  24. PROTOCOL = IoTHubTransportProvider.MQTT
  25. MESSAGE_TIMEOUT = 10000
  26.  
  27. # Define the JSON message to send to IoT Hub.
  28. ###TEMPERATURE = 20.0
  29. ###HUMIDITY = 60
  30. ###MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}"
  31.  
  32. def send_confirmation_callback(message, result, user_context):
  33.     print ( "IoT Hub responded to message with status: %s" % (result) )
  34.  
  35. def iothub_client_init():
  36.     # Create an IoT Hub client
  37.     client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
  38.     return client
  39.  
  40. def iothub_client_telemetry_sample_run():
  41.  
  42.     try:
  43.         client = iothub_client_init()
  44.         print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
  45.  
  46.         while True:
  47.             # Build the message with simulated telemetry values.
  48.             uid = pn532.read_passive_target(timeout=0.5)
  49.             print(end="", flush=True)
  50.             if uid is None:
  51.                 continue
  52.             MSG_TXT = bytearray([int(i) for i in uid])
  53.             #temperature = TEMPERATURE + (random.random() * 15)
  54.             #humidity = HUMIDITY + (random.random() * 20)
  55.             msg_txt_formatted = MSG_TXT
  56.             message = IoTHubMessage(msg_txt_formatted)
  57.  
  58.             # Add a custom application property to the message.
  59.             # An IoT hub can filter on these properties without access to the message body.
  60.             #prop_map = message.properties()
  61.             #if temperature > 30:
  62.               #prop_map.add("temperatureAlert", "true")
  63.             #else:
  64.               #prop_map.add("temperatureAlert", "false")
  65.  
  66.             # Send the message.
  67.             print( "Sending message: %s" % message.get_string() )
  68.             client.send_event_async(message, send_confirmation_callback, None)
  69.             time.sleep(1)
  70.  
  71.     except IoTHubError as iothub_error:
  72.         print ( "Unexpected error %s from IoTHub" % iothub_error )
  73.         return
  74.     except KeyboardInterrupt:
  75.         print ( "IoTHubClient sample stopped" )
  76.  
  77. if __name__ == '__main__':
  78.     print ( "IoT Hub Quickstart #1 - Simulated device" )
  79.     print ( "Press Ctrl-C to exit" )
  80.     iothub_client_telemetry_sample_run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement