Hi,
I am new to arduino and I am not a C/C++ programer.
I am subscribing to a MQTT Topic and would like to print the information to an LCD screen. The information from the MQTT Topic is of type byte* and to print the information to the LCD requires type char*. I have been unsuccessful in converting the byte* to char*.
The function to receive the MQTT information is as follows:
void callback(char* topic, byte* payload, unsigned int length)
{
digitalWrite(13, HIGH);
Console.println("Callback");
Console.print("\tTopic: ");
Console.println(topic);
Console.print("\tLength: ");
Console.println(length);
Console.print("\tPayload: ");
Console.write(payload,length);
Console.println();
digitalWrite(13, LOW);
}
I have tried the following, but it produces an error:
incompatible types in assignment of ‘char*’ to ‘char [7]’
char LCD_Pres[7];
if (String(topic) == "aujen/weather/current/pressure")
{
LCD_Pres=(char *)payload;
}
I have also tried:
char LCD_Pres[7];
for (int c = 0;c <=length;c++)
{
LCD_Pres[c] = char(payload[c]);
Console.print(LCD_Pres[c]);
}
This sort of works, the first iteration is correct, but there after it adds a "9" value at the end:
860.28
860.339
860.359
I then reduced the length of LCD_Pres to 6 - char LCD_Pres[6];
This stalls the script and the board hangs, have to power reset board:
Callback
Topic: aujen/weather/current/time
Length: 10
Payload: 1395480336Callback
Topic: aujen/weather/current/pressure
Length: 6
Payload: 860.36
860.36
Then I came across the sprintf command, but I can't seem to get that to work either:
if (String(topic) == "aujen/weather/current/pressure")
{
sprintf(LCD_Pres,"%u6",payload);
Console.print("Pressure: "); Console.println(LCD_Pres);
This prints, I don't know what...
Pressure: 9166
Pressure: 9166
Pressure: 9166
If I try :
sprintf(LCD_Pres,"%u",payload);
or
sprintf(LCD_Pres,"%d",payload);
Both print:
Pressure: 916
Pressure: 916
Pressure: 916
If someone can please point me in the right direction ?