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.



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 -


 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 - 

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

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

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() {
  //connecting to a WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");

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

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())) {
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(doorPin, INPUT_PULLUP);
  client.setServer(mqtt_server, 1883);
  digitalWrite(ledPin, LOW); 

void loop() {
  if (!client.connected()) {

  long now = millis();
  if (now - lastMsg > 30000) { //30 seconds for between door opening and closing
    lastMsg = now;
      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.



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


3D print an enclosure


ESP8266 - Door Closed


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



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

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("", 1883, 60) 



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 -