add push to opensensemap
parent
e289cd3683
commit
71f1b7310a
156
tri.py
156
tri.py
|
@ -1,7 +1,11 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import numpy as np
|
||||
import os, serial,time,socket,sys,json,smbus2,bme280
|
||||
import os, serial,time,socket,sys,json,smbus2,bme280,logging,requests
|
||||
# import the server implementation
|
||||
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
|
||||
# import ADC
|
||||
from Adafruit_ADS1x15 import ADS1115
|
||||
|
||||
# import vedirect from https://github.com/karioja/vedirect
|
||||
# description of channels:https://beta.ivc.no/wiki/index.php/Victron_VE_Direct_DIY_Cable
|
||||
|
@ -89,35 +93,74 @@ def print_data_callback(data):
|
|||
print data
|
||||
# end import vedirect
|
||||
|
||||
# def upload from forum.sensebox.de
|
||||
def upload_osm(sensor_id,value):
|
||||
url="https://ingress.opensensemap.org/boxes/%s/%s" % (sensebox_id,sensor_id)
|
||||
r = requests.post(url,json={'value': value})
|
||||
if r.status_code != requests.codes.ok:
|
||||
print("Error %d: %s" % (r.status_code,r.text))
|
||||
# end upload osm
|
||||
|
||||
a = 2
|
||||
# host and port of logging server
|
||||
|
||||
# push options to internet
|
||||
push_count=5 # wait 5 cycles till upload to opensensemap
|
||||
push_counter=0
|
||||
sensebox_id=5cf60a0907460b001b8881fd # id of opensensemap
|
||||
sensebox_temp=5cf60a0907460b001b888200 # id for temperaturemeasurement
|
||||
sensebox_hum=5cf60a0907460b001b8881ff # id for humidity measurement
|
||||
sensebox_pres=5cf60a0907460b001b8881fe # id for pressure measurement
|
||||
luftdaten_id=56009074600018881
|
||||
|
||||
# host and port of internal logging server
|
||||
HOST, PORT = "banana", 24048
|
||||
|
||||
# config of bme280 sensor
|
||||
bme_port=1
|
||||
bme_add=0x77
|
||||
bme_bus=smbus2.SMBus(bme_port)
|
||||
calibration_params=bme280.load_calibration_params(bme_bus,bme_add)
|
||||
bbme=True
|
||||
try:
|
||||
bme_bus=smbus2.SMBus(bme_port)
|
||||
except:
|
||||
bbme=False
|
||||
else:
|
||||
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 ADS1115
|
||||
|
||||
# configure the client logging
|
||||
import logging
|
||||
logging.basicConfig()
|
||||
log = logging.getLogger('./modbus.error')
|
||||
log.setLevel(logging.ERROR)
|
||||
|
||||
# choose the serial client
|
||||
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600, timeout=1)
|
||||
client.connect()
|
||||
btristar=True
|
||||
|
||||
try:
|
||||
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600, timeout=1)
|
||||
except:
|
||||
btristar=False
|
||||
else:
|
||||
client.connect()
|
||||
|
||||
# declare ve mppt
|
||||
ve=vedirect('/dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_VE1SSBVT-if00-port0',60)
|
||||
bve=True
|
||||
try:
|
||||
ve=vedirect('/dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_VE1SSBVT-if00-port0',60)
|
||||
except:
|
||||
bve=False
|
||||
|
||||
# declare adc
|
||||
adc = ADS1115()
|
||||
adcc = ADS1115(address=0x49, busnum=1)
|
||||
badc1=True
|
||||
badc2=True
|
||||
try:
|
||||
adc = ADS1115()
|
||||
except:
|
||||
badc1=False
|
||||
try:
|
||||
adcc = ADS1115(address=0x49, busnum=1)
|
||||
except:
|
||||
badc2=False
|
||||
|
||||
GAIN = 1
|
||||
|
||||
# declare vars
|
||||
|
@ -145,49 +188,55 @@ while a > 1:
|
|||
tFile.close()
|
||||
|
||||
# read adc's
|
||||
for i in range(4):
|
||||
ch_val[channel_names.index("a0_{0}".format(i))]=adc.read_adc(i,gain=GAIN)
|
||||
ch_val[channel_names.index("a1_{0}".format(i))]=adcc.read_adc(i,gain=GAIN)
|
||||
if badc1 or badc2:
|
||||
for i in range(4):
|
||||
if badc1:
|
||||
ch_val[channel_names.index("a0_{0}".format(i))]=adc.read_adc(i,gain=GAIN)
|
||||
if badc2:
|
||||
ch_val[channel_names.index("a1_{0}".format(i))]=adcc.read_adc(i,gain=GAIN)
|
||||
|
||||
if btristar:
|
||||
#read the registers from logical address 0 to 30.
|
||||
rr = client.read_holding_registers(0,90,unit=1)
|
||||
rr = client.read_holding_registers(0,90,unit=1)
|
||||
|
||||
#scaling
|
||||
ch_val[channel_names.index("volt_scale")]=rr.registers[0]*65536+rr.registers[1]
|
||||
ch_val[channel_names.index("amp_scale")]= rr.registers[2]*65536+rr.registers[3]
|
||||
ch_val[channel_names.index("volt_scale")]=rr.registers[0]*65536+rr.registers[1]
|
||||
ch_val[channel_names.index("amp_scale")]= rr.registers[2]*65536+rr.registers[3]
|
||||
|
||||
#the stuff we want
|
||||
ch_val[channel_names.index("volt_bat_term")]=rr.registers[25]
|
||||
ch_val[channel_names.index("volt_bat_sens")]=rr.registers[26]
|
||||
ch_val[channel_names.index("volt_arr")]=rr.registers[27] # Array voltage
|
||||
ch_val[channel_names.index("amp_bat")]=rr.registers[28] # Battery current
|
||||
ch_val[channel_names.index("amp_arr")]=rr.registers[29] # Array current
|
||||
ch_val[channel_names.index("temp_heatsink")]=rr.registers[35] # Temperature heatsink
|
||||
ch_val[channel_names.index("temp_bat")]=rr.registers[36] # Temperature battery
|
||||
ch_val[channel_names.index("ah_res")]=rr.registers[52] * 65536 + rr.registers[53] # Ah resetable
|
||||
ch_val[channel_names.index("ah_tot")]=rr.registers[54] * 65536 + rr.registers[55] # Ah total
|
||||
ch_val[channel_names.index("kwh_res")]=rr.registers[56] # kwh resetable
|
||||
ch_val[channel_names.index("kwh_tot")]=rr.registers[57] # kwh total
|
||||
ch_val[channel_names.index("watt_in")]=rr.registers[58] # Power in
|
||||
ch_val[channel_names.index("watt_out")]=rr.registers[59] # Power out
|
||||
ch_val[channel_names.index("hour_tot")]=rr.registers[42] * 65536 + rr.registers[43] # hour total
|
||||
ch_val[channel_names.index("state")]=rr.registers[50] # State
|
||||
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
|
||||
ch_val[channel_names.index("volt_bat_term")]=rr.registers[25]
|
||||
ch_val[channel_names.index("volt_bat_sens")]=rr.registers[26]
|
||||
ch_val[channel_names.index("volt_arr")]=rr.registers[27] # Array voltage
|
||||
ch_val[channel_names.index("amp_bat")]=rr.registers[28] # Battery current
|
||||
ch_val[channel_names.index("amp_arr")]=rr.registers[29] # Array current
|
||||
ch_val[channel_names.index("temp_heatsink")]=rr.registers[35] # Temperature heatsink
|
||||
ch_val[channel_names.index("temp_bat")]=rr.registers[36] # Temperature battery
|
||||
ch_val[channel_names.index("ah_res")]=rr.registers[52] * 65536 + rr.registers[53] # Ah resetable
|
||||
ch_val[channel_names.index("ah_tot")]=rr.registers[54] * 65536 + rr.registers[55] # Ah total
|
||||
ch_val[channel_names.index("kwh_res")]=rr.registers[56] # kwh resetable
|
||||
ch_val[channel_names.index("kwh_tot")]=rr.registers[57] # kwh total
|
||||
ch_val[channel_names.index("watt_in")]=rr.registers[58] # Power in
|
||||
ch_val[channel_names.index("watt_out")]=rr.registers[59] # Power out
|
||||
ch_val[channel_names.index("hour_tot")]=rr.registers[42] * 65536 + rr.registers[43] # hour total
|
||||
ch_val[channel_names.index("state")]=rr.registers[50] # State
|
||||
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) # Pressure
|
||||
ch_val[channel_names.index("humidity")]=int(1000*bme_data.humidity) # Humidity
|
||||
if bbme:
|
||||
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) # Pressure
|
||||
ch_val[channel_names.index("humidity")]=int(1000*bme_data.humidity) # Humidity
|
||||
|
||||
# read ve data
|
||||
vedata=ve.read_data_single()
|
||||
ch_val[channel_names.index("volt_bat_ve")]=int(vedata['V']) # Battery voltage measured by ve
|
||||
ch_val[channel_names.index("volt_arr_ve")]=int(vedata['VPV']) # Array voltage measured by ve
|
||||
ch_val[channel_names.index("amp_ve")]=int(vedata['I']) # loading current by ve
|
||||
ch_val[channel_names.index("watt_ve")]=int(vedata['PPV']) # Array power measured by ve
|
||||
ch_val[channel_names.index("days_ve")]=int(vedata['HSDS']) # total days online ve
|
||||
if bve:
|
||||
vedata=ve.read_data_single()
|
||||
ch_val[channel_names.index("volt_bat_ve")]=int(vedata['V']) # Battery voltage measured by ve
|
||||
ch_val[channel_names.index("volt_arr_ve")]=int(vedata['VPV']) # Array voltage measured by ve
|
||||
ch_val[channel_names.index("amp_ve")]=int(vedata['I']) # loading current by ve
|
||||
ch_val[channel_names.index("watt_ve")]=int(vedata['PPV']) # Array power measured by ve
|
||||
ch_val[channel_names.index("days_ve")]=int(vedata['HSDS']) # total days online ve
|
||||
|
||||
timefile=round(timestamp/3600000)
|
||||
f1=open("/home/pi/log/data_{:d}.txt".format(int(timefile)),"a")
|
||||
|
@ -197,6 +246,13 @@ while a > 1:
|
|||
f1.write(channel_names[i] + ":{0};".format(int(ch_val[i])))
|
||||
if channel_names[i] != "time":
|
||||
payload[channel_names[i]]=int(ch_val[i])
|
||||
if push_counter > push_count:
|
||||
if i = "temperature":
|
||||
upload_osm(sensebox_temp,ch_val[i]/1000)
|
||||
if i = "humidity":
|
||||
upload_osm(sensebox_hum,ch_val[i]/1000)
|
||||
if i = "pressure":
|
||||
upload_osm(sensebox_pres,ch_val[i]/1000)
|
||||
f1.write("\n")
|
||||
f1.close()
|
||||
json_out={"time": ch_val[channel_names.index("time")],"device": "rasolar","payload":payload}
|
||||
|
@ -242,7 +298,11 @@ while a > 1:
|
|||
# print "hours: %.2f" % hourm
|
||||
# print "charge state : %.2f" % charge_state
|
||||
|
||||
|
||||
if push_counter>push_count:
|
||||
push_counter = 1
|
||||
else:
|
||||
push_counter=push_counter+1
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue