Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. #include <linux/printk.h>
  4. #include <linux/time64.h>
  5.  
  6. #define BTPWR 5
  7. #define BTRX 6
  8. #define BTTX 7
  9. #define RELAY 10
  10.  
  11. String command;
  12. SoftwareSerial bt(BTRX, BTTX);
  13.  
  14. int relay_state = LOW;
  15.  
  16. unsigned long last_start[HIGH + 1];
  17.  
  18. static inline unsigned long minutes(int m)
  19. {
  20.     return m * MSEC_PER_MIN;
  21. }
  22.  
  23. unsigned long duration[HIGH + 1] = {
  24.     [LOW]  = minutes(55),
  25.     [HIGH] = minutes(5),
  26. };
  27.  
  28. void setup(void)
  29. {
  30.     Serial.begin(9600);
  31.     pr_debug("tacqua:\r\n"
  32.          "duration[off]: %ld\r\n"
  33.          "duration[on] : %ld\r\n",
  34.          duration[LOW] / MSEC_PER_MIN,
  35.          duration[HIGH] / MSEC_PER_MIN);
  36.  
  37.     pinMode(RELAY, OUTPUT);
  38.     /*
  39.      * Use BTPWR as a 5v, 40mA power source for the Bluetooth module.
  40.      */
  41.     pinMode(BTPWR, OUTPUT);
  42.     digitalWrite(BTPWR, OUTPUT);
  43.  
  44.     bt.begin(9600);
  45.     /* bt.write("AT+NAMETACQUA"); */
  46. }
  47.  
  48. static int bt_print_tstamp(void)
  49. {
  50.     return spr_info(bt, "%ld: ", millis() / MSEC_PER_MIN);
  51. }
  52.  
  53. static const char *str_state(int s)
  54. {
  55.     return s ? "On" : "Off";
  56. }
  57.  
  58. static void switch_relay(int new_state)
  59. {
  60.     if (new_state != relay_state) {
  61.         pr_debug("Switching to: %s\r\n", str_state(new_state));
  62.         bt_print_tstamp();
  63.         bt.println(str_state(new_state));
  64.         relay_state = new_state;
  65.         digitalWrite(RELAY, relay_state);
  66.         last_start[relay_state] = millis();
  67.     }
  68. }
  69.  
  70. static void show_settings(void)
  71. {
  72.     bt_print_tstamp();
  73.  
  74.     if (duration[HIGH] == 0) {
  75.         spr_info(bt, "timer Off, relay %s\r\n", str_state(relay_state));
  76.         return;
  77.     }
  78.  
  79.     spr_info(bt, "last[%s]: %ld\r\n", "On", last_start[HIGH] / MSEC_PER_MIN);
  80.     bt_print_tstamp();
  81.     spr_info(bt, "duration[%s]: %ld\r\n", "On ", duration[HIGH] / MSEC_PER_MIN);
  82.     bt_print_tstamp();
  83.     spr_info(bt, "last[%s]: %ld\r\n", "Off", last_start[LOW] / MSEC_PER_MIN);
  84.     bt_print_tstamp();
  85.     spr_info(bt, "duration[%s]: %ld\r\n", "Off", duration[LOW] / MSEC_PER_MIN);
  86. }
  87.  
  88. static void do_command(void)
  89. {
  90.     if (command == "0") {
  91.         switch_relay(LOW);
  92.         duration[HIGH] = 0;
  93.     } else if (command == "1") {
  94.         switch_relay(HIGH);
  95.         duration[HIGH] = 0;
  96.     } else {
  97.         char *endptr;
  98.         unsigned long d = strtol(command.c_str(), &endptr, 10);
  99.  
  100.         if (d != 0) {
  101.             duration[HIGH] = minutes(d);
  102.             pr_debug("duration[%s]: %ld\r\n", "HIGH", duration[HIGH]);
  103.             if (*endptr == ',') {
  104.                 d = strtol(endptr + 1, NULL, 10);
  105.  
  106.                 if (d != 0) {
  107.                     duration[LOW] = minutes(d);
  108.                     pr_debug("duration[%s]: %ld\r\n", "LOW", duration[LOW]);
  109.                 }
  110.             }
  111.  
  112.             switch_relay(HIGH);
  113.         } else {
  114.             show_settings();
  115.         }
  116.     }
  117. }
  118.  
  119. void loop(void)
  120. {
  121.     int c;
  122.     unsigned long now, delta;
  123.  
  124.     while (bt.available()) {
  125.         c = bt.read();
  126.         if (c == '\r')
  127.             break;
  128.         command += char(c);
  129.     }
  130.  
  131.     if (c == '\r' && command != "") {
  132.         pr_debug("command: %s\r\n", command.c_str());
  133.         do_command();
  134.         command = "";
  135.     }
  136.  
  137.     delay(1 * 1000);
  138.  
  139.     /*
  140.      * Check if timer is on
  141.      */
  142.     if (duration[HIGH] == 0)
  143.         return;
  144.  
  145.     now = millis();
  146.     delta = now - last_start[relay_state];
  147.     if (delta >= duration[relay_state]) {
  148.         pr_debug("%ld: \r\n", now);
  149.         switch_relay(!relay_state);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement