-->
Page 1 of 1

esp_mqtt coding - where to put main loop?

PostPosted: Tue Apr 07, 2015 7:25 pm
by Marshall
Hi,

I have setup an esp-07 to read from the ADC. It is working perfectly, but I do not see where in Tuan's user_main.c that I can put my code in a 'main' loop. Under function mqttConnectedCb(uint32_t *args) I have added a code snippet to read from the ADC and store it in a buffer to be written to topic '/mqtt/topic/adc':

Code: Select all   
    MQTT_Publish(client, "/mqtt/topic/0", "hello0", 6, 0, 0);
    MQTT_Publish(client, "/mqtt/topic/1", "hello1", 6, 1, 0);
    MQTT_Publish(client, "/mqtt/topic/2", "hello2", 6, 2, 0);

    // Publish to ADC topic.
    uint8_t buffer[4];
    uint16 adc = system_adc_read();
    ets_sprintf( buffer, "%d", adc );
    MQTT_Publish(client, "/mqtt/topic/adc", buffer, 4, 1, 0);


The above gets run exactly one time AFAICT. I know the code is working OK, b/c I can publish (using mosquitto_pub) from my Mac terminal to the topics and it immediately pushes to the esp-07 subscriber. Also, I see the repeated keepAlive output.

I am a newbie at this and C is not a language I write code in for my day job. I'm just looking for that one comment that says:

Code: Select all// put main loop below here...
// ...
// end main loop.


In that main loop I would read from my ADC every n seconds and publish it to the mqtt broker.

Sorry if I'm not being clear. Sample code would be much appreciated!

Cheers,

Marshall

Re: esp_mqtt coding - where to put main loop?

PostPosted: Wed Apr 08, 2015 3:07 am
by m-cin
The code is event-driven. You could e.g. define a free-running timer and do your stuff when timer fires.

Code: Select all   static volatile os_timer_t some_timer;   //func declaration

   void ICACHE_FLASH_ATTR
   timer_callback_func(void)
   {
//your code goes here...
   }

//somewhere in user_main
   os_timer_disarm(&some_timer);
   os_timer_setfn(&some_timer, (os_timer_func_t *)timer_callback_func, 1);
   os_timer_arm(&some_timer, 50, 1);


Then everytime timer counts to zero, timer_callback_func is called. Parameters you may find in Espressif manual.
HTH
M.

Re: esp_mqtt coding - where to put main loop?

PostPosted: Wed Apr 08, 2015 3:02 pm
by Marshall
Your advice helped me very much.

I added the following function near the top of user_main.c.

Code: Select all/* Read the ADC and publish to MQTT broker */
void ICACHE_FLASH_ATTR adcPublish( uint32_t *args ) {

    MQTT_Client* client = (MQTT_Client*)args;

    // Publish to ADC topic.
    uint8_t buffer[4];
    uint16 adc = system_adc_read();
    ets_sprintf( buffer, "%d", adc );
    INFO("MQTT: Read ADC value [%s]. Publishing now.\r\n", buffer);
    MQTT_Publish(client, "/mqtt/topic/adc", buffer, 4, 1, 0);
}


At the bottom of the existing function declared as "void mqttConnectedCb(uint32_t *args)". I added:

Code: Select allos_timer_disarm(&adc_timer);
os_timer_setfn(&adc_timer, (os_timer_func_t *)adcPublish, client);
os_timer_arm(&adc_timer, 10000, 1);


Now every 10s the ADC value is being published to the broker (cloudmqtt.com in my case).

Thanks very much!

Marshall

Re: esp_mqtt coding - where to put main loop?

PostPosted: Thu Apr 09, 2015 3:41 am
by m-cin
Glad it helped :)

Regards
M.