Example Code for Arduino-HTTP Message Interaction (SIM7000 Library)

The program uses SIM7000 to achieve HTTP message interaction. Users can learn how to set up the SIM7000 module, connect to a network, send HTTP requests, and receive responses using the DFRobot_SIM7000 library.

Hardware Preparation

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

Software Preparation

Wiring Diagram

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

SIM7000C

Other Preparation Work

Since Arduino UNO only has one hardware port, which will make a conflict with the other Serial device, DFRobot_SIM7000 Library will use SoftwareSerial as communication port. Please turn the switch to the terminal "Tx>D8; Rx > D7".

Sample Code

#include <Wire.h>
#include <DFRobot_SIM7000.h>

#define PIN_TX     7
#define PIN_RX     8
SoftwareSerial     mySerial(PIN_RX,PIN_TX);
DFRobot_SIM7000    sim7000;
static char        buff[350];

void setup(){
    int signalStrength,dataNum;
    Serial.begin(115200);
    sim7000.begin(mySerial);
    sim7000.turnOFF();
    delay(5000);
    Serial.println("Turn ON SIM7000......");
    if(sim7000.turnON()){                                                                      //Turn ON SIM7000
        Serial.println("Turn ON !");
    }
    Serial.println("Set baud rate......");
    if(sim7000.setBaudRate(19200)){                                                            //Set baud rate from 115200 to 19200
        Serial.println("Set baud rate:19200");
    }else{
        Serial.println("Faile to set baud rate");
        while(1);
    }
    Serial.println("Check SIM card......");
    if(sim7000.checkSIMStatus()){                                                              //Check SIM card
        Serial.println("SIM card READY");
    }else{
        Serial.println("SIM card ERROR");
        while(1);
    }
    delay(500);
    Serial.println("Set net mod......");
    if(sim7000.setNet(NB)){                                                                    //Set net mod NB-IOT
        Serial.println("Set NB-IOT mode");
    }else{
        Serial.println("Fail to set mode");
    }
    Serial.println("Get signal quality......");
    delay(500);
    signalStrength=sim7000.checkSignalQuality();                                               //Check signal quality from (0-30)
    Serial.print("signalStrength =");
    Serial.println(signalStrength);
    delay(500);
    Serial.println("Attaching service......");
    if(sim7000.attacthService()){                                                              //Open the connection
        Serial.println("Attach service");
    }else{
        Serial.println("Fail to Attach service");
        while(1);
    }
    delay(200);
    Serial.println("Connecting......");
    if(sim7000.connect(TCP,"www.taobao.com",80)){                                              //Start Up TCP or UDP Connection
        Serial.println("Connect OK");
    }else{
        Serial.println("Fail to connect");
        while(1);
    }
    sim7000.send("HEAD/HTTP/1.1\r\nHost:www.taobao.com\r\nConnection:keep-alive\r\n\r\n");     //Send Data Through TCP or UDP Connection
    dataNum=sim7000.recv(buff,350,0);                                                          //Receive data
    Serial.print("dataNum=");
    Serial.println(dataNum);
    Serial.println(buff);
    delay(500);
    if(sim7000.close()){                                                                       //End the connection
        Serial.println("Close connection");
    }else{
        Serial.println("Fail to close connection");
    }
    delay(2000);
    sim7000.turnOFF();                                                                         //Turn OFF SIM7000
}

void loop() {
    delay(1000);
}

Function

  • Set software serial port. SIM7000 uses the software serial port to control TX to D8, RX to D7.
begin(Stream &s_)
  • Turn ON SIM7000 in a software way then Net indicator will blink. SIM7000C will initialize for about 2s. The function back to true when initialized successfully.
 turnON();
  • Turn OFF SIM7000 in a software way then Net indicator will go out.
 turnOFF();
  • Set baud rate of software serial port, SIM7000 serial port recognize 115200 baud rate in default. So we need to reset the baud rate to 19200 and below to avoid garbled.  The function back to true when initialized successfully (other qualified baud rates are 9600, 4800, 2400, 1200).
setBaudRate(int rate);
  • Initialize AT commands and the function back to true when initialized successfully.
 checkSIMStatus();
  • Set network mode and the function back to true when initialized successfully. Qualified modes: GPRS/GPRS-NB/NB-IOT
setNet(Net net);
  • Check signal strengths, the return value is the signal strength. Return=99 means no signal can be detected.
checkSignalQuality();
  • Start a connection service and the function back to true when connected successfully.
attacthService();
  • Connect to network: select a connection protocol (TCP/UDP), domain name, port ID. The function back to true when initialized successfully.
connect(Protocol ptl,const char *host, int port);
  • Data transmission: send data to the server when connected. Data size can be specified or inputted directly.
send(const char *str);
send(void* buf,size_t len);
  • Receive data: receive and save data from the server. The maximum data to receive and waiting time can be set. The result is the character value been received.
recv(char* buf,int maxlen,int timeout);
  • Disconnect and the function back to true.
close();

Result

SIM7000C-PROJECT

Additional Information

Software/Hardware Serial Port Switch: Arduino UNO/Mega controllers connect USB with the hardware interface D0,D1 in default. So serial port conflicts are common when serial ports are used by the expansion shields. Considering these conflicts, SIM7000C Arduino NB-IoT/LTE/GPRS expansion shield equipped with a software serial port to communicate with Arduino. Learn more about Arduino SoftwareSerial Library.

Boot button connected to D12 in default to control program conveniently. You can pull up D12 for 2s to ON/OFF. SIM7000C ON will initialize to get ready to work in about 2s.

Was this article helpful?

TOP