azureiot_central_simpletest.py
1 import json 2 import random 3 import time 4 import board 5 import busio 6 from digitalio import DigitalInOut 7 import neopixel 8 from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager 9 import adafruit_esp32spi.adafruit_esp32spi_socket as socket 10 from adafruit_ntp import NTP 11 12 # Get wifi details and more from a secrets.py file 13 try: 14 from secrets import secrets 15 except ImportError: 16 print("WiFi secrets are kept in secrets.py, please add them there!") 17 raise 18 19 # ESP32 Setup 20 try: 21 esp32_cs = DigitalInOut(board.ESP_CS) 22 esp32_ready = DigitalInOut(board.ESP_BUSY) 23 esp32_reset = DigitalInOut(board.ESP_RESET) 24 except AttributeError: 25 esp32_cs = DigitalInOut(board.D13) 26 esp32_ready = DigitalInOut(board.D11) 27 esp32_reset = DigitalInOut(board.D12) 28 29 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 30 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) 31 32 """Use below for Most Boards""" 33 status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards 34 """Uncomment below for ItsyBitsy M4""" 35 # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) 36 # Uncomment below for an externally defined RGB LED 37 # import adafruit_rgbled 38 # from adafruit_esp32spi import PWMOut 39 # RED_LED = PWMOut.PWMOut(esp, 26) 40 # GREEN_LED = PWMOut.PWMOut(esp, 27) 41 # BLUE_LED = PWMOut.PWMOut(esp, 25) 42 # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) 43 wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) 44 45 print("Connecting to WiFi...") 46 47 wifi.connect() 48 49 print("Connected to WiFi!") 50 51 print("Getting the time...") 52 53 ntp = NTP(esp) 54 # Wait for a valid time to be received 55 while not ntp.valid_time: 56 time.sleep(5) 57 ntp.set_time() 58 59 print("Time:", str(time.time())) 60 61 # To use Azure IoT Central, you will need to create an IoT Central app. 62 # You can either create a free tier app that will live for 7 days without an Azure subscription, 63 # Or a standard tier app that will last for ever with an Azure subscription. 64 # The standard tiers are free for up to 2 devices 65 # 66 # If you don't have an Azure subscription: 67 # 68 # If you are a student, head to https://aka.ms/FreeStudentAzure and sign up, validating with your 69 # student email address. This will give you $100 of Azure credit and free tiers of a load of 70 # service, renewable each year you are a student 71 # 72 # If you are not a student, head to https://aka.ms/FreeAz and sign up to get $200 of credit for 30 73 # days, as well as free tiers of a load of services 74 # 75 # Create an Azure IoT Central app by following these instructions: https://aka.ms/CreateIoTCentralApp 76 # Add a device template with telemetry, properties and commands, as well as a view to visualize the 77 # telemetry and execute commands, and a form to set properties. 78 # 79 # Next create a device using the device template, and select Connect to get the device connection details. 80 # Add the connection details to your secrets.py file, using the following values: 81 # 82 # 'id_scope' - the devices ID scope 83 # 'device_id' - the devices device id 84 # 'key' - the devices primary key 85 # 86 # The adafruit-circuitpython-azureiot library depends on the following libraries: 87 # 88 # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): 89 # * adafruit-circuitpython-minimqtt 90 # * adafruit-circuitpython-requests 91 from adafruit_azureiot import IoTCentralDevice 92 93 # Create an IoT Hub device client and connect 94 device = IoTCentralDevice(socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]) 95 96 print("Connecting to Azure IoT Central...") 97 98 # Connect to IoT Central 99 device.connect() 100 101 print("Connected to Azure IoT Central!") 102 103 message_counter = 60 104 105 while True: 106 try: 107 # Send telemetry every minute 108 # You can see the values in the devices dashboard 109 if message_counter >= 60: 110 message = {"Temperature": random.randint(0, 50)} 111 device.send_telemetry(json.dumps(message)) 112 message_counter = 0 113 else: 114 message_counter = message_counter + 1 115 116 # Poll every second for messages from the cloud 117 device.loop() 118 except (ValueError, RuntimeError) as e: 119 print("Connection error, reconnecting\n", str(e)) 120 # If we lose connectivity, reset the wifi and reconnect 121 wifi.reset() 122 wifi.connect() 123 device.reconnect() 124 continue 125 126 time.sleep(1)