TerAnYu

WatchDog

Oct 14th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <DigiCDC.h>
  2.  
  3. //USB hardware watchdog project
  4. //Jason Greathouse 07/11/2011
  5.  
  6. #define level_1 60 // 1 min
  7. #define level_2 300 // 5 min
  8. #define level_3 480 // 8 min
  9. #define level_4 540 // 9 min
  10. #define level_end 600 // 10 min
  11.  
  12. int incomingByte = 0;  // for incoming serial data
  13. int notAlive = 0;   // counter for seconds between alive
  14. int handshake = 0;  // have we connected to watchdog daemon
  15.  
  16. int relayPin = 2; //pin for relay
  17. int intled = 1; //pin for led
  18. int beeper =  0; //beep
  19.  
  20. long tick = 1000; //miliseconds in a second
  21. long previousMillis = 0;
  22. int printedl1 = 0; //Just "flags" to keep it from repeating messages
  23. int printedl2 = 0;
  24. int printedl3 = 0;
  25. int printedl4 = 0;
  26. int printedend = 0;
  27.  
  28. void setup() {
  29.   SerialUSB.begin(); // opens serial port, sets data rate to 9600 bps
  30.   pinMode(relayPin, OUTPUT); //Set relay pin
  31.   pinMode(intled, OUTPUT);
  32.   pinMode(beeper, OUTPUT);
  33.   digitalWrite(relayPin, LOW);
  34.   establishContact(); //Wait for inital Keep Alive before starting timers
  35. }
  36.  
  37.  
  38.  
  39. void loop() {
  40.   unsigned long currentMillis = millis(); //Currnet number of miliseconds since boot.
  41.  
  42.   if (SerialUSB.available() > 0) {
  43.     // read the incoming byte:
  44.     incomingByte = SerialUSB.read();
  45.   }
  46.    
  47.   if (incomingByte == 'A') {
  48.     //we got an alive message
  49.     SerialUSB.println("Received Alive");
  50.     notAlive = 0;
  51.     printedl1 = 0; //Just "flags" to keep it from repeating messages
  52.     printedl2 = 0;
  53.     printedl3 = 0;
  54.     printedl4 = 0;
  55.     printedend = 0;
  56.    
  57.     digitalWrite(intled, HIGH);   // set the LED on
  58.     delay(2000);                  // wait for a second
  59.     digitalWrite(intled, LOW);    // set the LED off
  60.   }
  61.   if (incomingByte == 'R') {
  62.     //we got a going down for reboot message, going back to establish contact.
  63.     SerialUSB.println("Received Reboot Message");
  64.     establishContact();
  65.     notAlive = 0;
  66.     printedl1 = 0; //Just "flags" to keep it from repeating messages
  67.     printedl2 = 0;
  68.     printedl3 = 0;
  69.     printedl4 = 0;
  70.     printedend = 0;
  71.    
  72.   }
  73.  
  74.  
  75.   // count off seconds
  76.   if (currentMillis - previousMillis > tick) {
  77.     previousMillis = currentMillis;
  78.     notAlive = notAlive + 1;
  79.     //Serial.print("No Alive for ");
  80.     //Serial.print(notAlive);
  81.     //Serial.println(" seconds");
  82.   }
  83.  
  84.   //Not alive warnings for 60/300/600 seconds
  85.   if (notAlive == level_1) {
  86.     if (printedl1 == 0) {
  87.       SerialUSB.println("No Alive for level_1");
  88.       printedl1 = 1;
  89.      
  90.           digitalWrite(intled, HIGH);   // set the LED on
  91.           delay(60);                  // wait for a second
  92.           digitalWrite(intled, LOW);    // set the LED off
  93.           delay(1);                  // wait for a second
  94.           analogWrite(beeper, 10);
  95.           delay(80);
  96.           analogWrite(beeper, 0);
  97.     }
  98.   }
  99.   if (notAlive == level_2) {
  100.     if (printedl2 == 0) {
  101.       SerialUSB.println("No Alive for level_2");
  102.       printedl2 = 1;
  103.      
  104.           digitalWrite(intled, HIGH);   // set the LED on
  105.           delay(120);                  // wait for a second
  106.           digitalWrite(intled, LOW);    // set the LED off
  107.           delay(1);                  // wait for a second
  108.           analogWrite(beeper, 10);
  109.           delay(80);
  110.           analogWrite(beeper, 0);
  111.           delay(80);
  112.           analogWrite(beeper, 10);
  113.           delay(100);
  114.           analogWrite(beeper, 0);
  115.        }
  116.   }
  117.   if (notAlive == level_3) {
  118.     if (printedl3 == 0) {
  119.       SerialUSB.println("No Alive for level_3");
  120.       printedl3 = 1;
  121.      
  122.           digitalWrite(intled, HIGH);   // set the LED on
  123.           delay(120);                  // wait for a second
  124.           digitalWrite(intled, LOW);    // set the LED off
  125.           delay(1);                  // wait for a second
  126.           analogWrite(beeper, 10);
  127.           delay(80);
  128.           analogWrite(beeper, 0);
  129.           delay(80);
  130.           analogWrite(beeper, 10);
  131.           delay(100);
  132.           analogWrite(beeper, 0);
  133.        }
  134.   }
  135.   if (notAlive == level_4) {
  136.     if (printedl4 == 0) {
  137.       SerialUSB.println("No Alive for level_4");
  138.       printedl4 = 1;
  139.      
  140.           digitalWrite(intled, HIGH);   // set the LED on
  141.           delay(120);                  // wait for a second
  142.           digitalWrite(intled, LOW);    // set the LED off
  143.           delay(1);                  // wait for a second
  144.           analogWrite(beeper, 10);
  145.           delay(80);
  146.           analogWrite(beeper, 0);
  147.           delay(80);
  148.           analogWrite(beeper, 10);
  149.           delay(100);
  150.           analogWrite(beeper, 0);
  151.        }
  152.   }  
  153.   if (notAlive >= level_end) {
  154.     //we have not seen an "A" in 600 seconds
  155.     SerialUSB.println("No Alive for level_end - Rebooting Now!");
  156.    
  157.           digitalWrite(intled, HIGH);   // set the LED on
  158.           delay(500);                  // wait for a second
  159.           digitalWrite(intled, LOW);    // set the LED off
  160.           delay(80);                  // wait for a second
  161.           analogWrite(beeper, 50);
  162.           delay(80);
  163.           analogWrite(beeper, 0);
  164.           delay(80);
  165.           analogWrite(beeper, 50);
  166.           delay(80);
  167.           analogWrite(beeper, 0);
  168.           delay(80);
  169.           analogWrite(beeper, 50);
  170.           delay(200);
  171.           analogWrite(beeper, 0);
  172.    
  173.     digitalWrite(relayPin, HIGH); //Hit reset button
  174.     delay(1000);
  175.     digitalWrite(relayPin, LOW);
  176.        
  177.     //delay(600000); //wait 10 min for the reboot
  178.     notAlive = 0;
  179.     printedl1 = 0; //Just "flags" to keep it from repeating messages
  180.     printedl2 = 0;
  181.     printedl3 = 0;
  182.     printedl4 = 0;
  183.     printedend = 0;
  184.     establishContact();
  185.   }
  186.  
  187.   incomingByte = 0;          
  188. }
  189.  
  190. //wait for an inital keepalive before starting timer
  191. void establishContact() {
  192.   while (incomingByte != 'A') {
  193.     if (SerialUSB.available() > 0) {
  194.       // read the incoming byte:
  195.       incomingByte = SerialUSB.read();
  196.     }
  197.     SerialUSB.println("Waiting for Initial KeepAlive...");
  198.     delay(1000);
  199.    
  200.           digitalWrite(intled, HIGH);   // set the LED on
  201.           delay(50);                  // wait for a second
  202.           digitalWrite(intled, LOW);    // set the LED off
  203.   }
  204.   SerialUSB.println("Initialized, Timer Started.");
  205.   incomingByte = 0;
  206.           digitalWrite(intled, HIGH);   // set the LED on
  207.           delay(1000);                  // wait for a second
  208.           digitalWrite(intled, LOW);    // set the LED off
  209.           //delay(1);                  // wait for a second
  210.           analogWrite(beeper, 10);
  211.           delay(50);
  212.           analogWrite(beeper, 0);
  213. }
Add Comment
Please, Sign In to add comment