1
1
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2
2
# SPDX-License-Identifier: MIT
3
3
4
+ from os import getenv
4
5
import time
5
6
import board
6
7
import busio
13
14
import adafruit_minimqtt .adafruit_minimqtt as MQTT
14
15
from adafruit_gc_iot_core import Cloud_Core , MQTT_API
15
16
16
- ### WiFi ###
17
+ # Get WiFi details and Google Cloud keys, ensure these are setup in settings.toml
18
+ ssid = getenv ("CIRCUITPY_WIFI_SSID" )
19
+ password = getenv ("CIRCUITPY_WIFI_PASSWORD" )
20
+ cloud_region = getenv ("cloud_region" )
21
+ device_id = getenv ("device_id" )
22
+ private_key = getenv ("private_key" )
23
+ project_id = getenv ("project_id" )
24
+ registry_id = getenv ("registry_id" )
25
+ jwt = getenv ("jwt" )
17
26
18
- # Get wifi details and more from a secrets.py file
19
- try :
20
- from secrets import secrets
21
- except ImportError :
22
- print ("WiFi secrets are kept in secrets.py, please add them there!" )
23
- raise
27
+ ### WiFi ###
24
28
25
29
# If you are using a board with pre-defined ESP32 Pins:
26
30
esp32_cs = DigitalInOut (board .ESP_CS )
35
39
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
36
40
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
37
41
"""Use below for Most Boards"""
38
- status_light = neopixel .NeoPixel (
42
+ status_pixel = neopixel .NeoPixel (
39
43
board .NEOPIXEL , 1 , brightness = 0.2
40
44
) # Uncomment for Most Boards
41
45
"""Uncomment below for ItsyBitsy M4"""
42
- # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
46
+ # status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
43
47
# Uncomment below for an externally defined RGB LED
44
48
# import adafruit_rgbled
45
49
# from adafruit_esp32spi import PWMOut
46
50
# RED_LED = PWMOut.PWMOut(esp, 26)
47
51
# GREEN_LED = PWMOut.PWMOut(esp, 27)
48
52
# BLUE_LED = PWMOut.PWMOut(esp, 25)
49
- # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50
- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
53
+ # status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
54
+ wifi = adafruit_esp32spi_wifimanager .WiFiManager (
55
+ esp , ssid , password , status_pixel = status_pixel
56
+ )
51
57
52
58
### Code ###
53
59
@@ -101,7 +107,14 @@ def message(client, topic, msg):
101
107
ssl_context = adafruit_connection_manager .get_radio_ssl_context (esp )
102
108
103
109
# Initialize Google Cloud IoT Core interface
104
- google_iot = Cloud_Core (esp , secrets )
110
+ settings = {
111
+ cloud_region : cloud_region ,
112
+ device_id : device_id ,
113
+ private_key : private_key ,
114
+ project_id : project_id ,
115
+ registry_id : registry_id ,
116
+ }
117
+ google_iot = Cloud_Core (esp , settings )
105
118
106
119
# Optional JSON-Web-Token (JWT) Generation
107
120
# print("Generating JWT...")
@@ -112,7 +125,7 @@ def message(client, topic, msg):
112
125
client = MQTT .MQTT (
113
126
broker = google_iot .broker ,
114
127
username = google_iot .username ,
115
- password = secrets [ " jwt" ] ,
128
+ password = jwt ,
116
129
client_id = google_iot .cid ,
117
130
socket_pool = pool ,
118
131
ssl_context = ssl_context ,
@@ -129,7 +142,8 @@ def message(client, topic, msg):
129
142
google_mqtt .on_publish = publish
130
143
google_mqtt .on_message = message
131
144
132
- print ("Attempting to connect to %s" % client .broker )
145
+
146
+ print (f"Attempting to connect to { client .broker } " )
133
147
google_mqtt .connect ()
134
148
135
149
# Pump the message loop forever, all events are
0 commit comments