More stable time keeping and Using new init-functions

The old time keeping routine was a bit flaky, this one might blow up in
 irritating ways if a cycle takes more than 1 second. Also uses the new
 init functions to set up the SGP.
 Be ware of the dangerous i2c-general call that resets any device on the
 buss that listens to general call.
This commit is contained in:
Simon Albinsson
2018-04-07 22:03:50 +00:00
parent 7f31f5a116
commit 8bff008b36

View File

@@ -73,15 +73,38 @@ def store_baseline(n):
baseline= rw(GET_BASELINE) baseline= rw(GET_BASELINE)
json.dump(baseline.raw,conf) json.dump(baseline.raw,conf)
def http_post_data(url="http://localhost/setdata"):
try:
requests.get(url,params={"co2": co2eq,"tvoc":tvoc},timeout=.5)
except:
pass
with SMBusWrapper(1) as bus: with SMBusWrapper(1) as bus:
rw=partial(read_write,bus=bus) rw=partial(read_write,bus=bus)
print(rw(GET_FEATURES)) i2c_geral_call(bus)
print(rw(GET_SERIAL)) print("Features: %s"%repr(rw(GET_FEATURES)))
print(rw(IAQ_INIT)) print("Serial: %s"%repr(rw(GET_SERIAL)))
#print(rw(IAQ_SELFTEST,sleep_time=.1)) init_sgp(bus)
for i in range(600): #print(rw(IAQ_SELFTEST))
print( "CO_2eq: %d ppm, TVOC: %d"%tuple( rw(IAQ_MEASURE,sleep_time=.1))) print("Testing meassure")
sleep(2) print(rw(IAQ_MEASURE))
print("Testing meassure again")
sleep(1)
print(rw(IAQ_MEASURE))
sleep(1)
print("Running")
with open("/tmp/air.txt","w") as f:
n=0
while(True):
start = time()
n+=1
co2eq, tvoc = rw(IAQ_MEASURE).data
res = ( "%s CO_2eq: %d ppm, TVOC: %d"%( asctime(), co2eq, tvoc ))
print(res)
f.write("%i %i %i \n"%(time(),co2eq,tvoc))
http_post_data()
store_baseline(n)
elapsed = (time() - start)
sleep(1 - elapsed )
bus.close() bus.close()