Establishing MQTT Communication With A9G Board Using Ai-Thinker Serial Tool
by Fusion Automate in Circuits > Microcontrollers
363 Views, 0 Favorites, 0 Comments
Establishing MQTT Communication With A9G Board Using Ai-Thinker Serial Tool
In this guide, we'll walk through the steps to set up an A9G board to communicate with a cloud-based MQTT broker. MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol widely used in IoT applications for efficient communication between devices and servers.
Setting Up A9G Board for MQTT Communication
Activating GPRS
GPRS (General Packet Radio Service) activation is essential for establishing cellular communication. Use the AT+CGATT=1 command to activate GPRS and enable data transmission over the cellular network.
AT+CGATT=1
Configuring Access Point Name (APN)
Configure the Access Point Name (APN) settings using the AT+CGDCONT command. APN settings provide the necessary connection parameters for accessing the internet via the cellular network.
AT+CGDCONT=1,"IP","CMNET"
Activating PDP Context
Activate the Packet Data Protocol (PDP) context using the AT+CGACT command. PDP context activation establishes a data connection, enabling internet access for the A9G board.
AT+CGACT=1,1
Connecting to MQTT Broker
Establish a connection to the MQTT broker using the AT+MQTTCONN command. Provide the broker address, port, client ID, and authentication credentials to establish the MQTT connection.
AT+MQTTCONN="0.tcp.in.ngrok.io",12945,12345,120,0,"",""
AT+MQTTCONN="<Host>",<Port>,<ClientID>,<KeepAlive>,<CleanSession>,"<Username>","<Password>"
Here's a breakdown of the AT+MQTTCONN command parameters:
- <Host>: MQTT broker hostname or IP address.
- <Port>: Port number on which the MQTT broker is listening.
- <ClientID>: Unique client identifier for the A9G board.
- <KeepAlive>: Keep-alive interval for the MQTT connection.
- <CleanSession>: Indicates whether the broker should clean session state.
- <Username> and <Password>: Optional authentication credentials.
Publishing a Message
Publish a message to an MQTT topic using the AT+MQTTPUB command. Specify the topic, message content, QoS level, retain flag, and duplicate flag in the command.
AT+MQTTPUB="Test","Hello from Ai-Thinker Serial Tool",0,0,0
AT+MQTTPUB=<topic>,<message>,<QoS>,<retain>,<dup>
- <topic>: The MQTT topic where the message will be published. It must be enclosed in double quotes ("). For example, "Test" represents the topic in your command.
- <message>: The content of the message to be published. It must be enclosed in double quotes ("). For instance, "Hello from Ai-Thinker Serial Tool" is the message content in your command.
- <QoS>: The Quality of Service (QoS) level for message delivery. It should be an integer: 0, 1, or 2. In your command, 0 signifies QoS level 0 (at most once delivery).
- <retain>: Indicates whether the broker should retain the message for future subscribers. It can be 0 (do not retain) or 1 (retain). In your command, 0 means the message should not be retained.
- <dup>: Denotes whether the message is a duplicate. It can be 0 (not a duplicate) or 1 (duplicate). In your command, 0 indicates the message is not a duplicate.
So, in summary, the AT+MQTTPUB command in your example publishes the message "Hello from Ai-Thinker Serial Tool" to the MQTT topic "Test" with QoS level 0, not retaining the message, and indicating that it is not a duplicate.
Conclusion
Establishing MQTT communication with an A9G board opens up possibilities for IoT applications requiring cellular connectivity. By following these steps, you can seamlessly integrate the A9G board into your IoT projects and leverage the power of MQTT for efficient data exchange.