|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +import digitalio |
| 5 | +from adafruit_fona.adafruit_fona import FONA |
| 6 | +import adafruit_fona.adafruit_fona_socket as socket |
| 7 | + |
| 8 | +import adafruit_minimqtt as MQTT |
| 9 | + |
| 10 | +# Get Adafruit IO details and more from a secrets.py file |
| 11 | +try: |
| 12 | + from secrets import secrets |
| 13 | +except ImportError: |
| 14 | + print("GPRS secrets are kept in secrets.py, please add them there!") |
| 15 | + raise |
| 16 | + |
| 17 | +### Cellular ### |
| 18 | + |
| 19 | +# Create a serial connection for the FONA connection using 4800 baud. |
| 20 | +# These are the defaults you should use for the FONA Shield. |
| 21 | +# For other boards set RX = GPS module TX, and TX = GPS module RX pins. |
| 22 | +uart = busio.UART(board.TX, board.RX, baudrate=4800) |
| 23 | +rst = digitalio.DigitalInOut(board.D4) |
| 24 | + |
| 25 | +### Feeds ### |
| 26 | + |
| 27 | +# Setup a feed named 'photocell' for publishing to a feed |
| 28 | +photocell_feed = secrets["aio_username"] + "/feeds/photocell" |
| 29 | + |
| 30 | +# Setup a feed named 'onoff' for subscribing to changes |
| 31 | +onoff_feed = secrets["aio_username"] + "/feeds/onoff" |
| 32 | + |
| 33 | +### Code ### |
| 34 | + |
| 35 | +# Define callback methods which are called when events occur |
| 36 | +# pylint: disable=unused-argument, redefined-outer-name |
| 37 | +def connected(client, userdata, flags, rc): |
| 38 | + # This function will be called when the client is connected |
| 39 | + # successfully to the broker. |
| 40 | + print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) |
| 41 | + # Subscribe to all changes on the onoff_feed. |
| 42 | + client.subscribe(onoff_feed) |
| 43 | + |
| 44 | + |
| 45 | +def disconnected(client, userdata, rc): |
| 46 | + # This method is called when the client is disconnected |
| 47 | + print("Disconnected from Adafruit IO!") |
| 48 | + |
| 49 | + |
| 50 | +def message(client, topic, message): |
| 51 | + # This method is called when a topic the client is subscribed to |
| 52 | + # has a new message. |
| 53 | + print("New message on topic {0}: {1}".format(topic, message)) |
| 54 | + |
| 55 | + |
| 56 | +# Connect to Cellular Network |
| 57 | +print("Initializing FONA (this may take a few seconds)") |
| 58 | +fona = FONA(uart, rst, debug=True) |
| 59 | + |
| 60 | +# Enable GPS |
| 61 | +fona.gps = True |
| 62 | + |
| 63 | +# Bring up cellular connection |
| 64 | +fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) |
| 65 | + |
| 66 | +# Bring up GPRS |
| 67 | +fona.gprs = True |
| 68 | +print("FONA initialized") |
| 69 | + |
| 70 | +# Initialize MQTT interface with the cellular interface |
| 71 | +MQTT.set_socket(socket, fona) |
| 72 | + |
| 73 | +# Set up a MiniMQTT Client |
| 74 | +# NOTE: We'll need to connect insecurely for ethernet configurations. |
| 75 | +mqtt_client = MQTT.MQTT( |
| 76 | + broker="http://io.adafruit.com", |
| 77 | + username=secrets["aio_username"], |
| 78 | + password=secrets["aio_key"], |
| 79 | +) |
| 80 | + |
| 81 | +# Setup the callback methods above |
| 82 | +mqtt_client.on_connect = connected |
| 83 | +mqtt_client.on_disconnect = disconnected |
| 84 | +mqtt_client.on_message = message |
| 85 | + |
| 86 | + |
| 87 | +# Connect the client to the MQTT broker. |
| 88 | +print("Connecting to Adafruit IO...") |
| 89 | +mqtt_client.connect() |
| 90 | + |
| 91 | +photocell_val = 0 |
| 92 | +while True: |
| 93 | + # Poll the message queue |
| 94 | + mqtt_client.loop() |
| 95 | + |
| 96 | + # Send a new message |
| 97 | + print("Sending photocell value: %d..." % photocell_val) |
| 98 | + mqtt_client.publish(photocell_feed, photocell_val) |
| 99 | + print("Sent!") |
| 100 | + photocell_val += 1 |
| 101 | + time.sleep(5) |
0 commit comments