These examples publish data from the Pico LTE to a ThingSpeak channel over MQTT, and subscribe to the channel’s fields, over the cellular connection. No extra hardware is needed.
Before you begin
Hardware
- Sixfab Pico LTE or Pico LTE 2Registered and active in Sixfab Connect, connected to the computer with the micro USB cable.
Software
- Thonny with MicroPython and the Pico LTE SDKSet up as in the quickstart.
- config.jsonIn the root of the board: Prepare the configuration file.
- ThingSpeak accountA MathWorks account.
Create channel
-
Open your channels.
Sign in to ThingSpeak with your MathWorks account, or create one, and open Channels → My Channels.

-
Create channel.
Click New Channel, tick Field 1 and Field 2, set the other values as you need (they can be changed later) and click Save Channel.

The channel has these tabs: Private View (only you see it), Public View (selected fields and charts, if the channel is public), Channel Settings (edit, clear or delete the channel), Sharing (private, public or shared with given users), API Keys (to read and write the channel) and Data Import/Export. The channel ID is shown on the channel page.
Create MQTT device
A ThingSpeak MQTT device holds the credentials of your MQTT client and the channels it may use.
-
Open MQTT devices.
In the ThingSpeak menu, click Devices → MQTT, then Add a new device.


-
Describe device.
Enter a name and, optionally, a description. Under Authorize Channels, select your channel and click Add Channel; repeat for each channel, and allow publish and subscribe as needed. Click Add Device.

-
Save credentials.
ThingSpeak shows the client ID, the username and the password of the device. Click Download Credentials → Plain Text and keep the file, then click Done.
The password is shown only once: save it now.

The device is listed on the MQTT Devices page; Edit changes its settings and channels.
Run examples
The scripts are in the examples/thingspeak folder of the SDK repository.
-
Set channel and credentials in config.json.
{ "thingspeak": { "channel_id": "YOUR_CHANNEL_ID", "mqtts": { "client_id": "DEVICE_MQTT_CLIENT_ID", "username": "DEVICE_MQTT_USERNAME", "password": "DEVICE_MQTT_PASSWORD", "pub_topic": "YOUR_MQTT_TOPIC" } } }The publish topic is
channels/[CHANNEL_ID]/publish. -
Run mqtt_publish.py.
Open
examples/thingspeak/mqtt_publish.pyin Thonny and click Run. It publishes 30 to field 1 and 40 to field 2.
INFO: Publishing data to ThingSpeak... INFO: Result: {'status': 0, 'response': ['OK'], 'interval': 0} -
Check channel.
The values appear in the channel’s charts.

-
Set channel, credentials and topics in config.json.
{ "thingspeak": { "channel_id": "YOUR_CHANNEL_ID", "mqtts": { "client_id": "DEVICE_MQTT_CLIENT_ID", "username": "DEVICE_MQTT_USERNAME", "password": "DEVICE_MQTT_PASSWORD", "sub_topics": [ ["YOUR_MQTT_TOPIC", QOS] ] } } }- ThingSpeak MQTT supports QoS 0 only.
[["channels/[CHANNEL_ID]/subscribe/fields/+", 0]]subscribes to all fields of the channel.[["channels/[CHANNEL_ID]/subscribe/fields/[FIELD_NAME]", 0]]subscribes to one field, such asfield1.
-
Run mqtt_subscribe.py.
Open
examples/thingspeak/mqtt_subscribe.pyin Thonny and click Run. The log shows the messages of the subscribed fields:
INFO: Subscribing to topics... INFO: Result: {'status': 0, 'response': ['+QMTSUB: 0,1,0,0'], 'interval': 0} INFO: Reading messages from subscribed topics... INFO: [{'message id': 0, 'topic': 'channels/.../subscribe/fields/...', 'message': '30'}]