Date and Time in Twitter Message

Hey guys,

I am running a sketch that detects water with a sensor and it sends a Tweet via Twitter. I use a Arduino UNO and an Ethernet Shield. Now I want the date and time to be included in the Tweet but have not idea how and found nothing in the internet . Can someone help me????

thx

  • drimrim

Do you have an RTC?

drimrim:
I am running a sketch that detects water with a sensor and it sends a Tweet via Twitter. I use a Arduino UNO and an Ethernet Shield. Now I want the date and time to be included in the Tweet but have not idea how and found nothing in the internet . Can someone help me????

If your device has Internet access by using the Ethernet Shield, you could retrieve the current UTC time in "Unix time seconds since 01.01.1970) from a NTP time server on the Internet. NTP = Network Time Protocol

There is also a programming example in the Arduino-IDE. Open from the menu:
File - Examples - Ethernet - UdpNtpClient

If you want to convert UTC time into local time, you will have to add your timezone hours.

jurs:
If your device has Internet access by using the Ethernet Shield, you could retrieve the current UTC time in "Unix time seconds since 01.01.1970) from a NTP time server on the Internet. NTP = Network Time Protocol

There is also a programming example in the Arduino-IDE. Open from the menu:
File - Examples - Ethernet - UdpNtpClient

If you want to convert UTC time into local time, you will have to add your timezone hours.

@jurs:
How to convert from Unix time to human-readable date and time? I know how I would do it, but what do you recommend?

An issue is that Twitter may block your account if they detect robot activity.

PaulMurrayCbr:
An issue is that Twitter may block your account if they detect robot activity.

Karma++

Better to use an IFTTT channel with Twitter, it's sort of vetted for this purpose with the Maker channel.

odometer:
@jurs:
How to convert from Unix time to human-readable date and time? I know how I would do it, but what do you recommend?

I'm not him, but ... the Time library.

jurs:
If your device has Internet access by using the Ethernet Shield, you could retrieve the current UTC time in "Unix time seconds since 01.01.1970) from a NTP time server on the Internet. NTP = Network Time Protocol

There is also a programming example in the Arduino-IDE. Open from the menu:
File - Examples - Ethernet - UdpNtpClient

If you want to convert UTC time into local time, you will have to add your timezone hours.

Yes I have internet access and I already ran this example sketch but I don't know which parts of the programm contains the sent date and time from the ndp Server and how to convert it into char.

PaulMurrayCbr:
An issue is that Twitter may block your account if they detect robot activity.

I did already 20+ Tweets, works fine for me. The message changes itself everytime because I already use the intern time of the Arduino with the time library ( which starts at 0 when the program starts).

aarg:
Do you have an RTC?

nope :-/ , but internet access

Here’s my sketch

int ledPin= 7;                  

#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <SPI.h>
#include <Time.h>
#include <Twitter.h>






//Ethernet Shield settings
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x00 };


byte ip[] = { 000, 000, 0, 000 };

// Token for tweet
Twitter twitter("xxxxxx");

// message
char msg[128] = "Wassereinbruch,";      // Wassereinbruch = flooding
boolean msgSent = false;



void setup() 
{
 Serial.begin(9600);
 pinMode(ledPin,INPUT);
 digitalWrite(ledPin, HIGH);

}

void loop() {
  
 
  delay(2000);

if (digitalRead(ledPin) == LOW) // if sensor detects water

{
  if (msgSent == false)
  {
    Ethernet.begin(mac, ip); // start ethernet connection
    
     char buffer[128]; // Could be smaller
     sprintf(buffer, "%s  %d:%d:%d", msg, hour(), minute(), second());
     
    Serial.println("connecting ...");
    if (twitter.post(buffer)) {
      // 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);
      msgSent = true;
      if (status == 200) {
        Serial.println("OK.");
        
      } else {
        Serial.print("failed : code ");
        Serial.println(status);
      }
    } else {
      Serial.println("connection failed.");
    }
  }
}
else
  {
    msgSent = false; // set back, if sensor is out of water
  }
}

odometer:
@jurs:
How to convert from Unix time to human-readable date and time? I know how I would do it, but what do you recommend?

In general I'd do:

1st step: add local time zone offset
const int8_t tzHours=1;
uint32_t localTime= unixTime+ tzHours*3600L;

2nd step: then convert from timestamp to years, months, day, hours, minutes, seconds
(most Arduino users will prefer to use a time library, I'd use some time routines I wrote myself)

3rd step: (summertime if required)
find out whether it is a Daylight Saving Time / summertime and if so, add another hour to local time

In normal case I'd stop after 2nd step and use local zone time for time keeping.
3rd step is then done immediately before a local time needs to be displayed in human readable format

If you're going to disregard Daylight Saving Time, then day-of-week, hour, minute, second is easy.

jurs:
2nd step: then convert from timestamp to years, months, day, hours, minutes, seconds
(most Arduino users will prefer to use a time library, I'd use some time routines I wrote myself)

Could you post those? Sounds handy!

3rd step: (summertime if required) find out whether it is a Daylight Saving Time / summertime and if so, add another hour to local time

You should probably do this at step 1, when changing the hour may shift you into a different day, month or year.

Cheers,
/dev

/dev:
Could you post those? Sounds handy!
You should probably do this at step 1, when changing the hour may shift you into a different day, month or year.

Cheers,
/dev

But DST transition times are local (here it is 02:00) times on specified days, not UTC. So for that I think it is more straightforward to convert to local time first. It is like "the Nth Nday of Nmonth". But the day begins and ends in this time zone.

aarg:
But DST transition times are local (here it is 02:00) times on specified days, not UTC. So for that I think it is more straightforward to convert to local time first. It is like "the Nth Nday of Nmonth". But the day begins and ends in this time zone.

But hours carry into days, and days carry into months.

odometer:
But hours carry into days, and days carry into months.

I know. That was sort of my point. Savings time changes are defined in terms of local time. In local time, there are no such carries at 02:00. I'm suddenly at a loss to explain it, I hope someone knows what I mean.

If it's 02:00 UTC on the second Thursday of March, it doesn't help me calculate DST for other zones because many of them will not be on the same day. That's the best I can manage at this late hour.

I'm suddenly at a loss to explain it

Are you referring to when the DST begins and ends? Those twice-a-year date/times are specified with respect to local date/time, and so the DST offset gets added when the local date/time is between those two special date/times. Those change-over date/times are chosen so that beginning to add or ceasing to add the DST hour to the local time does not move you into a different date.

That's separate (confusingly?) from the steps required to actually add the one-hour offset to the local time. That must be performed in seconds, so that the final conversion to the date/time elements takes into account rollovers into different hours, days, months or even the year.

Cheers,
/dev

Guys thanks for the advises but I'm rlly struggling can someone tell me how to do that?

-drimrim

have you tried to use TimeZone.h?