#!/usr/bin/env python import cayenne.client from gpiozero import Button import time import math import logging # Cayenne authentication info. This should be obtained from the Cayenne Dashboard. MQTT_USERNAME = "xxxx" MQTT_PASSWORD = "xxxx" MQTT_CLIENT_ID = "xxxx" client = cayenne.client.CayenneMQTTClient() client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883) # For a secure connection use port 8883 when calling client.begin: # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO) wind_count = 0 # counts how many half-rotations radius_cm = 9.0 # radius in cm of the anemometer wind_interval = 10 # how often (secs) to report speed ADJUSTMENT = 1.18 # adjustment of the anemometer given by the manufacturer timestamp = 0 # 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 wind_speed_sensor = Button(5) wind_speed_sensor.when_pressed = spin while True: client.loop() if (time.time() > timestamp + 10): print(calculate_speed(wind_interval), "km/h") x = calculate_speed(wind_interval) client.windspeedWrite(1,x) timestamp = time.time()