MQTT demo code

// includes gauge feedback to input button

// Load Basic OTA first to enable wireless uploads

// Following is Adafruit MQTT example modified for wireless upload 

//Corrected error in example code for button. As shown below should be  Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoffbutton"); Error in code had only /feeds/onoff" which doesn't work.

/********************************************************

  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 <ArduinoOTA.h> // Enable OTA programming 1 of 3 lines

WiFiServer server(80);

 

#include <ESP8266WiFi.h>

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

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

#define WLAN_SSID       "your SSID"

#define WLAN_PASS       "your router password"

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

#define AIO_SERVER      "io.adafruit.com"

#define AIO_SERVERPORT  1883                   // use 8883 for SSL

#define AIO_USERNAME    "***your username from Adafruit.io***"

#define AIO_KEY                 "***your Active Key from Adafruit.io***"

/************ 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>

Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.

Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoffbutton");

/*************************** 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() {

 ArduinoOTA.begin();  // Enable OTA programming 2 of 3 lines

   server.begin();

  String Strreceive;

  pinMode(D4, OUTPUT);     // Initialize the LED_BUILTIN pin as an output

  

  

    WiFi.begin(WLAN_SSID, WLAN_PASS);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    

  }

 

  // Setup MQTT subscription for onoff feed.

  mqtt.subscribe(&onoffbutton);

}

uint32_t x=0;

uint32_t oldx=0;

void loop() {

  delay(500); //Reduce rate of messages. Use RTC later instead

  ArduinoOTA.handle();     // Enable OTA programming 3 of 3 lines

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

  // this is our 'wait for incoming subscription packets' busy subloop

  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(100))) {

    if (subscription == &onoffbutton) {

      String(Strreceive) = (char *)onoffbutton.lastread;

      

      

         }

  }

      String(StrON)  = "ON";

      String(StrOFF) = "OFF";

      String(Strreceive) = (char *)onoffbutton.lastread;

    if (Strreceive == StrON) digitalWrite(D4, LOW);// Turn the LED on (Note that LOW turns LED ON

    if (Strreceive == StrOFF) digitalWrite(D4, HIGH);// Turn the LED off (Note that HIGH turns LED OFF

    if (Strreceive == StrON) x=50;    

    if (Strreceive == StrOFF) x=0;

    

  // Now we can publish stuff!

  if (x != oldx){ // only publish on change

    oldx = x;

  

   photocell.publish(x); // Publish command

  }

  // ping the server to keep the mqtt connection alive

  // NOT required if you are publishing once every KEEPALIVE seconds

  /*

  if(! mqtt.ping()) {

    mqtt.disconnect();

  }

  */

}

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

  }

 

  uint8_t retries = 3;

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected

       

       mqtt.disconnect();

       delay(5000);  // wait 5 seconds

       retries--;

       if (retries == 0) {

         // basically die and wait for WDT to reset me

         while (1);

       }

  }

  

}