Open carminelau opened 3 years ago
Hi, I can't understand why the serial.available is always 0. I have two codes, one working and one not working. This is the one that works:
#define DEBUG 0 #define TINY_GSM_MODEM_SIM7000 #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #define SerialAT Serial1 // set GSM PIN, if any #define GSM_PIN "" // Your GPRS credentials, if any const char apn[] = "em"; //SET TO YOUR APN const char gprsUser[] = ""; const char gprsPass[] = ""; #include <TinyGsmClient.h> #include <SPI.h> #include <SD.h> #include <Ticker.h> #ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib #include <StreamDebugger.h> StreamDebugger debugger(SerialAT, Serial); TinyGsm modems(debugger); #else TinyGsm modems(SerialAT); #endif #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds #define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds) #define UART_BAUD 115200 #define PIN_DTR 25 #define PIN_TX 27 #define PIN_RX 26 #define PWR_PIN 4 int counter, lastIndex, numberOfPieces = 24; String pieces[24], input; int cont = 0; void modem_on() { Serial.println("\nStarting Up Modem per GPS..."); pinMode(PWR_PIN, OUTPUT); digitalWrite(PWR_PIN, HIGH); delay(300); digitalWrite(PWR_PIN, LOW); delay(10000); //Wait for the SIM7000 communication to be normal, and will quit when receiving OK int i = 10; while (i) { SerialAT.println("AT"); delay(500); if (SerialAT.available()) { String r = SerialAT.readString(); if ( r.indexOf("OK") >= 0 ) { break; } } delay(500); i--; } } void setup() { Serial.begin(9600); // Set console baud rate SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX); delay(100); modem_on(); } void loop() { Serial.print("Campionatura: "); Serial.println(cont); Serial.println("Accensione GPS ATCOMMAND"); SerialAT.println("AT+CGNSPWR=1"); delay(2000); if (SerialAT.available()) { String ri = SerialAT.readString(); Serial.println(ri); } delay(2000); Serial.println("Richiesta Dati GPS"); SerialAT.println("AT+CGNSINF"); delay(5000); if (SerialAT.available()) { String r = SerialAT.readString(); Serial.println(r.length()); String latitudine = r.substring(46, 55); String longitudine = r.substring(56, 65); Serial.print("Latitudine: "); Serial.print(latitudine); Serial.print(" Longitudine: "); Serial.println(longitudine); Serial.println(r); } delay(8000); Serial.println("Spegnimento GPS"); SerialAT.println("AT+CGNSPWR=0"); delay(2000); if (SerialAT.available()) { String ri = SerialAT.readString(); Serial.println(ri); } cont = cont + 1; }
I have integrated this working code into the firmware I am creating, but it doesn't work. This not working:
const char gprsUser[] = ""; const char gprsPass[] = ""; #define TINY_GSM_MODEM_SIM7000 #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #define TINY_GSM_USE_GPRS true #define SerialAT Serial1 #include "ArduinoHttpClient.h" #include <TinyGsmClient.h> #include <Ticker.h> TinyGsm modem(SerialAT); TinyGsmClient clientSim(modem); int counters, lastIndexs, numberOfPiecess = 24; String piecess[24], inputs; #define UART_BAUD 115200 #define PIN_DTR 25 #define PIN_TX 27 #define PIN_RX 26 #define PWR_PIN 4 void initSim() { Serial.begin(9600); SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX); modem.init(); modem.sendAT("+SGPIO=0,4,1,0"); } bool connectModem() { if (!modem.restart()) { pinMode(PWR_PIN, OUTPUT); digitalWrite(PWR_PIN, HIGH); delay(300); digitalWrite(PWR_PIN, LOW); delay(5000); //Wait for the SIM7000 communication to be normal, and will quit when receiving OK modem.factoryDefault(); } modem.setNetworkMode(51); modem.setPreferredMode(1); ATConnect(); #if DEBUG 1 Serial.print("Waiting for network..."); #endif if (!modem.waitForNetwork()) {//default un minuto #if DEBUG 1 Serial.println(" fail"); #endif modem.gprsDisconnect(); modem.poweroff(); return false; } else { modem.gprsConnect(apn.c_str(), gprsUser, gprsPass); } #if DEBUG 1 Serial.println(" success"); #endif if (modem.isGprsConnected()) { #if DEBUG 1 Serial.println("GPRS 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); } return true; #endif } } void spegniModem() { modem.gprsDisconnect(); modem.poweroff(); } String getDateTime() { String s = ""; #if DEBUG 1 Serial.print("gprs: "); Serial.println(gprs); #endif const char server[] = "188.166.29.27"; const char resource[] = "/datatime"; const int port = 5000; if (!connectModem()) { return "-1"; } HttpClient httpSim(clientSim, server, port); int err = httpSim.get(resource); if (err != 0) { #if DEBUG 1 Serial.println(F("ERRORE")); #endif return "-1"; } else { int status = httpSim.responseStatusCode(); #if DEBUG 1 Serial.print(F("Response status code: ")); Serial.println(status); #endif if (status == 200) { String response = httpSim.responseBody(); #if DEBUG 1 Serial.println(F("Response:")); Serial.println(response); #endif s = splitStringData(response); } else { #if DEBUG 1 Serial.println("errore config data"); #endif return "-1"; } } modem.gprsDisconnect(); modem.poweroff(); return s; } String splitStringData(String stringa) { char s[stringa.length() + 1]; String stringReturn = ""; char c1 = '{'; char c2 = '}'; bool copia = false; int i = 0; stringa.toCharArray(s, stringa.length() + 1); while (s[i] != c2) { if (s[i] == c1) { copia = true; i++; } if (copia) { stringReturn += s[i]; } i++; } #if DEBUG 1 Serial.println(stringReturn); #endif return stringReturn; } void ATConnect() { SerialAT.println("AT+CGDCONT?"); delay(500); if (SerialAT.available()) { inputs = SerialAT.readString(); for (int i = 0; i < inputs.length(); i++) { if (inputs.substring(i, i + 1) == "\n") { piecess[counters] = inputs.substring(lastIndexs, i); lastIndexs = i + 1; counters++; } if (i == inputs.length() - 1) { piecess[counters] = inputs.substring(lastIndexs, i); } } // Reset for reuse inputs = ""; counters = 0; lastIndexs = 0; for ( int y = 0; y < numberOfPiecess; y++) { for ( int x = 0; x < piecess[y].length(); x++) { char c = piecess[y][x]; //gets one byte from buffer if (c == ',') { if (inputs.indexOf(": ") >= 0) { String data = inputs.substring((inputs.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"); } inputs = ""; break; } // Reset for reuse inputs = ""; } else { inputs += c; } } } } } void readGPS() { int i = 10; while (i) { SerialAT.println("AT"); delay(500); if (SerialAT.available()) { String r = SerialAT.readString(); Serial.println(r); if ( r.indexOf("OK") >= 0 ) { break; } } delay(500); i--; } #if DEBUG 1 Serial.println("Accensione GPS ATCOMMAND"); #endif SerialAT.print("AT+CGNSPWR=1\r\n"); delay(500); if (SerialAT.available()) { #if DEBUG 1 String accensione = SerialAT.readString(); Serial.print(accensione); #endif } delay(2000); for (int i = 0; i < 15; i++) { Serial.println("Richiesta Dati GPS"); SerialAT.print("AT+CGNSINF\r\n"); delay(500); while(SerialAT.available()) { Serial.print(SerialAT.available()); String richiesta = SerialAT.readString(); #if DEBUG 1 Serial.print(richiesta.length()); #endif lat = richiesta.substring(46, 55); lon = richiesta.substring(56, 65); #if DEBUG 1 Serial.print("Latitudine: "); Serial.print(lat); Serial.print(" Longitudine: "); Serial.print(lon); Serial.print(richiesta); if (lat.toFloat() 0 && lon.toFloat() 0) { break; } #endif } } delay(2000); Serial.println("Spegnimento GPS"); SerialAT.print("AT+CGNSPWR=0\r\n"); delay(500); if (SerialAT.available()) { #if DEBUG 1 String spegnimento = SerialAT.readString(); Serial.print(spegnimento); #endif } }
Is there a reason you're not using the gprsConnect(...) and other library functions?
gprsConnect(...)
Hi, I can't understand why the serial.available is always 0. I have two codes, one working and one not working. This is the one that works:
I have integrated this working code into the firmware I am creating, but it doesn't work. This not working: