Dear Community,
I m a newbie and would like to implement an anemometer and BME280 sensor to my weather station project.
Regarding the anemometer, the code I show here is from our colleagues of projects.raspberrypi.org. In this approach, the Rpi detects via GPIO pin (the anemometer has 2 wires, one goes to GPIO and the other to GND), how many times the reed switch within the anemometer is triggered (twice for a complete rotation) and then, by counting and timing these signals, the speed of the wind is calculated. The gpiozero library is used in this project. The code is attached below.
I beg for a good samaritan to give me a hand with this code to adapt it to MQTT and be compatible with cayenne. Any other way to find a solution is welcomeâŚ(e.g. use other gpio library and end with gpio.cleanup, etc).
âŚI also beg for the same or another good samaritan, to share a code to connect BME280 (temp, humidity and pressure sensor) via MQTT protocol to cayenneâŚ
Thanks Shramik, but I need some more detail to adapt and send data via MQTT to cayenne, because I don t see clearly how is done with the examples provided in those links.
You have to use client.virtualWrite(1, x) where 1 is the channel and x is the data to be sent. so in the cayenne example, you have to add anemometer and BME280 code in it.
Thanks again Shramik.
Using the example file now I get the read of the anemometer in cayenne dashboard every 10 seconds BUT first read is 0 (0 secs), second read (after 10 secs) is an individual real read (the one I want), BUT third value (after 20 secs of running) is the result obtained in second read added to the current oneâŚand so on in the following reads.
Please, I don t see the modifications I need to do in the script to get every 10 secs an invidual result and not a summatory function I am getting now instead.
Thanks again,
Oscar
Sure! I really appreciate your interest Shramik. The problem I see in the code might be to establish a fixed interval to count the number of spins of the anemometer (and then do the calculations to get the speed), but in true, I m lostâŚ
#!/usr/bin/env python
import cayenne.client
from gpiozero import Button
import time
import logging
import math
MQTT_USERNAME = ""
MQTT_PASSWORD = ""
MQTT_CLIENT_ID = ""
wind_count = 0 # counts how many half-rotations
radius_cm = 9.0 # radius in cm of the anemometer
wind_interval = 5 # how often (secs) to report speed
ADJUSTMENT = 1.18 # adjustment of the anemometer given by the manufacturer
# The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# every half-rotation, add 1 count
def spin():
global wind_count
wind_count = wind_count + 1
# print("spin" + str(wind_count))
# calculate the wind speed
def calculate_speed(time_sec):
global wind_count
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_km = (circumference_cm * rotations) / 100000.0 # divided by cm in a km
km_per_sec = dist_km / time_sec
km_per_hour = km_per_sec * 3600 # multiplied by secs in an hour
return km_per_hour * ADJUSTMENT
def reset_wind():
global wind_count
wind_count = 0
# recommended function to reset the wind_count to zero when assembling the weather station
wind_speed_sensor = Button(5)
wind_speed_sensor.when_pressed = spin
i=0
timestamp = 0
# loop to measure wind speed and report at 5 secs interval
while True:
client.loop()
if (time.time() > timestamp + 10):
wind_count = 0
x = calculate_speed(wind_interval)
client.virtualWrite(1,x)
print(x)
timestamp = time.time()
I say in advance that every time I test your new code, I run the anemometer.txt that I know it works before just to check that everything is well connected.