0

I am trying to send gps location data every 3 seconds to mqtt server if it's updated but my device shows weird stack errors and resets. What i am missing?

I am using tinygps++ and pubsubclient as libraries.

LOOP:

void loop() {
  if (WiFi.status() != WL_CONNECTED)
  {
    connectWifi();
  }
  if (WiFi.status() == WL_CONNECTED && !client.connected())
  {
    mqtt_connect();
  }
  client.loop();

  if (WiFi.status() == WL_CONNECTED && client.connected())
  {
    long now = millis();
    if (ss.available() > 0)
    {
      if (now - lastMsg > 3000) {
        lastMsg = now;
        gps.encode(ss.read());
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.setCursor(0, 0);
        display.println("Lat:" + String(gps.location.lat(), 6));
        display.setCursor(0, 10);
        display.println("Lng:" + String(gps.location.lng(), 6));
        Serial.print("Latitude  : ");
        Serial.println(gps.location.lat(), 5);
        Serial.print("Longitude : ");
        Serial.println(gps.location.lng(), 4);
        if (gps.location.isUpdated())
        {
          String pos = String(gps.location.lat(), 6) + ";" + String(gps.location.lng(), 6);
          client.publish(mqtt_output, pos.c_str());
          display.setCursor(0, 50);
          display.println("Data Sent");
          m_alert();
        }
        display.setCursor(0, 30);
        display.println("Satellites: " + gps.satellites.value());
        display.setCursor(0, 40);
        display.println("HDOP: " + gps.hdop.value());
        display.display();
      }
    }
  }
}

connectWifi :

void connectWifi() {
  Serial.println('\n');

  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print("*"); Serial.print(' ');
  }
  w_alert();
  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         
}

mqtt_connect :

void mqtt_connect() {
  Serial.print(F("MQTT Connecting..."));

  char* clientId = "app";
  while (!client.connected()) {
    mqttConnectAttempt = millis();
    if (client.connect(clientId, mqtt_user, mqtt_pass)) {
      s_alert();
      Serial.println(F("connected"));
      String ready = "Connection Success!\r\n";
      client.publish(mqtt_output, ready.c_str());
      client.subscribe(mqtt_output);
    }
    else {
      Serial.print(F("Failure, rc="));
      Serial.print(client.state());
      Serial.println(F("Connecting Again in "));
      Serial.print(MQTT_PERIOD + " Seconds");
      delay(MQTT_PERIOD);
    }
  }
}
  • 1
    you are missing an explanation of what weird stack errors and resets means – jsotola Feb 27 at 18:45
  • 1
    reduce you code .... send a single fixed value to the MQTT server ...... what errors do you get? – jsotola Feb 27 at 18:47
  • Do you get any of your Serial.print output at all? – Jaromanda X Feb 27 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.