Pages: [1]   Go Down
Author Topic: Ethernet shield/twitter question.  (Read 911 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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);
}
}


« Last Edit: March 21, 2010, 07:49:43 pm by BrockLee » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: March 23, 2010, 06:29:51 pm by BrockLee » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check out the TextString library  - 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.

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I'll check that out.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works! and you're awesome Digitalman!

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

Code:
#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);
}
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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...

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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. smiley

Thanks for the suggestions!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Pages: [1]   Go Up
Jump to: