MQTT example for Arduino

gpezzella
Posts: 21
Joined: Sun Jan 27, 2019 8:04 pm

MQTT example for Arduino

Postby gpezzella » Mon Apr 29, 2019 10:20 am

Hi
I need to publish some data on MQTT server.
I have done it using PubSubClient but it stop to work if web client is turn on ( this line in particular ESP32_Client = server.available(); )

I would ask if exist an arduino example that use IDF core library or some other good MQTT client

Thanks

TomWS1
Posts: 14
Joined: Wed May 01, 2019 2:50 pm

Re: MQTT example for Arduino

Postby TomWS1 » Wed May 01, 2019 3:32 pm

I have the Webserver and PubSubClient working together without any problems. I checked my code and didn't use server.available() anywhere so can't answer if that function is a problem for me. Why do you need it? I use the server.handleClient() method as it takes care of all the client gorp on its own.

Here are my web object declarations:

Code: Select all

WebServer server(80);
WiFiClient            espClient;
PubSubClient          mqttClient(espClient);
and my initialization calls:

Code: Select all

  server.begin();
  MDNS.addService("http", "tcp", 80);
  
  //=======================================================================================
  //  set up MQTT connection
  //=======================================================================================
  mqttClient.setServer(mqtt_server, 1883);
  mqttClient.setCallback(callback);
and my handler loop (in a core 0 task):

Code: Select all

  // finally, create a loop to monitor the web & MQTT interfaces
  //===========================================================================
  //  'LOOP' (Core 0)
  //===========================================================================
  while (1) {
    ArduinoOTA.handle();
    server.handleClient();
    // now take care of MQTT client...
    if (!mqttClient.connected()) {
      reconnect();
    } else 
    {
      mqttClient.loop();
    
      // manage publications...
      if (bGotPublication) {
        char topic[80];
        sprintf(topic,"%s_out/%s",clientID,pubTopic);
        mqttClient.publish(topic,pubMsg);
        bGotPublication = false;  // signal sent
      }
    }
    delay(10);    // add delay so task yields...
  }

gpezzella
Posts: 21
Joined: Sun Jan 27, 2019 8:04 pm

Re: MQTT example for Arduino

Postby gpezzella » Thu May 09, 2019 11:39 am

Hi TomWS1

Now I use ESPmDNS class and it work well

Thanks

Who is online

Users browsing this forum: No registered users and 9 guests