Back to Smartcitizen.me

Setting time manually

hi,

i plan to use sck_beta_v0_8_6_SDCARD. now i am wondering what ways there are to set the RTC time.
i would like to have some command like “#data” or “set time update XXX\r” which as far as i understands sets the interval between updates.

a short search throw the source gave me the idea that sck_beta_v0_8_6 is doing a request to a “timeserver” (see WEBTIME and sckWIFItime() in ServerUpdate.ino)

is there any command to set the RTC time?
if not i would like to implement such a command. probably someone can give me some hints on how to do this.

looking forward to any hints :wink:

tom

The time of the RTC is set during the programming of the Arduino. This time is set by your computer.

why do i see 2000-01-01 then?
does there something go wrong?
i uploaded sck_beta_v0_8_6_SDCARD.ino and sck_beta_v0_8_6.ino several times. the smartcitizen seems always to have 2000-01-01.

proably at “programming” the arduine connects to a timeserver via its wlan connection: its configuration seems to be in Constants.h ( sck_beta_v0_8_6 )
https://github.com/fablabbcn/Smart-Citizen-Kit/blob/master/sck_beta_v0_8_6/Constants.h#L194 :
... // Time server request - EndPoint: http://data.smartcitizen.me/datetime char* WEBTIME[3]={ /*Servidor de tiempo*/ "GET /datetime HTTP/1.1 \ ", "Host: data.smartcitizen.me \ ", "User-Agent: SmartCitizen \ \ " }; ...

the code to get the time from the timeserver ‘data.smartcitizen.me’ seems to be here:
https://github.com/fablabbcn/Smart-Citizen-Kit/blob/master/sck_beta_v0_8_6/ServerUpdate.ino#L24 :
... for(byte i = 0; i<3; i++) Serial1.print(WEBTIME[i]); //Requests to the server time ...

i would like to set time via some other interface if possible without having to connect to the internet. i think of some command over the usb interface like they are described here:

With this expression takes time, in the code sck_beta_v0_8_6_SDCARD.ino

sckRTCadjust(sckDate(DATE,TIME));

The command list that you mention, is to change the time on the update of the sensors.

i just managed to change SDCARD version so far that it takes a time string from command line:

#!/bin/bash

this script sends output of smartcitizens time server or system date to ttyACM:

servertime=$(wget -q -O- http://data.smartcitizen.me/datetime)

this is the format smartcitizen likes the date:

systemtime=$(date +%Y,%-m,%-d,%-H,%-M,%-S#)
echo " servertime: $servertime"
echo "setting systemtime: $systemtime"
echo $systemtime | sed -e ‘s/UTC://g’ > /dev/ttyACM1
`

to do so i manipulated the loop() to listen on serial port:
`
if ( terminal_mode )
{
// read a line
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readLine += c; //makes the string readString
}
}
// this is what we get from: http://data.smartcitizen.me/datetime
// UTC:2014,7,3,20,41,27#

  // if line is not empty:
  if ( readLine != "" )
  {
   // simulate the part of sckWIFItime without requesting the server:
  Serial.println(sckTOMtime(readLine));
  ok = sckRTCadjust(sckTOMtime(readLine));
  if ( ok )
    {
      Serial.println("set time ok");
    }

  }
  readLine = "";
}

`

and added my sckTOMtime() that does the same as sckWIFItime() from ServerUpdate but without querying the timeserver:

`
char* sckTOMtime(String serverTime) {
boolean ok=false;
uint8_t count = 0;
//if (sckFindInResponse(“UTC:”, 2000))

char newChar;
byte offset = 0;
byte tomCounter = 0;
while (offset < TIME_BUFFER_SIZE) {
// read next char
newChar = serverTime.charAt(tomCounter);
//Serial.print(“nC:”);
//Serial.println(newChar, DEC);
tomCounter++;

  if (newChar == '#') {
    ok = true;
    buffer[offset] = '\\x00';
    break;
  } 
  else if (newChar != -1) {
    if (newChar==',') 
    {
      if (count<2) buffer[offset]='-';
      else if (count>2) buffer[offset]=':';
      else buffer[offset]=' ';
      count++;
    }
    else buffer[offset] = newChar;
    offset++;
  }

}

if (!ok)
{
buffer[0] = ‘#’;
buffer[1] = 0x00;
}

return buffer;
}
`

if i call my setTime.sh script i can set the time on arduino without being connected to the internet. the output looks like this:
`


Temperature: 2620.80 C RAW
Humidity: 2908.00 % RAW
Light: 14.20 lx
Battery: 100.00 %
Solar Panel: 0.00 V
Carbon Monxide: 0.00 kOhm
Nitrogen Dioxide: 0.00 kOhm
Noise: 0.00 mV
UTC: 2014-07-05 01:01:00


2014-7-5 1:1:18
set time ok
Temperature: 2621.60 C RAW
Humidity: 2901.60 % RAW
Light: 14.20 lx
Battery: 100.00 %
Solar Panel: 0.00 V
Carbon Monxide: 4.49 kOhm
Nitrogen Dioxide: 0.00 kOhm
Noise: 2.35 mV
UTC: 2014-07-05 01:02:17



`