|
Post by dertown on Aug 3, 2022 9:42:46 GMT -8
[email protected]:~/SDL_Pi_WeatherSense $ sudo python3 WeatherSenseMonitor.py
using updateWebConfig
-----------------
WeatherSense Monitoring Software
Software Version V016
-----------------
Pending jobs:
readSensors (trigger: date[2022-08-03 13:35:36 EDT], pending)
startMQTT (trigger: date[2022-08-03 13:35:36 EDT], pending)
cleanPictures (trigger: cron[day='*', hour='3', minute='4'], pending)
cleanTimeLapses (trigger: cron[day='*', hour='3', minute='10'], pending)
buildTimeLapse (trigger: cron[day='*', hour='5', minute='30'], pending)
readSensors (trigger: date[2022-08-03 13:35:36 EDT], pending)
-----------------
######
Read Wireless Sensors
Scheduled Jobs
creating new MQTT instance
######
-----------------
######
Read Wireless Sensors
######
Jobstore default:
cleanPictures (trigger: cron[day='*', hour='3', minute='4'], next run at: 20 22-08-04 03:04:00 EDT)
connecting to broker
cleanTimeLapses (trigger: cron[day='*', hour='3', minute='10'], next run at: 2022-08-04 03:10:00 EDT)
buildTimeLapse (trigger: cron[day='*', hour='5', minute='30'], next run at: 2022-08-04 05:30:00 EDT)
-----------------
starting 433MHz scanning
######
processing Generic Data
This is the raw data: [30] Generic Remote SC226x EV1527
starting 433MHz scanning
######
processing Generic Data
Connected with result code 0
This is the raw data: [38] Generic temperature sensor 1
processing Generic Data
This is the raw data: [30] Generic Remote SC226x EV1527
processing Generic Data
This is the raw data: [38] Generic temperature sensor 1
processing Generic Data
This is the raw data: [87] Generic wireless motion sensor
processing Generic Data
This is the raw data: [87] Generic wireless motion sensor
Job "readSensors (trigger: date[2022-08-03 13:35:36 EDT], next run at: 2022-08-0 3 13:35:36 EDT)" raised an exception
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", l ine 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 723, in readSenso rs
processWeatherSenseTB(sLine)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 455, in processWe atherSenseTB
state = json.loads(sLine)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 12 (char 11)
Extra data: line 1 column 12 (char 11)
File "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", l ine 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 723, in readSenso rs
processWeatherSenseTB(sLine)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 455, in processWe atherSenseTB
state = json.loads(sLine)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
Job "readSensors (trigger: date[2022-08-03 13:35:36 EDT], next run at: 2022-08-0 3 13:35:36 EDT)" raised an exception
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", l ine 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 723, in readSenso rs
processWeatherSenseTB(sLine)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 455, in processWe atherSenseTB
state = json.loads(sLine)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 12 (char 11)
Extra data: line 1 column 12 (char 11)
File "/usr/local/lib/python3.7/dist-packages/apscheduler/executors/base.py", l ine 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 723, in readSenso rs
processWeatherSenseTB(sLine)
File "/home/pi/SDL_Pi_WeatherSense/wirelessSensors.py", line 455, in processWe atherSenseTB
state = json.loads(sLine)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
Hi SDL I have setup the Rad Sensor and I am running the the software on a SDL provided SD card. As per the instructions I ran git pull to get the latest versions. How ever when I run the software I get the above message. Is there a way to test the software to see it connecting? I am using a NooElec NESDR Mini USB RTL-SDR for the SDR. Derek
|
|
|
Post by dertown on Aug 3, 2022 11:12:36 GMT -8
Can you post the actual code contents of the file? Thanks, Jason #
# wireless sensor routines
import config
import json
import random
import sys
from subprocess import PIPE, Popen, STDOUT
from threading import Thread
import datetime
import MySQLdb as mdb
import traceback
import state
import os
import util
import updateWeb
from paho.mqtt import publish
import aqi
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------
# cmd = [ '/usr/local/bin/rtl_433', '-q', '-F', 'json', '-R', '147']
cmd = ['/usr/local/bin/rtl_433', '-q', '-F', 'json', '-R', '146', '-R', '147', '-R', '148', '-R', '150', '-R', '151', '-R', '152', '-R', '153']
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------
# A few helper functions...
def nowStr():
return (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# stripped = lambda s: "".join(i for i in s if 31 < ord(i) < 127)
# We're using a queue to capture output as it occurs
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x
ON_POSIX = 'posix' in sys.builtin_module_names
def enqueue_output(src, out, queue):
for line in iter(out.readline, b''):
queue.put((src, line))
out.close()
def randomadd(value, spread):
return round(value + random.uniform(-spread, spread), 2)
# MQTT Publish Line
def mqtt_publish_single(message, topic):
topic = '{0}/{1}'.format("weathersense", topic)
return
try:
publish.single(
topic=topic,
payload=message,
hostname=config.MQTThost,
port=config.MQTTport,
qos=config.MQTTqos
)
except:
traceback.print_exc()
print('Mosquitto not available')
# process functions
import gpiozero
def processFT020T(sLine, lastFT020TTimeStamp, ReadingCount):
if (config.SWDEBUG):
sys.stdout.write("processing FT020T Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
sys.stdout.write('ReadingCount=: ' + str(ReadingCount) + '\n')
var = json.loads(sLine)
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "FT020T")
if (lastFT020TTimeStamp == var["time"]):
# duplicate
if (config.SWDEBUG):
sys.stdout.write("duplicate found\n")
return ""
lastFT0202TTimeStamp = var["time"]
# now check for adding record
if ((ReadingCount % config.RecordEveryXReadings) != 0):
# skip write to database
if (config.SWDEBUG):
sys.stdout.write("skipping write to database \n")
return ""
# outside temperature and Humidity
mainID = var["id"]
lastMainReading = nowStr()
wTemp = var["temperature"]
ucHumi = var["humidity"]
wTemp = (wTemp - 400) / 10.0
# deal with error condtions
if (wTemp > 140.0):
# error condition from sensor
if (config.SWDEBUG):
sys.stdout.write("error--->>> Temperature reading from FT020T\n")
sys.stdout.write('This is the raw temperature: ' + str(wTemp) + '\n')
# put in previous temperature
wtemp = state.currentOutsideTemperature
# print("wTemp=%s %s", (str(wTemp),nowStr() ));
if (ucHumi > 100.0):
# bad humidity
# put in previous humidity
ucHumi = state.currentOutsideHumidity
# convert temperature reading to Celsius
OutdoorTemperature = round(((wTemp - 32.0) / (9.0 / 5.0)), 2)
#OutdoorTemperature = round(wTemp, 2)
OutdoorHumidity = ucHumi
WindSpeed = round(var["avewindspeed"] / 10.0, 1)
WindGust = round(var["gustwindspeed"] / 10.0, 1)
WindDirection = var["winddirection"]
TotalRain = round(var["cumulativerain"] / 10.0, 1)
Rain60Minutes = 0.0
wLight = var["light"]
#if (wLight >= 0x1fffa):
# wLight = wLight | 0x7fff0000
wUVI = var["uv"]
if (wUVI >= 0xfa):
wUVI = wUVI | 0x7f00
SunlightVisible = wLight
SunlightUVIndex = round(wUVI / 10.0, 1)
if (var['batterylow'] == 0):
BatteryOK = "OK"
else:
BatteryOK = "LOW"
# SkyWeather2 Compatiblity
AQI = 0
Hour24_AQI = 0
IndoorTemperature = 0
IndoorHumidity = 0
BarometricPressure = 0.0
BarometricPressureSeaLevel = 0.0
BarometricTemperature = 0.0
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
cpu = gpiozero.CPUTemperature()
CPUTemperature = cpu.temperature
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
fields = "OutdoorTemperature, OutdoorHumidity, IndoorTemperature, IndoorHumidity, TotalRain, SunlightVisible, SunlightUVIndex, WindSpeed, WindGust, WindDirection,BarometricPressure, BarometricPressureSeaLevel, BarometricTemperature, AQI, AQI24Average, BatteryOK, CPUTemperature"
values = "%6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f, \'%s\',%6.2f" % (
OutdoorTemperature, OutdoorHumidity, IndoorTemperature, IndoorHumidity, TotalRain, SunlightVisible,
SunlightUVIndex, WindSpeed, WindGust, WindDirection, BarometricPressure, BarometricPressureSeaLevel,
BarometricTemperature, float(AQI), Hour24_AQI, BatteryOK, CPUTemperature)
query = "INSERT INTO WeatherData (%s) VALUES(%s )" % (fields, values)
# print("query=", query)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return lastFT0202TTimeStamp
# processes Inside Temperature and Humidity
def processF016TH(sLine, ReadingCountArray):
if (config.SWDEBUG):
sys.stdout.write('Processing F016TH data' + '\n')
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if config.SWDEBUG:
print(ReadingCountArray)
var = json.loads(sLine)
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "F016TH")
lastIndoorReading = nowStr()
# check for reading count per device
# indoor T/H sensors support channels 1-8
# the sensor channel needs to be lowered by one
chan_array_pos = var['channel'] - 1
#if ((ReadingCountArray[var["channel"]] % config.IndoorRecordEveryXReadings) != 0):
if (ReadingCountArray[chan_array_pos] % config.IndoorRecordEveryXReadings) != 0:
if config.SWDEBUG:
print("skipping write to database for channel=", var["channel"])
# increment ReadingCountArray
# ReadingCountArray[var["channel"]] = ReadingCountArray[var["channel"]] + 1
ReadingCountArray[chan_array_pos] += 1
return ""
# increment ReadingCountArray
# ReadingCountArray[var["channel"]] = ReadingCountArray[var["channel"]] + 1
ReadingCountArray[chan_array_pos] += 1
IndoorTemperature = round(((var["temperature_F"] - 32.0) / (9.0 / 5.0)), 2)
#IndoorTemperature = var["temperature_F"]
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
fields = "DeviceID, ChannelID, Temperature, Humidity, BatteryOK, TimeRead"
values = "%d, %d, %6.2f, %6.2f, \"%s\", \"%s\"" % (
var["device"], var["channel"], IndoorTemperature, var["humidity"], var["battery"], var["time"])
query = "INSERT INTO IndoorTHSensors (%s) VALUES(%s )" % (fields, values)
# print("query=", query)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return
# processes Generic Packets
def processWeatherSenseGeneric(sLine):
if (config.SWDEBUG):
sys.stdout.write("processing Generic Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSGeneric")
return
# processes Radiation Detector Packets
def processWeatherSenseRadiation(sLine):
# WeatherSense Protocol 19
state = json.loads(sLine)
myProtocol = state['weathersenseprotocol']
if (config.SWDEBUG):
sys.stdout.write("processing Radiation Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSRadiation")
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
# calculate Radiation 24 Hour
timeDelta = datetime.timedelta(days=1)
now = datetime.datetime.now()
before = now - timeDelta
before = before.strftime('%Y-%m-%d %H:%M:%S')
query = "SELECT uSVh, TimeStamp FROM RAD433MHZ WHERE (TimeStamp > '%s') ORDER BY TimeStamp " % (before)
cur = con.cursor()
cur.execute(query)
myRADRecords = cur.fetchall()
myRADTotal = 0.0
if (len(myRADRecords) > 0):
for i in range(0, len(myRADRecords)):
myRADTotal = myRADTotal + myRADRecords[i][0]
RAD24Hour = (myRADTotal + float(state['uSVh'])) / (len(myRADRecords) + 1)
else:
RAD24Hour = 0.0
batteryPower = float(state["batterycurrent"])* float(state["batteryvoltage"])
loadPower = float(state["loadcurrent"])* float(state["loadvoltage"])
solarPower = float(state["solarpanelcurrent"])* float(state["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(state["batteryvoltage"], 4.2)
fields = "uSVh24Hour, deviceid, protocolversion, softwareversion, weathersenseprotocol, CPM, nSVh, uSVh, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, auxa, solarpresent, radiationpresent, keepalivemessage, lowbattery, batterycharge, messageID, batterypower, loadpower, solarpower "
values = "%6.2f, %d, %d, %d, %d, %d,%d, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %d, %d, %d,%d,%d,%6.2f, %d,%6.2f, %6.2f, %6.3f" % ( RAD24Hour, state["deviceid"], state["protocolversion"], state["softwareversion"], state["weathersenseprotocol"],
state['CPM'], state['nSVh'], state['uSVh'],
state["batteryvoltage"], state["batterycurrent"], state["loadvoltage"], state["loadcurrent"],
state["solarpanelvoltage"], state["solarpanelcurrent"], state["auxa"],state["solarpresent"],state["radBoardPresent"],state["keepalivemessage"],state["lowbattery"], batteryCharge, state["messageid"],
batteryPower, loadPower, solarPower )
query = "INSERT INTO RAD433MHZ (%s) VALUES(%s )" % (fields, values)
# print("query=", query)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
CPM = state['CPM']
uSVh = state['uSVh']
# update web maps
updateWeb.update_SafeCast(CPM, uSVh)
updateWeb.update_RadMon(CPM)
updateWeb.update_GMCMap(CPM, uSVh)
return
# processes AfterShock Packets
def processWeatherSenseAfterShock(sLine):
# weathersense protocol 18
state = json.loads(sLine)
myProtocol = state['weathersenseprotocol']
if (config.SWDEBUG):
sys.stdout.write("processing AfterShock Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSAfterShock")
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
myTEST = ""
myTESTDescription = ""
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
batteryPower = float(state["batterycurrent"])* float(state["batteryvoltage"])
loadPower = float(state["loadcurrent"])* float(state["loadvoltage"])
solarPower = float(state["solarpanelcurrent"])* float(state["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(state["batteryvoltage"], 4.2)
fields = "deviceid, protocolversion, softwareversion, weathersenseprotocol, eqcount, finaleq_si, finaleq_pga, instanteq_si, instanteq_pga, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, auxa, solarpresent, aftershockpresent, keepalivemessage, lowbattery, batterycharge, messageID, batterypower, loadpower, solarpower, test, testdescription"
values = "%d, %d, %d, %d, %d,%6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f,%6.2f,%6.2f, %d, %d, %d, %d,%d,%6.2f, %6.2f, %6.2f,\'%s\', \'%s\'" % (
state["deviceid"], state["protocolversion"], state["softwareversion"], state["weathersenseprotocol"],
state['eqcount'], state['finaleq_si'], state['finaleq_pga'], state['instanteq_si'],
state['instanteq_pga'],
state["batteryvoltage"], state["batterycurrent"], state["loadvoltage"], state["loadcurrent"],
state["solarpanelvoltage"], state["solarpanelcurrent"], state["auxa"],state["solarpresent"],state["aftershockpresent"],state["keepalivemessage"],state["lowbattery"], batteryCharge, state["messageid"],
batteryPower, loadPower, solarPower, myTEST, myTESTDescription)
query = "INSERT INTO AS433MHZ (%s) VALUES(%s )" % (fields, values)
# print("query=", query)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return
def processWeatherSenseTB(sLine):
# weathersense protocol 16
state = json.loads(sLine)
myProtocol = state['weathersenseprotocol']
if (config.SWDEBUG):
sys.stdout.write("processing Lightning TB Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSLightning")
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
myTEST = ""
myTESTDescription = ""
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
batteryPower = float(state["batterycurrent"])* float(state["batteryvoltage"])
loadPower = float(state["loadcurrent"])* float(state["loadvoltage"])
solarPower = float(state["solarpanelcurrent"])* float(state["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(state["batteryvoltage"], 4.2)
fields = "deviceid, protocolversion, softwareversion, weathersenseprotocol,irqsource, previousinterruptresult, lightninglastdistance, sparebyte, lightningcount, interruptcount, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, auxa, batterycharge, messageID, batterypower, loadpower, solarpower, test, testdescription"
values = "%d, %d, %d, %d, %d, %d, %d, %d,%d, %d,%6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f,%6.2f,%6.2f,%d,%6.2f, %6.2f, %6.2f,\'%s\', \'%s\'" % (
state["deviceid"], state["protocolversion"], state["softwareversion"], state["weathersenseprotocol"],
state['irqsource'], state['previousinterruptresult'], state['lightninglastdistance'], state['sparebyte'],
state['lightningcount'], state['interruptcount'],
state["batteryvoltage"], state["batterycurrent"], state["loadvoltage"], state["loadcurrent"],
state["solarpanelvoltage"], state["solarpanelcurrent"], state["auxa"], batteryCharge, state["messageid"],
batteryPower, loadPower, solarPower, myTEST, myTESTDescription)
query = "INSERT INTO TB433MHZ (%s) VALUES(%s )" % (fields, values)
# print("query=", query)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return
def processWeatherSenseAQI(sLine):
# weathersense protocol 15
state = json.loads(sLine)
myProtocol = state['weathersenseprotocol']
if (config.SWDEBUG):
sys.stdout.write("processing AQI Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSAQI")
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
myTEST = ""
myTESTDescription = ""
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
batteryPower = float(state["batterycurrent"])* float(state["batteryvoltage"])
loadPower = float(state["loadcurrent"])* float(state["loadvoltage"])
solarPower = float(state["solarpanelcurrent"])* float(state["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(state["batteryvoltage"], 4.2)
# calculate AQI 24 Hour
timeDelta = datetime.timedelta(days=1)
now = datetime.datetime.now()
before = now - timeDelta
before = before.strftime('%Y-%m-%d %H:%M:%S')
query = "SELECT AQI, TimeStamp FROM AQI433MHZ WHERE (TimeStamp > '%s') ORDER BY TimeStamp " % (before)
cur.execute(query)
myAQIRecords = cur.fetchall()
myAQITotal = 0.0
if (len(myAQIRecords) > 0):
for i in range(0, len(myAQIRecords)):
myAQITotal = myAQITotal + myAQIRecords[i][0]
AQI24Hour = (myAQITotal + float(state['AQI'])) / (len(myAQIRecords) + 1)
else:
AQI24Hour = 0.0
# HOTFIX for AQI problem from the wireless AQI sensor
# recalculate AQI from RAW values and write in database
myaqi = aqi.to_aqi([
(aqi.POLLUTANT_PM25, state['PM2.5A']),
(aqi.POLLUTANT_PM10, state['PM10A'])
])
if (myaqi > 500):
myaqi = 500
print("myaqi=", myaqi)
state['AQI'] = myaqi
fields = "deviceid, protocolversion, softwareversion, weathersenseprotocol, PM1_0S, PM2_5S, PM10S, PM1_0A, PM2_5A, PM10A, AQI, AQI24Hour, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, auxa, batterycharge, messageID, batterypower, loadpower, solarpower, test, testdescription"
values = "%d, %d, %d, %d, %d, %d, %d, %d, %d,%d, %d, %6.2f,%6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f,%6.2f,%6.2f,%d,%6.2f, %6.2f, %6.2f,\'%s\', \'%s\'" % (
state["deviceid"], state["protocolversion"], state["softwareversion"], state["weathersenseprotocol"],
state['PM1.0S'], state['PM2.5S'], state['PM10S'], state['PM1.0A'], state['PM2.5A'], state['PM10S'],
state['AQI'], AQI24Hour,
state["batteryvoltage"], state["batterycurrent"], state["loadvoltage"], state["loadcurrent"],
state["solarpanelvoltage"], state["solarpanelcurrent"], state["auxa"], batteryCharge, state["messageid"],
batteryPower, loadPower, solarPower, myTEST, myTESTDescription)
query = "INSERT INTO AQI433MHZ (%s) VALUES(%s )" % (fields, values)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return
def processSolarMAX(sLine):
state = json.loads(sLine)
if (config.SWDEBUG):
sys.stdout.write("processing SolarMAX Data\n")
sys.stdout.write('This is the raw data: ' + sLine + '\n')
# only accept SolarMAX Protocols (8,10,11)
myProtocol = state['weathersenseprotocol']
if ((myProtocol == 8) or (myProtocol == 10) or (myProtocol == 11)):
if (config.enable_MQTT == True):
mqtt_publish_single(sLine, "WSSolarMAX")
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
myTEST = ""
myTESTDescription = ""
con = mdb.connect(
config.MySQL_Host,
config.MySQL_User,
config.MySQL_Password,
config.MySQL_Schema
)
cur = con.cursor()
batteryPower = float(state["batterycurrent"])* float(state["batteryvoltage"])
loadPower = float(state["loadcurrent"])* float(state["loadvoltage"])
solarPower = float(state["solarpanelcurrent"])* float(state["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(state["batteryvoltage"], 13.2)
fields = "deviceid, protocolversion, softwareversion, weathersenseprotocol, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, auxa, internaltemperature,internalhumidity, batterycharge, messageID, batterypower, loadpower, solarpower, test, testdescription"
values = "%d, %d, %d, %d, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f,%6.2f,%6.2f,%d,%6.2f, %6.2f, %6.2f,\'%s\', \'%s\'" % (
state["deviceid"], state["protocolversion"], state["softwareversion"], state["weathersenseprotocol"],
state["batteryvoltage"], state["batterycurrent"], state["loadvoltage"], state["loadcurrent"],
state["solarpanelvoltage"], state["solarpanelcurrent"], state["auxa"], state["internaltemperature"],
state["internalhumidity"], batteryCharge, state["messageid"], batteryPower, loadPower, solarPower,
myTEST, myTESTDescription)
query = "INSERT INTO SolarMax433MHZ (%s) VALUES(%s )" % (fields, values)
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
print("Error %d: %s" % (e.args[0], e.args[1]))
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
return
# main read 433HMz Sensor Loop
def readSensors():
print("")
print("######")
print("Read Wireless Sensors")
print("######")
# Create our sub-process...
# Note that we need to either ignore output from STDERR or merge it with STDOUT due to a limitation/bug somewhere under the covers of "subprocess"
# > this took awhile to figure out a reliable approach for handling it...
p = Popen(cmd, stdout=PIPE, stderr=STDOUT, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=('stdout', p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------
pulse = 0
print("starting 433MHz scanning")
print("######")
# last timestamp for FT020T to remove duplicates
lastFT020TTimeStamp = ""
FT020Count = 0
IndoorReadingCountArray = [0, 0, 0, 0, 0, 0, 0, 0]
# temp value
#config.SWDEBUG = False
while True:
# Other processing can occur here as needed...
# sys.stdout.write('Made it to processing step. \n')
try:
src, line = q.get(timeout=1)
# print(line.decode())
except Empty:
pulse += 1
else: # got line
pulse -= 1
sLine = line.decode()
# See if the data is something we need to act on...
if (sLine.find('F007TH') != -1) or (sLine.find('FT0300') != -1) or (sLine.find('F016TH') != -1) or (
sLine.find('FT020T') != -1):
if ((sLine.find('F007TH') != -1) or (sLine.find('F016TH') != -1)):
processF016TH(sLine, IndoorReadingCountArray)
if ((sLine.find('FT0300') != -1) or (sLine.find('FT020T') != -1)):
lastFT020TTimeStamp = processFT020T(sLine, lastFT020TTimeStamp, FT020Count)
FT020Count = FT020Count + 1
if (sLine.find('SolarMAX') != -1):
processSolarMAX(sLine)
if (sLine.find('AQI') != -1):
processWeatherSenseAQI(sLine)
if (sLine.find('TB') != -1):
processWeatherSenseTB(sLine)
if (sLine.find('Generic') != -1):
processWeatherSenseGeneric(sLine)
if (sLine.find('AfterShock') != -1):
processWeatherSenseAfterShock(sLine)
if (sLine.find('Radiation') != -1):
processWeatherSenseRadiation(sLine)
sys.stdout.flush()
|
|