Shubham Santosh
Published © GPL3+

Wireless alert bell using Adafruit IO

Uses a single button to trigger 2 types of alert via the internet.

IntermediateShowcase (no instructions)1,708
Wireless alert bell using Adafruit IO

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Arduino UNO
Arduino UNO
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Resistor 47.5k ohm
Resistor 47.5k ohm
×1
LED
DIYables LED
×1
Buzzer
Buzzer
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×1
Jumper Wires
DIYables Jumper Wires
×1
Battery Holder, AA x 2
Battery Holder, AA x 2
×1
AA Batteries
AA Batteries
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit IO

Story

Read more

Schematics

ESP8266-01 Schematic

NodeMCU Schematic

Code

ESP8266-01 Code

Arduino
/***************************************************
  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       "your SSID"
#define WLAN_PASS       "enter Password "

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

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "Enter Username"
#define AIO_KEY         "Enter Aio key"

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


// modified by Shubham Santosh
// publish to the feed 'alert' 
Adafruit_MQTT_Publish alertbutton = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/alert");

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

  pinMode(0,INPUT);            // reads button press
  pinMode(2,OUTPUT);         //connected to LED providing status
  Serial.begin(115200);
  delay(10);
  Serial.println(F("Adafruit MQTT 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());

}
int newstatus=0,buttonstats=1;
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();
  // this is our 'wait for incoming subscription packets' busy subloop

 
  if(digitalRead(0)==HIGH)
  {
   buttonstats= readbutton();
  if(buttonstats==2)
  {
  Serial.println("Alert-1 triggered");
  digitalWrite(2,HIGH);
  delay(500);
  digitalWrite(2,LOW);
  }
  else if(buttonstats==3)
  {
  Serial.println("Alert-2 triggered");
  digitalWrite(2,HIGH);
  }
  else
  {
  Serial.println("ALerts Disabled");
  digitalWrite(2,LOW);
  }
  }
  if(newstatus!=buttonstats)      //if a change of button state is detected
  {
  Serial.print(F("\nSending button status "));
   // Now we can publish stuff!
  if (! alertbutton.publish(buttonstats)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }
  newstatus=buttonstats;
  delay(1000);
  }
delay(50);
  // 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;
  }

  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!");
  digitalWrite(2,HIGH);
  delay(1000);
  digitalWrite(2,LOW);
}
int readbutton()          // reads the button status
{
int alert1time=1500;
float times=0;
  while(digitalRead(0)==HIGH)
  {
    times=times+50;
        delay(50);
    }
    digitalWrite(2,LOW);
   if (times>500  and times<alert1time)
   return(2);
   else if(times>alert1time)
   return(3);
   else if(times<500) 
   return(1);
}

NodeMCU Code

Arduino
/***************************************************
  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       "enter SSID"
#define WLAN_PASS       "enter Password"

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

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "enter username"
#define AIO_KEY         "enter aio key"

/************ Global State (you don't need to change this!) ******************/

#define buzzerpin D1
// 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 ***************************************/

// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
//modified by Shubham Santosh
// Setup a feed called 'alert' for subscribing to changes.
Adafruit_MQTT_Subscribe buzzer = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/alert");

/*************************** 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);
  pinMode(buzzerpin,OUTPUT);
  pinMode(LED_BUILTIN,OUTPUT);
  delay(10);

  Serial.println(F("MQTT SOS Receiver"));

  // 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 subscription for alert feed.
  mqtt.subscribe(&buzzer);
}
int value;

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

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

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(200))) {
    if (subscription == &buzzer) {
      Serial.print(F("Got: "));
      value=atoi((char *)buzzer.lastread);
      Serial.println(value);
    }
  }
    if(value==2)    // triggers alert-1
      {
      digitalWrite(buzzerpin,HIGH);
      digitalWrite(LED_BUILTIN,LOW);
      delay(500);
      digitalWrite(buzzerpin,LOW);
      digitalWrite(LED_BUILTIN,HIGH);
      }
    else if(value==3)     // triggers alert-2
    {
    digitalWrite(buzzerpin,HIGH);
    digitalWrite(LED_BUILTIN,LOW);
    }
    else            // disable alerts
    {
    digitalWrite(buzzerpin,LOW);
    digitalWrite(LED_BUILTIN,HIGH);
}
}
// 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!");
  digitalWrite(LED_BUILTIN,LOW);
  delay(1000);
 digitalWrite(LED_BUILTIN,HIGH);   // to show MQTT is connected
}

Credits

Shubham Santosh

Shubham Santosh

13 projects • 95 followers
I am a 23 yo electronic enthusiast.

Comments