Spacebot

From Randomdata wiki
Revision as of 22:34, 30 January 2012 by Fish (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Spacebot is used to control space related automation tasks.

Cactispacesample.png

Goals

Our goals for now

  • Temperature monitor and (remote)control
  • Space status (open, close) automation and interfacing to Twitter

Accomplished

  • Monitoring of light and temperature:
    • graphs
    • IRC #randomdata !status
    • Status.html also used for hackerspaces.nl and blogpage: status.html
    • Environment page: [1]
  • Space status tweeter to @randomdataspace

How

  • Arduino: [2]
  • Sensor shield: [3]
  • DS18B20 Temperature sensor: [4]
  • LDR (Light Dependant Resistor): [5]

The Arduino with the sensorshield and sensors is connected thru USB to a NSLU2 (this could also be a regular pc). A cron job reads every minute the serial port of the [Arduino]. It will display the value's of the light sensor and the temperature sensor. Thru a http call to our server the cron job will post these value's to the Randomdata webserver. The webserver will store the value's and add them to cacti, make some calculations if the light of the space is already on (or off) for 5 minutes and take some actions like twitter and adjust some html pages.

To fix, adjust etc

  • Publish documented code of the Arduino
  • Publish the scripts of the cron job
  • Publish the scripts of the webserver
  • Publish the IRC scripts
  • create a more permanent casing for the Arduino
  • add an electronic valve to the heating system
  • activate the central heating valve
  • add functionality to the system to put the temperature up and down (IRC "!temp up" & "!temp down" & "!temp up 17:00-23:00"

code

Arduino


#define TEMP_PIN  18
#define LDR_PIN 3

void OneWireReset(int Pin);
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);
int lightcounter;

void setup()
{
    digitalWrite(TEMP_PIN, LOW);
    pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
    Serial.begin(9600);
    delay(100);
}

void loop()
{
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0xbe);

  LowByte = OneWireInByte(TEMP_PIN);
  HighByte = OneWireInByte(TEMP_PIN);
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;

  
  if (SignBit) // If its negative
  {
     Serial.print("-");
  }
  Serial.print("temp=");
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10)
  {
     Serial.print("0");
  }
  Serial.print(Fract);
  
  int light;
  light = analogRead(LDR_PIN);
  Serial.print("&light=");
  Serial.print(light);
  
  //counter for twitter
  if (light >= 300)
  {
  lightcounter = lightcounter + 1;
  if (lightcounter > 120) lightcounter = 120;
  }
  else
  {
  lightcounter = lightcounter - 1;
  if (lightcounter <= 1) lightcounter = 0;
  }
  
  Serial.print("&lc=");
  Serial.print(lightcounter);
  Serial.print("\n");
  
  delay(5000);      // 5 second delay.  Adjust as necessary
}

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n=8; n!=0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
         pinMode(Pin, INPUT);
      }

      d=d>>1; // now the next bit is in the least sig bit position.
   }
   
}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    for (n=0; n<8; n++)
    {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        b = digitalRead(Pin);
        delayMicroseconds(50);
        d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}