SHARE
TWEET

Sync'd Blinker for Spark Core

sweenig Feb 17th, 2014 (edited) 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. UDP UDP;
  2. //////////////////////////////////////////////////////
  3. // INITIAL PARAMETERS
  4. int houron = 7;int minuteon = 30; //what time to turn on the led
  5. int houroff = 10;int minuteoff = 0; //what time to turn off the led
  6. unsigned int localPort = 123;
  7. const char timeServer[] = "pool.ntp.org"; //which ntp server to hit
  8. unsigned int timeZone = -5; //tz offset
  9. int lowlight = 0; //easy way to globally set the intensity of "off"
  10. int midlight = 128; //global middle intensity setting
  11. int highlight = 255; //easy way to globally set the intensity of "ON!"
  12. int napduration = 1.5 * 60 * 60; //minimum number of seconds for a nap
  13. bool flasher = true; //a variable to use when ack'ing a button press
  14. //////////////////////////////////////////////////////
  15. int timeon = houron * 60 + minuteon; //convert "ON time" to minutes from midnight
  16. int timeoff = houroff * 60 + minuteoff; //convert "OFF time" to minutes from midnight
  17. char string[ 17 ] = { "" };
  18. int hour, minute, second;
  19. const int NTP_PACKET_SIZE= 48;byte packetBuffer[NTP_PACKET_SIZE]; //setup some ntp stuff
  20. int ntpsent = 0; //number of ntp requests sent
  21. int ntplistened = 0; //number of times we have listened for an ntp response
  22. int synced = 0; //whether or not the clock is synced
  23. unsigned int offset = 0;
  24. unsigned long secsSince1900 = 0;
  25. void setup() {
  26.         UDP.begin(localPort);
  27.         RGB.control(true); //take control of the onboard RGB LED
  28.         pinMode(D2, INPUT_PULLUP); //start nap trigger
  29.         attachInterrupt(D2, startnap, FALLING); //listen for a button press on D2, connecting it to gnd
  30. }
  31. void loop() {
  32.         if (!synced) {
  33.                 RGB.control(true); //take control of the onboard RGB LED
  34.                 RGB.color(synced, 0, 0); //red portion of RGB LED indicates synced status
  35.                 if (!ntpsent || ntplistened >= 3000) {sendNTPpacket(timeServer);} //send request if not yet requested or if no response in 3000 loops
  36.                 receiveTimeTick(); //try to read a response
  37.         }
  38.         if (synced) {
  39.                 RGB.control(true); //take control of the onboard RGB LED
  40.                 //calculate the time
  41.                 const unsigned long seventyYears = 2208988800UL;
  42.                 unsigned long epoch = secsSince1900 - seventyYears;
  43.                 epoch += ((millis() - offset) / 1000.0); //this is the epoch, use as needed
  44.                 hour = (epoch % 86400L) / 3600;minute = (epoch % 3600) / 60;second = (epoch % 60); //calculate h, m, & s
  45.                 int currtime = hour * 60 + minute; //calculate the current time in minutes since midnight
  46.                 //done calculating the time
  47.                 if ( currtime < timeon || currtime > timeoff ) { //if it's time to be aleep, keep the light low
  48.                     RGB.color(synced,0,lowlight);
  49.             } else { //if it's time to be awake, turn the light up high
  50.                 RGB.color(synced,0,highlight);
  51.         }
  52.                 delay(1000); //in no hurry here since we are waiting for time to pass, but do not stop too long
  53.         }
  54. }
  55. unsigned long sendNTPpacket(const char *address) {
  56.         memset(packetBuffer, 0, NTP_PACKET_SIZE);
  57.         //setup the ntp packet (boiler plate NTP stuff)
  58.         packetBuffer[0] = 0b11100011;
  59.         packetBuffer[1] = 0;
  60.         packetBuffer[2] = 6;
  61.         packetBuffer[3] = 0xEC;
  62.         packetBuffer[12] = 49;
  63.         packetBuffer[13] = 0x4E;
  64.         packetBuffer[14] = 49;
  65.         packetBuffer[15] = 52;
  66.         UDP.beginPacket(address, 123); //start sending a packet to the ntp server on port 123
  67.         UDP.write(packetBuffer, NTP_PACKET_SIZE); //send the request payload
  68.         UDP.endPacket(); //finish sending the packet
  69.         ntpsent += 1; //count this as a send
  70.         ntplistened = 0; //start listening over again
  71. }
  72. void receiveTimeTick() {
  73.         ntplistened += 1; //we are listening, start counting again
  74.         if ( UDP.parsePacket() ) { //if a udp packet is here
  75.                 UDP.read(packetBuffer, NTP_PACKET_SIZE); //read the packet
  76.                 unsigned long highWord = (packetBuffer[40] << 8) + packetBuffer[41]; //get the high word
  77.                 unsigned long lowWord = (packetBuffer[42] << 8) + packetBuffer[43]; //get the low word
  78.                 secsSince1900 = highWord << 16 | lowWord; //calculate the epoch
  79.                 secsSince1900 += timeZone*60*60; //apply the tz_offset
  80.                 offset = millis(); //make the actual calculation
  81.                 synced = 1; //mark the clock as synced
  82.         }
  83.         while ( UDP.parsePacket() ) {UDP.read(packetBuffer, NTP_PACKET_SIZE);} //go through the rest of the payload/packets
  84. }
  85. void startnap() {
  86.     EXTI_ClearITPendingBit(EXTI_Line5); // D2 "startnap"
  87.     NVIC_DisableIRQ(EXTI9_5_IRQn); // stop listening for an interrupt on D2
  88.     RGB.color(synced,midlight,0); //indicate that we've received the request to start a nap
  89.     for ( int j=0; j<20; j++ ) { //flash between green and blue (50% intensity) 20 times to give a good indication
  90.         if ( flasher ) {RGB.color(synced,0,midlight);}
  91.         if ( !flasher ) {RGB.color(synced,midlight,0);}
  92.         flasher = !flasher;
  93.         delay(100);
  94.     }
  95.     RGB.color(synced,0,lowlight); //when done ack'ing the button push, go to low light mode
  96.     napduration = 10; //just for testing, pretend naps last only a few seconds
  97.     for (int i=0; i<napduration; i++) {delay(1000);} //actively cycle through delays without pausing too long
  98.     RGB.color(synced,0,highlight); //the nap is done! turn on the blue light!
  99.     for (int k=0; k<napduration; k++) {delay(1000);} //leave the blue light on for a while (same time as the nap)
  100.     RGB.color(synced,0,lowlight); //the blue light has been on long enough, turn it off now.
  101. }
RAW Paste Data
Top