Friday, January 1, 2016

Standalone ESP8266/ESP-12 to read Adafruit IO feed

This example of Standalone ESP8266/ESP-12 show how to read Adafruit IO feed of toggle button and turn on/off on-board LED.

Adafruit IO is a IoT solution by Adafruit. It's now open beta. You can sign-up and join HERE.

Then you can create a dashboard (testOnOff) with feed of toggle button (OnOff).


Then you can program standalone ESP8266/ESP-12 to read the feed from Adafruit IO, and set on-board LED accordingly.


(As shown in the video, sometimes it cannot update!)

Adafruit MQTT Library is needed. You can install it in Arduino IDE Library Manager.


The program modified from GitGub: openhomeautomation/adafruit-io-esp8266. You have edit it with your AIO_USERNAME, AIO_KEY, and also WLAN_SSID and WLAN_PASS for your WiFi router.

The on-board LED share with Serial TX pin. So we have to end Serial and set the on-board LED as output after connected to Adafruit IO server.

AdafruitIoTestOnOff.ino
/***************************************************
 * Modified from:
 * https://github.com/openhomeautomation/adafruit-io-esp8266/tree/master/esp8266_lamp_module
 * 
 * =================================================
  Adafruit ESP8266 Lamp Controller Module
  
  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino
  Works great with Adafruit's Huzzah ESP board:
  ----> https://www.adafruit.com/product/2471
  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.
  Adafruit IO example additions by Todd Treece.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

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

// WiFi parameters
#define WLAN_SSID       "testAP"
#define WLAN_PASS       "12345678"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "your AIO username"
#define AIO_KEY         "your AIO key"

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM  = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Setup a feed called 'OnOff' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/OnOff";
Adafruit_MQTT_Subscribe OnOff = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);

/*************************** Sketch Code ************************************/

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

void setup() {

  Serial.begin(115200);

  Serial.println(F("Adafruit IO Example"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // listen for events on the OnOff feed
  mqtt.subscribe(&OnOff);

  // connect to adafruit io
  connect();

  delay(500);
  //In-order to control the on-board LED,
  //have to end Serial
  Serial.end();
  // Set On-board LED to output
  pinMode(BUILTIN_LED, OUTPUT);

}

void loop() {

  Adafruit_MQTT_Subscribe *subscription;

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  // this is our 'wait for incoming subscription packets' busy subloop
  while (subscription = mqtt.readSubscription(1000)) {

    // we only care about the OnOff events
    if (subscription == &OnOff) {

      // convert mqtt ascii payload to int
      char *value = (char *)OnOff.lastread;
      //Serial.print(F("Received: "));
      //Serial.println(value);

      // Apply message to OnOff
      String message = String(value);
      message.trim();
      if (message == "ON") {
        digitalWrite(BUILTIN_LED, LOW);
        //Serial.println("On-board LED ON");
        }
      if (message == "OFF") {
        digitalWrite(BUILTIN_LED, HIGH);
        //Serial.println("On-board LED OFF");
        }

    }

  }

}




No comments:

Post a Comment