# A7670E with ESP32 Devkit v1 and Level Shifter GSM-MQTT (uart connection) connection

**URL:** <https://forum.arduino.cc/t/a7670e-with-esp32-devkit-v1-and-level-shifter-gsm-mqtt-uart-connection-connection/1102190>\
**Category:** Home Automation\
**Created:** [March 15, 2023, 10:14am UTC](https://forum.arduino.cc/t/a7670e-with-esp32-devkit-v1-and-level-shifter-gsm-mqtt-uart-connection-connection/1102190 "2023-03-15T10:14:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdask](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisdask/32/679116_2.png) [@chrisdask](https://forum.arduino.cc/u/chrisdask)\
**Post date:** [March 15, 2023, 10:14am UTC](https://forum.arduino.cc/t/a7670e-with-esp32-devkit-v1-and-level-shifter-gsm-mqtt-uart-connection-connection/1102190/1 "2023-03-15T10:14:56Z")

</div>

Greetings ,

I'm trying to establish mqtt connection using AT commands in the form of :

```cpp
// Function to send AT command and check for expected response

bool sendATCommand(String command, String expectedResponse)

```

I have managed to properly setup the SIM that i'm using following this sequence

- AT+CFUN
- ATEO
- AT
- AT+CPIN
- AT+GMR
- AT+CSQ
- AT+COPS
- AT+CREG
- AT+CNMP
- AT+CMEE
- AT+CGDCONT
- AT+CGACT  
(these seem to work properly)

MQTT Connection

- AT+CMQTTSTART (works properly)  
\*AT+CMQTTACCQ=0,"myClient"", "OK");
- AT+CMQTTCON=0 (returns +CMQTTCONNECT: 0,0 )
- AT+CMQTTKEEPALIVE (Sending command: AT+CMQTTKEEPALIVE=0,30  
Received response: ERROR)
- AT+CMQTTSUBTOPIC=0,20,0 (ERROR)
- AT+CMQTTSUB=0,20,0 (ERROR)  
The code that i'm using the is the following :

```cpp
void initializeMQTT() 
{
  uint8_t retries = 3;
  bool commandSuccess = true;
  //Initialize MQTT stack
  do{
    sendATCommand("AT+CMQTTSTART", "OK");
    delay(2000);
    retries--;
  }while(!commandSuccess && retries > 0);

  if(!commandSuccess)
  {
    Serial.println("Failed to initialize MQTT stack.");
    return;
  }
  
  //Initialize MQTT client
  retries = 3;
  do{
    sendATCommand("AT+CMQTTACCQ=0,\"myClient\"", "OK");
    delay(2000);
    retries--;
  }while(!commandSuccess && retries > 0);

  if(!commandSuccess)
  {
    Serial.println("Failed to initialize MQTT client.");
    return;
  }
 

  //Connect to MQTT server
  retries = 3;
  do{
      sendATCommand("AT+CMQTTCONNECT=0,\"tcp://broker.hivemq.com:1883/\",60,0", "+CMQTTCONNECT: 0,0,0");
      delay(2000);
      sendATCommand("AT+CMQTTKEEPALIVE=0,30", "OK");
      delay(2000);
    retries--;
  }while(!commandSuccess && retries > 0);

  if(!commandSuccess)
  {
    Serial.println("Failed to connect to MQTT server.");
    return;
  }

```

The main issue that i have is that even after initializing the mqtt stack properly , and connecting to the broker i'm unable to progress further. Any suggestions or feedback would be appreciated.  
I followed this datasheet: [https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/module/sim7680/A76XX%20Series\_MQTT(S)\_Application%20Note\_V1.02.pdf](https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/module/sim7680/A76XX%20Series_MQTT(S)_Application%20Note_V1.02.pdf)

```cpp
// Connect to the broker
    sendATCommand("AT+CMQTTCONNECT=0,\"tcp://broker.hivemq.com:1883/\",60,0", "+CMQTTCONNECT: 0,0,0");
    delay(2000);

    // Set the keep alive time to 30 seconds
    sendATCommand("AT+CMQTTKEEPALIVE=0,30", "OK");
    delay(2000);

    // Subscribe to the topic "test/topic"
    sendATCommand("AT+CMQTTSUB=0,20,0,\"test/topic\",0", "+CMQTTSUBACK: 0,0,0");
    delay(5000);

    if (gsmSerial.available() > 0) 
    {
      String response = gsmSerial.readString();
      Serial.println(response); // Debug

    if (response.indexOf("+CMQTTSUB: 0,") != -1) 
    {
      if (response.indexOf("hello there") != -1) 
      {        
        Serial.println("Received message:hello there"); //debug
        gsmSerial.println("AT+CMQTTPUBLISH=0,\"test/topic\",0,0,\"hello there\"");
      }
    } 
  }

```

Thank you for your time.

Disclaimer : Pardon if not in right category.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 11, 2023, 10:15am UTC](https://forum.arduino.cc/t/a7670e-with-esp32-devkit-v1-and-level-shifter-gsm-mqtt-uart-connection-connection/1102190/2 "2023-09-11T10:15:36Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
