Sim7000g AT Command Power on and Power off Help

Hello Everyone,

I have a sim7000g that I would like to send an AT command to power it off and then an AT command to power it back on if there is ever an error. I think it should be placed here in the code:

else {

    if (!modem.gprsConnect(apn, gprsUser, gprsPass))
    {
      Serial.println("---GPRS not connected!----");
      Serial.println("---Re-initit GSM/GPRS---");
///
      initGSM();
      connectAPN();

But I do not know the specific command for the sim7000G module. I tried AT+CPWROFF and then AT+CPWRO=1, but not sure if I am doing it right. The whole code is below:

#include <esp_now.h>
#include <WiFiClientSecure.h>
#include <WiFi.h>

#define TINY_GSM_MODEM_SIM7000
#define SerialMon Serial
#define SerialAT Serial1
#define TINY_GSM_RX_BUFFER 1024
#define DUMP_AT_COMMANDS
#define TINY_GSM_DEBUG SerialMon
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#define GSM_PIN ""

const char apn[]      = "213x.m2m.com.attz";
const char gprsUser[] = "";
const char gprsPass[] = "";

const char server[]   = "bridgesense.net";
const int  port       = 80;


#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm        modem(debugger);
#else
TinyGsm        modem(SerialAT);
#endif

#define UART_BAUD   115200
#define PIN_DTR     25
#define PIN_TX      27
#define PIN_RX      26
#define PWR_PIN     4

TinyGsmClient client(modem);
HttpClient    http(client, server, port);
int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;
float value_data[8] = {0.0};
int count = 0;
int url_Check = 0;

//Function Declaration
void updateData();
void initGSM();
void connectAPN();

//String data1 , data2 , data3 , data4, data5, data6, data7;
String data_Post = "";
char resource[16384];
typedef struct struct_message {

  float depth1;
  float depth2;
  float depth3;
  float depth4;
  float accel_x;
  float accel_y;
  float accel_z;
  char TTime[15];
  float Temp;


} struct_message;


struct_message myData;
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  /*
    Serial.print("\n Bytes received: ");
    Serial.println(len);
    Serial.print("\n Depth_1: ");
    Serial.println(myData.depth1);
    Serial.print("\n Depth_2: ");
    Serial.println(myData.depth2);
    Serial.print("\n Depth_3: ");
    Serial.println(myData.depth3);
    Serial.print("\n Depth_4: ");
    Serial.println(myData.depth4);
    Serial.print("\n X-Accel: ");
    Serial.println(myData.accel_x);
    Serial.print("\n Y-Accel: ");
    Serial.println(myData.accel_y);
    Serial.print("\n Z-Accel: ");
    Serial.println(myData.accel_z);

  */
  //convert to Float
  //  value_data[0] = String(myData.depth1).toFloat();
  //  value_data[1] = String(myData.depth2).toFloat();
  //  value_data[2] = String(myData.depth3).toFloat();
  //  value_data[3] = String(myData.depth4).toFloat();
  //  value_data[4] = String(myData.accel_x).toFloat();
  //  value_data[5] = String(myData.accel_y).toFloat();
  //  value_data[6] = String(myData.accel_z).toFloat();
  //  value_data[7] = String(myData.Temp).toFloat();


  /*
    &D1= depth1 sensor
    &D2= depthe2 sensor
    &D3= deppth3 sensor
    &D4= depthe4 sensor
    &X= X-acceleration
    &Y= Y-acceleration
    &Z= Z-acceleration
    &Te= Temperature
    &T= Time
  */
  if (myData.depth1 == 0 && myData.depth2 == 0 && myData.depth3 == 0 && myData.depth4 == 0 &&  myData.Temp == 0) // If depth1,2,3,4 & Temp is equal to zero. It means we are only sending Acceleration & time
  {
    if (url_Check == 0)
    {
      data_Post += "/update4.php?data=1";

    }

    data_Post += "&X=" + String(myData.accel_x, 4 ) + "&Y=" + String(myData.accel_y, 4) + "&Z=" + String(myData.accel_z, 4) + "&T=" + String(myData.TTime);
                 count++;
                 url_Check = 1;
                 if (count == 40) //change the packet size
  {
    Serial.println(data_Post);
      updateData();
     // delay(11115000);
      count = 0;
      data_Post = "";
      url_Check = 0;

    }

  }
  else  // send complete packet
  {
    data_Post += "/update4.php?data=2";
    data_Post += "&D1=" + String(myData.depth1, 2 ) + "&D2=" + String(myData.depth2, 2) + "&D3=" + String(myData.depth3, 2) + "&D4=" + String(myData.depth4, 2);
    data_Post += "&X=" + String(myData.accel_x, 4 ) + "&Y=" + String(myData.accel_y, 4) + "&Z=" + String(myData.accel_z, 4) + "&Te=" + String(myData.Temp, 0);
    data_Post += "&T=" + String(myData.TTime);
    Serial.println(data_Post);
    updateData();
    data_Post = "";

  }


}


//This Setup code is executed once then moves to void loop

void setup() {
  SerialMon.begin(115200);
  delay(10);
  initGSM();
  delay(2000);
  connectAPN();
  delay(2000);
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  else {
    Serial.println("ESP-NOW initialized Sucessfully");
  }
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {

}


//Intilization and Setup is everything above this comment Line


//The board is only intended to perform specific tasks once during the setup phase and
//then wait for external events or interrupts to trigger new actions or behaviors. In this case,
//the loop() function may be left blank because the board does not need to do anything continuously.
//You have to have a loop() function, but it doesn't have to have anything in it.
//So put everything in setup(), and have an empty loop().


void updateData() {

  if (modem.isGprsConnected()) {
    char resource[4096];


    // SerialMon.print(F("Performing HTTP GET request... "));



    data_Post.toCharArray(resource , data_Post.length() + 1);
    Serial.println(resource);
    Serial.println("---------------END PACKET------");
    Serial.println("-----Sending Data starts----");
    int err = http.get(resource);
    if (err != 0) {
      SerialMon.println(F("failed to connect"));
      delay(10000);
      return;
    }
    Serial.println("----sending data ends----");
    //  int status_code = http.responseStatusCode();
    //      SerialMon.print(F("Response status code: "));
    //      SerialMon.println(status_code);
    //      if (!status_code) {
    //        //delay(10000);
    //       // return;
    //      }
    //      SerialMon.println(F("Response Headers:"));
    //      while (http.headerAvailable()) {
    //        String headerName  = http.readHeaderName();
    //        String headerValue = http.readHeaderValue();
    //        SerialMon.println("    " + headerName + " : " + headerValue);
    //      }
    //      int length = http.contentLength();
    //      if (length >= 0) {
    //        SerialMon.print(F("Content length is: "));
    //        SerialMon.println(length);
    //      }
    //      if (http.isResponseChunked()) {
    //        SerialMon.println(F("The response is chunked"));
    //    }
    String body = http.responseBody();
    //  SerialMon.println(F("Response:"));
    //   SerialMon.println(body);
    // http.stop();
    //  http.connectionKeepAlive();
    //      SerialMon.println(F("Server disconnected"));


  }
  else {

    if (!modem.gprsConnect(apn, gprsUser, gprsPass))
    {
      Serial.println("---GPRS not connected!----");
      Serial.println("---Re-initit GSM/GPRS---");
///
      initGSM();
      connectAPN();

    }
    else
    {
      Serial.println("---GPRS is connected!---");
    }

  }


}



void initGSM() {

  Serial.println("-------InitGSM starts-----");

  pinMode(PWR_PIN, OUTPUT);
  digitalWrite(PWR_PIN, HIGH);
  delay(300);
  digitalWrite(PWR_PIN, LOW);

  SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
  Serial.println("Initializing modem...");
  //  if (!modem.restart()) {
  //    Serial.println("Failed (modem.restart()) to restart modem, attempting to continue without restarting");
  //  }
  Serial.println("Initializing modem...");
  if (!modem.init()) {
    Serial.println("Failed  (modem.init()) to restart modem, attempting to continue without restarting");
  }

  String name = modem.getModemName();
  //delay(500);
  Serial.println("Modem Name: " + name);

  String modemInfo = modem.getModemInfo();
  //delay(500);
  Serial.println("Modem Info: " + modemInfo);
  if ( GSM_PIN && modem.getSimStatus() != 3 ) {
    modem.simUnlock(GSM_PIN);
  }
  modem.sendAT("+CFUN=0 ");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" +CFUN=0  false ");
  }
  //delay(200);
  String res;
  res = modem.setNetworkMode(2);
  if (res != "1") {
    DBG("setNetworkMode  false ");
    return ;
  }
  //delay(200);
  res = modem.setPreferredMode(1);
  if (res != "1") {
    DBG("setPreferredMode  false ");
    return ;
  }
  //delay(400);
  modem.sendAT("+CFUN=1 ");
  if (modem.waitResponse(10000L) != 1) {
    DBG(" +CFUN=1  false ");
  }
  //delay(200);

  SerialAT.println("AT+CGDCONT?");
  delay(50);
  if (SerialAT.available()) {
    input = SerialAT.readString();
    for (int i = 0; i < input.length(); i++) {
      if (input.substring(i, i + 1) == "\n") {
        pieces[counter] = input.substring(lastIndex, i);
        lastIndex = i + 1;
        counter++;
      }
      if (i == input.length() - 1) {
        pieces[counter] = input.substring(lastIndex, i);
      }
    }
    input = "";
    counter = 0;
    lastIndex = 0;

    for ( int y = 0; y < numberOfPieces; y++) {
      for ( int x = 0; x < pieces[y].length(); x++) {
        char c = pieces[y][x];  //gets one byte from buffer
        if (c == ',') {
          if (input.indexOf(": ") >= 0) {
            String data = input.substring((input.indexOf(": ") + 1));
            if ( data.toInt() > 0 && data.toInt() < 25) {
              modem.sendAT("+CGDCONT=" + String(data.toInt()) + ",\"IP\",\"" + String(apn) + "\",\"0.0.0.0\",0,0,0,0");
            }
            input = "";
            break;
          }
          // Reset for reuse
          input = "";
        } else {
          input += c;
        }
      }
    }
  } else {
    Serial.println("Failed to get PDP!");
  }

  Serial.println("\n\n\nWaiting for network...");
  if (!modem.waitForNetwork()) {
    //delay(10000);
    return;
  }

  if (modem.isNetworkConnected()) {
    Serial.println("Network connected");
  }

  Serial.println("\n---Starting GPRS TEST---\n");
  Serial.println("Connecting to: " + String(apn));
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    //delay(10000);
    return;
  }

  Serial.print("GPRS status: ");
  if (modem.isGprsConnected()) {
    Serial.println("connected");
  } else {
    Serial.println("not connected");
  }

  Serial.println("--------Init GSM ends-------");
}


void connectAPN() {
  Serial.print("Connecting to APN: ");
  Serial.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    Serial.println(" fail");
  }
  else {
    Serial.println(" OK");
  }
}

Power ON takes a few seconds.
What I do, is ping the modem with AT\n every second until I get an OK response. Then continue with initia;lisation..

Power OFF is a whole different kettle of fish.
AT+CPOF takes as long as 15 sconds to kill the modem.

Check the AT commands datasheet.

Have you studied the SIMCOM documentation? There will be a document that describes all available AT commands...

Errr .... if it's OFF, how will it ever see an incoming AT command? :thinking:

If you've actually turned it off, then you will need some sort of hardware signal to turn it back on.

Does it not have an AT command to restart it?
eg, AT+CFUN

I don’t know the 800, but your existing code toggles the power control pin.
That’s a hardware action, not an AT command.

Where does it toggle the power control pin?

Maybe here…

``

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