0

Huazzah ESP8266 MQTT automatic triggering
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Huazzah ESP8266 MQTT automatic triggering

by raspi123test on Thu May 02, 2019 10:06 am

Hi
I am building a home automation system using Huzzah basic.I'm currently toggling the on bord red LED.I am using Adafruit secure MQTT with auto connect.The problem is that i am getting automatic response during read subscription sometimes although the LED button is not changed in the adafruit IO website.The "Got" response is also weired in the terminal(I should receive a 1 or 0 but i am receiving a string).Please take a look at the screenshot.(Don't emphasize on the 2nd line as it is a output of a string compare function.It is basically checking whether a '1' is present in the received string)

got.jpg
got.jpg (303.81 KiB) Viewed 170 times


I m attaching my code below.Please take a look.Why this is happening?

Code: Select all | TOGGLE FULL SIZE
//These 3 libraries are required to gain access to unknown Wifi Network
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

//SoftwareSerial
#include<SoftwareSerial.h>

//Adafruit Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Strings.h>

//Wifi credentials.Not used by this code
#define WLAN_SSID (WiFi.SSID())
#define WLAN_PASS (WiFi.psk())

//Adafruit IO setup
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  8883                   // use 8883 for SSL
#define AIO_USERNAME    "****"
#define AIO_KEY         "****"

//ESP8266 GPIO setup
#define RED_LED 0

// 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);

//MQTT feed settings
Adafruit_MQTT_Subscribe DigitalIO = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/digital", MQTT_QOS_1);
Adafruit_MQTT_Subscribe Dev1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/dev1", MQTT_QOS_1);

// io.adafruit.com SHA1 fingerprint
const char* fingerprint = "77 00 54 2d da e7 d8 03 27 31 23 99 eb 27 db cb a5 4c 57 18";
                         

//Connect to MQTT
void MQTT_connect();
void verifyFingerprint();




void setup() {
  //First,auto connect to Wifi
  pinMode(RED_LED,OUTPUT);
  digitalWrite(RED_LED, HIGH);
  Serial.begin(115200);
  WiFiManager wifiManager;
  wifiManager.autoConnect("Blueroom");//This is a loop & it will execute until valid credentials are received to connect to a network
  Serial.println("Connected to wifi...");

  //Verify if the connection is secure
  verifyFingerprint();

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&DigitalIO);
  mqtt.subscribe(&Dev1);
  MQTT_connect();

}

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.
  if(!mqtt.connected())
  {
    //If connection is lost,first check if the connection is secure before connect
    verifyFingerprint();
    //If secure,connect
    MQTT_connect(); 
  }
 
  char str1[50];

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &DigitalIO) {
      Serial.print(F("Got: "));
      Serial.println((char *)DigitalIO.lastread);
      strcpy(str1,((char *)DigitalIO.lastread));

      if(strstr(str1,"1"))
      {
        digitalWrite(RED_LED, LOW);
        Serial.println("1 received");
      }
      else
      {
        digitalWrite(RED_LED, HIGH);
        Serial.println("0 received");
      }
    }

    if(subscription == &Dev1) {
      Serial.print(F("Got: "));
      Serial.println((char *)Dev1.lastread);
    }
  }

  // Now we can publish stuff!
  /*
  Serial.print(F("\nSending test val "));
  Serial.print(x);
  Serial.print("...");
  if (! test.publish(x++)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }
  */

  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
 
  if(! mqtt.ping(5)) {//ping 5 times
    mqtt.disconnect();
    delay(5000);
    MQTT_connect();
  }

}

//Secure connection check
void verifyFingerprint() {

  const char* host = AIO_SERVER;

  Serial.print("Connecting to ");
  Serial.println(host);

  if (! client.connect(host, AIO_SERVERPORT)) {
    Serial.println("Connection failed. Halting execution.");
    while(1);
  }

  if (client.verify(fingerprint, host)) {
    Serial.println("Connection secure.");
  } else {
    Serial.println("Connection insecure! Halting execution.");
    while(1);
  }

}

// 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 = 100;
  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!");
}

raspi123test
 
Posts: 5
Joined: Thu Nov 29, 2018 9:18 am

Re: Huazzah ESP8266 MQTT automatic triggering

by raspi123test on Sun May 05, 2019 2:16 am

Please guys.....reply!!

raspi123test
 
Posts: 5
Joined: Thu Nov 29, 2018 9:18 am

Re: Huazzah ESP8266 MQTT automatic triggering

by brubell on Thu May 09, 2019 9:54 am

Have you tried using the Adafruit IO Arduino library: https://github.com/brentru/Adafruit_IO_Arduino

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

Please be positive and constructive with your questions and comments.