azureiot_hub_twin_operations.py
1 import random 2 import time 3 import board 4 import busio 5 from digitalio import DigitalInOut 6 import neopixel 7 from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager 8 import adafruit_esp32spi.adafruit_esp32spi_socket as socket 9 from adafruit_ntp import NTP 10 11 # Get wifi details and more from a secrets.py file 12 try: 13 from secrets import secrets 14 except ImportError: 15 print("WiFi secrets are kept in secrets.py, please add them there!") 16 raise 17 18 # ESP32 Setup 19 try: 20 esp32_cs = DigitalInOut(board.ESP_CS) 21 esp32_ready = DigitalInOut(board.ESP_BUSY) 22 esp32_reset = DigitalInOut(board.ESP_RESET) 23 except AttributeError: 24 esp32_cs = DigitalInOut(board.D13) 25 esp32_ready = DigitalInOut(board.D11) 26 esp32_reset = DigitalInOut(board.D12) 27 28 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 29 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) 30 31 """Use below for Most Boards""" 32 status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards 33 """Uncomment below for ItsyBitsy M4""" 34 # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) 35 # Uncomment below for an externally defined RGB LED 36 # import adafruit_rgbled 37 # from adafruit_esp32spi import PWMOut 38 # RED_LED = PWMOut.PWMOut(esp, 26) 39 # GREEN_LED = PWMOut.PWMOut(esp, 27) 40 # BLUE_LED = PWMOut.PWMOut(esp, 25) 41 # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) 42 wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) 43 44 print("Connecting to WiFi...") 45 46 wifi.connect() 47 48 print("Connected to WiFi!") 49 50 print("Getting the time...") 51 52 ntp = NTP(esp) 53 # Wait for a valid time to be received 54 while not ntp.valid_time: 55 time.sleep(5) 56 ntp.set_time() 57 58 print("Time:", str(time.time())) 59 60 # You will need an Azure subscription to create an Azure IoT Hub resource 61 # 62 # If you don't have an Azure subscription: 63 # 64 # If you are a student, head to https://aka.ms/FreeStudentAzure and sign up, validating with your 65 # student email address. This will give you $100 of Azure credit and free tiers of a load of 66 # service, renewable each year you are a student 67 # 68 # If you are not a student, head to https://aka.ms/FreeAz and sign up to get $200 of credit for 30 69 # days, as well as free tiers of a load of services 70 # 71 # Create an Azure IoT Hub and an IoT device in the Azure portal here: https://aka.ms/AzurePortalHome. 72 # Instructions to create an IoT Hub and device are here: https://aka.ms/CreateIoTHub 73 # 74 # The free tier of IoT Hub allows up to 8,000 messages a day, so try not to send messages too often 75 # if you are using the free tier 76 # 77 # Once you have a hub and a device, copy the device primary connection string. 78 # Add it to the secrets.py file in an entry called device_connection_string 79 # 80 # To us twins, you will need either a free or standard tier IoT Hub. Basic tier doesn't 81 # support twins 82 # 83 # The adafruit-circuitpython-azureiot library depends on the following libraries: 84 # 85 # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): 86 # * adafruit-circuitpython-minimqtt 87 # * adafruit-circuitpython-requests 88 from adafruit_azureiot import IoTHubDevice 89 90 # Create an IoT Hub device client and connect 91 device = IoTHubDevice(socket, esp, secrets["device_connection_string"]) 92 93 # Subscribe to device twin desired property updates 94 # To see these changes, update the desired properties for the device either in code 95 # or in the Azure portal by selecting the device in the IoT Hub blade, selecting 96 # Device Twin then adding or amending an entry in the 'desired' section 97 def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int): 98 print("Property", desired_property_name, "updated to", str(desired_property_value), "version", desired_version) 99 100 101 # Subscribe to the device twin desired property updated event 102 device.on_device_twin_desired_updated = device_twin_desired_updated 103 104 print("Connecting to Azure IoT Hub...") 105 106 # Connect to IoT Central 107 device.connect() 108 109 print("Connected to Azure IoT Hub!") 110 111 message_counter = 60 112 113 while True: 114 try: 115 if message_counter >= 60: 116 # Send a reported property twin update every minute 117 # You can see these in the portal by selecting the device in the IoT Hub blade, selecting 118 # Device Twin then looking for the updates in the 'reported' section 119 patch = {"Temperature": random.randint(0, 50)} 120 device.update_twin(patch) 121 message_counter = 0 122 else: 123 message_counter = message_counter + 1 124 125 # Poll every second for messages from the cloud 126 device.loop() 127 except (ValueError, RuntimeError) as e: 128 print("Connection error, reconnecting\n", str(e)) 129 # If we lose connectivity, reset the wifi and reconnect 130 wifi.reset() 131 wifi.connect() 132 device.reconnect() 133 continue 134 135 time.sleep(1)