Maka Hernandezdanielarico
Published © LGPL

Every Time We Get a New Customer, Mario Bros Wins a Coin

Rebuilding the functionality of two police sirens to notify all the team about the clients status in real-time but in a funny way.

BeginnerFull instructions provided4 hours629
Every Time We Get a New Customer, Mario Bros Wins a Coin

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
ESP8266 - 1 channel relay module
×2
WTV020-SD-16P
×1
Metal Speaker w/ Wires - 8 ohm 0.5W
×1
Rotating Police Light - Green
×1
Rotating Police Light - Red
×1

Software apps and online services

Ubidots
Ubidots

Story

Read more

Code

Code snippet #1

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define VARIABLE_LABEL "churn"  // Put here your Ubidots variable label 

Ubidots client(TOKEN);

byte relayOn[] = {0xA0, 0x01, 0x01, 0xA2}; //0xA00101A2
byte relayOff[] = {0xA0, 0x01, 0x00, 0xA1}; //0xA00100A1

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) 
{
  if ((char)payload[0]=='1') // Relay on
  { 
    Serial.write(relayOn, sizeof(relayOn));
    delay(10000);
    Serial.write(relayOff, sizeof(relayOff));
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  }
  client.loop();
}

Code snippet #2

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define VARIABLE_LABEL "churn"  // Put here your Ubidots variable label 

Ubidots client(TOKEN);

byte relayOn[] = {0xA0, 0x01, 0x01, 0xA2}; //0xA00101A2
byte relayOff[] = {0xA0, 0x01, 0x00, 0xA1}; //0xA00100A1

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) 
{
  if ((char)payload[0]=='1') // Relay on
  { 
    Serial.write(relayOn, sizeof(relayOn));
    delay(10000);
    Serial.write(relayOff, sizeof(relayOff));
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  }
  client.loop();
}

Code snippet #3

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define VARIABLE_LABEL "new-customer"  // Put here your Ubidots variable label 

Ubidots client(TOKEN);

byte relayOn[] = {0xA0, 0x01, 0x01, 0xA2}; //0xA00101A2
byte relayOff[] = {0xA0, 0x01, 0x00, 0xA1}; //0xA00100A1

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) 
{
  if ((char)payload[0]=='1') // Relay on
  { 
    Serial.write(relayOn, sizeof(relayOn));
    delay(10000);
    Serial.write(relayOff, sizeof(relayOff));
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  }
  client.loop();
}

Code snippet #4

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define VARIABLE_LABEL "new-customer"  // Put here your Ubidots variable label 

Ubidots client(TOKEN);

byte relayOn[] = {0xA0, 0x01, 0x01, 0xA2}; //0xA00101A2
byte relayOff[] = {0xA0, 0x01, 0x00, 0xA1}; //0xA00100A1

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) 
{
  if ((char)payload[0]=='1') // Relay on
  { 
    Serial.write(relayOn, sizeof(relayOn));
    delay(10000);
    Serial.write(relayOff, sizeof(relayOff));
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  }
  client.loop();
}

Code snippet #5

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define RED_SIREN "churn"  // Put here your Ubidots variable label
#define GREEN_SIREN "new-customer"  // Put here your Ubidots variable label 

#define RESET D5
#define NEXT D6
#define PLAY D7

Ubidots client(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/
void activate(int pin)
{
  digitalWrite(pin, LOW);
  delay(500);
  digitalWrite(pin, HIGH);
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  char val = payload[0];
  //Serial.println(topic);

  if (strstr(topic,"new-customer") && val == '1')
  {
    activate(RESET);
    activate(PLAY);
    delay(2000);
  }
  else if (strstr(topic,"churn") && val == '1')
  {
    activate(RESET);
    activate(NEXT);
    delay(2000);
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {

  pinMode(RESET, OUTPUT);  
  pinMode(NEXT, OUTPUT);
  pinMode(PLAY, OUTPUT);

  digitalWrite(RESET, HIGH);
  digitalWrite(NEXT, HIGH);
  digitalWrite(PLAY, HIGH);
  
  // put your setup code here, to run once:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, GREEN_SIREN); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, RED_SIREN);
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, GREEN_SIREN); //Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe(DEVICE_LABEL, RED_SIREN);
  }
  client.loop();
}

Code snippet #6

Plain text
/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "***" // Your Ubidots TOKEN
#define WIFINAME "***" //Your SSID
#define WIFIPASS "***" // Your Wifi Pass
#define DEVICE_LABEL "customers"  // Put here your Ubidots device label
#define RED_SIREN "churn"  // Put here your Ubidots variable label
#define GREEN_SIREN "new-customer"  // Put here your Ubidots variable label 

#define RESET D5
#define NEXT D6
#define PLAY D7

Ubidots client(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/
void activate(int pin)
{
  digitalWrite(pin, LOW);
  delay(500);
  digitalWrite(pin, HIGH);
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  char val = payload[0];
  //Serial.println(topic);

  if (strstr(topic,"new-customer") && val == '1')
  {
    activate(RESET);
    activate(PLAY);
    delay(2000);
  }
  else if (strstr(topic,"churn") && val == '1')
  {
    activate(RESET);
    activate(NEXT);
    delay(2000);
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {

  pinMode(RESET, OUTPUT);  
  pinMode(NEXT, OUTPUT);
  pinMode(PLAY, OUTPUT);

  digitalWrite(RESET, HIGH);
  digitalWrite(NEXT, HIGH);
  digitalWrite(PLAY, HIGH);
  
  // put your setup code here, to run once:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, GREEN_SIREN); //Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe(DEVICE_LABEL, RED_SIREN);
  Serial.begin(9600);
  }

void loop()
{
  if(!client.connected())
  {
    client.reconnect();
    client.ubidotsSubscribe(DEVICE_LABEL, GREEN_SIREN); //Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe(DEVICE_LABEL, RED_SIREN);
  }
  client.loop();
}

Github

https://github.com/ubidots/ubidots-mqtt-esp

Credits

Maka Hernandez

Maka Hernandez

29 projects • 124 followers
danielarico

danielarico

0 projects • 0 followers

Comments