rasolar/log_bme280.py

94 lines
3.1 KiB
Python
Raw Normal View History

2019-06-24 08:32:20 -07:00
#!/usr/bin/env python
import numpy as np
import os, serial,time,socket,sys,json,logging,requests,getopt
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-25 03:46:17 -07:00
parameter={"device":socket.gethostname(),"mean_count":5,"ring_length":10,"wait":0.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
# 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
bme_temp=meas_data(var_name="temperature",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",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",ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_pressure",sigma=bme_sigma)
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-25 03:12:49 -07:00
bme_temp.append(int(1000*bme_data.temperature))
bme_hum.append(int(1000*bme_data.humidity))
bme_press.append(int(1000*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")