In this tutorial, we'll be using the Wemos D1 mini (ESP8266 based development board) and a button shield to create a smart IoT button that messages or voicemails your friends.
To get set up with the Wemos D1 mini, you'll need to follow our tutorial on getting started with the ESP8266. It details setting up the correct environment to complete the rest of this tutorial.
You'll also need to follow our tutorial on getting started with MessageBird. The MessageBird API adds capabilities like voice, video, and messaging to their applications. This enables businesses to provide the right communication experience for their customers.
TX
RST.
Sketch > Include Libraries > Manage Libraries
If a library doesn't show up in the results when you search for it, you may need to update your Arduino IDE version. You can download the latest version here.
In the Arduino IDE, copy and paste the following code:
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
#define USE_SERIAL Serial
#define NOT_PRESSED HIGH
#define PRESSED LOW
const char* ssid = "your-ssid";
const char* password = "your-password";
// get this from the wia dashboard. it should start with `d_sk`
const char* device_secret_key = "your-device-secret-key";
char server[] = "api.wia.io";
char path[] = "/v1/events";
int port = 80;
WiFiClient client;
int status = WL_IDLE_STATUS;
HttpClient httpClient = HttpClient(client, server, port);
StaticJsonDocument<200> jsonBuffer;
JsonObject root = jsonBuffer.to<JsonObject>();
typedef struct Buttons {
const byte pin = D3;
const int debounce = 100;
const unsigned long shortPress = 100;
const unsigned long doublePress = 600;
const unsigned long longPress = 1000;
unsigned long counter = 0;
int shortPressAmount = 0;
bool previousState = NOT_PRESSED;
bool currentState;
} Button;
// create a Button variable type
Button button;
void setup() {
// put your setup code here, to run once:
pinMode(button.pin, INPUT);
//");
}
void loop() {
// put your main code here, to run repeatedly:
button.currentState = digitalRead(button.pin);
if (button.currentState != button.previousState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == PRESSED) {
button.counter = millis();
}
else if (button.currentState == NOT_PRESSED) {
unsigned long currentMillis = millis();
if ((currentMillis - button.counter >= button.shortPress) && !(currentMillis - button.counter >= button.longPress)) {
// short press detected, add press to amount
button.shortPressAmount++;
}
else if ((currentMillis - button.counter >= button.longPress)) {
// long press was detected
handleLongPress();
}
}
button.previousState = button.currentState;
}
if (button.shortPressAmount >= 2) {
button.shortPressAmount = 0;
handleDoublePress();
}
else if (button.shortPressAmount == 1) {
unsigned long currentMillis = millis();
if (currentMillis - button.counter >= button.doublePress) {
handleShortPress();
button.shortPressAmount = 0;
}
}
}
void handleShortPress() {
Serial.println("short Press");
root["name"] = "shortPress";
postToWia(root);
}
void handleDoublePress() {
Serial.println("Double Press");
root["name"] = "doublePress";
postToWia(root);
}
void handleLongPress() {
Serial.println("Long Press");
root["name"] = "longPress";
postToWia(root);
}
// Adds the correct headers for HTTP and sends Data to Wia
void postToWia(JsonObject& data) {
String values of the following variables (Place the correct value between the quotation marks right of the variable names in the code):
your-ssid
your-device-secret-key
Make sure the correct board is selected - go to Tools > Board and select Wemos D1 mini Pro.
Make sure the correct port is selected - Tools > Port.
Tools > Board
Wemos D1 mini Pro
Tools > Port
Click Sketch > Upload in the menu.
Sketch > Upload
The code above publishes the three distinct Events to Wia. shortPress, doublePress and longPress. Each Event corresponds to the different types of button press that we coded onto the board. We will need three different types of Events to set up our Flows in the Wia dashboard later.
shortPress
doublePress
longPress
Certain versions of the ArduinoJson library will cause an error in the code above. If you get an error (such as 'StaticJsonBuffer' does not name a type) when compiling the code, open Sketch > Include library > Manage libraries, search for ArduinoJson and click update.
'StaticJsonBuffer' does not name a type
Sketch > Include library > Manage libraries
update
Go to your Wia dashboard and view the Events as they come in via the Device debugger.
To handle our different button presses, we will be setting up different flows that all will correspond to different actions. This will enable us to do different things with our button pressing.
Go to your Wia dashboard and click Flows in the left hand side menu. Create a new Flow with any name you like.
Flows
In Flow Studio, drag an Event Created node from the triggers section and:
Event Created
Name
Next, under the Action tab on the left panel, drag over an SMS Message node from the MessageBird section:
Action
SMS Message
Click update, then connect the Event Created node to the Send SMS Message node.
Send SMS Message
For doublePress, we will repeat the same process by building another Flow, except this time we will switch out the Send SMS Message node with a Send Voice Message node.
Send Voice Message
Once you have set up Flows for both shortPress and doublePress, go ahead and press your IoT button and see the results.
Now you are a King/Queen of MessageBird!
Note: We still haven’t used our longPress Event yet so we are going to leave that up to you, to see what you come up with.
That's it for now!