Hi,
I've seen projects where people tweet from Arduinos. I have also seen projects where people read twitter as a trigger.
What I want to do is this
Tweet sent from phone> Tweet read by Arduino > Arduino feeds pets or makes coffee etc> Arduino replies to me to let me know my bidding is done> I rest safe in the knowledge my trivial tasks are cared for.
Has anyone seen an example of this?
Do you think it is possible?
It seems possible. I'm cautious that no one has done it before to my knowledge.
Quick question. Currently when I run my code I tap the button and it sends all of the tweets in one shot (every time it loops. How do I make it only send one tweet each time the button is pressed?
I think the problem is here...
for (int i = 0; i < 4; i++){
if (twitter.post(buttonPressed[i]))
full code
#include <Twitter.h>
#include <SPI.h>
#include <Ethernet.h>
// Ethernet Shield Settings
byte mac[] = { blahblahblahblahblah };
// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { blahblahblahblahblah };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("blahblahblahblahblahblahblah");
//which pins are used as INPUT/OUTPUT
const int buttonPin = 2;
const int ledPin = 13;
//Set the buttons state to 0 to start
int buttonState = 0;
// Messages to post
char* buttonPressed[] = {"please work"," come on, please!", "I hope it works", "come on you swine!"};
void setup ()
{
delay(1000);
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println("connecting ...");
//what the pins are used as
pinMode (ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
//buttonState is the reading from the input buttonPin
buttonState = digitalRead(buttonPin);
//if the button is pressed,
if (buttonState ==HIGH){
//turn the light on
digitalWrite(ledPin, HIGH);
//and tweet
Serial.println("button pressed");
for (int i = 0; i < 4; i++){
if (twitter.post(buttonPressed[i])) {
int status = twitter.wait();
if (status ==200){
Serial.println("Tweeted!");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
}else {
Serial.println("connection failed");
}
}
}
else{
}
}
I think we are getting mixed messages. I will try to explain myself in full.
When I send the same message twice I get a 403 error due to twitters rejection of the repeat message. I'm not sure if attaching the time to each tweet will stop it and I don't know how to read the time over Ethernet and attach it to the tweet. My second plan of action was to create a list of about 30 messages that mean the same thing, these will be sent one after another, each time the button is pressed.
Example
buttonPressed > Send message 1
buttonPressed again > Send message 2
buttonPressed again > Send message 3
etc
The only issue I see with this so far is that if the unit is reset after say message 5 it could cause a 403 by repeating the same message too soon.
I'm not sure how to do this. This is my second project and I could do with a pointer. I've scanned the Arduino Cookbook and countless webpages, references and so forth. I did something like this in Processing once but I cannot remember how and those files disappeared with my old computer.
Sorry for being a pain, Coding isn't my strong point at this stage.
Does it have to be the time? If the only requirement is that consecutive messages are non-identical, wouldn't just tacking a random number on be enough?
Rather than use the for loop that James identified as the problem, keep a global int variable x (give it a proper name) that you use to tell you which message to send. When you notice a button press, send the xth message, increment x and if it's too big, set it back to zero.
Perhaps twitter.wait already deals with this, but your code checks for the button being pressed - even with the suggested change it may send multiple messages per press because the arduino is fast and checks again before you can get your finger off the button. It's usually better in such cases to check for transition instead. Search the forum for prevbuttonstate or lastbuttonstate - they're commonly used names in transition example code. Debounce would be another thing to search for too.
One of the things you will need to deal with, regardless of how you make the messages unique, is sending just one tweet when a switch is pressed. To do that, you need to detect the transition from released to pressed. In order to do that, you need to know the previous state of the switch. You have no variable(s) to record this, and no code to record the previous state(s).
int currState;
int prevState = HIGH;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
}
prevState = currState;
}
Where the comment is, you would put code to determine which transition occurred (released to pressed or pressed to released). That is easy enough to determine. If the current state is pressed, the transition was from released to pressed, and it's time to twit. Otherwise, the transition is from pressed to released, and you don't need to do anything.