Skip to content

Commit 8aa3018

Browse files
authored
Merge pull request adafruit#9 from brentru/cert-example
Add example for using MiniMQTT with User-Provided Certificate/Key
2 parents 3acedda + 38b76ed commit 8aa3018

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

examples/minimqtt_certificate.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import board
2+
import busio
3+
from digitalio import DigitalInOut
4+
import neopixel
5+
from adafruit_esp32spi import adafruit_esp32spi
6+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
9+
from adafruit_minimqtt import MQTT
10+
11+
### WiFi ###
12+
13+
# Get wifi details and more from a secrets.py file
14+
try:
15+
from secrets import secrets
16+
except ImportError:
17+
print("WiFi secrets are kept in secrets.py, please add them there!")
18+
raise
19+
20+
# If you are using a board with pre-defined ESP32 Pins:
21+
esp32_cs = DigitalInOut(board.ESP_CS)
22+
esp32_ready = DigitalInOut(board.ESP_BUSY)
23+
esp32_reset = DigitalInOut(board.ESP_RESET)
24+
25+
# If you have an externally connected ESP32:
26+
# esp32_cs = DigitalInOut(board.D9)
27+
# esp32_ready = DigitalInOut(board.D10)
28+
# esp32_reset = DigitalInOut(board.D5)
29+
30+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
31+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
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+
### Topic Setup ###
46+
47+
# MQTT Topic
48+
# Use this topic if you'd like to connect to a standard MQTT broker
49+
mqtt_topic = 'test/topic'
50+
51+
# Adafruit IO-style Topic
52+
# Use this topic if you'd like to connect to io.adafruit.com
53+
# mqtt_topic = 'aio_user/feeds/temperature'
54+
55+
### Code ###
56+
57+
# Define callback methods which are called when events occur
58+
# pylint: disable=unused-argument, redefined-outer-name
59+
def connect(client, userdata, flags, rc):
60+
# This function will be called when the client is connected
61+
# successfully to the broker.
62+
print('Connected to MQTT Broker!')
63+
print('Flags: {0}\n RC: {1}'.format(flags, rc))
64+
65+
def disconnect(client, userdata, rc):
66+
# This method is called when the client disconnects
67+
# from the broker.
68+
print('Disconnected from MQTT Broker!')
69+
70+
def subscribe(client, userdata, topic, granted_qos):
71+
# This method is called when the client subscribes to a new feed.
72+
print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos))
73+
74+
def unsubscribe(client, userdata, topic, pid):
75+
# This method is called when the client unsubscribes from a feed.
76+
print('Unsubscribed from {0} with PID {1}'.format(topic, pid))
77+
78+
def publish(client, userdata, topic, pid):
79+
# This method is called when the client publishes data to a feed.
80+
print('Published to {0} with PID {1}'.format(topic, pid))
81+
82+
# Get certificate and private key from a certificates.py file
83+
try:
84+
from certificates import DEVICE_CERT, DEVICE_KEY
85+
except ImportError:
86+
print("Certificate and private key data is kept in certificates.py, please add them there!")
87+
raise
88+
89+
# Set Device Certificate
90+
esp.set_certificate(DEVICE_CERT)
91+
92+
# Set Private Key
93+
esp.set_private_key(DEVICE_KEY)
94+
95+
# Connect to WiFi
96+
wifi.connect()
97+
98+
# Set up a MiniMQTT Client
99+
client = MQTT(socket,
100+
broker = secrets['broker'],
101+
username = secrets['user'],
102+
password = secrets['pass'],
103+
network_manager = wifi)
104+
105+
# Connect callback handlers to client
106+
client.on_connect = connect
107+
client.on_disconnect = disconnect
108+
client.on_subscribe = subscribe
109+
client.on_unsubscribe = unsubscribe
110+
client.on_publish = publish
111+
112+
print('Attempting to connect to %s'%client.broker)
113+
client.connect()
114+
115+
print('Subscribing to %s'%mqtt_topic)
116+
client.subscribe(mqtt_topic)
117+
118+
print('Publishing to %s'%mqtt_topic)
119+
client.publish(mqtt_topic, 'Hello Broker!')
120+
121+
print('Unsubscribing from %s'%mqtt_topic)
122+
client.unsubscribe(mqtt_topic)
123+
124+
print('Disconnecting from %s'%client.broker)
125+
client.disconnect()

0 commit comments

Comments
 (0)