Files
esp-base/old/pnd.py
2025-10-01 18:30:41 +00:00

190 lines
6.6 KiB
Python

import pndSRC as mypnd
import pndConfig as cfg
import ntptime
import ujson
import time
import machine
import array
import gc
gc.enable()
from machine import Pin, I2C, ADC
gc.enable()
class rt():
wifi = mypnd.wifi
Psystem = mypnd.Psystem
lastTry = 0
cycle = 0
looping = 0
def run(self):
self.cycle = 1
timer = "s"
while True:
try:
## main loop
gc.collect()
machine.idle()
print(self.Psystem.status())
##display
temp, hum = self.Sensors[0].getStr()
mois = self.Sensors[1].getStr()
ptemp = "T:" + str(temp)
ptemp += " C"
phum = "H:" + str(hum)
phum += " %"
pmoi = "M:" + str(mois[:6])
pmoi += " %"
#up = self.cycle * self.looping
if(up > 300 and timer == "s"):
runtime = up / 60
timer = "m"
elif(timer == "m" and up > 3600):
runtime = up / 3600
timer = "h"
elif(timer == "m"):
runtime = up / 60
elif(timer == "h"):
runtime = up / 3600
else:
runtime = up
year, mon, day, h, m, s, nope, nope2 = time.localtime()
if((m % 55) == 0):
# self.wifi.reconnect()
# self.cycle = self.cycle + 1
if(self.cycle > 10000): self.cycle = 1
machine.idle()
time.sleep(self.looping)
except OSError as e:
mypnd.ecx.handle(mypnd.ecx(),self,e)
def backgroundTask(self):
if(not self.online):
lhttp = mypnd.webserver()
print('starting Webserver...')
## method with main loop goes here
lhttp.showStatusPage()
else:
print('offline Mode: no Webserver...')
def init(self, lastTry):
try:
self.online = cfg.defaults.gc_offline
self.Psystem.setCallback(self)
i = 0
self.lastTry = lastTry
if(not self.online):
self.wifi.connect()
ntptime.host = cfg.defaults.gc_ntp_host
try:
ntptime.settime()
except:
pass
if hasattr(cfg.tasks, "gc_sensors"):
self.Sensors = {}
for sens in cfg.tasks.gc_sensors:
if cfg.tasks.gc_sensors[sens]['ADC']:
self.Sensors[i] = mypnd.adcsens(cfg.tasks.gc_sensors[sens]['name'],cfg.tasks.gc_sensors[sens]['pin'])
else:
self.Sensors[i] = mypnd.sens(cfg.tasks.gc_sensors[sens]['name'],cfg.tasks.gc_sensors[sens]['type'],cfg.tasks.gc_sensors[sens]['pin'])
i += 1
if hasattr(cfg.tasks, "gc_display"):
self.dispService = dispService();
self.looping = cfg.defaults.gc_looping
except OSError as e:
mypnd.ecx.handle(mypnd.ecx(),self,e)
class mqService():
def __init__(self):
try:
self.mqtt = mypnd.mqtt()
self.mqclient = self.mqtt.connect()
except OSError as e:
self.lastTry = 1
mypnd.ecx.handle(mypnd.ecx(),self,e)
def logConf(self):
topic = cfg.mqtt.gc_topic_config
sys = mypnd.Psystem
ip = mypnd.wifi.getIp()
status = sys.statusJSON()
self.mqtt.publish(self.mqclient, topic, status)
def logSensors(self, sensors):
year, mon, day, h, m, s, nope, nope2 = time.localtime()
date = str(year) + "-" + str(mon) + "-" + str(day) + "T" + str(h) + ":" + str(m) + ":" + str(s)
for sens in sensors:
if sensors[sens].ADC:
jsens = ujson.dumps({ "name" : cfg.gc_name, "Time": date, "senso_name": sensors[sens].id, "value": sensors[sens].update()})
self.mqtt.publish(self.mqclient, cfg.mqtt.gc_topic_sensor, jsens)
else:
temp, hum = sensors[sens].update()
jsens = ujson.dumps({ "name" : cfg.gc_name, "Time": date, "senso_name": "Temperature_" + sensors[sens].id[-1], "value": temp})
self.mqtt.publish(self.mqclient, cfg.mqtt.gc_topic_sensor, jsens)
jsens = ujson.dumps({ "name" : cfg.gc_name, "Time": date, "senso_name": "Humidity_" + sensors[sens].id[-1], "value": hum})
self.mqtt.publish(self.mqclient, cfg.mqtt.gc_topic_sensor, jsens)
def close(self):
self.mqtt.disconnect(self.mqclient)
del self.mqclient
del self.mqtt
class dispService():
def __init__(self):
import pndSRC as mypnd
self.disp = mypnd.display(cfg.tasks.gc_display)
self.disp.contrast(50)
self.disp.flush()
def show4TxtLines(self, l1, l2, l3, l4 ):
self.disp.flush()
year, mon, day, h, m, s, nope, nope2 = time.localtime()
if((m % 2) == 0):
self.disp.addTxt(l1, 0, 0, 1)
self.disp.addTxt(l2, 0, 13, 1)
self.disp.addTxt(l3, 0, 25, 1)
self.disp.addTxt(l4.date, 0, 37, 1)
elif((m % 3) == 0):
self.disp.addTxt(l2, 0, 0, 1)
self.disp.addTxt(l4.time, 0, 13, 1)
self.disp.addTxt(l1, 0, 25, 1)
self.disp.addTxt(l3, 0, 37, 1)
else:
self.disp.addTxt(l4.time, 0, 0, 1)
self.disp.addTxt(l3, 0, 13, 1)
self.disp.addTxt(l2, 0, 25, 1)
self.disp.addTxt(l1, 0, 37, 1)
self.disp.show()
self.disp.invert()
def showTxtLines(self, texts ):
self.disp.flush()
year, mon, day, h, m, s, nope, nope2 = time.localtime()
y = 0
if((m % 2) == 0):
for text in texts:
self.disp.addTxt(str(text), 0, y, 1)
y += 13
elif((m % 3) == 0):
y = 64
for text in texts:
y -= 13
self.disp.addTxt(str(text), 0, y, 1)
else:
for text in texts:
self.disp.addTxt(str(text), 0, y, 1)
y += 13
self.disp.show()
self.disp.invert()