peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

can not handle 2 PINs at same time #123

Closed armeya25 closed 8 months ago

armeya25 commented 8 months ago

main.txt

can't toggle pin23 and pin18 same time, when pin23 on cant turn on pin18 and vice versa

peterhinch commented 8 months ago

Please could you enclose your code in triple backticks (`) characters so that the indentation is retained.

armeya25 commented 8 months ago

Please could you enclose your code in triple backticks (`) characters so that the indentation is retained

armeya25 commented 8 months ago

Please could you enclose your code in triple backticks (`) characters so that the indentation is retained.

i added main.txt plz help me, thanks i advance

armeya25 commented 8 months ago

added main.txt plz verify it

peterhinch commented 8 months ago

I can't replicate this. I ran your code on an ESP32-S3. I had to change one of the pin numbers and also added a print statement:

from mqtt_as import MQTTClient, config
import asyncio
from machine import Pin

# Local configuration
config['ssid'] = 'misspiggy'
config['wifi_pw'] = 'my password'
config['server'] = '192.168.0.10'  # Change to suit e.g. 'iot.eclipse.org'

pin23 = Pin(13, Pin.OUT)  # Blue LED (pin 23 is invalid on my board)
pin23.off()
pin18 = Pin(18, Pin.OUT)
pin18.off()

async def messages(client):  # Respond to incoming messages
    async for topic, msg, retained in client.queue:
        t = topic.decode()
        m = msg.decode()
        print(t, m)  # Added print statement

        if t == "esp32/relay1":
            if m == "on":
                pin23.on()
            elif m == "off":
                pin23.off()

        if t == "esp32/relay2":
            if m == "on":
                pin18.on()
            elif m == "off":
                pin18.off()

async def up(client):  # Respond to connectivity being (re)established
    while True:
        await client.up.wait()  # Wait on an Event
        client.up.clear()
        await client.subscribe('esp32/relay1', 1)  # renew subscriptions
        await client.subscribe('esp32/relay2', 1)

async def main(client):
    await client.connect()
    for coroutine in (up, messages):
        asyncio.create_task(coroutine(client))
    n = 0
    while True:
        await asyncio.sleep(5)

config["queue_len"] = 1  # Use event interface with default queue size
MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(config)
try:
    asyncio.run(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors

Here was a session

>>> import rats3
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Connected to broker.
RAM free 107040 alloc 20960
RAM free 107040 alloc 20960
RAM free 107040 alloc 20960
RAM free 107040 alloc 20960
esp32/relay1 on
RAM free 106912 alloc 21088
esp32/relay2 on
RAM free 106912 alloc 21088
esp32/relay2 off
RAM free 106912 alloc 21088
esp32/relay1 off

The messages were received correctly and the blue LED behaved as expected. I tested as follows (typical message):

$ mosquitto_pub -h 192.168.0.10 -t esp32/relay1 -m "on" -q 1

As far as I can tell mqtt_as is behaving correctly.

armeya25 commented 8 months ago

sorry my mistake i was not paying attension to wiring, i connected them to series thats why LEDs behave like that,

ebolisa commented 8 months ago

🤦🏻‍♂️