Chat freely about anything...

User avatar
By rvbcrs
#6808
tuanpm wrote:
rvbcrs wrote:What I would like to do is as soon as I publish to a topic on the ESP8266, I want it to respond with a publish to the other subscribers so lets say I post to topic /gettemperature that the ESP publishes a message to topic /currenttemperature

Can you please tell me how to do that tuanpm?

You need a subscriber on channel /gettemperature, when it receive your publish msg, it will response by publish msg to /currenttemperature.


this is what I have done for now.. it responds on any topic for now but I don't care for now:

Code: Select allvoid mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
   char topicBuf[64], dataBuf[64];
   MQTT_Client* client = (MQTT_Client*)args;

   os_memcpy(topicBuf, topic, topic_len);
   topicBuf[topic_len] = 0;

   os_memcpy(dataBuf, data, data_len);
   dataBuf[data_len] = 0;

   INFO("MQTT topic: %s, data: %s \r\n", topicBuf, dataBuf);

   char tempBuf[128];
   uint32_t temp_len = 128;

   struct sensor_reading* r = readDHT();
   float lastTemp=r->temperature;
   float lastHum=r->humidity;
        char* temp = (char*)((lastTemp - (int)lastTemp)*100);

   os_memcpy(tempBuf, temp, temp_len);
   tempBuf[temp_len] = 0;

   MQTT_Publish(client, "/currenttemperature", tempBuf, temp_len, 0, 0);

   /* Echo back to /echo channel*/
   MQTT_Publish(client, "/echo", dataBuf, data_len, 0, 0);
}


I have MQTT Spy subscribed to the topic /currenttemperature but I never receive it! I do receive the /echo that is published right above it.. any ideas?
User avatar
By scargill
#6871 Hi Tuanpm

I think I can safely say you've done an excellent job here, so much so that some of us are seriously considering making this pretty much central to our home control in terms of..

1. Standalone ESP boards returning info and controlling things
2. Boards that talk serially to say Arduino.

As it stands it is easy to send material to an Arduino - personally I simply surround your serial message content in delimiters and have the Arduino look for them - hence a message content that says LAMP1=ON is easy to parse and use. I've even had an RGB light working merely by returning a message "RGB=255,244,233" in delimiters.

Here are some thoughts you might like to consider.

In any real world stand alone application we need to know the time - now, subscribing to the time is no problem but right now we can't run a clock.. again - in my christmas wish list, if the board could subscribe a standard "seconds since 1970" timestamp - and anyone can do that, but then store that value somewhere and have the processor handle the 1 second tick. A simple callback routine in user_main would call every second - passing the timestamp to the user code - that would just be SO good.

In any situation using an external processor, one would want to subscript to commands - you've done that.. but one would also want to setup AP and subscriptions - you've done that - but you have to reprogram the chip every time - it would be great if one could do that by SERIAL IN rather than having to re-program every time. I'd have a go but I have no idea what FLASH is available for me to use and I'm not 100% sure how to tap into the serial input.
User avatar
By mharizanov
#6885
alonewolfx2 wrote:can you share publish code? how can i use MQTT_Publish() function?


something like this:

Code: Select all   if(sysCfg.mqtt_enable==1) {
      os_printf("Sending MQTT\n");
      
      struct sensor_reading* result = readDHT();
      if(result->success) {
         char temp[32];
         char topic[128];
         int len = dht_temp_str(temp);
         os_sprintf(topic,"%s",sysCfg.mqtt_dht22_temp_pub_topic);
         MQTT_Publish(&mqttClient,topic,temp,len,0,0);
         os_printf("Published \"%s\" to topic \"%s\"\n",temp,topic);
         
         len = dht_humi_str(temp);
         os_sprintf(topic,"%s",sysCfg.mqtt_dht22_humi_pub_topic);
         MQTT_Publish(&mqttClient,topic,temp,len,0,0);
         os_printf("Published \"%s\" to topic \"%s\"\n",temp,topic);
      }


..but it fails to publish the first topic as there is no queue..
User avatar
By gicho
#6912 I also saw that buffering doesn't work but maybe it shouldn't ... you have "void mqttPublishedCb(uint32_t *args)" to know when first publishing is ready so you can proceed (e.g. to the next value from this sensor or to the next sensor that needs to be served). This is the concept of "event", asynchronous programming. This way there is no need of huge buffers.

My point is that sending small chunks of separate topics is not the most efficient way to work on the embedded device.
What I've used on other IoT boards (but not yet on the ESP8266) is to put either JSON or type-lenght-value encoded blocks in one report. This fits in your scenario where you get both readings (temp and humidity) at the same time. It is also logical if you have and "object" (DHT11 sensor) with more than one properties. This will save you (and the broker, and the network) the overhead of multiple messages and processings. Also, no need to worry if both readings are from the same time.
JSON parser is available in the SDK, sometimes it is heavy on RAM (heap) but no matter it is probably a good alternative to the additional buffers.

I believe that most nodes in fact follow a simple sequence - startup/wakeup, read from sensor(s), optionally check if there is a change in the value since last reading, that send the data, Than maybe check for retained command message (subscribe) to execute something - e.g. change wakeup rate from 10s to 180s, or enter continuous mode (e.g. for configuration).