Can't post twice to twitter, but the posts are not duplicate

Hello everyone!

I'm having some problems with my Arduino and Twitter. I'm using an Arduino Mega 2560 with the official Arduino WiFi Shield and a 4x4 Keypad.

I want my Arduino to tweet a message every time the right password is entered on the keypad. I've managed to get the code working fine, I can tweet once, with a message that changes everytime using millis().

However, I cannot send two or more tweets unless I reset my Arduino. The problem shouldn't be related to Twitters duplicate tweet prevention system, and it shouldn't be a network or hardware related problem either.

I'm posting my code below, if you have any questions, please ask

#include <SPI.h> // needed in Arduino 0019 or later
#include <WiFi.h> // for the WiFi-shield
#include <Twitter.h> //for Twitter
#include <Keypad.h> // for the keypad
#include <Password.h> // for the password functions


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {
  {
    '1','2','3','A'  }
  ,
  {
    '4','5','6','B'  }
  ,
  {
    '7','8','9','C'  }
  ,
  {
    '*','0','#','D'  }
};

byte rowPins[ROWS] = {
  30, 32, 34, 36}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  22, 24, 26, 28}; //connect to the column pinouts of the keypad


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


char ssid[] = "Qwerty";  //  your network SSID (name)
char pass[] = "Qwerty";  // your network password

Password password = Password( "1234" );

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


// Message to post
char msg[140];



void setup()
{
  
  delay(1000); //Ihan varmuuden vuoksi
  WiFi.begin(ssid, pass);
  sprintf(msg,"Tweet, %ld",millis());
  keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

void loop()
{
  keypad.getKey(); //Get key presses
}

void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    Serial.print("Pressed: ");
    Serial.println(eKey);
    switch (eKey){
    case '*':
      checkPassword();
      break; //Check password
    case '#':
      password.reset();
      break; // reset pass
    default:
      password.append(eKey);
    }
  }
}


void checkPassword(){

  if (password.evaluate()){

    (twitter.post(msg));
  }

  else{

    //do nothing
  }
}

}

You're building your message in setup which only runs once, so it does produce duplicates. Move the sprintf to loop or checkPassword.

I assume that is not your real Twitter token in the code. The code as posted will not Auto Format because of too many right curly braces.

I can tweet once, with a message that changes everytime using millis().

You only call millis() in setup so the message will be the same each time until you reset the Arduino and even then millis() could return the same value as it the number of milliseconds since the last reset which will be consistent.

wildbill:
You're building your message in setup which only runs once, so it does produce duplicates. Move the sprintf to loop or checkPassword.

UKHeliBob:
I assume that is not your real Twitter token in the code. The code as posted will not Auto Format because of too many right curly braces.

Thanks for the quick reply! For some reason I was thinking that the value for millis() was checked when the tweet would actually be sent.

I moved the sprintf line to loop, before the keypad.getKey line.
For some reason I still can't send multiple tweets without a reset inbetween. I'm not sure if twitter thinks they are duplicate even with the millis() in there.

Just before you tweet the message, serial.print it so you can see what's being sent.

So, after two weeks of going through my code and working on other projects to get a new view on the matter, I realized how simple and stupid the problem was. The code is now working perfectly, and I can tweet succesfully. It wasn't twitters spam detection or anythin like that.

The problem was in the keypad part of the code. After the first succesful post, it wouldn't post again. I realized that the password "buffer" didn't after pressing '*', the enter key for my keypad. After I pressed '#', the password was cleared, and the "buffer" was empty again, so I could input my password and press enter to send a tweet.

I fixed the code by adding a password.Reset(); to after the twitter.post(msg). I feel really stupid, but I'm happy that I managed to work it out.

A huge thanks to you guys who helped me with this code, and sorry for my bad english and ugly code.