mr_door_status

December 8th, 2011 § 3 comments

Schloss, Schalter, Arduino, Ethernet-Shield, Twitter
www.twitter.com/mr_door_status
http://mrdoor.paddd.de/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CODE:

#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();
    }
  }
}

§ 3 Responses to mr_door_status"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What's this?

You are currently reading mr_door_status at maschinenraum.

meta