rasolar/log_bme280.py

114 lines
3.8 KiB
Python
Raw Normal View History

2019-06-24 08:32:20 -07:00
#!/usr/bin/env python
import numpy as np
2019-06-29 04:30:20 -07:00
import os, serial,time,socket,sys,json,logging,requests
2019-06-24 08:32:20 -07:00
from meas_data import meas_data
# import the server implementation
# import ADC
#import vedirect
#import upload_osm
configfile="config.json"
cf=open(configfile,"r")
log_conf=json.load(cf)
cf.close()
2019-06-30 10:39:27 -07:00
parameter={"device":socket.gethostname(),"mean_count":5,"ring_length":10,"wait":0.5,"cycle":5}
2019-06-25 02:37:49 -07:00
for n in parameter:
if n in log_conf:
parameter[n]=log_conf[n]
2019-06-24 08:32:20 -07:00
2019-07-02 05:17:52 -07:00
bmqtt=False
if "mqtt" in log_conf:
bmqtt=True
lcmq=log_conf['mqtt']
mbroker=""
if 'broker' in lcmq:
mbroker=lcmq['broker']
else:
bmqtt=False
mport=1883
if 'port' in lcmq:
mport=lcmq['port']
2019-06-24 08:32:20 -07:00
# config of bme280 sensor
# use pip install RPi.bme280 for library
2019-06-25 01:37:32 -07:00
meas_types=["temperature","humidity","pressure"]
2019-06-24 08:32:20 -07:00
bbme=False
if "bme280" in log_conf:
bbme=True
if "enable" in log_conf['bme280']:
if log_conf['bme280']['enable'] == 0:
bbme=False
if bbme:
import smbus2
import bme280
if "port" in log_conf['bme280']:
bme_port=log_conf['bme280']['port']
else:
bme_port=1
if "address" in log_conf['bme280']:
bme_add=int(log_conf['bme280']['address'],16)
else:
bme_add=0x77
try:
bme_bus=smbus2.SMBus(bme_port)
except:
bbme=False
else:
calibration_params=bme280.load_calibration_params(bme_bus,bme_add)
2019-06-25 06:13:33 -07:00
bme_sigma=3
if "sigma" in log_conf['bme280']:
bme_sigma=int(log_conf['bme280']['sigma'])
if bme_sigma < 1:
bme_sigma=1
2019-06-30 10:39:27 -07:00
bme_cycle=5
if "cycle" in log_conf['bme280']:
bme_cycle=int(log_conf['bme280']['cycle'])
2019-06-30 21:27:26 -07:00
bme_temp=meas_data(var_name="temperature",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_temperature",sigma=bme_sigma)
bme_hum=meas_data(var_name="humidity",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_humidity",sigma=bme_sigma)
bme_press=meas_data(var_name="pressure",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_pressure",sigma=bme_sigma)
2019-07-02 05:17:52 -07:00
if ("mqtt" in log_conf['bme280']) and bmqtt:
bme_temp.set_mqtt(broker=mbroker,port=mport)
bme_hum.set_mqtt(broker=mbroker,port=mport)
bme_press.set_mqtt(broker=mbroker,port=mport)
2019-06-25 03:46:17 -07:00
if "sqlserver" in log_conf:
hostname="banana"
if "host" in log_conf['sqlserver']:
hostname=log_conf['sqlserver']['host']
port=24048
if "port" in log_conf['sqlserver']:
port=int(log_conf['sqlserver']['port'])
bme_temp.set_sql(host=hostname,port=port)
bme_hum.set_sql(host=hostname,port=port)
bme_press.set_sql(host=hostname,port=port)
2019-06-25 07:32:44 -07:00
if "opensensemap" in log_conf['bme280']:
osm=log_conf['bme280']['opensensemap']
if "sensebox_id" in osm:
sid=osm['sensebox_id']
if "sensors" in osm:
if "temperature" in osm['sensors']:
bme_temp.set_sensebox(sid,osm['sensors']['temperature'])
if "humidity" in osm['sensors']:
bme_hum.set_sensebox(sid,osm['sensors']['humidity'])
if "pressure" in osm['sensors']:
bme_press.set_sensebox(sid,osm['sensors']['pressure'])
2019-06-25 03:46:17 -07:00
2019-06-24 08:32:20 -07:00
if bbme:
while True:
# read bme280 (temperature, humidity, pressure)
2019-06-25 02:37:49 -07:00
bme_data=bme280.sample(bme_bus,bme_add,calibration_params)
2019-06-30 12:40:44 -07:00
bme_temp.append(bme_data.temperature)
bme_hum.append(bme_data.humidity)
bme_press.append(bme_data.pressure)
2019-06-25 02:37:49 -07:00
time.sleep(parameter['wait'])
2019-06-24 08:32:20 -07:00
# close the client
print("done")