# Ethernet shield/twitter question.

**URL:** <https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876>\
**Category:** Interfacing\
**Created:** [March 22, 2010, 12:33am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876 "2010-03-22T00:33:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 22, 2010, 12:33am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/1 "2010-03-22T00:33:18Z")

</div>

Hello,

I'll start by explaining the project I'm working on:  
I am building a system to make my mailbox (physical snailmail box) twitter. I can't see my mailbox from my house so I want to receive a tweet when my mail arrives.

I built a radio transmitter for the mailbox that, when triggered, sends a pin high on an Arduino. The Arduino then posts a message to Twitter via an ethernet shield.

I have all of this working fine. The problem is that Twitter dis-allows duplicate tweets within 24 hours so I somehow need to generate a random char msg every time the pin goes high.

Any ideas?

Here is my current code

```cpp
#include <Ethernet.h>
#include <Twitter.h>

const int buttonPin = 2;
int buttonState = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // arbitrary mac address for ethernet shield
byte ip[] = { 192, 168, 2, 9 };  // IP for ethernet shield. DHCP won't automatically assign.
byte gateway[] = { 192, 168, 2, 1 };  // LAN IP for network gateway (router)
byte subnet[] = { 255, 255, 255, 0 };  // subnet mask from network gateway. arbitrary doesn't seem to work

Twitter twitter("myusername:mypassword");
char msg[] = ("Mail");

void setup()
{
  pinMode(buttonPin, INPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
  twitter.post(msg);
  delay(200);
}
}

```

---

<div class="post-metadata">

**Author:** ![digitalman2112](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/digitalman2112/32/1178925_2.png) [@digitalman2112](https://forum.arduino.cc/u/digitalman2112)\
**Post date:** [March 22, 2010, 12:48am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/2 "2010-03-22T00:48:19Z")

</div>

Simple version - use the millis() command, append that to the end of the message. According to the documentation, it repeats about every 50 days.

More cool - get the current time from somewhere on the web and append that to the end of your message. Check out the "arduino time library" in the playground for sample code.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 22, 2010, 12:53am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/3 "2010-03-22T00:53:15Z")

</div>

Thanks for the reply,

I'm not exactly sure what that means but you sound pretty confident about it so I'll do some research and try it out.

Thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 23, 2010, 11:22pm UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/4 "2010-03-23T23:22:19Z")

</div>

OK, I read everything I could find on millis() and I tried in several ways to add or "append" this command to my code but to no avail.

I'm very new to programming and I'm probably trying to work well beyond my current level of understanding but I tend to learn in a very aggregate manner so please excuse my ignorance.

That said; I know what "append" means in plain English but not in code.  
Can I bother you for a further explanation?

---

<div class="post-metadata">

**Author:** ![digitalman2112](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/digitalman2112/32/1178925_2.png) [@digitalman2112](https://forum.arduino.cc/u/digitalman2112)\
**Post date:** [March 24, 2010, 10:34am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/5 "2010-03-24T10:34:09Z")

</div>

Check out the TextString library - [String() - Arduino Reference](http://www.arduino.cc/en/Tutorial/TextString)

There is an example with that library called "StringAppendNumber" that creates a string object, sets it to an initial value, appends a read from a pin (you would omit this part), appends the current millis and some ending text.

Suggest running that example by itself to see how it works, then modify it to create the message in the form that you want, then merge the working code with your existing code.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 24, 2010, 11:19am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/6 "2010-03-24T11:19:31Z")

</div>

Thanks, I'll check that out.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 25, 2010, 1:45pm UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/7 "2010-03-25T13:45:22Z")

</div>

It works! and you're awesome Digitalman!

In case anyone can use this for reference, here is my finished code.

```cpp
#include <Ethernet.h>
#include <Twitter.h>
#include <WString.h>

String dataString;
const int buttonPin = 2;
int buttonState = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // arbitrary mac address for ethernet shield
byte ip[] = { 192, 168, 2, 9 };  // IP for ethernet shield. DHCP won't automatically assign.
byte gateway[] = { 192, 168, 2, 1 };  // LAN IP for network gateway (router)
byte subnet[] = { 255, 255, 255, 0 };  // subnet mask from network gateway. arbitrary doesn't seem to work

Twitter twitter("myusername:mypassword");
char msg[] = ("Mail");

void setup()
{
  pinMode(buttonPin, INPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop(){
  dataString = "Mail";
  long timeStamp = millis();
  dataString.append(timeStamp);
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
  twitter.post(dataString);
  int status = twitter.wait();
  delay(10000);
}
}

```

---

<div class="post-metadata">

**Author:** ![digitalman2112](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/digitalman2112/32/1178925_2.png) [@digitalman2112](https://forum.arduino.cc/u/digitalman2112)\
**Post date:** [March 25, 2010, 2:23pm UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/8 "2010-03-25T14:23:53Z")

</div>

Glad it worked for you - you figured it out yourself, you just needed to know what was possible 🙂

Another quick thought - You have a long delay there - I'd be concerned that I wouldn't catch the mailbox trigger signal. If you are worried about sending duplicate messages from the same signal, you might consider decreasing the delay (to increase polling frequency) and then use the millis() value to know how long it has been since your last tweet and don't send again if less than X millis since the last message was sent...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 26, 2010, 12:43am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/9 "2010-03-26T00:43:20Z")

</div>

@digitalman2112

Yeah, I did assign such a long delay because I was worried about multiple messages being generated and I found out today that I have a pretty speedy postal carrier as I didn't receive a tweet when my mail arrived.

I'll figure out how to use the millis() command to amend my code and let you know how it goes.

@Richard Crowley

Inserting a date/time stamp onto each message is my ultimate goal. I'm still learning so it may take me awhile but I'll get there. 🙂

Thanks for the suggestions!

---

<div class="post-metadata">

**Author:** ![digitalman2112](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/digitalman2112/32/1178925_2.png) [@digitalman2112](https://forum.arduino.cc/u/digitalman2112)\
**Post date:** [March 26, 2010, 2:28am UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/10 "2010-03-26T02:28:55Z")

</div>

For date / time, check out the time library we discussed earlier - now you know how to build a custom message with the time once you get it off an internet server...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 7:08pm UTC](https://forum.arduino.cc/t/ethernet-shield-twitter-question/16876/11 "2021-05-06T19:08:15Z")

</div>

Closed on May 6, 2021, 7:08pm UTC


