Hello,
My goal is to connect to my HiveMQ MQTT broker, and publish data. For testing I just want to publish a counter value.
Below is the code i'm currently testing.
#include <SoftwareSerial.h>
// Define the pins for SoftwareSerial
#define SIM7600_TX_PIN 4 // Arduino RX (connect to SIM7600 TX)
#define SIM7600_RX_PIN 5 // Arduino TX (connect to SIM7600 RX)
// Define the pins for PWRKEY and SLEEP
#define PWRKEY_PIN 12 // PWRKEY pin
#define SLEEP_PIN 13 // SLEEP pin
// Create a SoftwareSerial object
SoftwareSerial sim7600Serial(SIM7600_TX_PIN, SIM7600_RX_PIN);
// Counter variable
int counter = 0;
// MQTT Broker Details
const char* MQTT_BROKER = "**URL TO BROKER**"; // Replace with your broker address
const int MQTT_PORT = 8883; // TLS port
const char* MQTT_CLIENT_ID = "myClientID"; // Replace with your client ID
const char* MQTT_USERNAME = "**USERNAME**"; // Replace with your username
const char* MQTT_PASSWORD = "**PASSWORD**"; // Replace with your password
const char* MQTT_TOPIC = "myTopic"; // Replace with your topic
void setup() {
// Initialize the serial communication
Serial.begin(9600); // For debugging (Serial Monitor)
sim7600Serial.begin(115200); // For SIM7600 communication (baud rate set to 115200)
// Initialize the PWRKEY and SLEEP pins
pinMode(PWRKEY_PIN, OUTPUT);
pinMode(SLEEP_PIN, OUTPUT);
// Power on the SIM7600 module
digitalWrite(PWRKEY_PIN, LOW);
delay(1000);
digitalWrite(PWRKEY_PIN, HIGH);
delay(2000);
digitalWrite(PWRKEY_PIN, LOW);
// Disable sleep mode (SLEEP pin HIGH = active mode, LOW = sleep mode)
digitalWrite(SLEEP_PIN, HIGH);
// Wait for the module to initialize
delay(10000);
// Send AT commands to check if the module is responding
sendCommand("AT", 1000);
sendCommand("AT+CPIN?", 1000);
sendCommand("AT+CREG?", 1000);
sendCommand("AT+CGATT=1", 1000); // Enable GPRS
sendCommand("AT+CGATT?", 1000);
// Configure TLS without certificate verification
sendCommand("AT+CSSLCFG=\"sslversion\",0,4", 1000); // Use TLS 1.2
sendCommand("AT+CSSLCFG=\"authmode\",0,1", 1000); // Disable certificate verification
// Initialize MQTT
sendCommand("AT+CMQTTSTART", 1000); // Start MQTT service
sendCommand("AT+CMQTTACCQ=0,\"" + String(MQTT_CLIENT_ID) + "\"", 1000); // Acquire client ID
// Set username and password
sendCommand("AT+CMQTTWILLTOPIC=0," + String(strlen(MQTT_USERNAME)), 1000);
sendCommand(MQTT_USERNAME, 1000);
sendCommand("AT+CMQTTWILLMSG=0," + String(strlen(MQTT_PASSWORD)) + ",1", 1000);
sendCommand(MQTT_PASSWORD, 1000);
// Connect to the MQTT broker
String connectCmd = "AT+CMQTTCONNECT=0,\"" + String(MQTT_BROKER) + "\"," + String(MQTT_PORT) + ",60,1";
sendCommand(connectCmd, 5000); // Connect with TLS
Serial.println("SIM7600 initialization and MQTT setup complete.");
}
void loop() {
// Increment the counter (0-100)
counter++;
if (counter > 100) {
counter = 0;
}
// Publish a message to the MQTT topic
String payload = "Counter: " + String(counter);
sendCommand("AT+CMQTTTOPIC=0," + String(strlen(MQTT_TOPIC)), 1000);
sendCommand(MQTT_TOPIC, 1000);
sendCommand("AT+CMQTTPAYLOAD=0," + String(payload.length()), 1000);
sendCommand(payload, 1000);
sendCommand("AT+CMQTTPUB=0,1", 1000); // Publish with QoS 1
// Wait for 5 seconds before publishing the next message
delay(5000);
}
// Function to send a command and read the response
void sendCommand(const String& command, int delayTime) {
sim7600Serial.println(command);
delay(delayTime);
readResponse();
}
// Function to read the response from the SIM7600 module
void readResponse() {
while (sim7600Serial.available()) {
String response = sim7600Serial.readString();
Serial.println("SIM7600 Response: " + response);
}
}
And, this is what happens in serial monitor. Something is wrong with the MQTT....
22:19:17.922 ->
22:19:19.854 -> SIM7600 Response: AT+CMQTTWILLTOPIC=0,8
22:19:19.893 -> >
22:19:21.869 -> SIM7600 Response: *** MY USERNAME ***
22:19:21.869 -> OK
22:19:21.869 ->
22:19:23.899 -> SIM7600 Response: AT+CMQTTWILLMSG=0,10,1
22:19:23.900 -> >
22:19:25.892 -> SIM7600 Response: *** MY PASSWORD ***
22:19:25.892 -> OK
22:19:25.892 ->
22:19:31.894 -> SIM7600 Response: AT+CMQTTCONNECT=0,"***URL TO MY BROKER ***
22:19:31.933 -> SIM7600 initialization and MQTT setup complete.
22:19:33.895 -> SIM7600 Response: AT+CMQTTTOPIC=0,7
22:19:33.941 -> +CMQTTTOPIC: 0,11
22:19:33.941 ->
22:19:33.941 -> ERROR
22:19:33.941 ->
22:19:36.894 -> SIM7600 Response: AT+CMQTTPAYLOAD=0,10
22:19:36.941 -> +CMQTTPAYLOAD: 0,11
22:19:36.941 ->
22:19:36.941 -> ERROR
22:19:36.941 ->
22:19:39.898 -> SIM7600 Response: AT+CMQTTPUB=0,1
22:19:39.944 -> +CMQTTPUB: 0,12
22:19:39.944 ->
22:19:39.944 -> ERROR
22:19:39.944 ->
22:19:46.914 -> SIM7600 Response: AT+CMQTTTOPIC=0,7
22:19:46.961 -> +CMQTTTOPIC: 0,11
22:19:46.961 ->
22:19:46.961 -> ERROR
22:19:46.961 ->
22:19:49.921 -> SIM7600 Response: AT+CMQTTPAYLOAD=0,10
22:19:49.921 -> +CMQTTPAYLOAD: 0,11
22:19:49.921 ->
22:19:49.959 -> ERROR
22:19:49.959 ->
22:19:52.924 -> SIM7600 Response: AT+CMQTTPUB=0,1
22:19:52.924 -> +CMQTTPUB: 0,12
22:19:52.924 ->
22:19:52.924 -> ERROR
22:19:52.924 ->
22:19:59.910 -> SIM7600 Response: AT+CMQTTTOPIC=0,7
22:19:59.956 -> +CMQTTTOPIC: 0,11
22:19:59.956 ->
22:19:59.956 -> ERROR
22:19:59.956 ->
22:20:02.931 -> SIM7600 Response: AT+CMQTTPAYLOAD=0,10
22:20:02.931 -> +CMQTTPAYLOAD: 0,11
22:20:02.931 ->
22:20:02.971 -> ERROR
22:20:02.971 ->
22:20:05.933 -> SIM7600 Response: AT+CMQTTPUB=0,1
22:20:05.933 -> +CMQTTPUB: 0,12
22:20:05.933 ->
22:20:05.933 -> ERROR