add bme280 sensor; add data storage > pysql

master
ademant 2019-06-02 18:51:03 +02:00
parent 5007ccba05
commit 0430650be0
1 changed files with 31 additions and 6 deletions

37
tri.py
View File

@ -2,12 +2,23 @@
import time
import numpy as np
import socket
import sys
import json
import smbus2
import bme280
a = 2
HOST, PORT = "banana", 24048
bme_port=1
bme_add=0x77
bme_bus=smbus2.SMBus(bme_port)
calibration_params=bme280.load_calibration_params(bme_bus,bme_add)
# import the server implementation
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
# import ADC
from Adafruit_ADS1x15 import ADS1015
from Adafruit_ADS1x15 import ADS1115
# configure the client logging
import logging
@ -20,14 +31,14 @@ client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600, timeout=
client.connect()
# declare adc
adc = ADS1015()
adcc = ADS1015(address=0x49, busnum=1)
adc = ADS1115()
adcc = ADS1115(address=0x49, busnum=1)
GAIN = 1
# declare vars
channel_names=["time","CPU_temp","a0_0","a0_1","a0_2","a0_3","a1_0","a1_1","a1_2","a1_3","volt_scale","amp_scale",
"volt_bat_term","volt_bat_sens","volt_arr","amp_bat","amp_arr","temp_heatsink","temp_bat","ah_res",
"ah_tot","kwh_res","kwh_tot","watt_in","watt_out","hour_tot","state","volt_sweep_mp","volt_sweep_oc"]
"ah_tot","kwh_res","kwh_tot","watt_in","watt_out","hour_tot","state","volt_sweep_mp","volt_sweep_oc","temperature","pressure","humidity"]
ch_val=np.zeros(len(channel_names))
for i in range(len(channel_names)):
ch_val[i]=(-1)
@ -38,7 +49,7 @@ while a > 1:
ch_old=ch_val.copy()
# set actual time
timestamp=time.time()
timestamp=int(1000*time.time())
ch_val[channel_names.index("time")]=timestamp
# get cpu temperature
@ -78,13 +89,27 @@ while a > 1:
ch_val[channel_names.index("volt_sweep_mp")]=rr.registers[61] # Array voltage
ch_val[channel_names.index("volt_sweep_oc")]=rr.registers[62] # Array voltage
# read bme280 (temperature, humidity, pressure)
bme_data=bme280.sample(bme_bus,bme_add,calibration_params)
ch_val[channel_names.index("temperature")]=int(1000*bme_data.temperature) # Temperature
ch_val[channel_names.index("pressure")]=int(1000*bme_data.pressure) # Temperature
ch_val[channel_names.index("humidity")]=int(1000*bme_data.humidity) # Temperature
timefile=round(timestamp/3600)
f1=open("/home/pi/log/data_{:d}.txt".format(int(timefile)),"a")
payload={}
for i in range(len(ch_val)):
if ch_val[i] != ch_old[i]:
f1.write(channel_names[i] + ":{0};".format(int(ch_val[i])))
if channel_names[i] != "time":
payload[channel_names[i]]=int(ch_val[i])
f1.write("\n")
f1.close()
json_out={"time": ch_val[channel_names.index("time")],"device": "rasolar","payload":payload}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(json.dumps(json_out))
# volt_batt=rr.registers[24] * volt_scaling / 65536 / 32768
# volt_batt_t=rr.registers[25] * volt_scaling / 65536 / 32768
@ -127,4 +152,4 @@ while a > 1:
client.close()
f1.close()
print "done"
print("done")