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