Часы из tm1637, ds3231 и Ethernet Shield W51 с синхронизацие через NTP, показывающие попеременно время и температуру.

Очень долго бился с этим модулем tm1637, в начале вообще не хотел работать, перепробовал несколько разных библиотек, но ничего хорошего из этого не получилось. Оказалось, что самая простая и понятная библиотека Digitaltube работает корректно только если пины DIO и CLK подключены именно к 11 и 10 портам на ардуино. Если кто-то найдёт и другие рабочие пины, то я вас поздравляю! Однако при подключении Ethernet Shield W5100 часы опять не хотели работать, ардуино просто завиасло и всё тут. И тут помогла лишь смена пинов на 10 и 9. Теперь всё корректно работает. К сожалению пингер и синхронизация одновременно работать отказывается. Удачи вам!
http://s3.uploads.ru/t/PWROq.jpg

На фотке пока что показан собранный прототип на ds1307, но позже будет фотка с ds3231. Возможно так же появятся схемы подключения.
Использованное оборудование:
- Arduino Uno R3
- TM1637
- DS3231
- Ethernet Shield W5100
Библиотеки:
- Скачать

Скетч:

Код:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EEPROM.h>
#include <Wire.h>
#include <DS3231.h>
#include <TM1637.h>
#define ON 1
#define OFF 0


#define CLK 10// Не рекомендую менять номера портов, так как на других модуль TM1673 может не завестись
#define DIO 9
TM1637 tm1637(CLK, DIO);
int8_t TimeDisp[] = {0xFF, 0xFF, 0xFF, 0xFF};
unsigned char ClockPoint = 1;

DS3231 clock;
RTCDateTime dt;
char compileTime[] = __TIME__;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
}; 

byte ip[] = {
  192, 168, 2, 177
}; // ip address for ethernet shield

IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );


int p = 0;
unsigned int localPort = 123;       // local port to listen for UDP packets
char timeServer[] = "172.22.0.1"; // 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
EthernetUDP Udp;


void setup()
{
  tm1637.init();
  tm1637.set();
  clock.begin();


  // 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);


  Serial.begin(9600);

  // Initialize DS3231
  Serial.println("Initialize DS3231");

  byte hour = getInt(compileTime, 0);
  byte minute = getInt(compileTime, 3);
  byte second = getInt(compileTime, 6);

  Serial.print(hour);
  Serial.println(":");
 Serial.println(minute);

  //Импровизированный хэш времени
  //Содержит в себе количество секунд с начала дня
  unsigned int hash =  hour * 60 * 60 + minute  * 60 + second;

  //Проверяем несовпадение нового хэша с хэшем в EEPROM
  if (EEPROMReadInt(0) != hash) {

    //Сохраняем новый хэш
    EEPROMWriteInt(0, hash);

    //Записываем эти данные во внутреннюю память часов.
    //С этого момента они начинают считать нужное для нас время
    clock.setDateTime(__DATE__, __TIME__);
  }

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  //Ручная установка времени и даты (YYYY, MM, DD, HH, II, SS)
  // clock.setDateTime(2014, 4, 13, 19, 21, 00);
  //pinger();
}

void loop()
{
  int tempa;
  dt = clock.getDateTime();
  if (dt.second >= 0 && dt.second <= 5 ||
      dt.second >= 20 && dt.second <= 25 ||
      dt.second >= 40 && dt.second <= 45)
    //dt.second >= 30 && dt.second <= 37) //
  {
    // clock.forceConversion();
    tm1637.point(POINT_OFF); // выключаем точки
    tempa = clock.readTemperature();
    //  tempa = tempa - 1; //Раскомментируйте и поставле своё значение для коррекции выводимой температуры
    TimeDisp[0] = tempa  / 10; // заполняем массив

    TimeDisp[1] = tempa  % 10;
    TimeDisp[2] = 0xF1;
    TimeDisp[3] = 12; // C
    Serial.println(tempa);
    delay(1000);
  }

  // часы
  else
  { ClockPoint = (~ClockPoint) & 0x01;


    if (ClockPoint) tm1637.point(POINT_ON);
    else tm1637.point(POINT_OFF);

    TimeDisp[0] = (dt.hour) / 10;
    TimeDisp[1] = (dt.hour) % 10;
    TimeDisp[2] = (dt.minute) / 10;
    TimeDisp[3] = (dt.minute) % 10;
    delay(500);

    Serial.print("Raw data: ");
    Serial.print(__DATE__); Serial.print("--");
    Serial.print(__TIME__); Serial.print("--");
    Serial.print(dt.year);   Serial.print("-");
    Serial.print(dt.month);  Serial.print("-");
    Serial.print(dt.day);    Serial.print(" ");
    Serial.print(dt.hour);   Serial.print(":");
    Serial.print(dt.minute); Serial.print(":");
    Serial.print(dt.second); Serial.println("");
    // Serial.println(clock.readTemperature());
    //Serial.println(tempa);

  }
  tm1637.display(TimeDisp);
  if (dt.hour == 9 && dt.minute == 0 && dt.second <= 2 )
  {
    Serial.println("Syncin from NTP");
    updateT();
    Serial.println("Sucsessful");
  }
  
}

void updateT()
{
  
  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 + 10800;
    // print Unix time:
    Serial.println(epoch);
    clock.setDateTime(epoch - 3600);


    // 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 ten seconds before asking for the time again
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(char* 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();

}
//Запись двухбайтового числа в память
void EEPROMWriteInt(int address, int value)
{
  EEPROM.write(address, lowByte(value));
  EEPROM.write(address + 1, highByte(value));
}

//Чтение числа из памяти
unsigned int EEPROMReadInt(int address)
{
  byte lowByte = EEPROM.read(address);
  byte highByte = EEPROM.read(address + 1);

  return (highByte << 8) | lowByte;
}

char getInt(const char* string, int startIndex) {
  return int(string[startIndex] - '0') * 10 + int(string[startIndex+1]) - '0';
}