WSPR: Arduino automatic timer - LED monitor status

From OnnoWiki
Revision as of 20:04, 30 May 2018 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search
// time ntp sync
// pin PIN_AUD - kuning - audio SD
// pin PIN_PTT  - merah  - PTT
// pin PIN_SYN  - hijau  - NTP sync
//
// file WAV WSPR diambil dari rekaman wsjt-x

#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "TMRpcm.h"
#include <SD.h>

// Ethernet library configuration
byte mac[] = { 0x24, 0x7B, 0xA2, 0x4A, 0x52, 0x10 };
IPAddress timeServer(128, 138, 140, 44 ); // time.nist.gov

unsigned int localPort = 8888;
const int NTP_PACKET_SIZE= 48;     
byte packetBuffer[NTP_PACKET_SIZE]; 
EthernetUDP Udp;                   

const int CS_PIN  = 4;
const int PIN_AUD = 13;
const int PIN_PTT = 8;
const int PIN_SYN = 7;
TMRpcm audio;

void setup() {
  pinMode(PIN_AUD, OUTPUT);  // LED kuning - blinking audio OK
  pinMode(PIN_PTT, OUTPUT);  // LED merah  - PTT ON
  pinMode(PIN_SYN, OUTPUT);  // LED hijau  - NTP SYNC 

  digitalWrite(PIN_AUD, LOW);
  digitalWrite(PIN_PTT, LOW);
  digitalWrite(PIN_SYN, LOW);
  
  audio.speakerPin = 9;
  audio.setVolume(3);
  pinMode(CS_PIN, OUTPUT);
  if (!SD.begin(CS_PIN)) digitalWrite(PIN_AUD, HIGH);
  Ethernet.begin(mac);
  
  setSyncInterval(10); // Set seconds between re-sync
  getNtpTime;
  setSyncProvider(getNtpTime);
  Udp.begin(localPort);
}

void loop() {
  setSyncProvider(getNtpTime);
   
  if(audio.isPlaying()==0) digitalWrite(PIN_PTT, LOW);
    else digitalWrite(PIN_PTT, HIGH);

  if(year()==1970) digitalWrite(PIN_SYN, LOW); // NTP unsync
    else digitalWrite(PIN_SYN, HIGH);          // NTP sync
   
  if( year()>2000 && minute()==0 )
    if(audio.isPlaying()==0) {
      if (!SD.begin(CS_PIN)) digitalWrite(PIN_AUD, HIGH);
       digitalWrite(PIN_PTT, HIGH);
      audio.play("YC0MLC.WAV");
      delay(2L*60L*1000);
      }
  if( year()>2000 && minute()==30 )
    if(audio.isPlaying()==0) {
      if (!SD.begin(CS_PIN)) digitalWrite(PIN_AUD, HIGH);
      digitalWrite(PIN_PTT, HIGH);
      audio.play("YC0MLC.WAV");
      delay(2L*60L*1000);
      }
  getNtpTime;
  delay(10*1000);
} 

/*-------- NTP code ----------*/

unsigned long getNtpTime()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
    delay(500);
  
    if ( Udp.parsePacket() ) {
     Udp.read(packetBuffer,NTP_PACKET_SIZE);
     unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
     unsigned long low = word(packetBuffer[42], packetBuffer[43]);
     unsigned long secsSince1900 = hi << 16 | low; 
     const unsigned long seventyYears = 2208988800UL;     
     unsigned long epoch = secsSince1900 - seventyYears;
     return epoch;
  }
  return 0; // return 0 if unable to get the time
}

unsigned long sendNTPpacket(IPAddress& address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0

  // Initialize values needed to form NTP request
  packetBuffer[0] = B11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum
  packetBuffer[2] = 6;     // Max Interval between messages in seconds
  packetBuffer[3] = 0xEC;  // Clock Precision
  // bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
  packetBuffer[12]  = 49;  // four-byte reference ID identifying
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // send the packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}