MQTT X and thingspeak - lost connection

Hi :-)
I have followed the instructions on mathworks site:
I have downloaded the MQTT X software.
Then I add a new connection as described. The username, password and ID i get from my thingspeak device in my account.
I then at 3) "Click" the connect button, the program replies "connected". But within 3-6 secounds the connection is lost... so I never get to test out the rest of the subscription and publish.
This should not be a difficult task, but I an stuck :-(
Are there anyone experiensing the same?
mvh vegar bartnes

6 Comments

You might be disconnected if you tried to send a publish message or subscribe to a channel that you hadn’t authorized, but it sounds like you aren’t doing that. Your client may be attempting to set some other preference that is causing disconnect. Have you checked the QOS and LWT settings?
The last thing I can think of is if your internet connection was intermittent, you might be disconnected. Is the problem consistent across different locations or different computers if you have a chance to test for that?
I never get past this third point at the instructions... So no publish and subrscribe is done... I lose the connection 😰 I have tried three different nettworks including gsm. Two different computers, it behaves the same way. I use QOS and lwt settings as I do with mosquitto. Vegar
Thanks for the feedback, it definitely sounds frustrating. Il ask around and see what I can find. The QoS and LWT settings may not necessarily math Mosquito. I would check then versus the doc.
Hi :-)
Something has changed. I have done nothing, but now I get no connection at all.
Now I get "Error: read ECONNRESET" is this a well known error.
I will try to delete my device in thingspeak and start over.
vegar
I have resett my device in thingspeak, new ID and password. Still the same read ECONNRESET.
Then I am not able to add new subscription because no connection.
vegar
@Vegar Bartnes: Did you see my screenshot below showing the connection settings? If you are still unable to connect, please share your clientID you are using to connect.

Sign in to comment.

Answers (2)

Stonez Chen
Stonez Chen on 13 Mar 2022
Edited: Stonez Chen on 13 Mar 2022
Hi Christopher,
I'm making a YouTube video in Traditional Chinese to show people how to use ThingSpeak MQTT.
However, I'm also facing the same problem that both my ESP32 & Node-RED lost mqtt3.thingspeak.com connection right after a successful connection. Both ESP32 & Node-RED are under the same Wi-Fi AP.
I'm sure that MQTT device is created and mapped to a Channel.
Server: mqtt://mqtt3.thingspeak.com
Retain: False
QoS: 0
Clean session: 1
I use client.state() to check on my ESP32 and the result is: -3 (MQTT_CONNECTION_LOST - the network connection was broken)
What could be the problem? Server issue or mine issue?
Thanks,
Stonez
PS: My last video is with home hosted MQTT server: MQTT + ESP32 + Mosquitto on Raspberry Pi

8 Comments

I really appreciate you putting together a movie. I don't see an error in what you have there. Can you try the desktop client out just to reduce the variables even more? If that works fine, can you share the connection part of your code? I once had an error were my cleint would disconnect if I didnt service the MQTT connection each time through my loop, though I suspect thats not necessarily your error.
Thanks for the quick reply.
I tried both Node-RED and MQTT Lens just now and both of them connected to mqtt3.thingspeak.com and disconnected right away. This cycle just continues and connected and disconnected. Could it be my account, channels, or my device Mac address being banned?
Here are screenshots for your refernce.
Screenshot 1-1:
I managed to get 2 message to the channel 1675230 with MQTT Lens. However, the message "true" didn't write to the csv file
Screenshot 1-2: Downloaded channel data, no message was written
Screenshot 2: MQTT connection successful
Screenshot 3: MQTT lost connection
Screenshot 4: MQTT connection re-established:
Here is the log from the Node-RED:
14 Mar 21:12:43 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:12:43 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:12:59 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:00 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:13:00 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:13:00 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:17 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:17 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:30 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:13:30 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:13:37 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:37 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:54 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:13:54 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:00 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:14:00 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:14:12 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:13 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:29 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:29 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:30 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:14:30 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:14:46 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:14:46 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:00 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:15:00 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:15:04 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:05 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:21 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:21 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:30 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:15:30 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:15:36 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:37 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:54 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:15:54 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:16:00 - [error] [function:Experiment(short version)] TypeError: Cannot read property '0' of undefined
14 Mar 21:16:00 - [error] [function:Format data for InfluxDB(Long)] TypeError: Cannot read property '0' of undefined
14 Mar 21:16:10 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:16:10 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:16:26 - [info] [mqtt-broker:ThingSpeak] Connected to broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
14 Mar 21:16:26 - [info] [mqtt-broker:ThingSpeak] Disconnected from broker: NyYnGAocADoBGAUdCgUZIiM@mqtt://mqtt3.thingspeak.com:1883
Vinod
Vinod on 14 Mar 2022
Edited: Vinod on 14 Mar 2022
Looking at our logs with the clientID, I can see that the connection is being disconnected because the client is trying to subscribe to an invalid topic "living.light2". ThingSpeak only supports specific topics related to ThingSpeak channels.
Also, with a free license you can only update a channel once every 15 seconds or slower. How frequently are you publishing?
Please look at Screenshot 4: MQTT connection re-established above:
The topic I try to publish is "channels/1675230/fields/field1/publish", not "Living.Light2".
See my setting in channel:
Vinod
Vinod on 14 Mar 2022
Edited: Vinod on 14 Mar 2022
@Stonez Chen The topic you need to publish to is channels/1675230/publish. The payload you will be publishing is field1=22&field2=33... etc. See the screenshot in my answer below.
Thanks for the syntax reminder. However, my main question was how did my MQTT connection got cut off right after a successful connection? How can I correct the disconnection problem?
Stonez
@Stonez Chen: Your connection will be disconnected if you try to publish or subscribe to an invalid topic or a topic your client is not authorized to access. So, if you only publish/subscribe to topics with valid payloads, your connection should be stable.
Stonez Chen
Stonez Chen on 16 Mar 2022
Edited: Stonez Chen on 16 Mar 2022
@Vinod You are right! A soon as I modifyed :
Publish topic: channels/1675230/publish
Message: field1=true
The MQTT worked! The issue got me for few days....Thanks!
Stonez

Sign in to comment.

Vinod
Vinod on 14 Mar 2022
Edited: Vinod on 14 Mar 2022
Here's the settings you need to connect with MQTTX:

5 Comments

Yes, I see this. But I am not allowed to add new Subscription bacause no connection.
my channel is VentilDuino : 1081886
I have tryed with SSL and no SSL.
with SSL:"Client network socket disconnected before secure TLS connection was established"
Ups! Sorry, I get connected when I now change to Mobile broad band nettwork... It is now firewall issue..... :-)
But is was not like this on Friday.....
vegar
Now with a brand new channel it works, almost...
Field1 does not work?
Ups again! just a empty space. Everything works fine now!
vegar

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Asked:

on 9 Mar 2022

Edited:

on 16 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!