
Well, the rain gauge has been working perfectly. Time to get some more data about the world around me.. I was reading about the BME280 sensor which provides accurate temperature, humidity and barometric pressure readings. I thought it might be a good choice for the next upgrade to my weather station. So, here we go.
I purchased the sensor, it was about $8 on Amazon. Connected it up in my office to a Raspberry Pi Pico with a LoRa RFM95 connected. See my previous posts about how I did that. Loaded Micropython on the Raspberry Pi Pico and installed the BME280 library. Simple..

Started taking readings with some test code and it was working very well. The questions was, how do I take readings and also wait for the rain gauge trigger? hmmm. After some research, I found that I could use an interrupt from the pin connected to the rain gauge reed switch to kick off the function to send the rain data. Of course, now that I was sending rain data and environmental data I needed to separate them so the data could be parsed easily on the receiving side. JSON to the rescue! I created a message type which included; rain, temp, humidity, and pressure. When sending the data, I send a type and the data. The receiving end parses the data by the type and publishes to the proper MQTT topic and/or writes it to the database for historical data retention. (So I can see how much it’s rained in the last day, week, month, year..) Or, how the temp dropped overnight etc.
Now that I had the code working, I needed a nice housing. I looked to see if someone had already made a solar radiation shield for the BME280 but didn’t find anything. Time to design something in Fusion 360. I designed a small solar radiation shield that I could mount to the fence, close to the rain gauge. I would run a cable from the new solar shield to the rain gauge as that is where the Raspberry Pi Pico was already mounted. It turned out better than expected. I used M4 threaded rods to hold the layers together and M3 bold and nuts to hold the mount parts together. The shield was printed using white PETG filament as it’s better in direct sunlight and heat.

After hours of printing it was time to get the code updated on the existing Raspberry Pi Pico and make the connections. Here is the Micropython code I used for the Raspberry Pi Pico.
import time
from ulora import LoRa, ModemConfig, SPIConfig
from machine import Pin, I2C
import bme280_float as BME280
import ujson
i2c = I2C(id=0, scl=Pin(5), sda=Pin(4), freq=10000)
# Lora Parameters
RFM95_RST = 9
RFM95_SPIBUS = SPIConfig.rp2_0
RFM95_CS = 8
RFM95_INT = 10
RF95_FREQ = 915.0
RF95_POW = 20
CLIENT_ADDRESS = 6
SERVER_ADDRESS = 2
# LED Setup
led = machine.Pin(13, machine.Pin.OUT)
led2 = machine.Pin(12, machine.Pin.OUT)
trigger_pin = machine.Pin(7, Pin.IN, Pin.PULL_UP)
last_trigger_time = 0
debounce_delay = 1000
# initialise radio
lora = LoRa(RFM95_SPIBUS, RFM95_INT, CLIENT_ADDRESS, RFM95_CS, reset_pin=RFM95_RST, freq=RF95_FREQ, tx_power=RF95_POW, acks=True)
def send_temp():
# Initialize BME280 sensor
bme = BME280.BME280(i2c=i2c)
# Read sensor data
tempC = bme.temperature
hum = bme.humidity
pres = bme.pressure
# Convert temperature to fahrenheit
tempF = (bme.read_temperature()/100) * (9/5) + 32
tempF = str(round(tempF, 2))
message_type = "env"
readings = {"type": message_type, "temperature": tempF, "humidity": hum, "pressure": pres}
message = ujson.dumps(readings)
print(message)
lora.send_to_wait(str(message), SERVER_ADDRESS)
def rain_trigger(pin):
led.on()
time.sleep(0.2)
led.off()
global last_trigger_time
message_type = "rain"
current_time = time.ticks_ms()
readings = {"type": message_type, "tip": "1"}
message = ujson.dumps(readings)
if time.ticks_diff(current_time, last_trigger_time) > debounce_delay:
last_trigger_time = current_time
lora.send_to_wait(message, SERVER_ADDRESS)
print(message)
def main():
trigger_pin.irq(trigger=machine.Pin.IRQ_FALLING, handler=rain_trigger)
time.sleep(1)
while True:
send_temp()
time.sleep(900)
if __name__ == "__main__":
main()
If my wife has been following along, which I know she hasn’t, she would know that I am using the “client address” to separate the communication from several LoRa devices on the “server” (receiver) side. So whenever the receiver identifies the client as client 6 in this case, it will run that info through a series of if statements which will pull out the proper type and publish to the correct location, either MQTT or write to the database or both. Here is the snippet from that Python code for this sensor. Note the use of the type to separate the rain gauge data from the environmental data..
if payload.header_from == 6:
logging.info("Recieved by: {0} Message: {1}".format(payload.header_from, payload.message))
decoded_string = payload.message.decode('utf-8')
local_pressure = 29.3
data_dict = json.loads(decoded_string)
if data_dict["type"] == "env":
temp = data_dict['temperature']
humidity = data_dict['humidity']
pressure = float(data_dict['pressure'])*0.0002952998751*100 # Convert from Pascals to Inces of Mercury
pressure_diff = round(pressure, 2)-local_pressure
pressure = pressure+pressure_diff
#pressure = data_dict['pressure']
print(pressure)
client.publish('homemade/BME280/pressure', pressure)
client.publish('homemade/BME280/tempurature', temp)
client.publish('homemade/BME280/humidity', humidity)
conn = connect_to_db()
cur = conn.cursor()
insert_data = "INSERT INTO outside_readings(temp, humidity, pressure) VALUES (%s,%s,%s)"
data_trup = (temp, humidity, pressure)
cur.execute(insert_data, data_trup)
conn.commit()
conn.close()
return None
if data_dict["type"] == "rain":
tips = list(data_dict['tip'])
conn = connect_to_db()
cur = conn.cursor()
insert_state = "INSERT INTO Rain_Data (tips) VALUES (?)"
cur.execute(insert_state, tips)
conn.commit()
conn.close()
return None
Here is the sensor all mounted up:

I started pulling in the data to Home Assistant and I found that the sensor reported pressure in Pascals which I wanted to convert to Inches of Mercury. I found that conversion and included it in my receiving side code. It does the conversion and sends out the data in inHG. I am noticing some abnormalities. Lower temps in the evening than reality… So, I will have to investigate that further but I am really happy with the project thus far.
This is what the data looks like in Home Assistant:



If you would like the STL files for the rain gauge or code, hit me up.
Happy Making Makers!