Example Code for Arduino-MQTT Connection

Last revision 2025/12/09

This example operates the SIM7070G expansion board to perform MQTT communication. Users can learn how to connect the SIM7070G to an MQTT server and send data.

Hardware Preparation

  • DFR0216 DFRduino UNO R3 - Arduino Compatible x1
  • SIM7070G Arduino NB-IoT/LTE/GPRS Expansion Shield x1
  • USB Wire x1
  • 7V~12V DC Power Supply x1

Software Preparation

Wiring Diagram

Plug the SIM7070G Arduino NB-IoT/LTE/GPRS Expansion Shield to DFRduino UNO R3 directly.

The module SIM7070G requires a 7-12V external power supply. And please switch the hardware and software serial control switch to TX-D8, RX-D7.

Wiring Diagram

Other Preparation Work

  • Since Arduino UNO only has one hardware serial port, it is recommended to use software serial communication when using it. The SIM7000 library file uses software serial by default. Please switch the software/hardware serial control switch to TX>D8, RX>D7.

Sample Code

#include <DFRobot_SIM7070G.h>

#define serverIP        "iot.dfrobot.com"
#define IOT_USERNAME    "USERNAME"
#define IOT_KEY         "PASSWORD"
#define IOT_TOPIC       "TOPIC"
#define IOT_CLIENT      "dfrobot"

#define PIN_TX          7
#define PIN_RX          8

SoftwareSerial          mySerial(PIN_RX, PIN_TX);
DFRobot_SIM7070G         sim7070g(&mySerial);

void setup()
{
  delay(1500);   
  int signalStrength;
  Serial.begin(115200);
  mySerial.begin(19200);

  Serial.println("Turn ON SIM7070G......");
  if (sim7070g.turnON()) {                                        
    Serial.println("Turn ON !");
  }

  Serial.println("Set baud rate......");
  while (1) {
    if (sim7070g.setBaudRate(19200)) {                          
      Serial.println("Set baud rate:19200");
      break;
    } else {
      Serial.println("Faile to set baud rate");
      delay(1000);
    }
  }

  Serial.println("Check SIM card......");
  if (sim7070g.checkSIMStatus()) {                                
    Serial.println("SIM card READY");
  } else {
    Serial.println("SIM card ERROR, Check if you have insert SIM card and restart SIM7070G");
    while (1);
  }


  Serial.println("Set net mode......");
  while (1) {
    if (sim7070g.setNetMode(sim7070g.eNB)) {                              
      Serial.println("Set NB mode");
      break;
    } else {
      Serial.println("Fail to set mode");
      delay(1000);
    }
  }

  Serial.println("Get signal quality......");
  delay(1500);
  signalStrength = sim7070g.checkSignalQuality();                 
  Serial.print("signalStrength =");
  Serial.println(signalStrength);
  delay(500);

  Serial.println("Attaching service......");
  while (1) {
    if (sim7070g.attacthService()) {                            
      Serial.println("Attach service");
      break;
    } else {
      Serial.println("Fail to Attach service");
      delay(1000);
    }
  }
}

void loop()
{
  String  sendData;
  Serial.print("Connect to :");
  Serial.println(serverIP);
  if (sim7070g.openNetwork(sim7070g.eTCP, serverIP, 1883)) {                  //Connect to server
    Serial.println("Connected !");
  } else {
    Serial.println("Failed to connect");
    return;
  }
  delay(200);

  Serial.print("Connect to : ");
  Serial.println(IOT_USERNAME);
  if (sim7070g.mqttConnect(IOT_CLIENT, IOT_USERNAME, IOT_KEY)) {    
    Serial.println("Connected !");
  } else {
    Serial.println("Failed to connect");
    return;
  }
  delay(200);

  Serial.println("Input data end with CRLF : ");
  sendData = readSerial(sendData);
  Serial.print("Send data : ");
  Serial.print(sendData);
  Serial.println(" ......");
  if (sim7070g.mqttPublish(IOT_TOPIC, sendData)) {                 
    Serial.println("Send OK");
  } else {
    Serial.println("Failed to send");
    return;
  }
  delay(200);

  Serial.println("Close connection......");
  if (sim7070g.closeNetwork()) {                                  
    Serial.println("Close connection !");
  } else {
    Serial.println("Fail to close connection !");
    return;
  }
  delay(2000);
}

String readSerial(String result)
{
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result += '\0';
        while (Serial.read() >= 0);
        return result;
      }
      if (i == 50) {
        Serial.println("The data is too long");
        result += '\0';
        while (Serial.read() >= 0);
        return result;
      }
      if (inChar != '\r') {
        result += inChar;
        i++;
      }
    }
  }
}

Result

The serial monitor will print some information, read the data you input, and send the data to the specified topic.

Was this article helpful?

TOP