ExploreEmbedded / Hornbill-Examples

89 stars 171 forks source link

Stops Publishing #29

Open co2meter opened 3 years ago

co2meter commented 3 years ago

Not sure how else to title it.

After running for about 2 and a half days, updating every 30 minutes, it suddenly will not publish anymore. I am able to connect, and I do not get a failed publish, however, the shadow is never updated.

Looking at AWS CLoudwatch Logs set to debug, there are no logs indicating that a publish was even attempted.

Let me know if there is any specific information that is needed and I'll try and get it as soon as possible.

Here is the output of the ESP32:

starting aws iot application
Attempting to connect to Wifi network: Attempting to connect to Wifi network: Connected to Wifi!
IP address: 192.168.86.44
Connected to AWS, bru
Sending data...
{"state":{"reported":{"co2":" 1647 ", "temperature":" 25.340000 ", "humidity":" 37.827148 ", "pressure":" 101369.523438 ","battery":" 3960 ",  "name":" co2-sensor ", "timestamp":" 1610827693 ", "ttl": 1611259693 }}}
Going to sleep now

Here is the relevant code for publishing:

// put your main code here, to run repeatedly:
  Serial.println("Sending data...");
  // Turning on the LED
  digitalWrite(18, HIGH);

  // Getting values
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure();
  int co2 = getco2();
  uint16_t battery = adp.batteryVoltage();

  // We want to make sure we're getting a CO2 Value
  if (co2 == 0) {
    adp.enableLDO(2,0);
    delay(3000);

    return;
  }

  // Making sure we got values from the BME sensor as well
  // We won't update the IoT shadow without these values
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from BME sensor!");
    char rule_payload[512];
    time_t now = time(nullptr);
    time_t delete_time = now + (5 * 24 * 60 * 60);
    sprintf(rule_payload, "{\"state\":{\"reported\":{\"co2\":\" %d \",\"battery\":\" %d \",  \"name\":\" co2-sensor \", \"timestamp\":\" %d \", \"ttl\": %d }}}",co2,temperature,humidity,pressure,battery,now,delete_time);
    Serial.println(rule_payload);
    while (hornbill.publish(TOPIC_NAME,rule_payload) != 0) {
      counter++;
      if (counter >= 10) {
        Serial.println("Message was not published");
        counter = 0;
        break;
      }
      delay(1000);
    }
    counter = 0;
  } else {
    // Generating the JSON Payload
    char rule_payload[512];
    time_t now = time(nullptr);
    time_t delete_time = now + (5 * 24 * 60 * 60);
    sprintf(rule_payload, "{\"state\":{\"reported\":{\"co2\":\" %d \", \"temperature\":\" %f \", \"humidity\":\" %f \", \"pressure\":\" %f \",\"battery\":\" %d \",  \"name\":\" co2-sensor \", \"timestamp\":\" %d \", \"ttl\": %d }}}",co2,temperature,humidity,pressure,battery,now,delete_time);
    Serial.println(rule_payload);
    while (hornbill.publish(TOPIC_NAME,rule_payload) != 0) {
      counter++;
      if (counter >= 10) {
        Serial.println("Message was not published");
        counter = 0;
        break;
      }
      delay(1000);
    }
    counter = 0;
  }

AWS Logs showing it connected:

2021-01-16 20:24:58.055 TRACEID:a45b3777-4cc7-b47e-45f3-93cd034b3f57 PRINCIPALID:AIDAQDMAHMFBPLGXBH6BQ [INFO]  EVENT:PublishOut TOPICNAME:$aws/events/presence/connected/co2-sensor MESSAGE:PublishOut Status: SUCCESS

AWS Logs showing it disconnected:

2021-01-16 20:26:07.459 TRACEID:9b4b1c6d-1d62-ef2d-17d6-ec835455c1bb PRINCIPALID:AIDAQDMAHMFBPLGXBH6BQ [INFO]  EVENT:MQTT Client Disconnect MESSAGE:Disconnect Status: SUCCESS

The message I would expect to see, according to AWS Documentation, would be something like this:

{
    "timestamp": "2017-08-07 18:43:59.436",
    "logLevel": "INFO",
    "traceId": "d0074ba8-0c4b-a400-69df-76326d414c28",
    "accountId": "123456789012",
    "status": "Success",
    "eventType": "UpdateThingShadow",
    "protocol": "MQTT",
    "deviceShadowName": "Jack",
    "topicName": "$aws/things/Jack/shadow/update"
}

Any help or information is much appreciated...

Thanks, RM