Firstly, you need a twitter account... funny enough and make your that your signed in before you do step 1, getting the OAuth token.
Download the Arduino Tweet Library here. Put the Twitter folder the Arduino Libraries folder.
The sketch below sends a tweet along with the temperature and the seconds number when motion is detected. I added 35 secs delay so as to not accidentally over do the tweets. Again as with most of my Arduino sketches I built up the string as best as I could, I'm sure it looks horrible to some programming gurus but it works and that when I stopped working with it :)
Anyways heres the Sketch.
#include "SPI.h"
#include "Ethernet.h"
#include <Twitter.h>
void getTemp();
void getMotionTemp();
//Data
float temperature = 0.00;
int temperaturePin = 0;
int secs = 0;
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 7; //the digital pin connected to the PIR sensor's output
int ledPin = 8;
// Enter a MAC address for your controller below
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
//Server to connect to
Twitter twitter(" ADD YOUR TOKEN HERE");
void setup()
{
delay(1000);
Ethernet.begin(mac);
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop()
{
delay(5000);
getTemp();
getMotionTemp();
//delay(300000);
delay(30000);
}
void getMotionTemp(){
//String data = String("Motion has been detected and the temperature in my room is "+ String(temperature,2));
//String msg = String(String(temperature,2) + "C");
char msg[140] = "Motion has been detected and the temperature in my room is ";
dtostrf(temperature,1,2, &msg[59]);
char* msg_ending = " C at ";
strcat(msg,msg_ending);
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
secs = millis()/1000;
Serial.print(secs);
Serial.println(" sec");
dtostrf(secs,1,0, &msg[70]);
char* msg_ending_to = " secs";
strcat(msg,msg_ending_to);
Serial.println(msg);
if (twitter.post(msg)) {
int status = twitter.wait();
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}
void getTemp()
{
//temperature = (((temperature - 0.5) * 100)*1.8) + 32;
temperature = (getVoltage(temperaturePin) - .5) * 100;
}
No comments:
Post a Comment