call of overloaded 'publish(int&)' is ambiguous, MQTT ESP32

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
mike500
 
Posts: 1
Joined: Tue Nov 09, 2021 5:50 pm

call of overloaded 'publish(int&)' is ambiguous, MQTT ESP32

Post by mike500 »

Hi,
I'm trying to publish at Adafruit MQTT server using a very simple code, just for test. But even being very simpe, I'm getting the error message"call of overloaded 'publish(int&)' is ambiguous," during compilation. Any help will be very appreciated. I'm novice in programming.

Code: Select all

// Libraries

#include <WiFi.h> 
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"


// WiFi parameters
#define WLAN_SSID       "WLAN_SSID"
#define WLAN_PASS       "WLAN_PASS"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "AIO_USERNAME"
#define AIO_KEY         "AIO_KEY"  // Obtained from account info on io.adafruit.com


// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// Setup feeds 
Adafruit_MQTT_Publish testfeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/testfeed");


/*************************** Sketch Code ************************************/

void setup() {

  Serial.begin(115200);
  Serial.println(F("Adafruit IO Example"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // connect to adafruit io
  connect();

}

void loop() {

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  int testfeed_data = 20;
  

   //Publish data
  if (! testfeed.publish(testfeed_data))
    Serial.println(F("Failed to publish test data"));
  else
    Serial.println(F("Test data published!"));

  // Repeat every 10 seconds
  delay(10000);

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

Error message:
C:\Users\lnare\Documents\Arduino\MQTT_ADAFRUIT_TESTE_ESCRITA_FEED\MQTT_ADAFRUIT_TESTE_ESCRITA_FEED.ino: In function 'void loop()':
MQTT_ADAFRUIT_TESTE_ESCRITA_FEED:85:39: error: call of overloaded 'publish(int&)' is ambiguous
if (! testfeed.publish(testfeed_data))
^
In file included from C:\Users\lnare\Documents\Arduino\MQTT_ADAFRUIT_TESTE_ESCRITA_FEED\MQTT_ADAFRUIT_TESTE_ESCRITA_FEED.ino:18:0:
C:\Users\lnare\Documents\Arduino\libraries\Adafruit_MQTT_Library-master/Adafruit_MQTT.h:282:8: note: candidate: bool Adafruit_MQTT_Publish::publish(double, uint8_t)
bool publish(
^~~~~~~
C:\Users\lnare\Documents\Arduino\libraries\Adafruit_MQTT_Library-master/Adafruit_MQTT.h:287:8: note: candidate: bool Adafruit_MQTT_Publish::publish(int32_t)
bool publish(int32_t i);
^~~~~~~
C:\Users\lnare\Documents\Arduino\libraries\Adafruit_MQTT_Library-master/Adafruit_MQTT.h:288:8: note: candidate: bool Adafruit_MQTT_Publish::publish(uint32_t)
bool publish(uint32_t i);
^~~~~~~
exit status 1
call of overloaded 'publish(int&)' is ambiguous

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: call of overloaded 'publish(int&)' is ambiguous, MQTT ES

Post by jwcooper »

If you are able, try our higher level library that simplifies a lot of the glue code to get this to work, you can see quite a few examples here:
https://github.com/adafruit/Adafruit_IO ... r/examples

If this is an exercise in coding and you need to use the MQTT library directly, the examples there may help as a good starting point:
https://github.com/adafruit/Adafruit_MQ ... r/examples

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”