Build

Build an Internet of Things Smart Button with Arduino

4 min read Chandler Mayo on Jul 30, 2019
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

Using PubNub and Arduino you can build a WiFi-enabled smart button that can be used for anything.

IoT Smart Button

Smart home buttons are simple to use and are able to trigger multiple functions from a single press. Using PubNub and Arduino you can build a WiFi-enabled smart button that can be used for anything. We are going to build a smart button that’ll be used as a smart doorbell in a later tutorial, but you can do things like:

  • Dim the lights and play mood music.
  • Build your own Amazon IoT Dash Button.
  • Flash all the exterior lights, in an instant, to scare an intruder.

In this tutorial, we’ll cover how to use the PubNub Arduino SDK to send alerts from a MKR1000 development board using a built-in ATWINC1500 WiFi module.

Build an IoT Smart Button

Our button will be simple. You only need two things:

Connect the button to pin 9 and ground. We’ll use an interrupt to monitor the button press.

This is what the inside of my button looks like (it was a tight fit):

IoT Smart Button Insides

Getting Started

Download and install the latest Arduino IDE. Arduino 1.8.9 on macOS Mojave was used to make this tutorial.

Next, sign up for an always free PubNub account. Once you sign up, you can get your unique PubNub keys from the PubNub Admin Dashboard. Sign up using the form below:

Arduino IDE Setup

You’ll need to add the MKR1000 in the Boards Manager before you can use the MKR1000 with the Arduino IDE

Open the Arduino IDE and navigate to the Board Manager by going to Tools > Boards > Boards Manager. Search for “Arduino SAMD Boards”. Install Arduino SAMD Boards (32bits ARM Cortex-M0+).

Arduino Board ManagerConnect your MKR1000 development board to your computer with a USB cable. The driver should install automatically if needed.

Select the port that your board is connected to under Tools > Port. Select the board for use by going to Tools > Boards.

Select MRK1000 Arduino IDE

Install PubNub Arduino SDK

Install the PubNub Arduino SDK by going to Sketch > Include Library > Manage Libraries. Search for “PubNub”. Then select Install.

PubNub Arduino SDK

Uploading to the Board

Create a new sketch (File > New) with the following code:

#include <WiFi101.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
 
const static char ssid[] = "YOUR_NETWORK_SSID";
const static char pass[] = "YOUR_NETWORK_PASSWORD"; 

void setup() {
    Serial.begin(9600);
    pinMode(9, INPUT_PULLUP);
    if(WiFi.begin(ssid, pass) != WL_CONNECTED) { // Connect to WiFi.
       Serial.println("Can't connect to WiFi.");
       while(1) delay(100);
    }
    else {
      //Serial.print("Connected to SSID: ");
      //Serial.println(ssid);
      PubNub.begin("YOUR_PUB_KEY_HERE", "YOUR_SUB_KEY_HERE"); // Start PubNub.
      Serial.println("PubNub connected.");
    }
}
 
void loop() {
    if (digitalRead(9) == HIGH) {
      Serial.println("Button pressed.");
      char msg[] = "{\"event\":{\"button\":\"pressed\"}}";
      WiFiClient* client = PubNub.publish("smart_buttons", msg); // Publish message.
      if (0 == client) {
          Serial.println("Error publishing message.");
          delay(1000);
          return;
      }
      client->stop();
      delay(1000);
    }
    delay(100);
 }

Get your unique PubNub keys from the PubNub Admin Dashboard. If you don’t have a PubNub account, you can sign up for a PubNub account for free.

Replace “YOUR_PUB_KEY_HERE” and “YOUR_SUB_KEY_HERE” with your keys.

Replace “YOUR_NETWORK_SSID” and “YOUR_NETWORK_PASSWORD” with your WiFi settings.

Be sure that you select the port that your board is connected to under Tools > Port and select the board for use by going to Tools > Boards.

Upload the sketch (Sketch > Upload).

Testing the Smart Button

We’re using an interrupt to monitor the button press. When a press occurs, we’ll send a message to the “smart_buttons” channel with the event details.

Use the Arduino Serial Monitor to verify you’re able to connect to WiFi and publish your event over the PubNub Data Stream Network.

Verify the event is making it to PubNub by going to your PubNub Admin Dashboard, select the keys you created previously, and create a client in the Debug Console. Set the “Default Channel” to “smart_buttons” and leave the other fields blank. Click “ADD CLIENT”.

You should see messages when you press the button.

Smart Button Output

What’s Next?

Next, we’ll build a React Native app to receive a button press alert and view the press history.

What else can I do with my Smart Button?

  • Control your lights.
  • Link with an IFTTT recipe.
  • Build your own Amazon Dash Button.

Have suggestions or questions about the content of this post? Reach out at devrel@pubnub.com.

More from PubNub

NPP and HIPAA: Notice of Privacy Practices Definition
Healthcare CategoryJan 6, 20235 min read

NPP and HIPAA: Notice of Privacy Practices Definition

A Notice of Privacy Practices (NPP) is one of the requirements of HIPAA and helps patients understand their personal data rights.

Michael Carroll

Michael Carroll

HIPAA Violation Examples
Healthcare CategoryJan 5, 20236 min read

HIPAA Violation Examples

HIPAA violations can be financially expensive and devastating to a brand. Examine some examples of HIPAA violations, and learn...

Michael Carroll

Michael Carroll

How to Add a Notification Badge to Icons in React Native
Real-time Chat BlogDec 19, 20226 min read

How to Add a Notification Badge to Icons in React Native

Display real-time notification badges with PubNub and React Native to display important information and bring users back to your...

Michael Carroll

Michael Carroll