def get_values(self): store_speeds = [] store_directions = [] start_time = time.time() try: while time.time() - start_time <= self.interval: wind_start_time = time.time() self.reset_wind() #time.sleep(wind_interval) while time.time() - wind_start_time <= self.wind_interval: store_directions.append(wind_direction_byo.get_value()) final_speed = self.calculate_speed(self.wind_interval) store_speeds.append(final_speed) wind_average = wind_direction_byo.get_average(store_directions) except ZeroDivisionError: logerr("there was a division by zero error calculating wind data") wind_average = None wind_gust = max(store_speeds) wind_speed = numpy.mean(store_speeds) rainfall = self.tip_count * self.bucket_size self.reset_bucket() humidity, pressure, ambient_temperature = None, None, None try: humidity, pressure, ambient_temperature = bme280_sensor.read_all() except OSError: logerr("something went wrong communicating with bme280 sensor") bme280_sensor.reset_sensor() except IOError: logerr("something went wrong communicating with bme280 sensor") bme280_sensor.reset_sensor() ground_temperature = self.ground_thermometer.read_temp() data = {} data['outTemp'] = ambient_temperature data['outHumidity'] = humidity data['pressure'] = pressure data['soilTemp1'] = ground_temperature data['windSpeed'] = wind_speed data['windGust'] = wind_gust data['windDir'] = wind_average data['rain'] = rainfall return data
return slp else: return False def pressure_inHg(pressure_hPa): return pressure_hPa * 0.02953 def altitude_ft(pressure_hPa, slPressure_hPa): alt_ft = (1.0-(pressure_hPa/slPressure_hPa)**0.190284)*145366.45 return round(alt_ft, 2) def dewPoint_c(humidity_pcnt, temp_c): dewpc = 243.04*((math.log(humidity_pcnt/100))+((17.625*temp_c)/(243.04+temp_c)))/(17.625-(math.log(humidity_pcnt/100))-((17.625*temp_c)/(243.04+temp_c))) return round(dewpc, 2) def c2f(tempc): tempf = round((9.0 / 5.0 * tempc + 32.0), 2) return tempf slp = seaLevelPressure_hPa('kmdt') humidity, pressure_hPa, ambient_tempc = bme280_sensor.read_all() dpc=dewPoint_c(humidity,ambient_tempc) elevation_ft = altitude_ft(pressure_hPa,slp) phg=pressure_inHg(pressure_hPa) phg=round(phg,2) pressure_hPa=round(pressure_hPa,2) ambient_tempc=round(ambient_tempc,2) humidity=round(humidity,2) print('elevation: %s ft\tbarometer: %s hPa, %s inHg\ttemp: %s c, %s F\thumidity: %s %%\tdewpoint: %s c, %s F' % (elevation_ft,pressure_hPa,phg,ambient_tempc,c2f(ambient_tempc),humidity,dpc,c2f(dpc)))
# loop to measure wind speed # report at 5 second intervals while True: start_time = time.time() while time.time() - start_time <= interval: wind_start_time = time.time() reset_wind() #time.sleep(wind_interval) while time.time() - wind_start_time <= wind_interval: store_directions.append(wind_direction_byo.get_value()) final_speed = calculate_speed(wind_interval) store_speeds.append(final_speed) humidity, pressure, temperature = bme280_sensor.read_all() rainfall = rain_count * BUCKET_SIZE reset_rainfall() wind_direction_avg = wind_direction_byo.get_average(store_directions) wind_speed_max = max(store_speeds) # wind gust wind_speed_last = store_speeds[-1] wind_speed_avg = statistics.mean(store_speeds) altitude, azimuth, radiation = sr.get_solar_details( solar_latitude, solar_longitude) # group temperature temp_ground = ground_temp_probe.read_temp() readings_dict = {
rain_sensor.when_pressed = bucket_tipped # Ground temp temp_probe = ground_temp.DS18B20() while True: start_time = time.time() while time.time() - start_time <= interval: wind_start_time = time.time() final_speed = calculate_speed(wind_interval) store_speeds.append(final_speed) while time.time() - wind_start_time <= wind_interval: store_directions.append(wind_direction_byo.get_value()) # Humidity, pressure, temperature ambient_temp, humidity, pressure = bme280_sensor.read_all() # Soil temp soil_temp = temp_probe.read_temp() # Wind speed and gust wind_gust = max(store_speeds) wind_speed = statistics.mean(store_speeds) reset_wind() store_speeds = [] # Wind direction wind_average_degrees = wind_direction_byo.get_average(store_directions) wind_average_direction = wind_direction_byo.deg_to_compass( wind_average_degrees) store_directions = []
wind_average = round(wind_direction.get_average(store_directions), 2) # direction in degrees if wind_average >= 360: wind_average -= 360 wind_gust = round(max(store_speeds), 2) # mph wind_speed = round(statistics.mean(store_speeds), 2) # mph rainfall = rain_count * BUCKET_SIZE # mm rain # reset rain and wind reset_rainfall() store_speeds = [] store_directions = [] ground_temp = temp_probe.read_temp() # deg Celsius humidity, pressure, ambient_temp = bme280_sensor.read_all( ) # ambient temp in deg Celsius wchill = weather_math.wind_chill(c2f(ambient_temp), wind_speed) # deg fahrenheit hidx = weather_math.heat_index(ambient_temp, humidity) # deg Celsius dewpt_c = weather_math.dewPoint_c(humidity, ambient_temp) # deg Celsius rpi_temp = round(cpu_temp(), 2) # RPi cpu temp in Fahrenheit ## print data to console ## w_dir w_spd w_gust wchill heatidx dewpt rain hum prsr a_temp g_temp time # print('{} {:^6} {:^5} {:^6} {:^6} {:^6} {:^6} {:^4} {:^6} {:^7} {:^6} {:^5} {:^6}'.format(time.strftime("%m/%d/%y, %H:%M:%S"), wind_average, wind_speed, wind_gust, wchill, c2f(hidx), c2f(dewpt_c), round(mm2in(rainfall), 2), humidity, pressure, c2f(ambient_temp), c2f(ground_temp), rpi_temp)) ## save data to db db.insert(c2f(ambient_temp), c2f(ground_temp), 0, pressure, humidity, wind_average, wind_speed, wind_gust, wchill, c2f(hidx), c2f(dewpt_c), round(mm2in(rainfall), 2), rpi_temp) #, time.strftime("%Y-%m-%d %H:%M:%S"))
def on_message(client, userdata, message): print("message received ", str(message.payload.decode("utf-8"))) print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) def on_log(client, userdata, level, buf): print("log: ", buf) #Loop to measure wind speed and report at 5-second intervals while True: #start_time = time.time() inside_humidity, pressure, inside_temp = bme280_sensor.read_all() inside_pressure = pressure + ((pressure * 9.80665 * hasl) / (287 * (273 + inside_temp + (hasl / 400)))) print("Humidity:", inside_humidity) print("Pressure:", inside_pressure) print("Inside Temp:", inside_temp) #Send to Home Assistant client = mqtt.Client("P1") #create new instance client.on_message = on_message #attach function to callback client.username_pw_set(username=username, password=password) client.connect(broker_address) #connect to broker client.loop_start() #start the loop client.subscribe("house/weather/inside_temp") client.subscribe("house/weather/inside_humidity")