NB-IoT module schematics

How to connect to MQTT broker? NB-IoT Part #3

NB-IoT #3

Configuration and connection to MQTT broker

In the previous article we have described hardware configuration of Waveshare modules SIM7020E and SIM7080G. In this article we are going to present how to configure settings for those modules and how to connect them to a MQTT broker

Initial Configuration

In order to use the module for communication with an external MQTT server, we need to perform some initial configuration first. Commands are given with a description, but for details, you need to refer to SIM7020 Series_AT Command Manual_V1.03 and SIM7080 Series_AT Command Manual_V1.02 (depending on which of the modules you are using). AT commands in blue, comments in green:

 

Example on SIM7020E:

set baudrate:

AT+IPR=115200 

disable entering sleep mode:

AT+CSCLK=0

select band

AT+CBAND=1,20

disable the radio

AT+CFUN=0

setup the APN – use the data from your connectivity provider

AT*MCGDEFCONT=”IP”,”internet.gma.iot”

Operator selection – select according to your location and provider

AT+COPS=1,2,”26002″

 

Reset the module

AT+CRESET

 

Wait at least 30 seconds and check if module got the APN correctly and got registered to network – should return 0,5 or 0,1

 

Example on SIM7080G

 

test basic response from the module

AT

AT

OK

set the baudrate

AT+IPR=115200

 

AT+IPR=115200

OK

 

set the configure the NB-IoT bands

AT+CBANDCFG=”NB-IOT”,1,20

AT+CBANDCFG=”NB-IOT”,1,20

OK

check the signal strength

AT+CSQ

 

AT+CSQ

+CSQ: 27,99

OK

 

switch off the echo

ATE0

 

ATE0

OK

 

disable RF

AT+CFUN=0

+CPIN: NOT READY

OK

 

define PDP context (specify Packet Data Protocol type and APN)

AT+CGDCONT=1,”IP”,”internet.gma.iot”

OK

 

enable RF

AT+CFUN=1

OK 

+CPIN: READY

 

Activate network

AT+CNACT=0,1

OK

+APP PDP: 0,ACTIVE



After completing these steps the module should be ready to set up a MQTT connection.

 

Setting up MQTT connection

In order to do an initial test of the MQTT connection with SIM7020E module it’s best to set up your own MQTT broker (e.g. using Mosquitto) and make it able to access it from outside of your local network using ngrok. Using your own broker enables you to troubleshoot possible problems more easily as you have control over the broker and you can verify the error logs that might appear. Instruction on how to set up Mosquitto broker with ngrok can be found here

 

set data format to raw

AT+CREVHEX=0

create new connection to MQTT broker, use proper address according to your set up broker. Here 0.tcp.eu.ngrok.io is used as an address and 12345 as port number

AT+CMQNEW=”0.tcp.eu.ngrok.io”,”12345″,12000,100

The module should return MQTT id, e.g.

+CMQNEW: 0

Send MQTT Connection Packet. Give the MQTT id returned in previous step as a first parameter and in place of “clientId”, “user” and “password” use data according to your broker and device setup.

AT+CMQCON=0,3,”clientId”,600,0,0, “user”, “password”

OK

AT+CMQSUB=0,”testTopic”,1

OK

AT+CMQPUB=0,”testTopic”,1,0,0,21,”helloWorldFromSIM7020″

OK

+CMQPUB: 0,”testTopic“,1,0,0,21,”helloWorldFromSIM7020

Potential issues

  • on SIM7020E it was important to set the data format to raw form using AT+CREVHEX=0
  • if you would like to use MQTTs (encrypted connection with the broker), the certificate size is limited to 3000 bytes, it might be a problem 

 

Example on SIM7080G:

check the connection by pinging example address

AT+SNPING4=”8.8.8.8″,4,32,3000

ping broker address, 0.tcp.eu.ngrok.io given here as an example and “0.123.456.789” as an example response

AT+CDNSGIP=:0.tcp.eu.ngrok.io”,2,6000

OK

+CDNSGIP: 1,”7.tcp.eu.ngrok.io”,”0.123.456.789″

 

configure the URL of the broker

AT+SMCONF=”URL”,3.125.188.168,13802

OK

 

configure the keepalive time for the connection

AT+SMCONF=”KEEPTIME”,60

OK

Clean session

AT+SMCONF=”CLEANSS”,1

OK

 

set client ID, `yourClientId` given as an example

AT+SMCONF=”CLIENTID”,”yourClientId”

OK

set user name (according to your broker configuration), `yourUserName` given as an example

AT+SMCONF=”USERNAME”,”yourUserName”

OK

 

set password (according to your broker configuration), `yourUserName` given as an example

AT+SMCONF=”PASSWORD”,”yourPassword”

OK

 

connect to the broker

AT+SMCONN

OK

 

subscribe to topic `exampleTopic` with QoS 1

AT+SMSUB=”exampleTopic”,1

OK

 

publish to topic `exampleTopic`, specify message length (18), QoS level (1) and retain parameter (1 – the server keeps messages for this topic pushed by the client). After CR character (pressing `Enter` in PuTTy) you need to enter the message (newline marked with `>`):

AT+SMPUB=”exampleTopic”,18,1,1

>helloFromSim7080G!

+SMSUB: “helloWorld”,”helloFromSim7080G!”

OK

 

Summary

 

In this article, we have presented how to configure SIM7020E and SIM7080G modules to communicate with a test MQTT broker over NB-IoT using a USB-UART converter. In the next article, we will show how to connect the module to ESP32 and implement the communication using the esp_modem component. Stay tuned for the next article!

References:

  1. Pico SIM7020E NB-IoT – Waveshare Wiki
  2. SIM7020 Series_AT Command Manual_V1.03
  3. SIM7020 Series_MQTT(S)_Application Note
  4. Pico SIM7080G Cat-M/NB-IoT – Waveshare WIki
  5. SIM7080 Series_AT Command Manual_V1.02 
  6. SIM7070_SIM7080_SIM7090Series_MQTT(S) _ApplicationNote

 

Recent entries

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.