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
#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);
}
}
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.
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?
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.
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...
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.
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...