struct dht_sensor_data* r = DHTRead();
float lastTemp = r->temperature;
float lastHum = r->humidity;
char _Temp[8];
ets_sprintf(_Temp, "%f", lastTemp);
//char _Humi[10];
//ets_sprintf(_Humi, "%f", lastHum);
if(r->success)
{
MQTT_Publish(&mqttClient, "sensor/temp", (const char*)_Temp, 14, 2, 0); //publishes %fX:
//MQTT_Publish(&mqttClient, "sensor/humi", (const char*)_Humi, 14, 2, 0);
console_printf("Temperature: %d.%d *C, Humidity: %d.%d %%\r\n", (int)(lastTemp),(int)((lastTemp - (int)lastTemp)*100), (int)(lastHum),(int)((lastHum - (int)lastHum)*100));
}
It publishes %fX:
However terminal gives correct Temperature: 20.0 *C, Humidity: 41.79 %
biobier wrote:It publishes %fX:
However terminal gives correct Temperature: 20.0 *C, Humidity: 41.79 %
The ets_sprintf() function (of the sdk) does not support float "%f".
You will have to build the _Temp string in the same way as you do in console_printf()
EadF wrote:The ets_sprintf() function (of the sdk) does not support float "%f".
You will have to build the _Temp string in the same way as you do in console_printf()
Thanks got it working!
This is my working code snipped in case it can help someone:
struct dht_sensor_data* r = DHTRead();
float lastTemp = r->temperature;
float lastHum = r->humidity;
console_printf("Get size of temp data\r\n");
int tempSize = sizeof("%d.%d", (int)(lastTemp),(int)((lastTemp - (int)lastTemp)*100));
console_printf("Data size is %d \r\n",tempSize);
char _Temp[tempSize];
console_printf("Make string of it\r\n");
ets_sprintf(_Temp, "%d.%d", (int)(lastTemp),(int)((lastTemp - (int)lastTemp)*100));
console_printf("Get size of humi data\r\n");
int humiSize = sizeof("%d.%d", (int)(lastHum),(int)((lastHum - (int)lastHum)*100));
console_printf("Data size is %d \r\n",humiSize);
char _Humi[humiSize];
console_printf("Make string of it\r\n");
ets_sprintf(_Humi, "%d.%d", (int)(lastHum),(int)((lastHum - (int)lastHum)*100));
console_printf("String created\r\n");
if(r->success)
{
MQTT_Publish(&mqttClient, "sensors/test/temp", (const char*)_Temp, tempSize, 0, 0); //%fX:
MQTT_Publish(&mqttClient, "sensors/test/humi", (const char*)_Humi, humiSize, 2, 0);
console_printf("Temperature: %d.%d *C, Humidity: %d.%d %%\r\n", (int)(lastTemp),(int)((lastTemp - (int)lastTemp)*100), (int)(lastHum),(int)((lastHum - (int)lastHum)*100));
}