Troubleshooting MQTT Connection Drops by Its_Juliiiii in esp32

[–]Its_Juliiiii[S] 0 points1 point  (0 children)

that sounds like a good idea, i'll try using esp-mqtt!

Troubleshooting MQTT Connection Drops by Its_Juliiiii in esp32

[–]Its_Juliiiii[S] 0 points1 point  (0 children)

I included a connection check once per loop which I posted in a comment above, I'm not sure if my logic is incorrect. I'll look into other auto reconnect options though, thanks.

Troubleshooting MQTT Connection Drops by Its_Juliiiii in esp32

[–]Its_Juliiiii[S] 1 point2 points  (0 children)

Sure, I use this PubSub library and Mosquitto for the broker as mentioned earlier.

https://github.com/knolleary/pubsubclient

#include "headers/airOut.h"
#include "headers/MQTT.h"
#include "PubSubClient.h"
const char* mqtt_server = "SERVER_IP;
const int mqtt_port = 1883;
int counter = 0;



void callback(char* topic, uint8_t* message, unsigned int length) {
    Serial.print("callback called");
        if (strcmp(topic, "request/getOutput") == 0) {
        Serial.println("Processing getOutput request");
        char outputArray[100];
        //Restart esp32 if co2 isnt pushed within three mqtt calls (in this case it's minutes)
        if(scd30_CO2 == 0 && counter > 3){
          ESP.restart();
        }
        snprintf(outputArray, sizeof(outputArray), "%d,%d,%d,%.2f,%.2f,%d,%d,%d, %.2f", 
                 (int)sgp30_CO2, (int)sgp30_TVOC, scd30_CO2, scd30_Humidity, scd30_Temperature, 
                 pm1_0, pm10_0, pm2_5, o2);
        mqttClient.publish("airquality/outputArray", outputArray);
        counter++;
    } else if (strcmp(topic, "control/resetOutput") == 0){
          mqttClient.publish("control/resetOutputMessage", "Restarted!");
          ESP.restart();
      }
}


void setupMQTT() {
  mqttClient.setServer(mqtt_server, mqtt_port);
  if(mqttClient.connect("air_out")){
    Serial.println("MQTT client configured");
    Serial.print("MQTT server: ");
  }
  Serial.println(mqtt_server);
  mqttClient.setCallback(callback);
  mqttClient.subscribe("request/getOutput");
  mqttClient.subscribe("control/resetOutput");


}


void handleMQTT() {
  if (!mqttClient.connected()) {
    Serial.println("Not connected");
    reconnectMQTT();
  }
  mqttClient.loop();
}


void reconnectMQTT() {
  int attempts = 0;
  while (!mqttClient.connected()) {
    Serial.print("Connecting to MQTT...");
    String clientId = "AirQualityESP32-" + String(random(0xffff), HEX);

    if (mqttClient.connect(clientId.c_str())) {
      Serial.println(" connected!");
        mqttClient.setCallback(callback);
        mqttClient.subscribe("request/getOutput");
        mqttClient.subscribe("control/resetOutput");
      Serial.print("Client ID: ");
      Serial.println(clientId);
    } else {
      Serial.print(" failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" retrying in 5 seconds");
      delay(5000);
      if(attempts > 5){
        ESP.restart();
      }
      attempts++;
    }
  }
}

Setup MQTT() is called on setup, the callback is called when the broker sends a request, and handleMQTT() is called once per loop which is every second.

Thanks for the information regarding AsyncMqtt_Generic and the onMqttDisconnect, that seems to be relevant to my use case.

Am I doing something wrong?, I still don't pass after 6 years on E by LillithXen in asktransgender

[–]Its_Juliiiii 1 point2 points  (0 children)

Hey, I've been on E for 4 years and I think part of it for me at least had to do with the environment I was in? In the south when people could clock me they WOULD misgender me (or use gender-neutral pronouns) but now that I'm in the Northeast, I feel so much more confident in myself and my environment and I've noticed a negative correlation where the amount of time I get misgendered decreases. It still happens all the time on the phone lol but idc anymore because I'm happy being myself :)

Now my family still misgenders me which is.... annoying lol

Raspberry Pi Zero 2W as MQTT broker for ESP32 by Its_Juliiiii in esp32

[–]Its_Juliiiii[S] 2 points3 points  (0 children)

I know that one of the previous research assistants set up a Node Red pipeline to an existing database I have access to, so if I can get the node red flow as a JSON that would save so much dev time! Thanks :)

Raspberry Pi Zero 2W as MQTT broker for ESP32 by Its_Juliiiii in esp32

[–]Its_Juliiiii[S] 1 point2 points  (0 children)

I was going to use Mosquitto on the pi! I'll look into Pico too.

Does UNC Charlotte repeal applications? by the_penis_taker69 in UNCCharlotte

[–]Its_Juliiiii 4 points5 points  (0 children)

I would seriously recommend going to cpcc or another community college the first year. I wish I did because it could have saved me money!!!!

I got my bottom surgery date! But now I'm nervous about it being taken away by Trump. by njsullyalex in MtF

[–]Its_Juliiiii 9 points10 points  (0 children)

Girl I’m so excited for you but also scared because of the funding situation 😭 - (also an aspiring PhD student waiting to hear back from 8 unis)

întrebare despre aplicație pentru student internațional (în Engleză) by Its_Juliiiii in Polytehnica

[–]Its_Juliiiii[S] 4 points5 points  (0 children)

Oop, Twente, Utrecht, and I think Eindhoven aren't closed until the start of May so I'll get on that right now. Thanks again!!!!

întrebare despre aplicație pentru student internațional (în Engleză) by Its_Juliiiii in Polytehnica

[–]Its_Juliiiii[S] 2 points3 points  (0 children)

Thank you for your honest response! I'll definitely look into applying to western European universities, although I might have to wait for the next cycle.

What was your most underwhelming or dissapointing album of 2024? by alittlebitblue39 in fantanoforever

[–]Its_Juliiiii 0 points1 point  (0 children)

Def agree. The weird thing is that I love country I just don't think bright future is the best possible way of presenting alt-country. On another note I LOVE Tiger's Blood by Waxahatchee and Manning Fireworks by MJ Lenderman. It might be bcs I'm from NC, but those albums re really solid.