Hi I have written a code for a home security system. I have finished that part of the code and now am trying to add onto it by making it send texts to my phone when certain events happen. I am using the twitter library to do this and after much grief that twitter has given me on the duplicate message error I have finally figured it out...Kinda
I found that I can use the snprint() command and it works perfectly. I just use a variable (j) and increment it every time in the loop that way it never sends a duplicate message to twitter. Like here
#include <SPI.h>#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
// The inclusion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>
// Ethernet Shield Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00F, 0xD7, 0xA7 };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("xxxxxxxxxxxxxxxxx");
// Message to post
char msg[128];
int delayS = 60;
char j = 0;
void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
}
void loop()
{
snprintf(msg, 128, "Im going fishing %ld", j);
Serial.println("Delaying ...");
for(int i = 0; i < delayS; i++)
{
delay(1000);
Serial.print(i);
Serial.print(" ");
}
Serial.println();
Serial.println("connecting ...");
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
j++;
delay (60000);
}
However I can not figure out how to increment the variable in my code for my security system. Also I'm not even sure if this is the problem cause I switched from using the variable to using millis() and it still doesn't work. I have a blue LED called bluePin and every time it goes past its for loop i want it to send me a message. It will only send the message once though...the first time the twitter.post() command is called.
Here is my code.
#include <SPI.h>#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <Timer.h>
Password password = Password( "xxxxxxxx" ); // my passcode
const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 2, 7, 6, 4 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 3, 1, 5 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Ethernet Shield Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00F, 0xD7, 0xA7 };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("xxxxxxxxxxxxx");
// Message to post
char msg1[128];
char msg2[128];
char msg3[128];
int redPin = 9; // represents alarm being off (at home) LED
int bluePin = 15 ; // represents alarm being on (not home) LED
int checkRedLight = 8; // checks to see if alarm is off
int motionSensorsOn = 16; // Infared sensors turned on
int doorSensorsOn = 17; // Turns door sensors on
int checkDoorSensors = 18; // checks to see if door contacts have been broken or not
int checkMotionSensors = 14; // checks to see if the infared sensors have picked up any movement
int triggerAlarm = 19;// Sets off the burglar alarm
int iCanSeeYou = 0;
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
int val_5 = 0;
int i;
int armedCheck; // function used to monitor all sensors
int setAlarmOff; // function used to set burglar alarm off
int turnCamerasOn; // function to turn on cameras
Timer t;
void setup()
{
Ethernet.begin(mac);
pinMode (redPin, OUTPUT);
pinMode (bluePin, OUTPUT);
pinMode (motionSensorsOn, OUTPUT);
pinMode (doorSensorsOn, OUTPUT);
pinMode (triggerAlarm, OUTPUT);
pinMode (iCanSeeYou, OUTPUT);
pinMode (checkDoorSensors, INPUT);
pinMode (checkRedLight, INPUT);
pinMode (checkMotionSensors, INPUT);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop(){
keypad.getKey();
t.update();
snprintf(msg3, 128, "Im at the house %ld", millis());
snprintf(msg1, 128, "Im leaving the house %ld", millis());
snprintf(msg2, 128, "My alarm is going off %ld", millis());
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword(){ // function to check the passcode and arm or disarm the system
if (password.evaluate()){
twitter.post(msg3);
digitalWrite (bluePin, LOW);
digitalWrite (redPin, HIGH);
digitalWrite (motionSensorsOn, LOW);
digitalWrite (doorSensorsOn, LOW);
digitalWrite (triggerAlarm, LOW);
digitalWrite (iCanSeeYou, LOW);
t.stop (setAlarmOff);
t.stop (armedCheck);
} else{
digitalWrite (redPin, LOW);
for ( i = 0; i <= 60; i++) { // allows me 60 sec to get out of my house before all sensors are activated
digitalWrite (bluePin, HIGH);
delay (500);
digitalWrite (bluePin, LOW);
delay(500);
}
digitalWrite (bluePin, HIGH);
twitter.post(msg1);
int check = t.after(1000, setAlarm); // jumps to setAlarm function after said seconds
}}
void setAlarm() { // function used to arm the system and to begin the countdown to turn all sensors on
digitalWrite (motionSensorsOn, HIGH);
digitalWrite (doorSensorsOn, HIGH);
delay(10000);
armedCheck = t.every(1000, checkSensors);
} //jumps to checkSensors function every second
void checkSensors () { // function used to check all sensors every second
val_2 = digitalRead (checkDoorSensors);
val_4 = digitalRead (checkMotionSensors);
if (val_4 == HIGH || val_2 == HIGH) {
setAlarmOff = t.after(50000, alarmOn); // if any sensors have been triggered then it jumps to alarmOn function
turnCamerasOn = t.after (15000, camerasOn); // if any sensors have been triggered then it jumps to cameras on
t.stop (armedCheck);
}
}
void alarmOn() {
val_3 = digitalRead (checkRedLight);
if (val_3 == LOW) {
digitalWrite (triggerAlarm, HIGH); //burglar alarm going off
twitter.post(msg2);
int doIt = t.after (15000, youCaughtBitch); }
else { digitalWrite (triggerAlarm, LOW);
t.stop (armedCheck);
}
}
void camerasOn () {
val_5 = digitalRead (checkRedLight);
if (val_5 == LOW ) {
digitalWrite (iCanSeeYou, HIGH);
}
else { digitalWrite (iCanSeeYou, LOW);
t.stop (armedCheck);
}
}
void youCaughtBitch () {
digitalWrite (triggerAlarm, LOW); // turns buglar alarm off after said time
}
Like I said above I have tried using a variable (x) instead of the millis() and writting an increment statement after the twitter.post(msg1) command..didn't work. I also tried the following within the void loop()
if (bluePin== HIGH) {
for (y=0; y<1; y++) {
x++;
}
}
That doesn't work either. Can anyone point me in the right direction? Is there a command that will automatically reset the board and would that be a good idea? Also as you can tell by my code I really hoping that I could get it to send me different messages when different events happen. Is this even possible? I can't find anything on it. In my code it will only send one message, the first one called upon, and no others. And it will only send this message once unless the board is restarted. Therein lies my problems. Thank you for your time.