-
Notifications
You must be signed in to change notification settings - Fork 10
Remove forced dep on ntp #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@brentru Alright, I think this is ready for review? I've managed to resolve all of the import and runtime issues (as it relates trying to use the GCP IOT Bridge (MQTT), but now it just hangs forever "connecting" :S I'll share my code as well, but we should resolve this issue before merging |
On my FeatherS2, this code executes without error on the Enabling logging has also proven difficult as the Notes:
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
2021/10/12 15:31:23 INFO : Connecting to The Promised LAN
2021/10/12 15:31:26 INFO : Connected to The Promised LAN
2021/10/12 15:31:26 INFO : Setting NTP
2021/10/12 15:31:27 INFO : Generating JWT - this will take a minute...
2021/10/12 15:31:54 DEBUG : Your JWT is: eyJ0.....VQ==
2021/10/12 15:31:54 INFO : Attempting to connect to mqtt.googleapis.com
2021/10/12 15:31:54 DEBUG : Attempting to establish MQTT connection...
2021/10/12 15:31:54 INFO : Establishing a SECURE SSL connection to mqtt.googleapis.com:8883
2021/10/12 15:31:56 DEBUG : Sending CONNECT to broker...
2021/10/12 15:31:56 DEBUG : Fixed Header: bytearray(b'\x10\xcc\x04\x00')
Variable Header: bytearray(b'\x04MQTT\x04\xc2\x00<')
2021/10/12 15:31:56 DEBUG : Receiving CONNACK packet from broker # main.py
import json
import socketpool
import wifi
import ssl
from settings import secrets
from arcti.ntp import NTP
from arcti.logging import Logger
from adafruit_gc_iot_core import MQTT_API, Cloud_Core
import adafruit_minimqtt.adafruit_minimqtt as MQTT
logger = Logger()
logger.info(f"Connecting to {secrets['WIFI_SSID']}")
wifi.radio.connect(secrets["WIFI_SSID"], secrets["WIFI_SSID_PASSWORD"])
logger.info(f"Connected to {secrets['WIFI_SSID']}")
logger.info("Setting NTP")
n = NTP()
n.update_rtc()
socketPool = socketpool.SocketPool(wifi.radio)
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connect(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
logger.info('Connected to Google Cloud IoT!')
logger.debug(f'Flags: {flags}\nRC: {rc}')
# Subscribes to commands/# topic
google_mqtt.subscribe_to_all_commands()
def disconnect(client, userdata, rc):
# This method is called when the client disconnects
# from the broker.
logger.warn('Disconnected from Google Cloud IoT!')
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new topic.
logger.info(f'Subscribed to {topic} with QOS level {granted_qos}')
def unsubscribe(client, userdata, topic, pid):
# This method is called when the client unsubscribes from a topic.
logger.info(f'Unsubscribed from {topic} with PID {pid}')
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a topic.
logger.info(f'Published to {topic} with PID {pid}')
def message(client, topic, msg):
# This method is called when the client receives data from a topic.
try:
# Attempt to decode a JSON command
msg_dict = json.loads(msg)
logger.debug(msg_dict)
except TypeError:
# Non-JSON command, print normally
logger.error(f"Message from {topic}: {msg}")
# Initialize Google Cloud IoT Core interface
google_iot = Cloud_Core(secrets={
"project_id": secrets.get('GCS_PROJECT_ID'),
"cloud_region": secrets.get('GCS_PROJECT_REGION'),
"registry_id": secrets.get('GCS_IOT_REGISTRY'),
"device_id": secrets.get('GCS_IOT_DEVICE'),
"private_key": secrets.get('GCS_PRIVATE_KEY')
})
# JSON-Web-Token (JWT) Generation
logger.info("Generating JWT - this will take a minute... ")
jwt = google_iot.generate_jwt()
logger.debug(f"Your JWT is: {jwt}")
# Set up a new MiniMQTT Client
client = MQTT.MQTT(broker=google_iot.broker,
# username=google_iot.username,
username="ItDoesntMatter",
password=jwt,
client_id=google_iot.cid,
socket_pool=socketPool,
ssl_context=ssl.create_default_context())
client.enable_logger(logger)
# Initialize Google MQTT API Client
google_mqtt = MQTT_API(client)
# Connect callback handlers to Google MQTT Client
google_mqtt.on_connect = connect
google_mqtt.on_disconnect = disconnect
google_mqtt.on_subscribe = subscribe
google_mqtt.on_unsubscribe = unsubscribe
google_mqtt.on_publish = publish
google_mqtt.on_message = message
logger.info(f'Attempting to connect to {client.broker}')
google_mqtt.connect()
logger.info('Connected to IOT Core! :D') # Pertinent settings.py values
# GCS Creds
"GCS_PROJECT_ID": " project-id",
"GCS_PROJECT_REGION": "us-east1",
"GCS_IOT_REGISTRY": "registry_name",
"GCS_IOT_DEVICE": "iot_core_device_name",
"GCS_PRIVATE_KEY": (...) |
Oh, there's one extra change that needs to be made to make this quickly/easily work diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py
index 6ca9b05..7e2e371 100755
--- a/adafruit_minimqtt/adafruit_minimqtt.py
+++ b/adafruit_minimqtt/adafruit_minimqtt.py
@@ -484,7 +484,7 @@ class MQTT:
if self.logger:
self.logger.debug("Sending CONNECT to broker...")
self.logger.debug(
- "Fixed Header: %s\nVariable Header: %s", fixed_header, var_header
+ f"Fixed Header: {fixed_header}\nVariable Header: {var_header}"
)
self._sock.send(fixed_header)
self._sock.send(var_header) |
|
@brentru do you think this is good to merge now? |
@FoamyGuy lgtm! |
Updating https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core to 3.1.1 from 3.0.5: > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#21 from FoamyGuy/fix_format > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#20 from geudrik/fix_ntp_requirement > Fixed readthedocs build > Post-patch cleanup > Consolidate Documentation sections of README > Consolidate Documentation sections of README Updating https://github.com/adafruit/Adafruit_CircuitPython_turtle to 2.2.4 from 2.2.3: > Merge pull request adafruit/Adafruit_CircuitPython_turtle#25 from lesamouraipourpre/ondiskbitmap-changes > Fixed readthedocs build > Consolidate Documentation sections of README
Removes a forced dep of NTP - it isn't necessary. Boards like the FeatherS2 don't natively support the NTP lib provided in Circuit Python (a better answer for NTP on the FeatherS2 is here)
Note that users wanting to take advantage of these changes will likely want this PR too