Show Posts
Pages: [1]
1  International / Nederlands / Re: Nieuweling zoekt hulp, arduino + android + hardwarebuttons on: September 22, 2012, 11:34:58 am
Mischien is dit iets voor je, http://samratamin.com/Ardumote.html?
Het is niet precies wat je wilt maar de basis principes zijn hetzelfde.

Groet, Michel

2  International / Nederlands / Re: SPI RTC met Arduino Ethernet on: September 22, 2012, 11:13:59 am
Ik weet niet welke RTC je hebt maar het lijkt erop dat je een DS32xx (DS3234) of iets dergelijks hebt.

Als dat zo is dan kan deze code je misschien verder helpen;
(Deze code synchroniseerd een DS3234 met een NTP server, je hebt dus naast een Arduino een EthernetShield en een DS3234 nodig. De DS is als volgt aangesloten; SS – pin 8, MOSI – pin 11, MISO – pin12, and SCK – Pin13).
Code:
/*
 
 Udp NTP Client
  
 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket
 For more on NTP time servers and the messages needed to communicate with them,
 see http://en.wikipedia.org/wiki/Network_Time_Protocol
  
 This code is in the public domain.
 
 */
 
#include <SPI.h>        
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
const int  RTCchipSelect = 8; //chip select
RTC_DS3234 RTC(RTCchipSelect);

// Enter a MAC address for your controller below.
 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x24 }; // Change this to your Ethernet Shield's mac!
 
unsigned int localPort = 8888;      // local port to listen for UDP packets
 
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
 
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
 
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
 
void setup()
{
  // Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }
 
  
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
   }
   Udp.begin(localPort);
 }

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  
  // wait to see if a reply is available
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);              
    
    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;  
    // print Unix time:
    Serial.println(epoch);
        
    // Syncing the UTC to the RTC
    SPI.begin();
    RTC.begin();
    SetTimeDate(DateTime(epoch).day(), DateTime(epoch).month(), DateTime(epoch).year() - 2000, DateTime(epoch).hour(), DateTime(epoch).minute(), DateTime(epoch).second());
    Serial.println(ReadTimeDate());
    SPI.setDataMode(SPI_MODE0);
    
    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');  
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch %60); // print the second
  }
  // wait a second before asking for the time again
  delay(1000);
}
 
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:        
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);        
digitalWrite(RTCchipSelect, HIGH);
int a=n & B00001111;    
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;        
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
        //Year
temp.concat("20") ;
        if (TimeDate[6] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[6]);
temp.concat("-") ;
        //Month
        if (TimeDate[5] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[5]);
temp.concat("-") ;
        //Day
        if (TimeDate[4] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[4]);
temp.concat("\t") ;
        //Hours
        if (TimeDate[2] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[2]);
temp.concat(":") ;
        //Minutes
        if (TimeDate[1] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[1]);
temp.concat(":") ;
        //Seconds
        if (TimeDate[0] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[0]);
  return(temp);
}

//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6; i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
 
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);        
digitalWrite(RTCchipSelect, HIGH);
  }
}
3  Using Arduino / Networking, Protocols, and Devices / Re: DS3234 Deadon RTC with NTP question on: September 21, 2012, 03:39:16 pm
Maybe I can help here.....
I have a sketch wich uses NTP to sync my DS3234. The sketch uses my Arduino Duemilanove, an Ardino Ethernet R3 Shield and a DS3234.
The shield is stacked on the Duemilanove and the DS is connected like this; SS – pin 8, MOSI – pin 11, MISO – pin12, and SCK – Pin13.
I connected the shield to my local network and uploaded the sketch....

Please note that the sketch uses the mac-address of my ethernet shield, change it to yours. Also I use the RTC_DS3234.h library.

Code:
/*
 
 Udp NTP Client
  
 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket
 For more on NTP time servers and the messages needed to communicate with them,
 see http://en.wikipedia.org/wiki/Network_Time_Protocol
  
 This code is in the public domain.
 
 */
 
#include <SPI.h>        
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
const int  RTCchipSelect = 8; //chip select
RTC_DS3234 RTC(RTCchipSelect);

// Enter a MAC address for your controller below.
 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x24 }; // Change this to your Ethernet Shield's mac!
 
unsigned int localPort = 8888;      // local port to listen for UDP packets
 
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
 
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
 
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
 
void setup()
{
  // Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }
 
  
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
   }
   Udp.begin(localPort);
 }

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  
  // wait to see if a reply is available
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);              
    
    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;  
    // print Unix time:
    Serial.println(epoch);
        
    // Syncing the UTC to the RTC
    SPI.begin();
    RTC.begin();
    SetTimeDate(DateTime(epoch).day(), DateTime(epoch).month(), DateTime(epoch).year() - 2000, DateTime(epoch).hour(), DateTime(epoch).minute(), DateTime(epoch).second());
    Serial.println(ReadTimeDate());
    SPI.setDataMode(SPI_MODE0);
    
    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');  
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch %60); // print the second
  }
  // wait a second before asking for the time again
  delay(1000);
}
 
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:        
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);        
digitalWrite(RTCchipSelect, HIGH);
int a=n & B00001111;    
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;        
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
        //Year
temp.concat("20") ;
        if (TimeDate[6] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[6]);
temp.concat("-") ;
        //Month
        if (TimeDate[5] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[5]);
temp.concat("-") ;
        //Day
        if (TimeDate[4] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[4]);
temp.concat("\t") ;
        //Hours
        if (TimeDate[2] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[2]);
temp.concat(":") ;
        //Minutes
        if (TimeDate[1] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[1]);
temp.concat(":") ;
        //Seconds
        if (TimeDate[0] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[0]);
  return(temp);
}

//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6; i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
 
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);        
digitalWrite(RTCchipSelect, HIGH);
  }
}

Here's a screen shot from the Serial console in the attachment.
4  Using Arduino / Networking, Protocols, and Devices / Re: Arduino WiFi Shield UDP Support on: September 09, 2012, 11:47:51 am
It says in the documentation that the WiFi library is much like the Ethernet library but UDP doesn't seem te be part of it...  smiley-confuse
I've search for this alot but haven't found a solution yet...
Anyone who found a solution?
5  Using Arduino / Networking, Protocols, and Devices / Re: WiFi Shield - Buffering outsending data on: August 30, 2012, 02:33:38 pm
Since the WiFiShield has a low power consuming chip, I suspect it needs to 'power up' after each period of inactivity. When powered up it will send the data without delay...

- Michel
Pages: [1]