vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.95k stars 723 forks source link

esp32 sim7000G does not connect LTE #454

Open EmanueleFeola opened 3 years ago

EmanueleFeola commented 3 years ago

Problem: esp32 does not connect to LTE (but it does connect to GPRS!) In the code, esp32 fails to get network (modem.waitForNetwork() line) Modem Name: SIMCOM SIM7000G Modem Info: SIM7000G R1529 Code:

#include "utils.h"

File root;

TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
MqttClient mqttc(&mqtt, MQTT_BROKER, MQTT_USER, MQTT_PWD);

int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;

time_t bootTimestamp;
uint8_t mac;

void setup(){
  Serial.begin(115200);
  Serial.println("System is on");

  initSD();
  initSim();

  mqttc.init();
  mqttc.getMqttClient()->subscribe(MQTT_TOPIC_UPDATE);
  mqttc.getMqttClient()->publish(MQTT_TOPIC_SYS, "system startup");

  esp_efuse_mac_get_default(&mac);
  bootTimestamp = getTimestamp();

  randomSeed(42);

  startMqttISR();
  //startLogISR();
}

void loop(){
  delay(1000);
}

/* ISR */
void IRAM_ATTR mqttISR(){
  while (!mqtt.connected()) {
    Serial.println("Disconnected");
    mqttc.connectMqtt();
  }

  mqtt.loop();

  char msg[100] = {0};
  int timestamp = bootTimestamp +  esp_timer_get_time() / 10000;
  int accX = random(-10,10);
  int accY = random(-10,10); 
  sprintf(msg, "%d;%d;%d", accX, accY, timestamp); 
  Serial.println(msg);

  bool success = mqtt.publish(MQTT_TOPIC_GPS, msg);

  if(!success)
    Serial.println("Failed publishing");
}

/* SIM7000 */
time_t getTimestamp(){
  const char *timestamp = modem.getGSMDateTime(DATE_FULL).c_str();

  struct tm tm;
  time_t now;

  //e.g: 20/05/28,15:59:13+08
  if (strptime(timestamp, "%y/%m/%d,%H:%M:%S", &tm) != NULL )
    now = mktime(&tm);
  else
    now = -1;

  return now;
}

void initSim(){
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

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

  Serial.println("\nWait...");

  delay(5000);

  SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

  Serial.println("Initializing modem...");
  if (!modem.init()) {
    Serial.println("Failed 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);

  modem.sendAT("+SGPIO=0,4,1,0");

  if ( GSM_PIN && modem.getSimStatus() != 3 ) {
    modem.simUnlock(GSM_PIN);
  }

  // 2 Automatic , 13 GSM only , 38 LTE only , 51 GSM and LTE only
  String res;
  do {
    res = modem.setNetworkMode(38);
    delay(500);
  } while (res != "OK");

  do {
    res = modem.setPreferredMode(1);
    delay(500);
  } while (res != "OK");

  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, GPRS_USER, GPRS_PWD)) {
    delay(10000);
    return;
  }

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

  String ccid = modem.getSimCCID();
  Serial.println("CCID: " + ccid);

  String imei = modem.getIMEI();
  Serial.println("IMEI: " + imei);

  String cop = modem.getOperator();
  Serial.println("Operator: " + cop);

  IPAddress local = modem.localIP();
  Serial.println("Local IP: " + String(local));

  int csq = modem.getSignalQuality();
  Serial.println("Signal quality: " + String(csq));

  SerialAT.println("AT+CPSI?");     //Get connection type and band
  delay(500);
  if (SerialAT.available()) {
    String r = SerialAT.readString();
    Serial.println(r);
  }

  Serial.println("\n---End of GPRS TEST---\n");
}
ejri commented 3 years ago

Yeah, unfortunately, it seems to be hardware related (or modem.. still unclear, after factory resets...). The idea of having such a board is really great and convenient enough for quick prototyping, but the irreproducibility/inconsistency is a huge hassle... anyways, thank you all for your responses. I'll try to find other boards..

SRGDamia1 commented 3 years ago

I'd suspect a power issue with that sort of flakiness; maybe not enough amperage or capacitance. With a all-in-one board, that's probably not easily fixable.

PanSuk21 commented 3 years ago

Thanks for info above, I have similar issue of sim7070G and sim7000G break out board of another manufacturer to share. I used global IoT sim. They could registered to network once and take a few minutes to hours. The 7070G registered to 4G which shown on sim dash board of sim supplier. It can’t registered again until now. For sim7000G the first used could not registered to network after hours but the second time it did within a few minutes. AT command show GPRS connection. I also have 7070G dongle version connect to pc via USB. It can registered to network without problem. That ‘s why I bought 7070G break out board to use with microcontroller. All the boards used can report GPS location doesn’t matter they were registered to network or not. It need to AT command (AT+CGNSPWR=1, note not CGPSPWR) to turn on GPS power first. One thing I have noted was the way to disconnect power supply from board. I have just simply disconnected from Vin. When reconnect power supply the board auto turn on. This would indicate of something wrong in firmware that cannot recover to initial conditions after power supply disconnection. Note that the boards used the Vin pin connect power via regulater chip to board at difference point if usb used. However, the board auto turn on either use usb or Vin. I also put IoT sim in mobile phone. The phone registered to network quickly if APN was correctly set. Please don’t do this unless you are aware of data rate to be paid. So the sim is not an issue. Finally I don’ think the network or sim card issued. The problem should on the board.

Updated on June 19, 2021 The network registration issue of mine has been already fixed by forced registration using AT+CMNB=1 to register to LTE-CAT M1, AT+CNMP=38 for LTE or AT+CNMP=13 for GSM. This shall depends on network availabilty in your area. Note that I use AT command via an onboard USB and PC serial monitor. The response was +CREG:5 and LED blinking status change to slow blink indicated of registeration. The auto start of board still an issue. Once it's fixed, It can register to network about 20 seconds next time. I did the same with all boards I have including SIM7080G. The results were consistent.

berlingozzo commented 2 years ago

@EmanueleFeola any updates on your side? I have the same exact device, Lilygo TTGO SIM7000g, and an hologram sim. I've tried everything, including changing country :D Things I have tried (all combinations): 1) country: Italy or Ireland 2) sim: hologram or Vodafone Italy or WindTre Italy 3) all settings of the AT commands 4) two different antennas (both the "flat" one that comes with the device, and an old rigid finger-looking one 5) two different Lilygo TTGO boards 6) two different sources of power

In the end, I've got a partial refund for the TTGO device.

Hope somebody cracks this issue at some point.

thanks

MarkSDS commented 2 years ago

Have had four hours of dissapointed views at the screen. Signal: 99 isNetworkConnected: NO CONNECT Finally, after altering lines 16 and 19 in this code the password ("0000" for my prepaid SIMcard) and for the APN ("advancedinternet") I had immediately a hit! -> Signal: 20 isNetworkConnected: CONNECT -> Device is connected .

This might be a local APN (Netherlands), and I found it by googling the formerly known APN-name of "portalmmm.nl". That's what my regular phone is registrated on.

Hope this is helpfull for someone. I'm happy for now ;)

cicarelli commented 1 year ago

Hi everybody.

I think I've addressed this problem, at least in my earlier tests. My LilyGo SIM-7000 v1.1 is acting like all of your descriptions. When I turn it on, it works perfectly, get LTE network, and detects CNMP=2 (auto) and CMNB=3 (both CAT-M and NB-IoT). It gets network, high signal level (about 26) and connects to HTTP via LTE (which I confirmed using +CPSI). If I turn the board off and turn it on again, it works perfectly in all my tests. But if I simple reset the board using the RST button, it takes longer to get network signal, and shows me lower signal strength (about 12), and terminates the test via GSM.

Then, I changed the start command: Instead of using "modem.init()", I forced the restart with "modem.restart()". It takes more time, but it always gets LTE signal in a very high level (26 - 30), at least untill now. One detail: the modem no longer detects CNMP=2 and CMNB=3; the response is CNMP=38 (just LTE) and CMNB=1 (CAT-M). So the HTTP access runs perfectly, and it appears a bit more quickly.

I'm from Brazil, using VIVO network, and a pre paid SIM card. Other SIM card I have (a M2M SIM Card from ARQIA) doesn't work: apparently it doesn't support LTE.

I hope this helps anyone who is having the same problem.

fgnievinski commented 1 year ago

here it's suggested to use the command CGDCONT to fix the PDP: https://github.com/vshymanskyy/TinyGSM/issues/421#issuecomment-664029242 edit: the command AT+SAPBR=3,1,"CONTYPE","GPRS" (note: it's all caps) is only defined for SIM800/SIM900, it's undefined for SIM7000; although it's tolerated (returns "OK"), the proper commands seem to be CSTT and CIICR in SIM7000.

iconnor commented 1 year ago

For me, adding these two lines solved my connection issues:

    modem.sendAT("+CNMP=2"); // Needed to use GSM and LTE
    modem.waitResponse();
    Serial.println("Modem apn gprsConnect");
    modem.gprsConnect(apn);
    modem.sendAT("+NETOPEN"); // I could not start anything until I then opened the network
    modem.waitResponse();

I am using Telstra over Woolworths so to debug I wanted to see if I could see the networks AT+COPS? but I could not until I changed the modes.

 AT+NETOPEN
ERROR
AT+CGREG?
+CGREG: 0,0

OK
AT+COPS=?
+CME ERROR: phone failure

OK
AT+CNMP?
+CNMP: 13

OK
AT+CNMP=2
OK
+CGEV: ME DETACH
+CGEV: EPS PDN ACT 1
AT+CNBP?
+CNBP: 0X0000000000000180,0X00000000000800D5,0X0000

OK
AT+COPS=?
+COPS: (2,"Woolworths","Woolworths","...),(3,"vodafone AU","voda AU","...),(3,"Optus AU","Optus","...

AT+CGREG?
+CGREG: 0,1

AT+NETOPEN
OK
PanSuk21 commented 10 months ago

Thank you very much,

Please accept my apologies for very delaying in reply.

Pan Suk

From: Ian Connor @.> Sent: Friday, September 1, 2023 11:28 AM To: vshymanskyy/TinyGSM @.> Cc: PanSuk21 @.>; Comment @.> Subject: Re: [vshymanskyy/TinyGSM] esp32 sim7000G does not connect LTE (#454)

For me, adding these two lines solved my connection issues:

modem.sendAT("+CNMP=2"); // Needed to use GSM and LTE
modem.waitResponse();
Serial.println("Modem apn gprsConnect");
modem.gprsConnect(apn);
modem.sendAT("+NETOPEN"); // I could not start anything until I then opened the network
modem.waitResponse();

I am using Telstra over Woolworths so to debug I wanted to see if I could see the networks AT+COPS? but I could not until I changed the modes.

AT+NETOPEN ERROR AT+CGREG? +CGREG: 0,0

OK AT+COPS=? +CME ERROR: phone failure

OK AT+CNMP? +CNMP: 13

OK AT+CNMP=2 OK +CGEV: ME DETACH +CGEV: EPS PDN ACT 1 AT+CNBP? +CNBP: 0X0000000000000180,0X00000000000800D5,0X0000

OK AT+COPS=? +COPS: (2,"Woolworths","Woolworths","...),(3,"vodafone AU","voda AU","...),(3,"Optus AU","Optus","...

AT+CGREG? +CGREG: 0,1

AT+NETOPEN OK

— Reply to this email directly, view it on GitHub https://github.com/vshymanskyy/TinyGSM/issues/454#issuecomment-1702139385 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AUNSD6YYDSF2WMQWYUZOXXDXYFP3HANCNFSM4SXZ7GOQ . You are receiving this because you commented. https://github.com/notifications/beacon/AUNSD66U3CD44U7SER5RWODXYFP3HA5CNFSM4SXZ7GO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOMV2JL6I.gif Message ID: @. @.> >