PIR + Ethernet Shield + Twitter

I got and highly modified this code for my arduino so that when it senses motion I will get a tweet telling my to check my dlink cloud camera. I've tried using other peoples written code but it seems to make the same behavior as the one I wrote, basically It will connect at boot up and send me a random tweet but when motion is sensed at a later time it will fail to connect and send a tweet. I've tried it with many different ethernet ports but all the same. I'm fairly new at programing so if anyone sees a flaw, I'd appreciate the help. Thank You

#include <SPI.h> / needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

byte mac[] = { 0xbe, 0xef, 0x8d, 0xe1, 0x16, 0xea };
byte ip[] = { 192, 168, 1, 10 };
Twitter twitter("1654347164-YNWo8oxIrePjftbw1pWImFYTTvcpm5kUPQMY7fq");

int ledPin = 13;             
int inputPin = 3;              
int pirState = LOW;             
int val = 0;                    
int pinSpeaker = 8;
const char msg[] = "@emilitwitt, Motion sensor triggered! Check Camera!";

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);     
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  
  if (val == HIGH) {            
    digitalWrite(ledPin, HIGH);  
    playTone(300, 160);
    tweetIt();
    delay(150);
    
      if (pirState = LOW) {
      Serial.println("HAULT");
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); 
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      Serial.println("CHILL MOTION ENDED!");
      pirState = LOW;
    }
  }
}
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

void tweetIt()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  Serial.println("connecting ...");
  //msg.concat(millis());
  if (twitter.post(msg)) {

    //i = i + 1;

    int status = twitter.wait(&Serial);
      if (status == 200) 
      {
        Serial.println("OK.");
      } 
      else
      {
        Serial.println("failed : code ");
        Serial.println(status);
      }
  }
  else
  {
   Serial.println("connection failed.");
  }
  delay(6000);
}

Perhaps you should drop back to simple tweet code that can repeatedly make tweets when requested, and get that working reliably.

I am already using the simple post sketch in my code, and it will work the first time, but other times the serial monitor will show me this

connecting ...
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Mon, 12 Aug 2013 06:52:26 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate"}]}
failed : code 403

but other times the serial monitor will show me this

Is there some part of that message that you don't understand?

If you want EVERY message, regardless of whether it is unique, use your own server.

Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate"}]}
failed : code 403

Tel you that Tweeter does'nt like when you send many time the same exact stuff. Maybe you should just add the time and date to the warning message and the problem will probably get settle.

Frédéric_Plante:

Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate"}]}

failed : code 403




Tel you that Tweeter does'nt like when you send many time the same exact stuff. Maybe you should just add the time and date to the warning message and the problem will probably get settle.

I will like to know how I can add time to msg!!

It's happening here:

void tweetIt()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  Serial.println("connecting ...");
  //msg.concat(millis());
  if (twitter.post(msg)) {                              // <<<-----Here to be very accurate

    //i = i + 1;

    int status = twitter.wait(&Serial);
      if (status == 200) 
      {
        Serial.println("OK.");
      } 
      else
      {
        Serial.println("failed : code ");
        Serial.println(status);
      }
  }
  else
  {
   Serial.println("connection failed.");
  }
  delay(6000);
}

Well unless you have a RTC module, or if you don't mind loosing a few bytes to "teach" Arduino how to keep the time, you can't really send the time as such, but you can add a line of text that say some thing like "Arduino has been running for " + millis() /1000 + " seconds" or something of the kind

Actualy you have the answer right above you just put it out of service :wink:

//msg.concat(millis());

I have an rtc and now I have this code:

         tweet( strcat("Temperatura del disipador alta a las: ",rtc.getTimeStr(FORMAT_LONG) ) );
void tweet(char *msg)
    {
    previous_Millis = millis();
    // if there's a successful connection:
      Serial.println("connecting tweeter...");
      if (twitter.post(msg)) {
                              int status = twitter.wait(&Serial);
                               if (status == 200) { Serial.println("OK.");}
                               else 
                               { Serial.print("failed : code "); Serial.println(status); }
                             } 
                             else
                             
          { Serial.println("connection tweeter failed.");}

Ok well talk about result or error message.

now it sends and error.

Tomorrow I will connect with arduino and explain with more detail.
But, What I want is "tweet" a text plus hour, like:

"Sistema ok a las: 21:30:15"

"Sistema falla a las 22:30:12"

How I can send to tweet a text with an variable?

//msg.concat(millis());

:wink: :stuck_out_tongue: :slight_smile:

But It´s not a text + variable like
"Alarma a las:" hora

const char msg[] = "@emilitwitt, Alarma a las:";

then

msg.concat(rtc.getTimeStr(FORMAT_LONG))

Try gimme news

ok
Tomorrow I will try it! 8)

Bad news.

It give me back an error " error: request for member 'concat' in 'msg', which is of non-class type 'const char [27]' "

I have this code:

void tweet(char *msg)
    {
    previous_Millis = millis();
    // if there's a successful connection:
      Serial.println("connecting tweeter...");
      if (twitter.post(msg)) {
                              int status = twitter.wait(&Serial);
                               if (status == 200) { Serial.println("OK.");}
                               else 
                               { Serial.print("failed : code "); Serial.println(status); }
                             } 
                             else
                             
          { Serial.println("connection tweeter failed.");}
          

             
          
  }

void check_alarmas() // Imprime el texto de las alarmas

{

setFont(LARGE, 255, 255, 0, 0, 0, 0);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(210, 120, 390, 192);

if (bitRead(alarma,0) == false) //SI EN ALARMA 0 TENEMOS 0 => SISTEMA OK
{
myGLCD.setColor(0, 255, 0);
myGLCD.print("SISTEMA", 250, 120);
myGLCD.print("FUNCIONANDO", 210, 145);

if (bitRead(msg_enviado,0) == false)
{
tweet( msg.concat(rtc.getTimeStr(FORMAT_LONG)) );
bitWrite(msg_enviado,0,1); //Marcamos que el msg de sit ok se ha enviado
bitWrite(msg_enviado,1,0); //y que ha sido el ultimo en enviarse
bitWrite(msg_enviado,2,0); //asi lo dejamos preparado para enviar las alarmas
}

}

else if (bitRead(alarma,1) == true) //SI EN ALARMA 1 TENEMOS 1 => ALARMA DISIPADOR
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("ALARMA", 250, 120);
myGLCD.print("TEMPERATURA", 210, 145);
myGLCD.print("DISIPADOR", 220, 165);

if (bitRead(msg_enviado,1) == false)
{
tweet( strcat("Temperatura del disipador alta a las: ",rtc.getTimeStr(FORMAT_LONG) ) );
bitWrite(msg_enviado,1,1);//Decimos que hemos enviado el del disipador
bitWrite(msg_enviado,0,0);// los demas a cero, porque no se han enviado
bitWrite(msg_enviado,2,0);
}
}

else if (bitRead(alarma,2) == true) //SI EN ALARMA 2 TENEMOS 1 => ALARMA AGUA
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("ALARMA", 250, 120);
myGLCD.print("TEMPERATURA", 210, 145);
myGLCD.print("ACUARIO", 230, 165);

if (bitRead(msg_enviado,2) == false)
{
tweet( strcat("Temperatura del agua fuera de parametros a las: ",rtc.getTimeStr(FORMAT_LONG) ) );
bitWrite(msg_enviado,2,1);//Decimos que hemos enviado el del temp agua
bitWrite(msg_enviado,1,0);// los demas a cero, porque no se han enviado
bitWrite(msg_enviado,0,0);
}
}
}

Twitter twitter("XXXXXX"); // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
const char msg[] = "@emilitwitt, Alarma a las:";

byte msg_enviado = 0x0;

Wait, i'm gonna hook on one of my RTC and check what is happening. Mean while can you send me the errors messages from you IDE debug windows.

I dont know how to ide debugging window. I have OS X.

Well there must be a black box at the bottom of the windows where the error message are written? copy what come up in there.

@emiliman101: You need to move these to setup() instead of tweetIt(). They should need to be called only once.

  Ethernet.begin(mac, ip);
  Serial.begin(9600);

Whose thread is this now?