├── README.md └── traccar.ino /README.md: -------------------------------------------------------------------------------- 1 | LTE-based GPS tracker for Traccar server, modify essential libraries, including APN, Traccar server details, port (if not 5055), and your Traccar ID. Adjust the delay to suit your preferences. Ensure compatibility with the LILYGO-TTGO-T-SIM7000G-ESP32 by confirming your SIM card matches its specifications. 2 | 3 | Last update 1-16-2024 4 | Updated code from @ThisIsTenou fork. Thanks 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /traccar.ino: -------------------------------------------------------------------------------- 1 | #define SerialMon Serial 2 | 3 | // Set serial for AT commands (to the module)` 4 | // Use Hardware Serial on Mega, Leonardo, Micro 5 | #define SerialAT Serial1 6 | 7 | #define TINY_GSM_MODEM_SIM7000 8 | #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb 9 | 10 | // See all AT commands, if wanted 11 | // #define DUMP_AT_COMMANDS 12 | String FINALLATI = "0", FINALLOGI = "0", FINALSPEED = "0", FINALALT = "0", FINALACCURACY = "0", FINALIGNITION = "false", ignition = "false"; 13 | float battery = 0.0; 14 | // set GSM PIN, if any 15 | #define GSM_PIN "" 16 | 17 | // Your GPRS credentials, if any 18 | const char apn[] = "YOUR-APN"; 19 | const char gprsUser[] = ""; 20 | const char gprsPass[] = ""; 21 | 22 | const char server[] = "Your Traccar Server IP"; 23 | 24 | const int port = 5055; 25 | String myid = "Traccar ID"; 26 | 27 | #include 28 | #include 29 | 30 | 31 | #define DUMP_AT_COMMANDS 32 | 33 | #ifdef DUMP_AT_COMMANDS 34 | #include 35 | StreamDebugger debugger(SerialAT, SerialMon); 36 | TinyGsm modem(debugger); 37 | #else 38 | TinyGsm modem(SerialAT); 39 | #endif 40 | 41 | TinyGsmClient client(modem); 42 | HttpClient http(client, server, port); 43 | 44 | #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds 45 | #define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds) 46 | 47 | #define UART_BAUD 115200 48 | #define PIN_DTR 25 49 | #define PIN_TX 27 50 | #define PIN_RX 26 51 | #define PWR_PIN 4 52 | #define SD_MISO 2 53 | #define SD_MOSI 15 54 | #define SD_SCLK 14 55 | #define SD_CS 13 56 | #define LED_PIN 12 57 | #define BAT_ADC 35 58 | 59 | float ReadBattery() { 60 | float vref = 1.100; 61 | uint16_t volt = analogRead(BAT_ADC); 62 | float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref); 63 | return battery_voltage; 64 | } 65 | 66 | void modemPowerOn() { 67 | pinMode(PWR_PIN, OUTPUT); 68 | digitalWrite(PWR_PIN, LOW); 69 | delay(1000); //Datasheet Ton mintues = 1S 70 | digitalWrite(PWR_PIN, HIGH); 71 | } 72 | 73 | void modemPowerOff() { 74 | pinMode(PWR_PIN, OUTPUT); 75 | digitalWrite(PWR_PIN, LOW); 76 | delay(1500); //Datasheet Ton mintues = 1.2S 77 | digitalWrite(PWR_PIN, HIGH); 78 | } 79 | 80 | void modemRestart() { 81 | modemPowerOff(); 82 | delay(1000); 83 | modemPowerOn(); 84 | } 85 | 86 | void enableGPS(void) { 87 | 88 | Serial.println("Start positioning . Make sure to locate outdoors."); 89 | Serial.println("The blue indicator light flashes to indicate positioning."); 90 | modem.sendAT("+SGPIO=0,4,1,1"); 91 | if (modem.waitResponse(10000L) != 1) { 92 | DBG(" SGPIO=0,4,1,1 false "); 93 | } 94 | modem.enableGPS(); 95 | } 96 | 97 | void disableGPS(void) { 98 | modem.sendAT("+SGPIO=0,4,1,0"); 99 | if (modem.waitResponse(10000L) != 1) { 100 | DBG(" SGPIO=0,4,1,0 false "); 101 | } 102 | modem.disableGPS(); 103 | } 104 | 105 | void send_data(float lat, float lon, float speed, float alt, float accuracy, float battery) { 106 | 107 | String FINALLATI = "0", FINALLOGI = "0", FINALSPEED = "0", FINALALT = "0", FINALACCURACY = "0", FINALBAT = "0", FINALIGNITION = "false", FINALBATLEVEL = ""; 108 | FINALLATI = String(lat, 8); 109 | FINALLOGI = String(lon, 8); 110 | FINALSPEED = String(speed, 2); 111 | FINALALT = String(alt, 0); 112 | FINALACCURACY = String(accuracy, 2); 113 | FINALIGNITION = String(ignition); 114 | if (battery == 0) { 115 | FINALBATLEVEL = ""; 116 | FINALBAT = ""; 117 | FINALIGNITION = "true"; 118 | } else { 119 | float batterylevel = (((float)battery - 3) / 1.2) * 100; 120 | FINALBAT = String(battery, 2); 121 | if (batterylevel > 100) { 122 | batterylevel = 100; 123 | } 124 | FINALBATLEVEL = String(batterylevel, 0); 125 | FINALIGNITION = "false"; 126 | } 127 | 128 | 129 | int err = http.post("/?id=" + myid + "&lat=" + FINALLATI + "&lon=" + FINALLOGI + "&accuracy=" + FINALACCURACY + "&altitude=" + FINALALT + "&speed=" + FINALSPEED + "&battery=" + FINALBAT + "&ignition=" + FINALIGNITION + "&batteryLevel=" + FINALBATLEVEL); 130 | Serial.println("/?id=" + myid + "&lat=" + FINALLATI + "&lon=" + FINALLOGI + "&accuracy=" + FINALACCURACY + "&altitude=" + FINALALT + "&speed=" + FINALSPEED + "&battery=" + FINALBAT + "&ignition=" + FINALIGNITION + "&batteryLevel=" + FINALBATLEVEL); 131 | if (err != 0) { 132 | SerialMon.println(F("failed to connect")); 133 | delay(10000); 134 | return; 135 | } 136 | 137 | int status = http.responseStatusCode(); 138 | 139 | if (!status) { 140 | delay(10000); 141 | return; 142 | } 143 | 144 | String body = http.responseBody(); 145 | SerialMon.println(F("Response:")); 146 | SerialMon.println(body); 147 | 148 | // Shutdown 149 | http.stop(); 150 | SerialMon.println(F("Server disconnected bye bye will connect soon")); 151 | } 152 | 153 | void setup() { 154 | // Set console baud rate 155 | SerialMon.begin(115200); 156 | 157 | delay(10); 158 | 159 | // Set LED OFF 160 | pinMode(LED_PIN, OUTPUT); 161 | digitalWrite(LED_PIN, HIGH); 162 | 163 | SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX); 164 | 165 | delay(10000); 166 | 167 | modemPowerOn(); 168 | 169 | Serial.println("Initializing modem..."); 170 | if (!modem.restart()) { 171 | Serial.println("Failed to restart modem, attempting to continue without restarting"); 172 | } 173 | 174 | // Unlock your SIM card with a PIN if needed 175 | if (GSM_PIN && modem.getSimStatus() != 3) { 176 | modem.simUnlock(GSM_PIN); 177 | } 178 | } 179 | 180 | void loop() { 181 | 182 | // GPRS connection parameters are usually set after network registration 183 | SerialMon.print(F("Connecting to ")); 184 | SerialMon.print(apn); 185 | if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { 186 | SerialMon.println(" fail"); 187 | delay(30000); 188 | return; 189 | } 190 | SerialMon.println(" success"); 191 | 192 | if (modem.isGprsConnected()) { 193 | SerialMon.println("GPRS connected"); 194 | } 195 | 196 | enableGPS(); 197 | 198 | float lat, lon, speed, alt, accuracy; 199 | int vsat, usat, year, month, day, hour, min, sec; 200 | while (1) { 201 | battery = ReadBattery(); 202 | if (modem.getGPS(&lat, &lon, &speed, &alt, &vsat, &usat, &accuracy, &year, &month, &day, &hour, &min, &sec)) { 203 | send_data(lat, lon, speed, alt, accuracy, battery); 204 | } 205 | digitalWrite(LED_PIN, !digitalRead(LED_PIN)); 206 | if (battery == 0) { 207 | delay(10000); 208 | } else { 209 | int count = 0; 210 | while ((battery > 0) and (count < 90)) { 211 | battery = ReadBattery(); 212 | delay(10000); 213 | count++; 214 | } 215 | } 216 | } 217 | } 218 | --------------------------------------------------------------------------------