Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// To start out we're going to define some things to allow for cleaner code below; let's start with our twitter auth info:
//
#define TWITTER_OAUTH "ENTER OAUTH TOKEN HERE"
#define LIB_DOMAIN "arduino-tweet.appspot.com"
//
// lets be good Twitter citizens and limit the number of times our pumpkins tweets when it's calm. Below you can see it's set to
// send a calm tweet every 45 minutes at the absolute most. It will send an "alarm" tweet only every minute
//
#define MS_IN_MINUTES (1000 * 60)
unsigned long minimumTweetDelay = 45 * MS_IN_MINUTES;
unsigned long minimumAlarmTweetDelay = 1 * MS_IN_MINUTES;
//
// define the pin for our buzzer
//
#define PiezoPin D0
//
// define pin for Motion Sensor
//
#define MotionSensorPin A0
//
// here's your change to edit, add or delete some of those pumpkin tweets
//
static const char* CALM_MSG1 = "Sure is quiet out here. Almost...too quiet...";
static const char* CALM_MSG2 = "What a beautiful fall day, I love being a pumpkin.";
static const char* CALM_MSG3 = "Sometimes I feel empty inside.";
static const char* CALM_MSG4 = "Did you guys watch Scandal last night?";
static const char* ALARM_MSG1 = "Eat it squirrel. Well, not literally";
static const char* ALARM_MSG2 = "Warning...I'm being eaten...warning...warning...";
static const char* ALARM_MSG3 = "Who you gonna call? Squirrelbusters!";
static const char* ALARM_MSG4 = "A squirrel is just a rat with a cuter outfit - Sarah Jessic Parker, truer words were never spoken";
//
// if you've added tweets be sure to edit the calm_messages and message_count variables
//
uint8_t calm_index = 0;
uint8_t calm_message_count = 4;
static const char* calm_messages[] = { CALM_MSG1, CALM_MSG2, CALM_MSG3, CALM_MSG4 };
//
// if you've added tweets be sure to edit the alarm_messages and alarm_message_count variables
//
uint8_t alarm_index = 0;
uint8_t alarm_message_count = 4;
static const char* alarm_messages[] = { ALARM_MSG1, ALARM_MSG2, ALARM_MSG3, ALARM_MSG4 };
bool had_squirrel = false;
unsigned long lastTweet = 0;
unsigned long lastAlarmTweet = 0;
TCPClient client;
//
// void setup() is your code that runs only once;
// below we will tell our Core what pins to read as outputs and inputs,
// and what to do the first time it gets set-up
//
void setup() {
pinMode(PiezoPin, OUTPUT); // declare buzzer as output
pinMode(MotionSensorPin, INPUT); // declare sensor as input
Serial.begin(9600);
Serial.println("waiting 10 sec for sensor to calibrate");
happyBeep();//we will define what happyBeep() means a little later
//lets wait a few seconds for the sensor to calibrate...
delay(5000);
Serial.println("Ok!");
readyBeep();//we will define what readyBeep() means a little later
}
//
// void loop() is your code that will run forever (hence loop); we've made it pretty short by defining our different
// functions below (in the Spark Web IDE you can define functions in your code after calling them)
//
void loop() {
// read motion sensor
bool motionDetected = (digitalRead(MotionSensorPin) == HIGH);
if (!motionDetected) {
had_squirrel = false;
next_calm_tweet();
}
else if (motionDetected && !had_squirrel) {
had_squirrel = true;
squirrelBeep();
next_alarm_tweet();
}
}
//
// Now let's define the functions that we just called ->
//
//
// below we define that the happyBeep() function should play 2 tones with a 250 ms delay after each of them
// since the tone function doesn't wait until it's done before moving to the next command.
//
void happyBeep() {
tone(PiezoPin,440,125);
delay(250);
tone(PiezoPin,620,125);
delay(250);
}
//
// below we define that the readyBeep() function should play 2 different tones with a 250 ms delay after each of them,
// since the tone function doesn't wait until it's done before moving to the next command.
//
void readyBeep() {
tone(PiezoPin,620,125);
delay(250);
tone(PiezoPin,440,125);
delay(250);
}
//
//below we define our main buzzer - it's high pitched and lasts for longer than our happy and ready beeps
//
void squirrelBeep() {
tone(PiezoPin,2600,500);
delay(500);
}
//
// next_alarm_tweet checks to see if enough time has passed since we tweeted last,
// we don't want to flood twitter with duplicate tweets.
//
void next_alarm_tweet() {
if (!can_we_alarm_tweet()) {
return;
}
Serial.println("Alarm!");
if (alarm_index >= alarm_message_count) {
alarm_index = 0;
}
lastAlarmTweet = millis();
sendTweet(alarm_messages[alarm_index]);
alarm_index++;
}
//
// below we define when a 'calm tweet' is sent
//
void next_calm_tweet() {
if (!can_we_tweet()) {
return;
}
if (calm_index >= calm_message_count) {
calm_index = 0;
}
lastTweet = millis();
sendTweet(calm_messages[calm_index]);
calm_index++;
}
//
// Now let's revisit that whole be-good-twitter-citizens-thing
// if it's been calm for more than minimumTweetDelay, then we can tweet
//
bool can_we_tweet() {
unsigned long now = millis();
if (lastTweet == 0) {
return true;
}
else {
return ((now - lastTweet) >= minimumTweetDelay);
}
}
//
// Same as above, but with a separate counter for alarms
//
bool can_we_alarm_tweet() {
unsigned long now = millis();
if (lastAlarmTweet == 0) {
return true;
}
else {
return ((now - lastAlarmTweet) >= minimumAlarmTweetDelay);
}
}
//
// (Fancy code that sends tweets.)
// Sends HTTP GET request to "LIB_DOMAIN" twitter service
//
int sendTweet(const char * msg) {
//unsigned int now = millis();
// const char * msg = (String(inputmsg) + String(now)).c_str();
Serial.println("Trying to Tweet...");
//turn green when tweeting
RGB.control(true);
RGB.color(0, 0, 255);
bool connected = client.connect(LIB_DOMAIN, 80);
if (connected) {
Serial.println("Connection success");
}
else {
Serial.println("Connection failed");
RGB.control(false);
delay(1000);
return 0;
}
client.flush();
client.print("POST http://");
client.print(LIB_DOMAIN);
client.println("/update HTTP/1.0");
client.println("Connection: close");
client.println("Accept: text/html, text/plain");
client.print("Content-Length: ");
client.println(strlen(msg)+strlen(TWITTER_OAUTH)+14);
client.println();
client.print("token=");
client.print(TWITTER_OAUTH);
client.print("&status=");
client.println(msg);
client.println();
delay(1000);
while (client.available()) {
Serial.print((char)client.read());
}
client.flush();
client.stop();
Serial.println("Done reading the result....");
Serial.println("Done tweeting...");
Spark.publish("Pumpkin/Tweet", msg);
RGB.control(false);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.