""" test new circuit python 6.2.0 from 01.04.2021 loaded to Raspberry Pi PICO ( USB connect while [bootsel] button pressed ) download from: https://circuitpython.org/board/raspberry_pi_pico/ for libraries: https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest download like adafruit-circuitpython-bundle-6.x-mpy-20210403.zip find: adafruit-circuitpython-bundle-6.x-mpy-20210403\lib\adafruit_dht.mpy and copy into PICO: /media/pi/CIRCUITPY/lib/adafruit_dht.mpy now can use: import adafruit_dht """ import board import time import supervisor import microcontroller from analogio import AnalogIn analog_in0 = AnalogIn(board.A0) # GP26 pin 31 analog_in1 = AnalogIn(board.A1) # GP27 pin 32 analog_in2 = AnalogIn(board.A2) # GP28 pin 34 def get_volt(pin): return (pin.value * 3.3) / 65536 def get_pct(pin): return (pin.value * 100.0) / 65536 #import busio #i2c = busio.I2C(scl=board.GP9, sda=board.GP8) # gives a: RuntimeError: No pull up found on SDA or SCL; check your wiring #print("Hello, i am PICO use I2C / SDA GP8 pin11 / SCL GP9 pin 12") import adafruit_dht print("dht-library loaded") dhtDevice = adafruit_dht.DHT22(board.GP7) # GP7 pin 10 """ sleep_timer = 2.0 retries = 3 samples = 3 # a good stand alone function, now unused and rewrite for non blocking... def sample_and_avg_DHT(sleep_timer, retries, samples): # takes min 6 sec or forever valid = 0 invalid = 0 temperature_c =0 humidity = 0 while valid < samples: try: temperature_c = temperature_c + dhtDevice.temperature humidity = humidity + dhtDevice.humidity valid += 1 print("valid: {}".format(valid) ) except: invalid += 1 print("invalid: {}".format(invalid) ) if invalid > retries: print("Retries exceeded Aborting") print("Last Error{}".format(dhtDevice.exit())) return(-1,-1) time.sleep(sleep_timer) return (round(temperature_c/valid, 1), round(humidity/valid,1)) """ valid = 0 invalid = 0 noskip = False # we assume we come from a one sec flag but for DHT need a two sec sampling diagprint = True # as parameter def get_dht(diagprint): global valid global invalid global dhtT global dhtH global noskip #local filter tuning fA = 0.9 fB = 1.0-fA if ( noskip ): try: newT = dhtDevice.temperature newH = dhtDevice.humidity valid += 1 if (diagprint): print("valid: {}, newT: {}, newH: {}".format(valid, newT,newH) ) dhtT = dhtT*fB + newT*fA dhtH = dhtH*fB + newH*fA noskip = False except: invalid += 1 if (diagprint): print("invalid: {}".format(invalid) ) else: # wait one more sec loop noskip = True def inputavail(): global inval if supervisor.runtime.serial_bytes_available: inval = input().strip() # as soon you start typing anything, it blocks until [enter] !! if ( inval == 'p' or inval == 'v' or inval == 't' or inval == 'w' ): print("ok") elif ( inval == 'q' ): loop = False print("END") elif ( inval == 'h' or inval == '?' ): help() else: print("what was that?") inval = 'p' def help(): print("Hello, i am Raspberry Pi PICO") print("and use Ain 0,1,2 pin 31,32,34 and print as PCT") print("with DHT22 sensor use GP7 pin 10") print("operation: type in REPL:") print("for Ain as VOLT or PCT, 'v' or 'p'") print("or cpu temperature 't'") print("or 'w' for DHT sensor") print("or 'h' or '?' for this help") print("or 'q' / [ctrl][d] for stop") #_____________________________________________________ globals loop = True inval = 'p' lastsec = 0 inow = 0 measure = False dhtT =-999.0 dhtH = -999.0 #_____________________________________________________ MAIN help() while loop: if ( measure ): measure = False # reset until main loop find new second if ( inval == 'p' ): print("Ain: {0:5.1f}, {1:5.1f}, {2:5.1f}, [PCT]".format(get_pct(analog_in0),get_pct(analog_in1),get_pct(analog_in2)) ) if ( inval == 'v' ): print("Ain: {0:5.1f}, {1:5.1f}, {2:5.1f}, [Volt]".format(get_volt(analog_in0),get_volt(analog_in1),get_volt(analog_in2)) ) if ( inval == 't' ): print("cpu temperature: {0:5.1f} [C]".format(microcontroller.cpu.temperature) ) if ( inval == 'w' ): # get temp and humidity from DHT22 get_dht(diagprint) # every 2 secs only and filtered to dhtT and dhtH, with (diagprint) print("DHT22: Temperature:{0:5.1f} [C], Humidity:{1:5.1f} [%]".format( dhtT, dhtH ) ) #print("DHT22: Temperature:{} [C], Humidity:{} [%]".format(*sample_and_avg_DHT(sleep_timer, retries, samples))) inputavail() # replace the time.sleep(1.0) with a 1 sec flag inow = time.time() # sec after 1.1.1970 if ( inow > lastsec ): lastsec = inow measure = True # seconds flag # end