Hi there, this was posted elsewhere, but it was a bit of a hijack of another thread so I wanted to post it again separately.
I want to send a message (randomly chosen from a list of 6 or 7 messages) to twitter when a button is pressed, as well as the number of times that the button has been pressed.
The code is below, however - I'm getting a strange error message 'code 400' back from twitter.
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 250 };
// Your Token to Tweet
Twitter twitter("YOUR TOKEN HERE");
// Message to post
char msg[64];
#define NUMMSG
prog_char msg0[] PROGMEM = "message A";
prog_char msg1[] PROGMEM = "message B";
prog_char msg2[] PROGMEM = "message C";
prog_char msg3[] PROGMEM = "message D";
prog_char msg4[] PROGMEM = "message E";
prog_char msg5[] PROGMEM = "message F";
prog_char msg6[] PROGMEM = "message G";
PROGMEM const char *messages[] = {
msg0, msg1, msg2, msg3, msg4, msg5, msg6
};
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
delay(1000);
//Ethernet.begin(mac, ip);
// or you can use DHCP for autoomatic IP address configuration.
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println("I am connecting ...");
}
void loop(){
buttonState = digitalRead(buttonPin);
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
digitalWrite(ledPin, HIGH);
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("button pressed");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
int msgNum = random(NUMMSG);
char msgBfr[64]; // Sized to hold string to retrieve
strcpy_P(msgBfr, (char*)pgm_read_word(&(messages[msgNum])));
snprintf (msg, 64, "%s, %d", msgBfr, buttonPushCounter);
if (twitter.post(msg)) {
int status = twitter.wait();
if (status == 200) {
Serial.println("Tweeted!");
}
else {
Serial.print("failed : code ");
Serial.println(status);
}
}
else {
Serial.println("connection failed.");
}
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
When I change the code to only tweet a single message and the counter (i.e. not randomly picked from a list) it works fine - so I know that my twitter token is definitely fine. Also, once I've pressed the button a twice to test it, I get the 'I am connecting" message back again in my serial monitor, which makes me think somethings going on to make the connection drop and come back again every 3rd press...