from machine import Pin, ADC import network import time from umqttsimple import MQTTClient import sys from machine import Pin, time_pulse_us import uasyncio trigger_pin = Pin(12, Pin.IN, Pin.PULL_UP) dataQueue = [] WIFI_SSID = 'WLAN1' WIFI_PASSWORD = 'aftgr3en61022412' mqtt_client_id = 'testclient' # Just a random client ID ADAFRUIT_IO_URL = '10.28.1.119' ADAFRUIT_USERNAME = '' ADAFRUIT_IO_KEY = '' TOGGLE_FEED_ID = 'TOPIC' async def SendData(): while True: print(len(dataQueue)) if len(dataQueue) > 0: wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.disconnect() wifi.connect(WIFI_SSID,WIFI_PASSWORD) if not wifi.isconnected(): print('connecting..') timeout = 0 while (not wifi.isconnected() and timeout < 100): print(100 - timeout) timeout = timeout + 1 time.sleep(1) if(wifi.isconnected()): print('connected') else: print('not connected') sys.exit() #connect_wifi() # Connecting to WiFi Router client = MQTTClient(client_id=mqtt_client_id, keepalive=30, server=ADAFRUIT_IO_URL, port = 1883) try: client.connect() except Exception as e: print('could not connect to MQTT server {}{}'.format(type(e).__name__, e)) sys.exit() print(dataQueue[0]) client.publish('testclient', str(dataQueue[0]), qos=1) await uasyncio.sleep_ms(100) client.disconnect() wifi.disconnect() dataQueue.clear() await uasyncio.sleep_ms(1000) def pulse2liter(pin): # Time a pulse on the given pin, and return the duration of the pulse in microseconds. # The pulse_level argument should be 0 to time a low pulse or 1 to time a high pulse. result = time_pulse_us(pin, 1) if result in (-1, -2): return 0 # alles was kürzer als 400 ms ist if result <= 400000: return 1 async def main(): x = 1 startzeit = time.time() countall = 0 while True: try: count = pulse2liter(trigger_pin) if count > 0: countall = count + countall if time.time() > startzeit + x*60: dataQueue.append(countall) #uasyncio.create_task(SendData()) countall = 0 startzeit = time.time() countall = countall + 1 print(countall) await uasyncio.sleep_ms(100) except: sys.exit() async def runBothThreads(): uasyncio.create_task(main()) uasyncio.create_task(SendData()) while True: await uasyncio.sleep_ms(100) uasyncio.run(runBothThreads())