api.thigspeak vs mqtt.thingspeak

2 views (last 30 days)
Gulzar Singh
Gulzar Singh on 4 Jun 2023
Edited: Ayush on 6 Jun 2023
I want to use MQTT broker service of Thingspeak . I have following queries: 1. After creating an account and channel, there are two APIs of type api.thingspeak for read and write. Are these APIs for http (REST) ? 2. In some tutorials, I found that mqtt.thingspeak or mqtt3.thigspeak should be used. 3. I tried mqtt.thingspeak, after sending a CONNECT packet to this, immediately closed response is sent by esp8266. 4. May the message for topic be sent inside CONNECT packet and will that message published ?, if yes, to which channel ? 5. Does each channel has separate user name and password to use in MQTT CONNECT, PUBLISH and SUBSCRIBE packet ? 6. Before sending, PUBLISH or SUBSCRIBE packet, is it required to send CONNECT packet ? if yes, then, the close response must not be sent immediately ? In what condition connection is closed immediately ?

Answers (1)

Ayush
Ayush on 4 Jun 2023
Edited: Ayush on 6 Jun 2023
Hi Gulzar, Answering all your queries one by one:
Query 1 : Are the two thingspeak APIs for read and write are for HTTP (REST).
Answer 1: Yes, the APIs provided by Thingspeak for reading and writing data to your channel are HTTP REST APIs. The API calls are made using HTTP GET or POST requests to interact with Thingspeak server to update and retrieve data.
You can refer more details about the APIs endpoint for reading and writing data here
Query 2 : In some tutorials, I found that mqtt3.thigspeak should be used. I tried mqtt.thingspeak, after sending a CONNECT packet to this, immediately closed response is sent by esp8266. May the message for topic be sent inside CONNECT packet and will that message published ?
Answer 2 : To use the ThingSpeak MQTT broker service, you should connect to mqtt3.thingspeak.com using a MQTT library. It's possible that the response is being closed due to issues in your code or with your ESP8266 setup.
For resolving the error, you might check the following:
  • Make sure you are using the correct certificates and port (encrypted vs unencrypted)
  • Check if there is a firewall setting that is blocking your connection
  • Verify that your ESP8266 is able to connect to other MQTT brokers and devices over the network
The CONNECT packet is used by your device to establish a connection with the MQTT broker. You need to specify the topic you want to publish messages to separately. The message payload will be sent in a separate message packet after the connection is established. You can specify the topic when creating a MQTT client object using a MQTT library in your programming language of choice. Once the connection is established, you can then publish your message to the specified topic.
Query 3 : Does each channel has separate user name and password to use in MQTT CONNECT, PUBLISH and SUBSCRIBE packet ? Before sending, PUBLISH or SUBSCRIBE packet, is it required to send CONNECT packet?if yes, then, the close response must not be sent immediately ? In what condition connection is closed immediately ?
Answer 3: Each ThingSpeak channel has a unique Write API key and Read API key. These keys can be used to authenticate and authorize MQTT clients to publish and subscribe to the ThingSpeak MQTT broker service.
Also, for your next query, YES , before sending PUBLISH or SUBSCRIBE packet, it is required to send a CONNECT packet to establish a connection with the ThingSpeak MTT broker service. (also as mentioned above, this CONNECT packet should contain the required authentication values (API keys)).
Now, if the connection is closed immediately after sending the CONNECT Packet , it cold mean there is an issue with the authentication or authorization values in the CONNECT Packet.In some cases, sending invalid authentication or authorization values can cause the broker to immediately close the connection. To resolve this issue, you should verify that the authentication and authorization values being sent in the CONNECT packet are correct and valid.
Now after the communication (publishing or subscribing to data) , you can send a DISCONNECT packet to close the connection.
I have broken your query into 3 parts and tried to explain each part in a little detail. Hope it helps, for any queries, do comment below.
You may refer to the documentation for reading in more detail.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!