I have a pretty good weather station with a solar panel, solar charger, a RPi PICO, and BME280 breakout as send unit, and an RPi-4 as reciever with rfm9x bonnet.
The send code has been pretty much debugged and error trapped. The receiving code, however occasionally crashes. Now, I do not care if I miss a reading but, I would like the receiving program to not crash. While I do not have the exact error message, it is essentially an error translating the bytearray into utf-8 as per message.
Here is receiving code:
Code: Select all
# SPDX-FileCopyrightText: 2020 Jerry Needell for Adafruit Industries
# SPDX-License-Identifier: MIT
# modified by Roger Ayotte 11/12/2021
# Example to receive addressed packed with ACK and send a response
import time
import board
import busio
import digitalio
import adafruit_rfm9x
from gpiozero import PingServer
adafruit=PingServer('adafruit.com', event_delay=20.0)
# Define radio parameters.
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your
# module! Can be a value like 915.0, 433.0, etc.
# Define pins connected to the chip.
# set GPIO pins as necessary - this example is for Raspberry Pi
CS = digitalio.DigitalInOut(board.CE1)
RESET = digitalio.DigitalInOut(board.D25)
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
#rfm69.encryption_key = (
# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
#)
# set delay before transmitting ACK (seconds)
rfm9x.ack_delay = 0.01
# set node addresses
rfm9x.node = 2
rfm9x.destination = 1
# initialize counter
counter = 0
ack_failed_counter = 0
from Adafruit_IO import Client, Feed, Data, RequestError
import datetime
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'aio_Fzve6425emwZSbMDz9nTeAvjI1dj'
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'Rcayot'
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Set up Adafruit IO Feeds.
# first test if internet is reachable
if adafruit.is_active:
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
pressure_feed = aio.feeds('pressure')
voltage_feed = aio.feeds('voltage')
else:
print("wifi not ready")
# Wait to receive packets.
print("Waiting for packets...")
while True:
# Look for a new packet: only accept if addresses to my_node
packet = rfm9x.receive(with_ack=True, with_header=True)
# If no packet was received during the timeout then None is returned.
if packet is not None:
flag=packet[3]
print(flag)
print("Received RSSI: {0}".format(rfm9x.last_rssi))
if flag==1:
temperature = str(packet[4:], "utf-8")
temperature = (float(temperature)*1.8 + 32)
temperature=round(temperature, 2)
print("temperature = ", temperature, "F")
if adafruit.is_active:
aio.send(temperature_feed.key, str(temperature))
else:
print("wifi not ready")
if flag==2:
pressure = str(packet[4:], "utf-8")
print("pressure = ", pressure, "mB")
if adafruit.is_active:
aio.send(pressure_feed.key, str(pressure))
else:
print("wifi not ready")
if flag==3:
humidity = str(packet[4:], "utf-8")
print("humidity = ", humidity, "%")
if adafruit.is_active:
aio.send(humidity_feed.key, str(humidity))
else:
print("wifi not ready")
if flag==4:
voltage = str(packet[4:], "utf-8")
voltage = (float(voltage)*(3.3*3)/65535)
voltage=round(voltage, 2)
print("voltage = ", voltage)
if adafruit.is_active:
aio.send(voltage_feed.key, str(voltage))
else:
print("wifi not ready")
counter += 1