Hi, I'm trying to send a floating variable through MQTT to Node-Red

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

It sounds like you need something to put your temperature into a string. Try dtostrf.

1 Like

No, you've sent Strings like that.

Thanks for your response, I'm not very familiar with dtostrf, how do you use it?

Syntax ERROR

No, semantics error.

https://cookbook.nodered.org/mqtt/connect-to-broker

Once MQTT is connected to nodeRED, you use the function node and properties to select, then set mapping colors and layout.

See how I do it serially ... same idea:

https://www.stm32duino.com/viewtopic.php?t=959

Which do you think would be most efficient or effective, dtostrf or the Function node in nodeRED?

I think function node is more flexible.

Thank you

I have a follow-up question, how many Arduinos with WiFi can be in node-RED, and how could I put them into separate nodes?

Serial connector:

A weather station with MQTT and Node-Red (yoctopuce.com)

Under software MQTT implementation.

About decimals: How to display number as 13.0 instead of 13 in a text node - FAQs - Node-RED Forum (nodered.org)

How many? (the ? is my answer...)

In my lab playing, nodeRED has proven to be rather lean, running on a rPi Zero-W. But you must remember that there is a full webserver running, too.

As far as the number of separate connectors, that is a question best asked on the nodeRED forum.
Node-RED Forum (nodered.org)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.