As part of the this blog post, the idea is to implement something that would allow the clock to detect the opening of my front door in the evening, when I get back home from work, which then reads out the temperature, weather condition and tweets. After looking into my components box, I found two thing that would help me achieve this, that is an ESP8266 board and Magnetic contact switch aka a door sensor. Basically as part of the setup, I am using MQTT to publish a message from the ESP8266 when the door is opened, which is then received by the Intel Edison attached at the back of the clock , which then reads out the weather condition and tweets via a speaker connected to a USB soundcard. Here is a quick video demo of the setup -

 

 

As part of the circuit, I have a Magnetic contact switch connected to pin#13, and I am also using the inbuilt LED on  pin#2 of the Adafuit's Huzzah ESP8266 breakout board, to light up when the door opens, this acts as an indicator for testing the setup.

Circuit.jpg

 

Here is the Arduino code uploaded to ESP8266 breakout board. For more details on how to setup Arduino IDE for ESP8266, check out the guide at -  https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout

 

/*
 Create by Carmelito for the Upcycled Clock project to send an MQTT message from ESP8266 to the Intel Edison
 Date :06-08-2017
 For more details on setting up ESP8266 breakout board checkout -https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout 
*/

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

// Update your WiFi router details below
const char* ssid = "xxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxx";
const char* mqtt_server = "test.mosquitto.org";


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0; //time before last message was sent
char msg[50];
int value = 0;
int doorPin = 13; //Magnetic contact switch
int ledPin =2; //Blue Led on the ESP8266 Huzzah board

void setup_wifi() {
  delay(10);
  //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(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    //Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(doorPin, INPUT_PULLUP);
  Serial.begin(115200); 
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  digitalWrite(ledPin, LOW); 
}

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


  long now = millis();
  if (now - lastMsg > 30000) { //30 seconds for between door opening and closing
    lastMsg = now;
    ++value;
      if(digitalRead(13) == LOW) //door is open
      {
      digitalWrite(ledPin, HIGH); 
      Serial.println("Publish message: closed ");
      client.publish("door", "closed");
      }
      else //door is closed - disable the else section after testing is done.
      {
        digitalWrite(ledPin, LOW); 
       Serial.println("Publish message:open ");
       client.publish("door", "open");
      }
      
  }
}

 

If you plan to run a test before the setting up the Intel Edison you can use chrome app called MQTT Lens which you can download using the chrome web-store. Basically, as you see in the screenshot below, I have subscribed to a topic called – door , which receives message every 30 seconds from the program above uploaded on the ESP8266.

 

MQTTlens.png

But as part of the final setup, a message will be received by the Edison only when the door is opened.

 

Now since the circuit needs to be mounted on door I 3D printed a case, which I can then nail just above the door as you see in the picture below.

 

{gallery} ESP8266

3Dprint1.jpg

3D print an enclosure

ESP8266_close.JPG

ESP8266 - Door Closed

ESP8266_open.JPG

ESP8266 - Door Open

 

Now, to setup eSpeak on the Edison check out the blog post at - Upcycled Clock - Reading out the weather using eSpeak

And once you have completed the setup for eSpeak and OpenWeatherMap API using the link above , you will have to install paho-mqtt package using pip

     $pip install paho-mqtt

 

Once done, copy paste the python code below and run a quick test using MQTT lens to publish messages to the door topic

Lens_EdisonSubscribe.png

 

Here is the python code used for testing

#Create for the Upcycle clock project using the Intel Edison -- test recive message from the ESP8266 connected to door on the topic- door
#Author : @CarmelitoA 06/09/2017
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected to server. Connection code: "+str(rc))
    #subscribe to "door" topic
    client.subscribe("door")

def on_message(client, userdata, msg):
   #print message on the topic,in this case door published from ESP8266
    #possible values msg.payload - connected, open and closed
    print("Topico: "+msg.topic+" -message recived: "+str(msg.payload))




client = mqtt.Client()
client.on_connect = on_connect   
client.on_message = on_message  


client.connect("test.mosquitto.org", 1883, 60) 
client.loop_forever()

 

 

Once you have completed your testing, for the final setup download the folder from the github repo and create a systemd service on the Intel Edison - https://github.com/CJAndrade/Upcycled-Clock/tree/master/DoorMQTT