Spivey
Published © MIT

Wemos D1 mini ESP8266 Smart IoT Button with MessageBird

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.

BeginnerFull instructions provided1 hour3,561
Wemos D1 mini ESP8266 Smart IoT Button with MessageBird

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1

Software apps and online services

Wia
Wia
MessageBird

Story

Read more

Code

Wia Code

Arduino
#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);
  // initialize serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  WiFi.begin(ssid, password);
  Serial.print("Attempting to connect to SSID: ");
  Serial.print(ssid);
  // attempt to connect to WiFi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP  network:
    // wait 5 seconds for connection:
    delay(5000);
  }
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Connected");
}

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 dataStr = "";
  serializeJson(data, dataStr);
  httpClient.beginRequest();
  httpClient.post(path);
  httpClient.sendHeader("Content-Type", "application/json");
  httpClient.sendHeader("Content-Length", dataStr.length());
  httpClient.sendHeader("Authorization", "Bearer "  + String(device_secret_key));
  httpClient.beginBody();
  httpClient.print(dataStr);
  httpClient.endRequest();
}

Credits

Spivey

Spivey

82 projects • 59 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup

Comments