Skip to content

Commit 2bd864b

Browse files
author
brentru
committed
add simpletest for messaging
1 parent 76c8273 commit 2bd864b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

adafruit_azureiot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
4646
AZURE_HTTP_ERROR_CODES = [400, 401, 404, 403, 412, 429, 500] # Azure HTTP Status Codes
4747

48-
class IOT_HUB:
48+
class IOT_Hub:
4949
"""
5050
Provides access to a Microsoft Azure IoT Hub.
5151
https://docs.microsoft.com/en-us/rest/api/iothub/

examples/azureiot_simpletest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import microcontroller
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
6+
import neopixel
7+
from adafruit_azureiot import IOT_Hub
8+
9+
# Get wifi details and more from a secrets.py file
10+
try:
11+
from secrets import secrets
12+
except ImportError:
13+
print("WiFi secrets are kept in secrets.py, please add them there!")
14+
raise
15+
16+
# ESP32 Setup
17+
try:
18+
esp32_cs = DigitalInOut(board.ESP_CS)
19+
esp32_ready = DigitalInOut(board.ESP_BUSY)
20+
esp32_reset = DigitalInOut(board.ESP_RESET)
21+
except AttributeError:
22+
esp32_cs = DigitalInOut(board.D9)
23+
esp32_ready = DigitalInOut(board.D10)
24+
esp32_reset = DigitalInOut(board.D5)
25+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
26+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
27+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
28+
"""Uncomment below for ItsyBitsy M4"""
29+
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
30+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
31+
32+
# Create an instance of the Azure IoT Hub
33+
hub = IOT_Hub(wifi, secrets['azure_iot_hub'], secrets['azure_iot_sas'])
34+
35+
# Set a device id (This name must match a device connected to your Azure IoT Hub)
36+
device_id = 'Blinka'
37+
38+
# Send a Device-to-Cloud message
39+
cpu_temp = microcontroller.cpu.temperature
40+
print('Sending Temperature to Azure IoT Hub...')
41+
hub.send_device_message(device_id, str(cpu_temp))
42+
print('Data Sent!')
43+
44+
# Receive a Cloud-to-Device message
45+
print('Receiving a message from an Azure IoT Hub...')
46+
message = hub.get_hub_message(device_id)
47+
if message == -1:
48+
print('IoT Hub Message Queue is empty!')
49+
else:
50+
print(message)

0 commit comments

Comments
 (0)