Trying To Tweet From Pushbotton

Below is my code. I’m simply trying to get a push button to send a tweet. The devices I’m using are the Arduino Uno and Ethernet Shield. It works fine if I have the tweet msg in the setup function, but as soon as I attach it to an if statement in the loop, it does not even connect to the server in my serial monitor. I don’t know what the problem is, it looks right to me…I really appreciate you alls help!

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

// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

int button1 = 11; // button1 put into pin 11
int button2 = 9; // button1 put into pin 9
int button3 = 12; // button1 put into pin 12
int ledRed = 5; // red led put into pin 5
int ledGreen = 6; // green led put into pin 6
int buttonState1 = 0;

// Ethernet Shield Settings
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don’t specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip = { 192, 168, 1, 18 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("");

// Message to post
char msg = “Hello #test”;
char msg1 = “Hello, it’s #yes”;
char msg2 = “Hello, it’s me #hello”;

void setup()
{
delay(1000);
Ethernet.begin(mac, ip);
// or you can use DHCP for autoomatic IP address configuration.
// Ethernet.begin(mac);
Serial.begin(9600);
Serial.println(“connecting …”);
pinMode(9, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}

void loop()
{

buttonState1 = digitalRead(button1);

byte inChar;

inChar = Serial.read();
delay(100);
if(buttonState1 == HIGH)
{
Serial.println(“connecting …”);
if (twitter.post(msg))
{
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait();

if (status == 200) {
Serial.println(“OK.”);
}
else
{
Serial.print("failed : code ");
Serial.println(status);
}

inChar = 0;
}
}

if (button2 = HIGH) {
if (twitter.post(msg1)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println(“OK.”);
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println(“connection failed.”);
}
}

if (button3 = HIGH) {
if (twitter.post(msg2)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println(“OK.”);
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println(“connection failed.”);
}
}
}

  pinMode(9, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);

You gave the pins names. Why aren’t you using them?

I don’t know what the problem is

The problem is that you haven’t told us how the switches are wired. At least one of the problems, that is.

   inChar = Serial.read();

Even if there is nothing to read?

   delay(100);

Why?

   if(buttonState1 == HIGH)
  if (button2 = HIGH) {
  if (button3 = HIGH) {

This == a problem.

I'd expect that code to print "connecting ..." to the serial monitor at startup. Does it do that?

I'd also expect it to print "connecting ..." again when input 11 goes HIGH. If it doesn't do that when you press the button then I suggest you check your wiring and circuit logic to see why that input isn't going HIGH.

You don't print anything if twitter.post(msg) returns false, and I think it would be sensible to add an else clause to print an error message when that happens. You don't print a message before calling twitter.post(msg1) or twitter.post(msg2) so we have no idea whether it is being attempted, and whether it is failing. I suggest you add print statements to make that clear.

Also note that "if (button2 = HIGH) {" is nonesense - you need to use == not =, and you need to test the result of digitalRead(button2) and not just compare (or assign) the pin number. Similarly for button3. These errors wouldn't prevent the first button from working but will cause the code for your other two buttons to execute at the wrong time. I think you would be better off chopping out the code for button2 and button3 until you have got button1 working.

Yes indeed. It does exactly what you said Peter. It does exactly that. I took what Paul said into account as well. I’ve added the == to the buttonSate1 and also took out the other buttons until I figure out this first one.

I check the serial monitor and see “Connecting…” then i press my push button and see “Connecting…” pop up again, exactly like you said. But the tweet is not send and nothing else runs. I have an else follow the if that reads…

else
{
Serial.print(“failed : code”);
Serial.printIn(status):
}

As fa as the “byte inChar” reading nothing. Should I just get rid of that? From my understanding, that reads the serial monitor and looks for what I specify in the Serial.read (); If I don’t want to input anything from the serial port and want the button to do everything, should I just get rid of it?

Sorry for all the questions, I’m new to this stuff. It’s awesome of you all to come on here and answer these questions. I bow down to you.

iamtheream: I have an else follow the if that reads...

else { Serial.print("failed : code"); Serial.printIn(status): }

There is no 'else' part to this if statement:

if (twitter.post(msg))

If twitter.post() returns false, your sketch does not do anything. Presumably this is what is happening. I suggest you add an else clause with a print statement that tells you when twitter.post() returns false. Then at least we will know where and when the problem is happening, even if we don't know why yet.

Just a note...I don't think you want to post your real Twitter token publicly. You can change it to "Twitter token" so people know that it is there, unless it is just a development account and you don't mind it getting hacked.

Thanks pekasus! I removed it, I did not even think of that. PeterH, Like this code that I posted below? thanks again for all the support.

buttonState1 = digitalRead(button1);

if(buttonState1 == HIGH) { if (twitter.post(msg)) { // Specify &Serial to output received response to Serial. // If no output is required, you can just omit the argument, e.g. // int status = twitter.wait(); int status = twitter.wait(&Serial); if (status == 200) { Serial.println("OK."); } else { Serial.print("failed : code "); Serial.println(status); } } else { Serial.println("connection failed."); }

I'd prefer to see the error message relate more directly to what caused it ("twitter post failed" rather than "connection failed") but otherwise you got the idea.

I added the above code and it is still reading only, “Connecting…” once there serial monitor is open, then “Connecting…” once I press the button. It does not execute the tweet.

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

// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

int button1 = 11; // button1 put into pin 11
int buttonState1 = 0;

// Ethernet Shield Settings
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don’t specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip = { 192, 168, 1, 18 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("");

// Message to post
char msg = “Hello #test”;
char msg1 = “Hello, it’s #yes”;
char msg2 = “Hello, it’s me #hello”;

void setup()
{
delay(1000);
Ethernet.begin(mac, ip);
// or you can use DHCP for autoomatic IP address configuration.
// Ethernet.begin(mac);
Serial.begin(9600);
Serial.println(“connecting …”);
pinMode(11, INPUT);

}

void loop()
{
buttonState1 = digitalRead(button1);

if(buttonState1 == HIGH) {
Serial.println(“connecting …”);
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println(“OK.”);
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println(“Twitter post failed”);
}
}
}

Does "connecting..." mean that you are trying to connect to twitter or does it mean that you are in setup()? Why print the same thing meaning two different things? "Hi, I'm Darryl, and this is my brother Darryl, and this is my other brother Darryl..."

How is your switch wired?

That is NOT how to post code. That is not how to format code. Use Tools + Auto Format BEFORE posting code (or, better yet, all the time) and use the # icon when posting code.

iamtheream: I added the above code and it is still reading only, "Connecting..." once there serial monitor is open, then "Connecting..." once I press the button. It does not execute the tweet.

Does it return from the twitter.post() call?

PeterH, it does not return at all which is very weird. It does not even print another "Connecting..." when I pressed the button again. PaulS, I'm sorry I'll start posting my code appropriately. The wiring is simple. Ground and 5V from the Arduino going to the bread boards - and + rails, a jumper form pin 11 going to the push button, jumper going to power on the push button and a 10k resistor going from the ground rail to the button. The serial monitor is acknowledging the button being pressed, but then stays and does not execute the Twitter command.

The wiring is simple. Ground and 5V from the Arduino going to the bread boards - and + rails, a jumper form pin 11 going to the push button, jumper going to power on the push button and a 10k resistor going from the ground rail to the button.

A picture is worth 1000 words.

Using the internal pullup resistors makes wiring switches so much easier. Connect one leg to ground. Connect the other leg to a digital pin. Enable the pullup resistor, using either:

pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);

or

pinMode(switchPin, INPUT_PULLUP);

Then, LOW means pressed.

iamtheream: PeterH, it does not return at all

Perhaps I was being too subtle. How could you tell whether it returns from that method call or not?

I tried adding the pullup resistor code in the set up and it still has the same effect. I hit the push button and it prints, “Starting to send tweet.” then stops.

void setup()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  // or you can use DHCP for autoomatic IP address configuration.
  // Ethernet.begin(mac);
  Serial.begin(9600);
  Serial.println("connecting ...");
  pinMode(11, INPUT);
  digitalWrite(11, LOW);

  
}
  
void loop()
{
   buttonState1 = digitalRead(button1);
  
   if(buttonState1 == HIGH) {
     Serial.println("Starting To Send Tweet");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    //int status = twitter.wait();
   int status = twitter.wait(&Serial);
   if (status == 200) {
      Serial.println("OK.");
   } else {
     Serial.print("failed : code ");
      Serial.println(status);
   }
  } else {
    Serial.println("Twitter post failed");
  }
   }
}

iamtheream: it prints, "Starting to send tweet." then stops.

You're still missing the point. [u]Where[/u] does it stop? Here's a clue - there is no way for you to tell, with the code as it is currently.

Below Is the code where I put a command to print after every line in the function. I press the push button and it prints "Starting To Send Tweet" to the serial monitor then prints nothing else. Which means none of the code below it is executing right? Could that be because the twitter.post(msg) is within another if statement? Sorry if I'm not getting it guys, it just looks like it should work.

void loop()
{
   buttonState1 = digitalRead(button1);
  
   if(buttonState1 == HIGH) {
     Serial.println("Starting To Send Tweet");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
      Serial.println("Sending Tweet");
      int status = twitter.wait(&Serial);
      Serial.println("It's About To Send");
    if (status == 200) {
      Serial.println("Sent");
  } else {
     Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("Twitter post failed");
  }
   }
}

That's more like it.

If twitter.post() returns a true value it should print "Sending Tweet"; if it returns a false value it should print "Twitter post failed".

If it doesn't print anything at all then either the Arduino has crashed or the call to twitter.post() has not returned.

I suggest the next step should be to look at the code that executes inside twitter.post() and see what circumstances would prevent it from returning. For example there may be some sort of retry logic in there which could get stuck in a loop in some situations.

ETA another thought: You say you have code which successfully sent a tweet within setup. You could start with that code and make incremental changes to see at what point it fails. For example, if you move the tweeting code in setup() into a function and call that at the same point in setup(), does it still work? It would be wierd if it didn't. If you make that function call the last thing you do in setup, does it still work? If you remove it from the end of setup() to the start of loop and put some code at the end of loop that just spins forever, does it still work? You'd expect it to. So, at what point does your initial working code stop working? It would be ironic if the problem was not related to your code and was just your twitter account being throttled, or something.