Skip to content

Commit b3bc7db

Browse files
author
brentru
committed
redo example!
1 parent 9cdcacd commit b3bc7db

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

examples/minimqtt_adafruitio_wifi.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,44 @@
99
from adafruit_esp32spi import adafruit_esp32spi
1010
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
12-
from adafruit_minimqtt import MQTT
1312

13+
import adafruit_minimqtt as MQTT
1414

15-
# Get Adafruit IO details and more from a secrets.py file
15+
### WiFi ###
16+
17+
# Get wifi details and more from a secrets.py file
1618
try:
1719
from secrets import secrets
1820
except ImportError:
19-
print("Adafruit IO secrets are kept in secrets.py, please add them there!")
21+
print("WiFi secrets are kept in secrets.py, please add them there!")
2022
raise
2123

22-
cs = DigitalInOut(board.D10)
23-
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
24-
25-
# Initialize ethernet interface with DHCP
26-
eth = WIZNET5K(spi_bus, cs)
24+
# If you are using a board with pre-defined ESP32 Pins:
25+
esp32_cs = DigitalInOut(board.ESP_CS)
26+
esp32_ready = DigitalInOut(board.ESP_BUSY)
27+
esp32_reset = DigitalInOut(board.ESP_RESET)
28+
29+
# If you have an externally connected ESP32:
30+
# esp32_cs = DigitalInOut(board.D9)
31+
# esp32_ready = DigitalInOut(board.D10)
32+
# esp32_reset = DigitalInOut(board.D5)
33+
34+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
35+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=True)
36+
"""Use below for Most Boards"""
37+
status_light = neopixel.NeoPixel(
38+
board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
39+
"""Uncomment below for ItsyBitsy M4"""
40+
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
41+
# Uncomment below for an externally defined RGB LED
42+
# import adafruit_rgbled
43+
# from adafruit_esp32spi import PWMOut
44+
# RED_LED = PWMOut.PWMOut(esp, 26)
45+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
46+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
47+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
48+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
49+
esp, secrets, status_light)
2750

2851
### Feeds ###
2952

@@ -56,15 +79,18 @@ def message(client, topic, message):
5679
print('New message on topic {0}: {1}'.format(topic, message))
5780

5881

59-
# Initialize MQTT interface with the ethernet interface
60-
MQTT.set_socket
82+
# Connect to WiFi
83+
print("Connecting to WiFi...")
84+
wifi.connect()
85+
print("Connected!")
86+
87+
# Initialize MQTT interface with the esp interface
88+
MQTT.set_socket(socket, esp)
6189

6290
# Set up a MiniMQTT Client
63-
# NOTE: We'll need to connect insecurely for ethernet configurations.
64-
mqtt_client = MQTT.MQTT(broker = 'io.adafruit.com',
65-
username = secrets['aio_username'],
66-
password = secrets['aio_key'],
67-
is_ssl = False)
91+
mqtt_client = MQTT.MQTT(broker='http://io.adafruit.com',
92+
username=secrets['aio_username'],
93+
password=secrets['aio_key'])
6894

6995
# Setup the callback methods above
7096
mqtt_client.on_connect = connected
@@ -85,4 +111,4 @@ def message(client, topic, message):
85111
mqtt_client.publish(photocell_feed, photocell_val)
86112
print('Sent!')
87113
photocell_val += 1
88-
time.sleep(1)
114+
time.sleep(5)

0 commit comments

Comments
 (0)