Chat freely about anything...

User avatar
By alonewolfx2
#6722
mharizanov wrote:@tuanpm If I call MQTT_Publish twice one after another, only the second publish gets through; I think you don't queue them, notice that the

#define MAX_PUBLISH_QUEUE 5

is not used anywhere.

Cheers

can you share publish code? how can i use MQTT_Publish() function?
User avatar
By ystrem
#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 :D, so I can ask now. I'm using this library https://github.com/CHERTS/esp8266-dht11_22

Thanks :]


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 allTest=%f


Code: Select all   float x = 0.61;
   char buf[10];
   ets_sprintf(buf, "Test=%.2f", x);
   os_printf(buf);
User avatar
By tuanpm
#6751
ystrem wrote: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 :D, so I can ask now. I'm using this library https://github.com/CHERTS/esp8266-dht11_22

Fixed your code
Code: Select allchar *_Temp;
ets_sprintf(_Temp, "%f", lastTemp);
const char* Temp =    _Temp;

to
Code: Select allchar _Temp[8];
ets_sprintf(_Temp, "%f", lastTemp);
//publish
MQTT_Publish(&mqttClient, "/Room/Temp", (const char*)_Temp, 14, 2, 0);
User avatar
By tuanpm
#6752
jimbo wrote:Thanks for the library tuanpm! I managed to compile and upload it successfully (I'm new to all this), and see in my serial monitor that it connects succesfully to my AP, the internet and the (cloud) MQTT broker. I didn't alter anything other than the necessary credentials.

However, I've got a few other subscribers/publishers that listened/posted to the sample 2 topics, but I don't see any messages on the serial monitor or on my other pub/sub applications. Is this standard behaviour?

Code: Select allMQTT: Connected to xxx.cloudmqtt.com:xxxx
MQTT: Connected
MQTT: subscribe, topic"/test/topic" at broker xxx.cloudmqtt.com:xxxx
MQTT: subscribe, topic"/test2/topic" at broker xxx.cloudmqtt.com:xxxx
TCP: Sent
TCP: data received
MQTT: Subscribe successful
[*snip*, and repeated every now and then:]
MQTT: Send keepalive packet to xxx.cloudmqtt.com:xxxx!
TCP: Sent
MQTT: Published
TCP: data received

Yes, Every xxx seconds, your client will send a keepalive package to broker