MQTT not working with correctly with values published from Mobile App.

Yo Apps on 18 Apr 2022 (Edited on 18 Apr 2022)
Latest activity Edit by Vinod on 19 Apr 2022

Hi... I managed to work out both the pub & sub parts of MQTT with my esp8266. I did the example from over here ... and all works fine. The publishing happens every 15secs... and it received the published values back almost immediately. see Img1.png

Now using flutter from my mobile app I did the same thing... and I was able to pub & sub... and also checked the Field1 values in my Thingspeak account that i am writing to and reading as an MQTTclient. It all works correctly.

HOWEVER... heres the problem================ When I only publish from the mobile a value for Field1. And subscribing at the same time from my esp8266 to receive the updated value of Field1... something weird happens... My Field1 in the channel, in my Thingspeak account has been updated correctly... but my esp8266 doesnt receive the updated value. INSTEAD, at the same time I can see in the serial monitor logs, the esp8266 reconnects back to our MQTT server!! No update was received. It felt as if everytime I published a value from my mobile, it made my esp8266 disconnect and hence it needed to reconnect to the MQTT server. i checked this 10 times and it happened without a coincidence. I ran the same code on the esp8266, except that I never let it publish every 15secs. So all it did was stay subscribed and await an update from my mobile app. please see img2.png

and this is the flutters connection code

 client.logging(on: false);
    client.setProtocolV311();
    client.keepAlivePeriod = 10;
    client.onDisconnected = onDisconnected;  //....callbacks
    client.onConnected = onConnected;  //....callbacks
    client.pongCallback = pong;  //....callbacks
    final connMess = MqttConnectMessage()
        .withClientIdentifier('xxxxxxxxxxxxxxxx')
        .withWillTopic('willtopic') // If you set this you must set a will message
        .withWillMessage('My Will message')
        .startClean() // Non persistent session for testing
        .authenticateAs('xxxxxxxxxxxxxxxx', 'aaaaaaaaaaaa')
        .withWillQos(MqttQos.atMostOnce); // means = 0
    print('client connecting....');

Any help will be appreciated. Just wanted to iterate that individually they both work fine. Just that i need the updated value... and i dont receive it. Any ideas?? thanks, YOhan

Yo Apps
Yo Apps on 19 Apr 2022

Hey Guys! Today is a great day!! I tried what you guys pointed out. Added a new MQTT device and... BOOOM! all is working brilliantly.

I wanted to thank both of you- Chisrtopher & Vinod for your great support because there really arent too many resources out there to show you Flutter + MQTT + Thingspeak. And the feeling of being lost mid way would lead to so much time wasting that I would have reverted back to the old concept of polling. Thanks again!

Vinod
Vinod on 19 Apr 2022 (Edited on 19 Apr 2022)

That's great to hear. Please consider sharing your code on GitHub with a link here so others may also benefit from it. Please redact your clientid, username and password when you put the code on GitHub 😀

Christopher Stapels
Christopher Stapels on 18 Apr 2022

Thanks for taking the time to make a detailed post! If you use the same client ID, one device will be disconnected. Are you using the same client ID for both devices?

Yo Apps
Yo Apps on 18 Apr 2022

Hi Christopher, Thanks for your reply! Wow.. You are right, I am using the same client ID. So, what you are advising is to make another MQTT device.. And use that devices clientID for the other my mobile? If yes .. Will give it a shot and revert back asap :-)

Vinod
Vinod on 19 Apr 2022

Each triplet of ClientID, Username and Password represents one device connecting to ThingSpeak with those credentials. If a second device connects with the same credentials as a device already connected, the first device gets disconnected.

You need to use one "MQTT device" for the ESP8266 and add another "MQTT device" for the phone. Remember to set up the permissions correctly when adding these devices.