I tried to use pms5003 wich give air quality data and need the uasyncio module.
I would like display data on a epaper.
I tried to add epaper code in the pms module in a simple definition (line 59) that I call in the def(print) of pms module (line 449). But after 2 rights cycles, nothing happen.
Is it possible to do it? Or usyncio means no more sensor or display? Maybe uasyncio need correct syntax, not like mine... with a new class... or maybe I could recall everything every 2 cycles wich have 60 sec of duration each one, with an other command of the run_forever command?
the code of my pms module:
Code: Select all
import uasyncio as asyncio
import time
###others datas of pms and epaper init
import epaper2in9
from pyb import SPI
#from pyb import LED
#from pyb import Switch
import pyb
#from machine import I2C, Pin
#from pycom_bme680 import *
from time import sleep
#import sgp30
#from ads1x15 import *
#import os
#sw = Switch()
#log = open('/sd/donnees/donnees.txt', 'wb')
###bmp680
#i2c=I2C(-1, Pin("X9"), Pin("X10"))
#bme=BME680_I2C(i2c)
####adc for 3 gas
#adc = ADS1115(i2c = i2c, address = 72, gain = 4)
###sgp
#sgp = sgp30.Adafruit_SGP30(i2c)
# SPI3 on Black STM32F407VET6
spi = SPI(2, SPI.MASTER, baudrate=2000000, polarity=0, phase=0)
cs = pyb.Pin('Y7')
dc = pyb.Pin('Y5')
rst = pyb.Pin('Y4')
busy = pyb.Pin('Y3')
e = epaper2in9.EPD(spi, cs, dc, rst, busy)
e.init()
w = 128
h = 296
x = 0
y = 0
import framebuf
buf = bytearray(128 * 296 // 8)
fb = framebuf.FrameBuffer(buf, 128, 296, framebuf.MONO_HLSB)
black = 0
white = 1
#global temp_affichage
#global temp_delay
#temp_delay = time.time()
def brut():
#temp_affichage = time.time()
#if (temp_affichage - temp_delay) >= 60:
# temp_delay = time.time()
#if sw.value() == False:
#date
date_now = time.localtime()
jour = date_now[2]
mois = date_now[1]
heure = date_now[3]
minu = date_now[4]
sec = date_now[5]
jour = str(jour)
mois = str(mois)
heure = str(heure)
minu = str(minu)
sec = str(sec)
#bme
# bmet = round(bme.temperature, 2)
# bmep = round(bme.pressure, 2)
# bmeh = round(bme.humidity - 10, 2)
# bmep = str(bmep)
# bmet = str(bmet)
# bmeh = str(bmeh)
#sgp
# co2eq, tvoc = sgp.iaq_measure()
###recalibration
# co2 = co2eq * 10 - 3600
# voc = tvoc * 8
# co2eq = str(co2eq)
# tvoc = str(tvoc)
###3 gas
# value_no2 = adc.read( rate=0, channel1=2 )
# value_nh3 = adc.read( rate=0, channel1=1 )
# value_co = adc.read( rate=0, channel1=0 )
# no2 = value_no2 * (10*(0.512/32767))
# nh3 = value_nh3 * (500*(0.512/32767))
# co = value_co * (1000*(0.512/32767))
# no2 = round(no2, 3)
# nh3 = round(nh3, 3)
# co = round(co, 3)
# no2 = str(no2)
# nh3 = str(nh3)
# co = str(co)
#epaper
fb.fill(white)
fb.text(jour,5,5,black)
fb.text("/",20,5,black)
fb.text(mois,30,5,black)
fb.text('--',40,5,black)
fb.text(heure,57,5,black)
fb.text(':',70,5,black)
fb.text(minu,76,5,black)
fb.text(':',90,5,black)
fb.text(sec,95,5,black)
# fb.text('Temp = ',5,30,black)
# fb.text(bmet,55,30,black)
# fb.text(' C',90,30,black)
#fb.text('Pres= ',5,50,black)
# fb.text(bmep,50,50,black)
# fb.text(' HPa',95,50,black)
# fb.text('Hum = ',5,70,black)
# fb.text(bmeh,55,70,black)
# fb.text(' %',90,70,black)
# fb.text('COV = ',5,130,black)
# fb.text(tvoc,55,130,black)
# fb.text(' ppb',80,130,black)
# fb.text('CO2 = ',5,150,black)
# fb.text(co2eq,55,150,black)
# fb.text(' ppm',80,150,black)
# fb.text('CO = ',5,170,black)
# fb.text(co,55,170,black)
# fb.text(' ppm',90,170,black)
#fb.text('NH3 = ',5,190,black)
# fb.text(nh3,55,190,black)
# fb.text(' ppm',90,190,black)
# fb.text('NO2 = ',5,210,black)
# fb.text(no2,55,210,black)
# fb.text(' ppm',90,210,black)
e.set_frame_memory(buf, x, y, w, h)
e.display_frame()
# LED(3).on()
# log.write('{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(jour,mois,heure,minu,bmet,bmep,bmeh,tvoc,co2eq,no2,nh3,co))
# LED(3).off()
# if sw.value() == True:
# log.close()
# LED(2).on()
# time.sleep(10)
# LED(2).off()
#########pms
try:
import struct
except ImportError:
import ustruct as struct
# Sensor settling after wakeup requires at least 30 seconds (sensor sepcifications).
WAIT_AFTER_WAKEUP = 40
# Normal data frame length.
DATA_FRAME_LENGTH = 28
# Command response frame length.
CMD_FRAME_LENGTH = 4
# Maximum tries after which the device is being reset, the actual tries are twice as much as library tries 2 times
MAX_COMMAND_FAILS = 3
DEBUG = False
def set_debug(debug):
global DEBUG
DEBUG = debug
class PMS5003_base:
def __init__(self, uart, lock, set_pin=None, reset_pin=None, interval_passive_mode=None, event=None,
active_mode=True, eco_mode=True, assume_sleeping=True):
self._uart = uart # accepts a uart object
self._set_pin = set_pin
if set_pin is not None:
set_pin.value(1)
self._reset_pin = reset_pin
if reset_pin is not None:
reset_pin.value(1)
self._active = True
self._active_mode = active_mode # passive mode will be set on first wakeUp() in _read()
self._eco_mode = eco_mode # only works with passive mode as sleep is not possible in active_mode
self._sreader = asyncio.StreamReader(uart)
self._interval_passive_mode = interval_passive_mode or 60 # in case someone forgets to set it
if self._eco_mode and self._active_mode is False and self._interval_passive_mode < WAIT_AFTER_WAKEUP + 5:
self._error(
"interval_passive_mode can't be less than DEVICE_WAKEUP_TIME of {!s}s".format(WAIT_AFTER_WAKEUP + 5))
self._interval_passive_mode = 60
self._event = event
self._lock = lock
self._timestamp = None
self._sleeping_state = assume_sleeping # assume sleeping on start by default
self._invalidateMeasurements()
self._callback = None # can be a short coroutine too; no args given
asyncio.get_event_loop().create_task(self._read())
[...........]
def print(self):
if self._active and self._timestamp is not None:
print("")
print("---------------------------------------------")
t = time.localtime()
print("Measurement {!s}ms ago at {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
time.ticks_ms() - self._timestamp, t[0], t[1], t[2], t[3], t[4], t[5]))
print("---------------------------------------------")
print("Concentration Units (standard)")
print("---------------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" % (self._pm10_standard, self._pm25_standard, self._pm100_standard))
print("Concentration Units (environmental)")
print("---------------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" % (self._pm10_env, self._pm25_env, self._pm100_env))
print("---------------------------------------------")
print("Particles > 0.3um / 0.1L air:", self._particles_03um)
print("Particles > 0.5um / 0.1L air:", self._particles_05um)
print("Particles > 1.0um / 0.1L air:", self._particles_10um)
print("Particles > 2.5um / 0.1L air:", self._particles_25um)
print("Particles > 5.0um / 0.1L air:", self._particles_50um)
print("Particles > 10 um / 0.1L air:", self._particles_100um)
print("---------------------------------------------")
print("")
####brut
brut()
time.sleep(30)
###
else:
print("PMS5003 Sensor not active")
async def _read(self):
woke_up = None
if self._sleeping_state:
await self.wakeUp() # just in case controller rebooted and left device in sleep mode
woke_up = time.ticks_ms()
elif self._active_mode is False:
await self.setPassiveMode()
last_reading = time.ticks_ms()
[........]
Thanks.