Note that I am using the latest AdafruitIO MQTT code from GitHub, as it has the latest fixes available. --> https://github.com/adafruit/Adafruit_MQTT_Library
In Adafruit IO, I created two feeds, called ledone and ledtwo, and added them to a group called leds.
I then created a dashboard with two toggle switches, and tied them to the feeds (one to each, obviously) using the default ON and OFF button text.
My hardware looks like this:
(ignore the diodes -- I used to have a 2n2222 transistor in there to drive a relay so I could turn on/off high voltages)
Here is the circuit diagram:
And, the code:
- Code: Select all | TOGGLE FULL SIZE
// Adafruit IO with two LEDS
// and two buttons
// and no_frills
// June 2016 SDM
// Load esp8266 modules
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
// Load Adafruit.io modules
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// Configure WiFi access point details.
#define WLAN_SSID "---SSID---"
#define WLAN_PASS "---PASSWORD---"
// Configure Adafruit IO access.
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "---USERNAME---"
#define AIO_KEY "---AIO-KEY---"
// Create an ESP8266 WiFiClient class to connect to the AIO server.
WiFiClient client;
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
const char BUTONE_FEED[] PROGMEM = AIO_USERNAME "/feeds/ledone";
Adafruit_MQTT_Publish butone = Adafruit_MQTT_Publish(&mqtt, BUTONE_FEED);
const char BUTTWO_FEED[] PROGMEM = AIO_USERNAME "/feeds/ledtwo";
Adafruit_MQTT_Publish buttwo = Adafruit_MQTT_Publish(&mqtt, BUTTWO_FEED);
const char LEDONE_FEED[] PROGMEM = AIO_USERNAME "/feeds/ledone";
Adafruit_MQTT_Subscribe ledone = Adafruit_MQTT_Subscribe(&mqtt, LEDONE_FEED);
const char LEDTWO_FEED[] PROGMEM = AIO_USERNAME "/feeds/ledtwo";
Adafruit_MQTT_Subscribe ledtwo = Adafruit_MQTT_Subscribe(&mqtt, LEDTWO_FEED);
void MQTT_connect();
// Mmount of time in milliseconds between uploads to adafruit.io
int long interval = 30000;
// Define hardware connections
int button01 = 0;
int button02 = 2;
int led01 = 14;
int led02 = 12;
// Define variables
int buttonState01 = HIGH;
int buttonState02 = HIGH;
int ledState01 = LOW;
int ledState02 = LOW;
int lastButtonState01 = HIGH;
int lastButtonState02 = HIGH;
int lastButtonTime01 = 0;
int lastButtonTime02 = 0;
int debounceDelay = 50;
long cts = 0;
long lts = 0;
long lats = 0;
String utc = "";
void setup() {
// Setup buttons and leds
pinMode(button01, INPUT_PULLUP);
pinMode(button02, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button01), btn1, FALLING);
attachInterrupt(digitalPinToInterrupt(button02), btn2, FALLING);
pinMode(led01, OUTPUT);
pinMode(led02, OUTPUT);
// Setup serial port.
Serial.begin(115200);
delay(10);
Serial.println(); Serial.println();
Serial.println(F("AIO Buttons"));
Serial.println(); Serial.println();
// Connect to WiFi access point.
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());
// SUBSCRIBE TO AIO FEEDS HERE
mqtt.subscribe(&ledone);
mqtt.subscribe(&ledtwo);
Serial.println(F("Ready!"));
}
void loop() {
MQTT_connect();
// get time sample
unsigned long currMillis = millis();
// check the button status
btn1();
btn2();
// get current time stamp
cts = millis();
// if enough time has passed, read the AIO status
if (cts - 10000 > lats) {
readAio();
// set last timestamp to current
lats = cts;
}
}
void readAio() {
//Serial.println("READING FROM MQTT");
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
// LED ONE AIO ON/OFF BLOCK
if (subscription == &ledone) {
String ledonedata = ((char *)ledone.lastread);
//Serial.print(F("Got LEDONE: "));
//Serial.println(ledonedata);
if (ledonedata == "ON") {
Serial.println("Turn on LED 1");
digitalWrite(led01, HIGH);
ledState01 = HIGH;
}
if (ledonedata == "OFF") {
Serial.println("Turn off LED 1");
digitalWrite(led01, LOW);
ledState01 = LOW;
}
}
// LED TWO AIO ON/OFF BLOCK
if (subscription == &ledtwo) {
String ledtwodata = ((char *)ledtwo.lastread);
//Serial.print(F("Got LEDTWO: "));
//Serial.println(ledtwodata);
if (ledtwodata == "ON") {
Serial.println("Turn on LED 2");
digitalWrite(led02, HIGH);
ledState02 = HIGH;
}
if (ledtwodata == "OFF") {
Serial.println("Turn off LED 2");
digitalWrite(led02, LOW);
ledState02 = LOW;
}
}
}
}
// LED ONE BUTTON ON/OFF BLOCK
void btn1() {
int readButton01 = digitalRead(button01);
if (readButton01 != lastButtonState01) {
lastButtonTime01 = millis();
}
if ((millis() - lastButtonTime01) > debounceDelay) {
if (readButton01 != buttonState01) {
buttonState01 = readButton01;
if (buttonState01 == LOW) {
Serial.print("BUTTON 1 PRESS : "); Serial.println(ledState01);
ledState01 = !ledState01;
Serial.println("start send"); int startmillis = millis();
if (ledState01 == LOW) { butone.publish("OFF"); };
if (ledState01 == HIGH) { butone.publish("ON"); };
Serial.println("stop send"); int stopmillis = millis();
Serial.print(" wait time : "); Serial.println(stopmillis - startmillis);
}
}
}
digitalWrite(led01, ledState01);
lastButtonState01 = readButton01;
}
// LED TWO BUTTON ON/OFF BLOCK
void btn2() {
int readButton02 = digitalRead(button02);
if (readButton02 != lastButtonState02) {
lastButtonTime02 = millis();
}
if ((millis() - lastButtonTime02) > debounceDelay) {
if (readButton02 != buttonState02) {
buttonState02 = readButton02;
if (buttonState02 == LOW) {
Serial.print("BUTTON 2 PRESS : "); Serial.println(ledState01);
ledState02 = !ledState02;
Serial.println("start send"); int startmillis = millis();
if (ledState02 == LOW) { buttwo.publish("OFF"); };
if (ledState02 == HIGH) { buttwo.publish("ON");; };
Serial.println("stop send"); int stopmillis = millis();
Serial.print(" wait time : "); Serial.println(stopmillis - startmillis);
}
}
}
digitalWrite(led02, ledState02);
lastButtonState02 = readButton02;
}
// CONNECT TO MQTT BLOCK
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
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
}
Serial.println("MQTT Connected!");
}
I used a more advanced timing technique (counting millis) instead of using delay, so that I was not holding up the CPU when I wanted to do other things. I also used interrupts on the physical buttons, so the human could press it and get a response.
Remember to add you SSID, PASSWORD, AIO Username, and AIO Key to your code before you upload to your 8266.
Enjoy!