0

processPackets(5000) does not work in my code anymore
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.

processPackets(5000) does not work in my code anymore

by goatcreek on Fri Aug 23, 2019 4:45 pm

Previously i had no problem with the MQTT processPackets() it ran my setCallbacks as expected.

Here is the working code snippets:
Code: Select all | TOGGLE FULL SIZE
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>

/*************
All my #define are correct in this block, but I omitted them for security purposes
there may be some missing variable initialisations but they are all correct as well.
************/

struct clockStorage {
  uint8_t lastHour;
  uint8_t lastMinute;
  uint8_t cycleCount = 0;
  bool lockState = false; // is door locked?

} clockCurrent;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);

// set up the time, doorbtn, and lockbtn feed
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
Adafruit_MQTT_Subscribe doorBtn = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/doorbtn", MQTT_QOS_1);
Adafruit_MQTT_Subscribe lockBtn = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/lockbtn", MQTT_QOS_1);

Adafruit_MQTT_Publish doorBtn_get = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/doorbtn/get");
Adafruit_MQTT_Publish lockBtn_get = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/lockbtn/get");

void setup() {

  // attach a handler to the time feed
  timefeed.setCallback(check_MQTT_Time);

  // attach a new message handler
  doorBtn.setCallback(door_Button);
  lockBtn.setCallback(lock_Button);
}

void loop() {

    if (MQTT_connect()) { // MQTT is Connected
      // GET CURRENT VALUES FROM AIO
      mqtt.subscribe(&lockBtn);
      mqtt.subscribe(&doorBtn);
      mqtt.subscribe(&timefeed);

      // read subscriptions and run 'setCallBack()'
      mqtt.processPackets(5000);
    }
}

void check_MQTT_Time(uint32_t current) {
  // Wifi, retrieving time from MQTT
  uint32_t mqttTime = current;

  // adjust to local time zone
  current += (timeZone * 60 * 60);
  int curr_hour = (current / 60 / 60) % 24;
  int curr_min  = (current / 60 ) % 60;
  // int curr_sec  = (current) % 60;

    sprintf(currentTime, "MQTT: %02d:%02d", curr_hour, curr_min);
    SERIALMON.println(currentTime);
   clockCurrent.lastHour = curr_hour;
   clockCurrent.lastMinute = curr_min;


  }
  if (DEBUG) {
    SERIALMON.println(currentTime);
  }
}

// Button Handler
void door_Button(char *doorTemp, uint16_t len) {
  // Parse doorBtn messages
  if (DEBUG) {
    SERIALMON.print(F("doorbtn -"));
    SERIALMON.println(doorTemp);
  }
  if (strcmp(doorTemp, test) == 0)
  {
    doorCommand = 0;
  } else {
    doorCommand = 1;
  }
}


// Button Handler
void lock_Button(char *lockTemp, uint16_t len) {
  // Parse lockBtn messages
  if (DEBUG) {
    SERIALMON.print(F("lockBtn -"));
    SERIALMON.println(lockTemp);
  }
  if (strcmp(lockTemp, test) == 0)
  {
    clockCurrent.lockState = 0;
  } else {
    clockCurrent.lockState = 1;
  }
}
bool MQTT_connect() {
  // Function to connect and reconnect as necessary to the MQTT server.
  // Should be called in the loop function and it will take care if connecting.
  // Requests doorBtn, lockBtn
  // Sets doorCommand, clockCurrent.lockState
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return true;
  }

  SERIALMON.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    SERIALMON.println(mqtt.connectErrorString(ret));
    SERIALMON.println("Retrying MQTT connection in 10 seconds...");
    mqtt.disconnect();
    delay(10000);  // wait 10 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      SERIALMON.println(F("Cannot Connect to MQTT"));
      return false;
    }
  }
  SERIALMON.println("MQTT Connected!");
  doorBtn_get.publish(0);
  lockBtn_get.publish(0);
  return true;
}



So all of that code worked, the correct time was retrieved, the doorBtn and lockBtn states were retrieved. now I have attempted to add it into my larger source and modified a few of the processes to make it more readable and easier to modify for me. And now it doesn't worrk. See the following for the changes. Note that I have only included the snippets that have changed, not all of the setup code above that didnt change at all.

Code: Select all | TOGGLE FULL SIZE
void loop() {
    if (MQTT_connect()) { // MQTT is Connected - get NTP time and door switches
      while (!MQTT_Feeds());
      yield();
    }
}

boolean MQTT_Feeds () {
  SERIALMON.println(F("MQTT_Feeds Start"));

  // GET CURRENT VALUES FROM AIO
  mqtt.subscribe(&lockBtn);
  mqtt.subscribe(&doorBtn);
  mqtt.subscribe(&timefeed);
  doorBtn_get.publish(0);
  lockBtn_get.publish(0);
  // read subscriptions and run 'setCallBack()'
  mqtt.processPackets(5000);
  mqtt.ping();
  return true;
}

void MQTT_Time(uint32_t current) {
  // Wifi, retrieving time from MQTT
  SERIALMON.println(F("check_MQTT_Time Start"));
  uint32_t mqttTime = current;

  // adjust to local time zone
  current += (timeZone * 60 * 60);
  int curr_hour = (current / 60 / 60) % 24;
  int curr_min  = (current / 60 ) % 60;
  // int curr_sec  = (current) % 60;

  if (curr_min > (clockRtc.lastMinute + 5)) {
    SERIALMON.println(F("Check_MQTT_Time: RTC has lost +5 minutes, updating with MQTT Time"));
    rtcCheck(mqttTime);
  }

  if (DEBUG) {
    sprintf(currentTime, "Check_MQTT_Time : %02d:%02d", curr_hour, curr_min);
    SERIALMON.println(currentTime);
  }
}



I can post the code in entirety if needed, but those are the relevant pieces. Again if there are any missing variable init's or other values I assure you they are set properly. The rest of my code works great.

And here is my serial monitor output with the new code:

Code: Select all | TOGGLE FULL SIZE
Starting Coop Monitor
Connecting to WiFi.......... WiFi connected.
Loop Start
sensor_Data start
doorState0>>doorState1
0>>0
rtcCheck : RTC: 16:27
MQTT_Connect: Connecting to MQTT...
MQTT_Connect: MQTT Connected!
MQTT_Feeds Start
check_Wake Start
Back to Sleep
Door Command: 255
Lock State: 0
door_Control Start
Door is Unlocked
Exiting door_control function
MQTT_Update Start
ESP8266 in sleep mode : (seconds) 3600


I can see the MQTT_connect() is working fine. And it runs the MQTT_feeds(). It just doesnt seem to run the setCallbacks upon processPackets(). But it did prior to this move to a seperate function called outside of the loop(). Also of note you may see at the bottom of the Serial monitor "MQTT_Update Start" that is a function I didnt show, but it properly updates the MQTT and I can verify that by logging in to io.adafruit. I can switch back to my old method, but I don't see why this would work. Any ideas?

Thanks for looking

goatcreek
 
Posts: 1
Joined: Sat Jun 29, 2019 12:26 pm

Please be positive and constructive with your questions and comments.