Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

LAST WILL TESTAMENT (LWT) DOES NOT WORK .
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.

LAST WILL TESTAMENT (LWT) DOES NOT WORK .

by sauditronix on Mon Jun 15, 2020 8:12 am

I have been working on the adafruit io LWT with NODEMCU 12E without success. i am not receiving the LWT when unplugging my device.
is there a complete code example?

ARDUINO IDE
NODEMCU 12E

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino

  Works great with Adafruit's Huzzah ESP board & Feather
  ----> https://www.adafruit.com/product/2471
  ----> https://www.adafruit.com/products/2821

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Tony DiCola for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "xxxxx"
#define WLAN_PASS       "xxxxx"


/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "xxxx"
#define AIO_KEY         "xxxx"
// Adjust as necessary, in seconds.  Default to 5 minutes.
#define MQTT_CONN_KEEPALIVE 60
/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED, MQTT_QOS_1);
 
// Define a will
const char WILL_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Publish lastwill = Adafruit_MQTT_Publish(&mqtt, WILL_FEED, MQTT_QOS_1);
/*************************** Sketch Code ************************************/
 
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  Serial.println(F("Adafruit MQTT with Will demo"));
 
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);
 
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
 
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
 
  // Setup MQTT will to set on/off to "OFF" when we disconnect
  mqtt.will(WILL_FEED, "OFF");
}
 
uint32_t x=0;
 
void loop() {
  // 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();
 
  lastwill.publish("ON");  // make sure we publish ON first thing after connecting
 
  // Now we can publish stuff!
  Serial.print(F("\nSending photocell val "));
  Serial.print(x);
  Serial.print("...");
  if (! photocell.publish(x++)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }
 
  delay(5000);
}
 
// 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.
void MQTT_connect() {
  int8_t ret;
 
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}



regards

sauditronix
 
Posts: 1
Joined: Sun Mar 01, 2020 4:02 pm

Re: LAST WILL TESTAMENT (LWT) DOES NOT WORK .

by brubell on Mon Jun 15, 2020 11:06 am

What keep-alive value is your code using? You may want to try reducing the keep-alive value.

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

Re: LAST WILL TESTAMENT (LWT) DOES NOT WORK .

by sunxing on Mon Jun 15, 2020 12:30 pm

#define MQTT_CONN_KEEPALIVE 60

sunxing
 
Posts: 2
Joined: Mon Jan 23, 2017 2:43 pm

Re: LAST WILL TESTAMENT (LWT) DOES NOT WORK .

by sunxing on Wed Jun 17, 2020 10:00 am

Up

sunxing
 
Posts: 2
Joined: Mon Jan 23, 2017 2:43 pm

Re: LAST WILL TESTAMENT (LWT) DOES NOT WORK .

by brubell on Wed Jun 17, 2020 11:03 am

Does this line:

Code: Select all | TOGGLE FULL SIZE
  lastwill.publish("ON");  // make sure we publish ON first thing after connecting


ever execute and write to the last will feed on IO?

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

Please be positive and constructive with your questions and comments.