Hi, every time I try to use this line of code
client.publish("/main_topic", tempC);
it gives me the error: no matching function for call to 'MQTTClient::publish(const char [12], float&)'.
I've sent strings like this
String txt = "Hello World!";
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/main_topic", txt);
And it goes through perfectly, here is my code in full:
#define BROKER_IP "10.0.0.90"
#define DEV_NAME "mqttdevice"
#define MQTT_USER "mqtt"
#define MQTT_PW "password"
const char ssid[] = "Xf123456";
const char pass[] = "11050927";
#include <MQTT.h>
#ifdef ARDUINO_SAMD_MKRWIFI1010
#include <WiFiNINA.h>
#elif ARDUINO_SAMD_MKR1000
#include <WiFi101.h>
#elif ESP8266
#include <ESP8266WiFi.h>
#else
#error unknown board
#endif
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect(DEV_NAME, MQTT_USER, MQTT_PW)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/main_topic"); //SUBSCRIBE TO TOPIC /main_topic
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
// You need to set the IP address directly.
//
// MQTT brokers usually use port 8883 for secure connections.
client.begin(BROKER_IP, 1883, net);
client.onMessage(messageReceived);
connect();
lcd.begin(16,2); // initializing the LCD 16 x 2
lcd.setBacklightPin(3,POSITIVE); // Enable or Turn On the backlight
lcd.setBacklight(HIGH);
}
void loop() {
if (! baro.begin()) {
Serial.println("Couldnt find sensor");
return;
}
float tempC = baro.getTemperature();
Serial.print(tempC); Serial.println("*C");
float altm = baro.getAltitude();
Serial.print(altm/-6); Serial.println(" meters");
client.loop();
if (!client.connected()) {
connect();
}
String txt = "Hello World!";
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/main_topic", tempC); //PUBLISH TO TOPIC /main_topic MSG world
}
lcd.home();
lcd.print(altm/-6);
lcd.setCursor(0,1);
lcd.print("Meters");
lcd.setCursor(9,0);
lcd.print(tempC);
lcd.setCursor(9,1);
lcd.print("Celsius");
lcd.setCursor(7,0);
lcd.print("|");
lcd.setCursor(7,1);
lcd.print("|");
}
Note: I used sample code from the Arduino projects page (https://create.arduino.cc/projecthub/officine/interfacing-arduino-mkr-or-esp-via-mqtt-node-red-101-4833bc), and added my sensor and LCD tweaks