FKainka / NanoESP

New NanoESP Library
19 stars 9 forks source link

Raspberry Pi doesn't receive my ping requests #14

Open W33KX opened 4 years ago

W33KX commented 4 years ago

I've made a mosquitto server on my raspberry pi and my Nano ESP is subscribed to a certain topic on that mqtt server. But the connection seems to hold it for a few seconds. I noticed on the terminal on my Pi that there are only ping requests from my phone (app that publishes to the same topic). I suppose the stayConnected(id) command should remain its connectivity to the server by pinging to it with a certain interval. Is there a way that the pings continue? **UPDATE** I've tried to count how many times I'm able to receive a message and it seems to stop at 20 each time... ***UPDATE*** I seem to have fixed the issue, I've made a counter and when it reaches 20 received messages, it'll disconnect from the server an reconnect again wich solved my problem for the meantime Anyway heres my code for the nano ESP:

#include <NanoESP.h>
#include <NanoESP_MQTT.h>

#include <SoftwareSerial.h>

#define ssid "mySSID"           //some info remains private ;)
#define pswd "myPassword"
#define username "myUsername"
#define mqtt_pswd "myPassword" 
#define nanoID 2              //this is my second nano
#define mqtt_serverID "192.168.1.103" //the address of my pi
#define send_topic "remote/samsung"
#define wifi_led 13

NanoESP esp = NanoESP();
NanoESP_MQTT mqtt = NanoESP_MQTT(esp);
long last = 0;                 //keep track of the last time I sent a pingrequest

void setup() {
  Serial.begin(19200);
  esp.init();                     //some basic code to establish connection
  esp.configWifiStation(ssid, pswd);

  if (esp.wifiConnected()) {
    Serial.println("Connected to WiFi");
    digitalWrite(wifi_led, HIGH);
    Serial.println("Current IP: " + String(esp.getIp()));
  }
  else {
    Serial.println("WiFi failed");
    while(true);
  }

  if (mqtt.connect(nanoID, mqtt_serverID, 1883, "NanoESP" + String(nanoID), username, mqtt_pswd)) {     //connect to my mosquitto mqtt server
    Serial.println("\nConnected to MQTT server: " + String(mqtt_serverID));
    if (mqtt.subscribe(nanoID, String(send_topic))) Serial.println("Subscribed to \""+ String(send_topic) + "\"");    //subscribing to my topic
  }
  else {
    Serial.println("MQTT failed");
    while(true);
  }
  Serial.println("program started");
}

void loop() {
  int id, len;      //basic code to receive my topic messages
  if (esp.recvData(id, len)) {
    String topic, val;
    if (mqtt.recvMQTT(id, len, topic, val)) {
      Serial.println(val.substring(2));     //this is just to trim down the message because the serial monitor can't display utf-8 characters
      //if(mqtt.publish(nanoID, String(send_topic), "nanoOK")) Serial.println("responded");     //respond check (this created a loop, but it works)
    }

  }
  mqtt.stayConnected(nanoID);         //trying to keep the connection alive wich doesn't work I suppose
  /*if (esp.wifiConnected()) digitalWrite(wifi_led, LOW);              //tried to check if still was connected but that caused lag on the program
  else digitalWrite(wifi_led, LOW);*/
  long timer = millis();
  if (timer - last > 1000) {        //tried to ping it myself each second without interrupting the loop with delay
    mqtt.ping(nanoID);
    last = timer;
    //Serial.println(timer);
  }

}
W33KX commented 4 years ago

The method I used to solve this issue:

//This happens in main loop
//when message is received
counter++;
if (counter%20 < 1) reconnect();
//rest of code...

//outside the loop with another method function
void reconnect() {
        if (mqtt.unsubscribe(nanoID, String(send_topic))) Serial.println("Unsubscribed to \"" + String(send_topic) + "\"");
        if (mqtt.disconnect(nanoID)) Serial.println("Disconnected, trying to reconnect");
        delay(5);
        if (mqtt.connect(nanoID, mqtt_serverID, 1883, "NanoESP" + String(nanoID), username, mqtt_pswd)) {
          Serial.println("\nConnected to MQTT server: " + String(mqtt_serverID));
          if (mqtt.subscribe(nanoID, String(send_topic), 2)) Serial.println("Subscribed to \"" + String(send_topic) + "\"");
        }
        else {
          Serial.println("MQTT failed");
          while (true);
        }
      }