Publish from Arduino Sketch to Node-Red - wanting to send Values (not strings) - results in [250, 0]

#1

I posted this earlier today on the Google Group before seeing that these new forums were up and running

Guys
,

Still a babe in the woods with all this stuff.

Essentially i am trying to send Temperature Values (and later others) from an Arduino with DS18B20 sensors

In the first instance i receive the values and store them as INT - - i then use the following to convert them to Strings to send to the publish topic on the MQTT broker.

The Publish messages get there (and i am receiving Subscribe messages that i am listening for - so i assume my sketch is OK in that regard

Here is the relevant code

void PublishValues()

{
String temp_str;
char temp[5];

 TankTemp2 = TankTemp;
 temp_str = String(TankTemp2); //converting ftemp (the float variable above) to a string 
 temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...

Serial.print("Publish message to MQTT on Topic/Temperature: ");//all of these Serial prints are to help with debuging
Serial.println(temp);   
MQTT.publish("topic/TankTemperature", temp); //money shot

}

TankTemp is defined as an INT initially - then when i was having no luck with that i created a new float TankTemp2 and tried that

The topic on the MQTT server is gettiing the info and displaying it in the Debug Window - however it is always the same value [250, 0]

I have been searching on Google and found many answers by Nick on this - i have tried taking the +1 off the end - however that truncates what i see locally down to a single digit but still gives me the same values at the receiving end [250,0]

Not sure what i am doing wrong here - must be something very obvious - i thought perhaps the 250,0 was Binary/Hex etc - but the value never changes - even when the temperature readings do change on my Arduino.

I am using Arduino IDE 1.8.2 and the included PubSub library with it.

I have an Arduino Mega with inbuilt ethernet.

regards

Craig

0 Likes

#2
  int TankTemp = 22;
  float TankTemp2;
  TankTemp2 = TankTemp;
  String temp_str;
  char temp[5];
  temp_str = String(TankTemp2); //converting ftemp (the float variable above) to a string 
  temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...

  Serial.print("Publish message to MQTT on Topic/Temperature: ");//all of these Serial prints are to help with debuging
  Serial.println(temp);   
  client.publish("topic/TankTemperature", temp); //money shot

Your code above works on my setup (I just added in dummy value of 22 for TankTemp) - does setting a dummy value work for you?
Simon

0 Likes

#3

Just realised my device was rebooting (it sends the mqtt msg before doing it so I did’nt notice it was going wrong)

I changed to
char temp[10]

and all OK - maybe you were getting a different effect from the same issue - give it a go.
Simon

0 Likes

#4

Note sure what is going on - i did a restart on everything - the Arduino, the Virtual Machine that is running my Node-Red and the PC i do the programming from and it is all working now - not sure what was going on.

Thanks for taking the time to play with it and at least validate this part

Craig

0 Likes