- Thu Jan 08, 2015 5:11 pm
#6728
hi, i have a problem. I'm trying to MQTT_Publish data from DHT11. The problem is the function want const char* and I have float. I tried some things for 2 hour now
, so I can ask now. I'm using this library
https://github.com/CHERTS/esp8266-dht11_22Thanks :]
Code: Select allLOCAL void ICACHE_FLASH_ATTR dht22_cb(void *arg)
{
struct dht_sensor_data* r = DHTRead();
float lastTemp = r->temperature;
float lastHum = r->humidity;
char *_Temp;
ets_sprintf(_Temp, "%f", lastTemp);
const char* Temp = _Temp;
if(r->success)
{
MQTT_Publish(&mqttClient, "/Room/Temp", Temp, 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));
}
else
{
MQTT_Publish(&mqttClient, "/Room/Temp", "Error reading temperature and humidity\r\n", 14, 2, 0);
console_printf("Error reading temperature and humidity\r\n");
}
}
On serial console I'm getting this
Code: Select allDHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
MQTT: sending publish...
Temperature: %f *C, Humidity: %f
TCP: Sent
TCP: data received
TCP: Sent
TCP: data received
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
MQTT: sending publish...
Temperature: %f *C, Humidity: %f
TCP: Sent
TCP: data received
TCP: Sent
TCP: data received
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
And on subscriber I getting this
Code: Select allystrem@ystrem-PC:~$ mosquitto_sub -t /Room/Temp
%f
%f
%f
%f
%f
%f
UPDATE:Negher this one... Prints only
Code: Select all float x = 0.61;
char buf[10];
ets_sprintf(buf, "Test=%.2f", x);
os_printf(buf);