Arduino Mega Server и часы реального времени

image

Arduino Mega Server , , , «» RTC- . « ».



- . , ( ), , , . . , , .

Arduino Mega Server , ( ) , , . RTC .


, , AMS «» RTC, « » , , , , . .

, RTC, . Time Library . , , Arduino, , :

\rduino\libraries\

, .


. «» «» , . , . Arduino Mega Server , . , . , AMS MajorDoMo, , « ».

, , , .

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h> 

Time.h , NTP ( Ethernet ).

, IP- ,

IPAddress timeServer(192, 168, 2, 8); //   MajorDoMo  



unsigned int localPort = 8888;

: 8888 , , , , 123:

unsigned int localPort = 123;



const int timeZone = 4;

EthernetUDP

EthernetUDP Udp;

. :

void rtcInit() {
  Udp.begin(localPort);
  Serial.println("Waiting for NTP sync...");
  setSyncProvider(getNtpTime);
}



setSyncProvider(getNtpTime);

( NTP ). , , RTC. ( ) , , . «» .

,

setSyncInterval(interval);

( , , - ).

image

, , Serial , . timeStamp():

void timeStamp() {
  serialRTC();
  Serial.print(" ");
}

serialRTC():

void serialRTC() {
  Serial.print(year()); 
  Serial.print("-");
  printDigits(month());
  Serial.print("-");
  printDigits(day());
  Serial.print(" ");
  printDigits(hour());
  Serial.print(":");
  printDigits(minute());
  Serial.print(":");
  printDigits(second());
}

- AMS , , , «» - Arduino Mega Server.

image

, . AMS 0.12 , . , , .

RTC Arduino Mega Server 0.12
/*
Modul Virtual RTC
part of Arduino Mega Server project
*/

// Virtual RTC

IPAddress timeServer(192, 168, 2, 8);
unsigned int localPort = 8888; // local port to listen for UDP packets
EthernetUDP Udp;

const int timeZone = 4;
time_t prevDisplay = 0; // when the digital clock was displayed

void rtcInit() {
Udp.begin(localPort);
Serialprint(«Waiting for NTP sync… \n»);
setSyncProvider(getNtpTime);
modulRtc = 1;
}

void rtcWorks() {
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { // update the display only if time has changed
setLifer();
prevDisplay = now();
//digitalClockDisplay();
}
}
}

void printDigits(int digits) {
if(digits < 10) {
Serial.print('0');
}
Serial.print(digits);
}

void serialRTC() {
Serial.print(year());
Serial.print("-");
printDigits(month());
Serial.print("-");
printDigits(day());
Serial.print(" ");
printDigits(hour());
Serial.print(":");
printDigits(minute());
Serial.print(":");
printDigits(second());
}

void timeStamp() {
serialRTC();
Serial.print(" ");
}

void printRTC(){
serialRTC();
Serial.println();
}

// NTP code

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

#ifdef RTC_FEATURE

time_t getNtpTime() {
while (Udp.parsePacket() > 0); // discard any previously received packets
Serialprint(«Transmit NTP request\n»);
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() — beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serialprint(«Receive NTP response\n»);
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 — 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serialprint(«No NTP response\n»);
return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void 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();
}

#endif

// Duration

void showDuration(time_t duration) {
// prints the duration in days, hours, minutes and seconds
Serialprint(" (duration ");
if(duration >= SECS_PER_DAY){
Serial.print(duration / SECS_PER_DAY);
Serialprint(" day ");
duration = duration % SECS_PER_DAY;
}
if(duration >= SECS_PER_HOUR){
Serial.print(duration / SECS_PER_HOUR);
Serialprint(" hour ");
duration = duration % SECS_PER_HOUR;
}
if(duration >= SECS_PER_MIN){
Serial.print(duration / SECS_PER_MIN);
Serialprint(" min ");
duration = duration % SECS_PER_MIN;
}
Serial.print(duration);
Serialprint(" sec) \n");
}

void checkEvent(time_t* prevEvent) {
time_t duration = 0;
time_t timeNow = now();

if (*prevEvent > 0) {
duration = timeNow — *prevEvent;
}
if (duration > 0) {
showDuration(duration);
}
*prevEvent = timeNow;
}




RTC ( ), , , CHIPSTER AMS , Ethernet W5500DS3231, RTC .

, CHIPSTER , Arduino Geegrow , , Arduino Mega 2560 «» Arduino Mega Server. , , . .


RTC , . Time Library. .

, , — «» ( , DS1307) «» ( DS3231, ). , «» , , . .

. .

, , .


, RTC Arduino Mega Server . , .

: — GND («»), VCC ( ), SCL (), SDA (). .

image

, GND «», VCC — . .

. RTC I2C, : Arduino . Arduino Uno A4 (SDA) A5 (SCL), arduino Mega D20 (SDA) D21 (SCL).

, SCL SDA «» 4,7 . , 2 — 10 .

image


AMS . , , Time Library. , DS1307RTC Library. :

\rduino\libraries\



#include <Wire.h>
#include <DS1307RTC.h>

, RTC.

void rtcInit() {
  Udp.begin(localPort);
  Serial.println("Waiting for NTP sync...");
  setSyncProvider(getNtpTime);
}



setSyncProvider(getNtpTime);



setSyncProvider(RTC.get);

Arduino Mega Server ( ) «» RTC, . , setSyncProvider(getNtpTime) setSyncProvider(RTC.get) , , .

, ,

if (timeStatus() != timeNotSet) {

}

.


: , «» RTC , . . «» , .

! RTC «», , , , .

, : , , , RTC, — .

, - , «» .

, , RTC :

void rtcSync() {
  setSyncProvider(getNtpTime);
  Serial.println("...getNtpTime...");
  if (timeStatus() != timeNotSet) {
    Serial.println("...set!...");
    time_t t = getNtpTime();
    RTC.set(t);
    setSyncProvider(RTC.get);
  }
}



setSyncProvider(getNtpTime);

, , RTC

RTC.set(t);



setSyncProvider(RTC.get);


. , RTC , . - . Arduino Mega Server : RTC ( ) Arduino Serial Commander.

RTC … . Arduino Mega Server Arduino Serial Commander. AMS, , Arduino Mega Server ( ) ( ).

image

RTC


Arduino Mega Server, 0.13, «» RTC. , .

, , CHIPSTER ( W5500 AMS ).

. Youtube Arduino Mega Server, .

Source: https://habr.com/ru/post/hi385349/


All Articles