mr door status

Remember our tweeting door?
http://www.gedankensuppe.de/twittdoor

Well, it worked just fine for a long time but one day we couldn’t unlock the door to our hackspace anymore. You should have seen the janitors face when he found out what we had done to the lock.
We had to remove the switch and promised ourselves to build something different that would allow people without a key to the room to stay in bed untill the room has been unlocked.

It’s been a while and during the last few months some of us started to tinker around with arduino microcontrollers. We thought having an arduino tweet the doors lock status would be awesome, this way we wouldn’t rely on our most of the time pretty unstable ubuntu server to send the message. We ordered a box of different switches and actually found one that would fit right into the lock. After hooking up an ethernet shield all we needed was the code (scroll down).

It works really well. See the status here: http://twitter.com/mr_door_status

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>
#include <EthernetDHCP.h>
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const char* ip_to_str(const uint8_t*);
int state = 1; //door will be open at boot!!!
int sensorValue = 0;

Twitter twitter("your-token-here");

void setup() {
  Serial.begin(9600);
  network();
}

void network() {
  Serial.println("attempting to obtain DHCP lease...");
  EthernetDHCP.begin(mac);

  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();

  Serial.println("DHCP lease has been obtained.");

  Serial.print("IP address is ");
  Serial.println(ip_to_str(ipAddr));

  Serial.print("gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));

  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr) {
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}

void loop() {
  door_status();
  EthernetDHCP.maintain();
  randomSeed(millis());
}

void door_status() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);

  if(sensorValue>915 && state==1){
    delay(3000);
    int sensorValue = analogRead(A0);
    if(sensorValue>915 && state==1){
      delay(3000);
      int sensorValue = analogRead(A0);
      if(sensorValue>915 && state==1){
        state=0;
        tweet(state);
      }
    }
  }
  if(sensorValue<916 && state==0) {
    delay(3000);
    int sensorValue = analogRead(A0);
    if(sensorValue<916 && state==0) {
      delay(3000);
      int sensorValue = analogRead(A0);
      if(sensorValue<916 && state==0) {
        state=1;
        tweet(state);
      }
    }
  }
  //update margin
  delay(10000);
}

void tweet(int state) {
  if(state==1) {

    int rand = random(5000);
    char txt [120];
    sprintf(txt, "der maschinenraum ist offen No. %d", state);

    Serial.println("connecting to twitter...");
    if (twitter.post(txt)) {
      int status = twitter.wait();
      if (status == 200) {
        Serial.println("status tweet OPEN sent.");
      } else {
        Serial.print("tweet failed : code ");
        Serial.println(status);
      }
    } else {
    Serial.println("connection to twitter failed.");
    setup();
    }

  }  

  if(state==0) {

    int rand = random(5000);
    char txt [120];
    sprintf(txt, "der maschinenraum ist geschlossen No. %d", state);

    Serial.println("connecting to twitter...");
    if (twitter.post(txt)) {
      int status = twitter.wait();
      if (status == 200) {
        Serial.println("status tweet CLOSED sent.");
      } else {
        Serial.print("tweet failed : code ");
        Serial.println(status);
      }
    } else {
    Serial.println("connection to twitter failed.");
    setup();
    }
  }
}

 

 

 

 

 

Flattr this!