azureiot_central_properties.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 # To use Azure IoT Central, you will need to create an IoT Central app. 61 # You can either create a free tier app that will live for 7 days without an Azure subscription, 62 # Or a standard tier app that will last for ever with an Azure subscription. 63 # The standard tiers are free for up to 2 devices 64 # 65 # If you don't have an Azure subscription: 66 # 67 # If you are a student, head to https://aka.ms/FreeStudentAzure and sign up, validating with your 68 # student email address. This will give you $100 of Azure credit and free tiers of a load of 69 # service, renewable each year you are a student 70 # 71 # If you are not a student, head to https://aka.ms/FreeAz and sign up to get $200 of credit for 30 72 # days, as well as free tiers of a load of services 73 # 74 # Create an Azure IoT Central app by following these instructions: https://aka.ms/CreateIoTCentralApp 75 # Add a device template with telemetry, properties and commands, as well as a view to visualize the 76 # telemetry and execute commands, and a form to set properties. 77 # 78 # Next create a device using the device template, and select Connect to get the device connection details. 79 # Add the connection details to your secrets.py file, using the following values: 80 # 81 # 'id_scope' - the devices ID scope 82 # 'device_id' - the devices device id 83 # 'key' - the devices primary key 84 # 85 # The adafruit-circuitpython-azureiot library depends on the following libraries: 86 # 87 # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): 88 # * adafruit-circuitpython-minimqtt 89 # * adafruit-circuitpython-requests 90 from adafruit_azureiot import IoTCentralDevice 91 92 # Create an IoT Hub device client and connect 93 device = IoTCentralDevice(socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]) 94 95 # Subscribe to property changes 96 # Properties can be updated either in code, or by adding a form to the view 97 # in the device template, and setting the value on the dashboard for the device 98 def property_changed(property_name, property_value, version): 99 print("Property", property_name, "updated to", str(property_value), "version", str(version)) 100 101 102 # Subscribe to the property changed event 103 device.on_property_changed = property_changed 104 105 print("Connecting to Azure IoT Central...") 106 107 # Connect to IoT Central 108 device.connect() 109 110 print("Connected to Azure IoT Central!") 111 112 message_counter = 60 113 114 while True: 115 try: 116 # Send property values every minute 117 # You can see the values in the devices dashboard 118 if message_counter >= 60: 119 device.send_property("Desired_Temperature", random.randint(0, 50)) 120 message_counter = 0 121 else: 122 message_counter = message_counter + 1 123 124 # Poll every second for messages from the cloud 125 device.loop() 126 except (ValueError, RuntimeError) as e: 127 print("Connection error, reconnecting\n", str(e)) 128 # If we lose connectivity, reset the wifi and reconnect 129 wifi.reset() 130 wifi.connect() 131 device.reconnect() 132 continue 133 134 time.sleep(1)