Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <linux/printk.h>
- #include <linux/time64.h>
- #define BTPWR 5
- #define BTRX 6
- #define BTTX 7
- #define RELAY 10
- String command;
- SoftwareSerial bt(BTRX, BTTX);
- int relay_state = LOW;
- unsigned long last_start[HIGH + 1];
- static inline unsigned long minutes(int m)
- {
- return m * MSEC_PER_MIN;
- }
- unsigned long duration[HIGH + 1] = {
- [LOW] = minutes(55),
- [HIGH] = minutes(5),
- };
- void setup(void)
- {
- Serial.begin(9600);
- pr_debug("tacqua:\r\n"
- "duration[off]: %ld\r\n"
- "duration[on] : %ld\r\n",
- duration[LOW] / MSEC_PER_MIN,
- duration[HIGH] / MSEC_PER_MIN);
- pinMode(RELAY, OUTPUT);
- /*
- * Use BTPWR as a 5v, 40mA power source for the Bluetooth module.
- */
- pinMode(BTPWR, OUTPUT);
- digitalWrite(BTPWR, OUTPUT);
- bt.begin(9600);
- /* bt.write("AT+NAMETACQUA"); */
- }
- static int bt_print_tstamp(void)
- {
- return spr_info(bt, "%ld: ", millis() / MSEC_PER_MIN);
- }
- static const char *str_state(int s)
- {
- return s ? "On" : "Off";
- }
- static void switch_relay(int new_state)
- {
- if (new_state != relay_state) {
- pr_debug("Switching to: %s\r\n", str_state(new_state));
- bt_print_tstamp();
- bt.println(str_state(new_state));
- relay_state = new_state;
- digitalWrite(RELAY, relay_state);
- last_start[relay_state] = millis();
- }
- }
- static void show_settings(void)
- {
- bt_print_tstamp();
- if (duration[HIGH] == 0) {
- spr_info(bt, "timer Off, relay %s\r\n", str_state(relay_state));
- return;
- }
- spr_info(bt, "last[%s]: %ld\r\n", "On", last_start[HIGH] / MSEC_PER_MIN);
- bt_print_tstamp();
- spr_info(bt, "duration[%s]: %ld\r\n", "On ", duration[HIGH] / MSEC_PER_MIN);
- bt_print_tstamp();
- spr_info(bt, "last[%s]: %ld\r\n", "Off", last_start[LOW] / MSEC_PER_MIN);
- bt_print_tstamp();
- spr_info(bt, "duration[%s]: %ld\r\n", "Off", duration[LOW] / MSEC_PER_MIN);
- }
- static void do_command(void)
- {
- if (command == "0") {
- switch_relay(LOW);
- duration[HIGH] = 0;
- } else if (command == "1") {
- switch_relay(HIGH);
- duration[HIGH] = 0;
- } else {
- char *endptr;
- unsigned long d = strtol(command.c_str(), &endptr, 10);
- if (d != 0) {
- duration[HIGH] = minutes(d);
- pr_debug("duration[%s]: %ld\r\n", "HIGH", duration[HIGH]);
- if (*endptr == ',') {
- d = strtol(endptr + 1, NULL, 10);
- if (d != 0) {
- duration[LOW] = minutes(d);
- pr_debug("duration[%s]: %ld\r\n", "LOW", duration[LOW]);
- }
- }
- switch_relay(HIGH);
- } else {
- show_settings();
- }
- }
- }
- void loop(void)
- {
- int c;
- unsigned long now, delta;
- while (bt.available()) {
- c = bt.read();
- if (c == '\r')
- break;
- command += char(c);
- }
- if (c == '\r' && command != "") {
- pr_debug("command: %s\r\n", command.c_str());
- do_command();
- command = "";
- }
- delay(1 * 1000);
- /*
- * Check if timer is on
- */
- if (duration[HIGH] == 0)
- return;
- now = millis();
- delta = now - last_start[relay_state];
- if (delta >= duration[relay_state]) {
- pr_debug("%ld: \r\n", now);
- switch_relay(!relay_state);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement