0

How to blink LED while connecting to MQTT?
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

How to blink LED while connecting to MQTT?

by stonez56 on Sun Aug 18, 2019 3:29 am

Thanks for the Adafruit.io service!
I have used voice Google Assistant to turn on light using IFTTT + Adafruit.io service without issues.

However, I would like blink LED or do other function loops while waiting mqtt.subscription.
How should I do that?

I tried to placed blinkLED() right after the loop(), but it only got executed every 1-2 second if MQTT is connected. If MQTT reconnect, then I have to wait more than 5-10 seconds.

In other words, blinkLED() will not get executed at the time I wanted.

Any recommendation?

Code: Select all | TOGGLE FULL SIZE

void loop() {

blinkLED();

String feed_lastread;
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {

    if (subscription == &light) {

      Serial.print(F("light:"));
      Serial.println((char *)light.lastread);
      feed_lastread = (char *)light.lastread;
      feed_lastread.trim(); //
      Serial.println("aven_light:" + feed_lastread); //for verifying the varialble
      // *** NOTICE: adafruit.io publishes the data as strings, not numbers!!!
      uint16_t reading = feed_lastread.toInt();
      if (reading > max_brightness) reading = max_brightness;

      Serial.print(F("Light last read: "));
      Serial.println(reading);

      changeBrightness(LEDpin, reading, prev_brightness );

      //remember this as prev brightness
      prev_brightness = reading;

    }
}

stonez56
 
Posts: 5
Joined: Mon Dec 24, 2018 3:03 am

Re: How to blink LED while connecting to MQTT?

by brubell on Mon Aug 19, 2019 10:18 am

MQTT does need to ping the server (and reconnect if disconnected). You could call blinkLED from within the MQTT_connect function so you know when your hardware is reconnecting/pinging the server (it should be doing this at the top of your loop).

brubell
 
Posts: 741
Joined: Fri Jul 17, 2015 10:33 pm

Re: How to blink LED while connecting to MQTT?

by stonez56 on Mon Aug 19, 2019 10:05 pm

brubell wrote:MQTT does need to ping the server (and reconnect if disconnected). You could call blinkLED from within the MQTT_connect function so you know when your hardware is reconnecting/pinging the server (it should be doing this at the top of your loop).


Thank you for the hint! I will give it a try.

Thank you,

Stonez56

stonez56
 
Posts: 5
Joined: Mon Dec 24, 2018 3:03 am

Please be positive and constructive with your questions and comments.