Esp32+mqtt+sim+gps

Hi everyone, I am writing code for TTGO T-7000G v1.1 I want to make a GPS Tracker (I will attach the code below).


What could be the problem? I've been logged into this project for 4 days and I can't figure out what's wrong!? I watched a video of an Indian man through and his code doesn't work! Changed APN and sim cards! In Serial Monitor (ctrl+shift+m) "Connecting to GSM network..." but does not write failed or GSM network connected successfully! I would like to figure out what is wrong!
All cookies:3

#define TINY_GSM_MODEM_SIM7000

#include <Wire.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <TinyGsmClient.h>

// MQTT connection parameters
const char* mqtt_server = "io.adafruit.com"; // Adafruit IO MQTT server address
const int mqtt_port = 1883; // MQTT port (usually 1883)
const char* mqtt_user = "iAYRE"; // Username (IO_USERNAME)
const char* mqtt_password = "aio_omsN91EKsq0FcI6tzSYmLre8kxvr"; // Access key (IO_KEY)
const char* mqtt_topic = "iAYRE/feeds/Map/csv";

// APN for your provider
#define GSM_APN       "omnitel" // Your provider's APN
#define GSM_USERNAME  "omni" // Username (usually not required)
#define GSM_PASSWORD  "omni" // Password (usually not required)

// Objects for GPS and SIM7000G
TinyGPSPlus gps;
HardwareSerial gpsSerial(2); // Using Serial2 for GPS
HardwareSerial sim7000Serial(2); // Using Serial2 for SIM7000G

// Create TinyGsmSim7000 and TinyGsmClient objects
TinyGsmSim7000 modem(sim7000Serial);
TinyGsmClient gsmClient(modem);

PubSubClient mqttClient(gsmClient);

unsigned long previousMillis = 0;
const long interval = 60000; // Send data every minute

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600, SERIAL_8N1, 22, 21); // RX (GPIO22), TX (GPIO21)
  sim7000Serial.begin(9600, SERIAL_8N1, 26, 27); // RX (GPIO26), TX (GPIO27)
  delay(1000);

  connectGSM();
  connectMQTT();
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    while (gpsSerial.available() > 0) {
      gps.encode(gpsSerial.read());
    }

    if (gps.location.isValid()) {
      // Read coordinates and send to MQTT server
      float latitude = gps.location.lat();
      float longitude = gps.location.lng();
      
      // Prepare JSON
      StaticJsonDocument<200> doc;
      doc["latitude"] = latitude;
      doc["longitude"] = longitude;
      char jsonBuffer[256];
      serializeJson(doc, jsonBuffer);

      mqttClient.publish(mqtt_topic, jsonBuffer);
    }
  }

  if (!mqttClient.connected()) {
    connectMQTT();
  }

  mqttClient.loop();
}

void connectGSM() {
  Serial.println("Connecting to GSM network...");
  while (!modem.init()) {
    delay(1000);
  }

  // Set APN
  if (!modem.gprsConnect(GSM_APN, GSM_USERNAME, GSM_PASSWORD)) {
    Serial.println("Failed to connect to GSM network!");
  } else {
    Serial.println("GSM network connected successfully!");
  }
}

void connectMQTT() {
  mqttClient.setServer(mqtt_server, mqtt_port);

  while (!mqttClient.connected()) {
    Serial.println("Attempting MQTT connection...");

    if (mqttClient.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("MQTT connection established!");
    } else {
      Serial.print("Failed to connect to MQTT, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Retry in 5 seconds");
      delay(5000);
    }
  }
}

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