11# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22# SPDX-License-Identifier: MIT
33
4+ from os import getenv
45import time
56import board
67import busio
1314import adafruit_minimqtt .adafruit_minimqtt as MQTT
1415from adafruit_gc_iot_core import Cloud_Core , MQTT_API
1516
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" )
1726
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 ###
2428
2529# If you are using a board with pre-defined ESP32 Pins:
2630esp32_cs = DigitalInOut (board .ESP_CS )
3539spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
3640esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
3741"""Use below for Most Boards"""
38- status_light = neopixel .NeoPixel (
42+ status_pixel = neopixel .NeoPixel (
3943 board .NEOPIXEL , 1 , brightness = 0.2
4044) # Uncomment for Most Boards
4145"""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)
4347# Uncomment below for an externally defined RGB LED
4448# import adafruit_rgbled
4549# from adafruit_esp32spi import PWMOut
4650# RED_LED = PWMOut.PWMOut(esp, 26)
4751# GREEN_LED = PWMOut.PWMOut(esp, 27)
4852# 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+ )
5157
5258### Code ###
5359
@@ -101,7 +107,14 @@ def message(client, topic, msg):
101107ssl_context = adafruit_connection_manager .get_radio_ssl_context (esp )
102108
103109# 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 )
105118
106119# Optional JSON-Web-Token (JWT) Generation
107120# print("Generating JWT...")
@@ -112,7 +125,7 @@ def message(client, topic, msg):
112125client = MQTT .MQTT (
113126 broker = google_iot .broker ,
114127 username = google_iot .username ,
115- password = secrets [ " jwt" ] ,
128+ password = jwt ,
116129 client_id = google_iot .cid ,
117130 socket_pool = pool ,
118131 ssl_context = ssl_context ,
@@ -129,7 +142,8 @@ def message(client, topic, msg):
129142google_mqtt .on_publish = publish
130143google_mqtt .on_message = message
131144
132- print ("Attempting to connect to %s" % client .broker )
145+
146+ print (f"Attempting to connect to { client .broker } " )
133147google_mqtt .connect ()
134148
135149# Pump the message loop forever, all events are
0 commit comments