Jonathan TindalJohnathan HottellRon DagdagHarold Pulcher (Microsoft MVP)
Published © GPL3+

Alexa: Are the Kids Up?

An Alexa app that tells you if your kids are up by using Walabot to uncreeply track them, then possibly wake them up or tell them to sleep.

IntermediateFull instructions provided5 hours7,309

Things used in this project

Story

Read more

Code

Arduino alarm sketch

Arduino
/*
//########################################################################
//#  ESP8266 MQTT client, can recieve colors to turn on an LED via MQTT          
//#  Also button press to send out a MQTT message.                            
//#  This can be used along with Node-Red and Twitter                                       
//#  Blog post, videos, and code can  be found at https://www.hackster.io/dfw-hackster/alexa-are-the-kids-up-0f0852 
//########################################################################
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Timer.h"


//#######Configuration Section#########
//Update these with values suitable for your network and MQTT Broker.
const char* ssid = "ssid";
const char* password = "password";
const char* mqtt_server = "192.168.137.1";
const char* mqtt_topic = "ArduinoIn";
const char* mqtt_Pub_topic = "ArduinoOut";
const char* mqtt_user = "user";
const char* mqtt_password = "password";

//#######General Variables#######
Timer t;
char buf[100];
int lf = 10;
char vInpD5 = 0;
String rx;
String tx;
int  rxLength = 0;
int  txLength = 0;
int Counter = 0;
String payload;
const char* cpayload;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int TweetCounter = 0;


//#######Void Setup######
void setup() {
  t.every(100, timers);
  pinMode(D1, OUTPUT);
  pinMode(D0, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, INPUT_PULLUP);
  pinMode(D6, OUTPUT);
  
  //pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  Serial.print("Starting...");

}


//#######Setup Wifi######
void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


//#######Callback for MQTT Messages######
void callback(char* topic, byte* payload, unsigned int length) {\
  //Serial.print("RX: ");
  //Convert and clean up the MQTT payload messsage into a String
  rx = String((char *)payload);                    //Payload is a Byte Array, convert to char to load it into the "String" object 
  rxLength = rx.length();                          //Figure out how long the resulting String object is 
  for (int i = length; i <= rxLength; i++)         //Loop through setting extra characters to null as garbage may be there
  {
    rx.setCharAt(i, ' ');
  }
  rx.trim();                                       //Use the Trim function to finish cleaning up the string   
  Serial.print(rx);                                //Print the recieved message to serial
  Serial.println();

  digitalWrite(D1, LOW);  
  digitalWrite(D0, LOW);  
  digitalWrite(D4, LOW);  
    
  if (rx == "Red") {
    digitalWrite(D1, HIGH);  
    Serial.println("Turning on Red");
    Counter = 1;
  } 
  if (rx == "Green") {
    digitalWrite(D0, HIGH);  
    Serial.println("Turning on Green");
    Counter = 1;
  } 
  if (rx == "Blue") {
    digitalWrite(D4, HIGH);  
    Serial.println("Turning on Blue");
    Counter = 1;
  }
  if (rx == "Magenta") {
    digitalWrite(D1, HIGH);  
    digitalWrite(D4, HIGH);  
    Serial.println("Turning on Magenta");
    Counter = 1;
  }    
  if (rx == "Cyan") {
    digitalWrite(D0, HIGH);  
    digitalWrite(D4, HIGH);  
    Serial.println("Turning on Cyen");
    Counter = 1;
  } 
  if (rx == "Yellow") {
    digitalWrite(D1, HIGH);  
    digitalWrite(D0, HIGH);  
    Serial.println("Turning on Yellow");
    Counter = 1;
  } 
  if (rx == "White") {
    digitalWrite(D1, HIGH);  
    digitalWrite(D0, HIGH);  
    digitalWrite(D4, HIGH);      
    Serial.println("Turning on White");
    Counter = 1;
  }   
  if (rx == "Alarm") {
    alarm();
    delay(1000);
    alarm();
    delay(1000);
    alarm();
    delay(1000);
    alarm();
    delay(1000);           
  }        
   
}


//########MQTT Connection#####
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    //if (client.connect(mqtt_topic,mqtt_user,mqtt_password)) {
    if (client.connect(mqtt_topic,mqtt_user,mqtt_password)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(mqtt_topic, "Bridge Connected");
      // ... and resubscribe
      client.subscribe(mqtt_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


//#######Loop######
void loop() {
  if (!client.connected()) {
    reconnect();
  }

  if (digitalRead(D5) != vInpD5){
    vInpD5 = digitalRead(D5);
    if (vInpD5 == LOW){    
      client.publish(mqtt_Pub_topic, "ButtonPress");
      client.publish("Arduino/Out", "@Electronhacks Tweet from Arduino ESP8266. Hi!");
      Serial.println("ButtonPress");
      rainbow();
    }
    else{
      client.publish(mqtt_Pub_topic, "ButtonRelease");
      Serial.println("ButtonRelease");
    }
  }

  client.loop();
  t.update();
}



void timers(void){
  if (Counter > 0){
    Counter = Counter + 1;
  }
  if (Counter > 60){
    digitalWrite(D1, LOW);  
    digitalWrite(D0, LOW);  
    digitalWrite(D4, LOW);  
    Serial.println("Turning LEDs off"); 
    Counter = 0;      
  }
}

void rainbow(void){
    digitalWrite(D1, HIGH);  //Red
    delay(200);  
    digitalWrite(D0, HIGH);  //Yellow
    delay(200);
    digitalWrite(D1, LOW);   //Green
    delay(200);       
    digitalWrite(D4, HIGH);   //Cyan
    delay(200);      
    digitalWrite(D0, LOW);   //Blue
    delay(200);       
    digitalWrite(D1, HIGH);   //Magenta        
    delay(200);
    digitalWrite(D0, HIGH);
    digitalWrite(D4, HIGH);  
    delay(200);
    digitalWrite(D1, LOW);
    digitalWrite(D0, LOW);
    digitalWrite(D4, LOW);   
}


void alarm(void){
    digitalWrite(D6, HIGH);  //Red
    digitalWrite(D1, HIGH);  //Red
    delay(200);  
    digitalWrite(D0, HIGH);  //Yellow
    delay(200);
    digitalWrite(D1, LOW);   //Green
    delay(200);       
    digitalWrite(D4, HIGH);   //Cyan
    delay(200);      
    digitalWrite(D0, LOW);   //Blue
    delay(200);       
    digitalWrite(D1, HIGH);   //Magenta        
    delay(200);
    digitalWrite(D0, HIGH);
    digitalWrite(D4, HIGH);  
    delay(200);
    digitalWrite(D1, LOW);
    digitalWrite(D0, LOW);
    digitalWrite(D4, LOW);   
    digitalWrite(D6, LOW);  //Red
}

Credits

Jonathan Tindal

Jonathan Tindal

3 projects • 4 followers
Telecom Software Engineer by day, maker and artist by evening before a reasonable bed time.
Johnathan Hottell

Johnathan Hottell

11 projects • 110 followers
my website http://electronhacks.com
Ron Dagdag

Ron Dagdag

47 projects • 438 followers
Microsoft MVP award / Lead Software Engineer / Augmented Reality. Developer Passionate to learn about Robotics, VR, AR, ML, IOT
Harold Pulcher (Microsoft MVP)

Harold Pulcher (Microsoft MVP)

7 projects • 63 followers
My biggest thrill is seeing my work in use. Seeing a project being used, and the users are happy is just awesome.

Comments