This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sdk:sleep-mode [2020/09/25 20:21] juraschek created |
sdk:sleep-mode [2021/07/20 10:28] (current) fachet |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | This page shows details about the Sleep mode of the NB1601 and NB800 | + | ====== Using the Sleep Mode of the NB1601 and NB800 ====== |
+ | |||
+ | For optimizing the power consumption you can put the NB1601 / NB800 into a deep sleep state over our SDK API. | ||
+ | |||
+ | In the Deep Sleep State the CPU is basicly not running. The only component still running is our Power Managment Chip and will consum a very low current (<1mA). Please keep in mind you still need to have power supply attached to the router in the deep sleep State. | ||
+ | |||
+ | ===== Requierements ===== | ||
+ | |||
+ | The Feature was introduce with NRSW Version 4.4.0.104 and newer. Please check our [[documentation:releases|NRSW Releases]] Page for the most current download link. | ||
+ | |||
+ | You need an NB1601 Router or a NB800 HW Rev. B Router to use this Feature. | ||
+ | |||
+ | |||
+ | ===== Limitations ===== | ||
+ | |||
+ | The minimum Sleep time is defined as 60sec. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Basic Usage ===== | ||
+ | |||
+ | The basic Usage of the API is defined as follows: | ||
+ | <code c basic-sleep.are> | ||
+ | |||
+ | nb_syslog("Sleep for 1 hour"); | ||
+ | // we will set the wakeup time in 9- | ||
+ | nb_wakeup(3600); | ||
+ | nb_syslog("Triggering deep sleep mode by SDK SKript NOW"); | ||
+ | |||
+ | res = nb_poweroff(); | ||
+ | if (res != 0) { | ||
+ | nb_syslog("failed unexpectedly"); | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | You can use this example code and a period timer every X hours. | ||
+ | |||
+ | ===== SMS Usage ===== | ||
+ | |||
+ | For easy triggering of the Sleep mode you can used this extended sms-control skript. Please combine it with the sms-received trigger. | ||
+ | The Command you would need to send is: | ||
+ | "sleep 10" | ||
+ | |||
+ | Where 10 is the Time in Minute after it will wake up again. | ||
+ | |||
+ | |||
+ | |||
+ | <code c sms-control-sleep.are> | ||
+ | /* DESC: This script will execute commands received by SMS. | ||
+ | * Copyright (C) 2012 NetModule AG, Switzerland | ||
+ | */ | ||
+ | |||
+ | INTERVAL = 10; /* only run every 10 seconds */ | ||
+ | MAXMSG = 5; /* process max. 5 msgs */ | ||
+ | MAXAGE = 300; /* message mustn't be older than 5 mins */ | ||
+ | MAXLINES = 32; /* max. number of lines in msg (incl. header) */ | ||
+ | AUTH = 1; /* perform authentication */ | ||
+ | ADMPWD = ""; /* password used for authentication */ | ||
+ | IGNORECASE = 1; /* ignore upper-/lower-case */ | ||
+ | SENDERS = mkarray(/* "+123456789" */); /* allowed senders */ | ||
+ | |||
+ | /* check if we should perform authentication */ | ||
+ | if (argc == 2 && argv[1] == "noauth") { | ||
+ | AUTH = 0; | ||
+ | } | ||
+ | |||
+ | /* retrieve password */ | ||
+ | if (AUTH) { | ||
+ | if (strlen(ADMPWD) == 0) { | ||
+ | /* use configured admin password */ | ||
+ | ADMPWD = nb_config_get("admin.password"); | ||
+ | if (strlen(ADMPWD) > 0) { | ||
+ | nb_syslog("using admin password for authentication"); | ||
+ | } else { | ||
+ | /* not there -> use a default password instead */ | ||
+ | nb_syslog("using default password for authentication"); | ||
+ | ADMPWD = "admin01"; | ||
+ | } | ||
+ | } | ||
+ | if (IGNORECASE) ADMPWD = tolower(ADMPWD); | ||
+ | } | ||
+ | |||
+ | /* parse message */ | ||
+ | string parse (string msg) | ||
+ | { | ||
+ | /* read by line */ | ||
+ | lnr = 0; | ||
+ | ishdr = 1; | ||
+ | tlnr = 0; | ||
+ | allowed = (length(SENDERS) == 0) ? 1 : 0; | ||
+ | |||
+ | lp = msg; | ||
+ | for (lnr = 0; lnr < MAXLINES && strlen(lp) > 0; lnr++) { | ||
+ | pos = strchr(lp, "\n"); | ||
+ | if (is_void(pos)) pos = strlen(lp); | ||
+ | |||
+ | line = left(lp, pos); | ||
+ | lp = substr(lp, pos + 1); | ||
+ | |||
+ | if (strlen(line) == 0) { | ||
+ | /* saw header separator */ | ||
+ | ishdr = 0; | ||
+ | continue; | ||
+ | } | ||
+ | if (ishdr) { | ||
+ | /* saw header line */ | ||
+ | if (left(line, 5) == "Sent:") { | ||
+ | /* check age of message */ | ||
+ | sentdate = trim(substr(line, 5)); | ||
+ | sent = strptime(sentdate, "%Y-%m-%d %H:%M:%S"); | ||
+ | |||
+ | if (!is_void(sent)) { | ||
+ | /* got a valid sent date */ | ||
+ | now = localtime(time()); | ||
+ | age = mktime(now) - mktime(sent); | ||
+ | nb_syslog("message has been sent %ds ago", age); | ||
+ | if (age > MAXAGE) { | ||
+ | nb_syslog("rejecting too old message"); | ||
+ | return ""; | ||
+ | } | ||
+ | } else { | ||
+ | nb_syslog("time check has been omitted"); | ||
+ | } | ||
+ | } else if (left(line, 5) == "From:") { | ||
+ | from = substr(line, 6); | ||
+ | if (length(SENDERS) > 0) { | ||
+ | for (s = 0; s < length(SENDERS); s++) { | ||
+ | sender = SENDERS[s]; | ||
+ | if (left(from, strlen(sender)) == sender) { | ||
+ | allowed = 1; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if (allowed == 0) { | ||
+ | nb_syslog("rejecting message from unknown sender %s", from); | ||
+ | return ""; | ||
+ | } else { | ||
+ | nb_syslog("sender %s can pass", from); | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | /* saw text line */ | ||
+ | if (IGNORECASE) line = tolower(line); | ||
+ | |||
+ | if (AUTH && tlnr == 0) { | ||
+ | /* first line of message must contain the password */ | ||
+ | if (left(line, strlen(ADMPWD)) != ADMPWD) { | ||
+ | nb_syslog("authentication failed"); | ||
+ | return ""; | ||
+ | } else { | ||
+ | nb_syslog("authentication succeeded"); | ||
+ | } | ||
+ | } else if ((AUTH && tlnr == 1) || (!AUTH && tlnr == 0)) { | ||
+ | /* this line must contain the command */ | ||
+ | if (left(line, 6) == "reboot") { | ||
+ | return "reboot"; | ||
+ | } else if (left(line, 7) == "connect") { | ||
+ | return "connect"; | ||
+ | } else if (left(line, 10) == "disconnect") { | ||
+ | return "disconnect"; | ||
+ | } else if (left(line, 6) == "status") { | ||
+ | return "status"; | ||
+ | } else if (left(line, 6) == "output") { | ||
+ | return left(line, 13); | ||
+ | } else if (left(line, 5) == "sleep") { | ||
+ | return left(line, 13); | ||
+ | } | ||
+ | } else { | ||
+ | break; | ||
+ | } | ||
+ | tlnr++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | nb_syslog("no command detected"); | ||
+ | |||
+ | return ""; | ||
+ | } | ||
+ | |||
+ | int setdio (string cmd) | ||
+ | { | ||
+ | newstate = substr(cmd, 9, 3); | ||
+ | port = substr(cmd, 7, 1); | ||
+ | |||
+ | if (port != "1" && port != "2") { | ||
+ | nb_syslog("invalid DIO port %s\n", port); | ||
+ | return -1; | ||
+ | } | ||
+ | if (newstate == "on") { | ||
+ | st = 1; | ||
+ | } else if (newstate == "off") { | ||
+ | st = 0; | ||
+ | } else { | ||
+ | nb_syslog("invalid new DIO state %s\n", newstate); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | rc = nb_dio_set(sprintf("out%s", port), st); | ||
+ | if (rc) { | ||
+ | nb_syslog("Unable to set state %s for DIO output port %s\n", newstate, port); | ||
+ | return -1; | ||
+ | } else { | ||
+ | nb_syslog("Setting state %s for DIO output port %s\n", newstate, port); | ||
+ | return 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | int setsleep (int minutes) | ||
+ | { | ||
+ | nb_syslog("Sleep for %i minutes",minutes); | ||
+ | // we will set the wakeup time in 9- | ||
+ | nb_wakeup(minutes*60); | ||
+ | nb_syslog("Triggering deep sleep mode by sms"); | ||
+ | |||
+ | res = nb_poweroff(); | ||
+ | if (res != 0) { | ||
+ | nb_syslog("failed unexpectedly"); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | /* check if we got stressed */ | ||
+ | LASTFILE = "/tmp/sms-control.last"; | ||
+ | last = 0; | ||
+ | |||
+ | fp = fopen(LASTFILE, "r"); | ||
+ | if (fp) { | ||
+ | /* we have been run at least one time */ | ||
+ | str = fread(fp, 32); | ||
+ | if (str) last = (int) str; | ||
+ | fclose(fp); | ||
+ | } | ||
+ | |||
+ | now = mktime(localtime(time())); | ||
+ | elapsed = now - last; | ||
+ | if (elapsed > 0 && elapsed < INTERVAL) { | ||
+ | nb_syslog("we have ben run %d seconds ago. skipping.", elapsed); | ||
+ | exit(0); | ||
+ | } | ||
+ | |||
+ | /* record timestamp */ | ||
+ | fp = fopen(LASTFILE, "w+"); | ||
+ | if (fp) { | ||
+ | str = sprintf("%d", now); | ||
+ | fwrite(fp, str); | ||
+ | fclose(fp); | ||
+ | } | ||
+ | |||
+ | /* read inbox */ | ||
+ | msgs = nb_sms_list(); | ||
+ | nr_msgs = length(msgs); | ||
+ | |||
+ | if (nr_msgs == 0) { | ||
+ | nb_syslog("there are no messages in your inbox"); | ||
+ | exit(0); | ||
+ | } | ||
+ | |||
+ | nb_syslog("you have %d message(s) in your inbox", nr_msgs); | ||
+ | |||
+ | /* track states */ | ||
+ | reboot = 0; | ||
+ | connecting = 0; | ||
+ | disconnecting = 0; | ||
+ | |||
+ | |||
+ | /* only process latest messages */ | ||
+ | start = nr_msgs - MAXMSG; | ||
+ | if (start < 0) start = 0; | ||
+ | |||
+ | for (i=start; i<nr_msgs; i++) { | ||
+ | msg = nb_sms_retrieve(msgs[i]); | ||
+ | if (!msg) continue; | ||
+ | |||
+ | nb_syslog("processing message %d of %d (ID %s)", | ||
+ | i, nr_msgs, msgs[i]); | ||
+ | |||
+ | cmd = parse(msg); | ||
+ | if (cmd == "reboot") { | ||
+ | nb_syslog("reboot command received"); | ||
+ | reboot = 1; | ||
+ | } else if (cmd == "connect") { | ||
+ | nb_syslog("connect command received"); | ||
+ | if (connecting) { | ||
+ | nb_syslog("already connecting"); | ||
+ | } else { | ||
+ | /* enable first wanlink */ | ||
+ | nb_config_set("wanlink.0.mode=1"); | ||
+ | connecting = 1; | ||
+ | } | ||
+ | } else if (cmd == "disconnect") { | ||
+ | nb_syslog("disconnect command received"); | ||
+ | if (disconnecting) { | ||
+ | nb_syslog("already disconnecting"); | ||
+ | } else { | ||
+ | /* disable first wanlink */ | ||
+ | nb_config_set("wanlink.0.mode=0"); | ||
+ | disconnecting = 1; | ||
+ | } | ||
+ | } else if (cmd == "status") { | ||
+ | nb_syslog("status command received"); | ||
+ | rcpt = nb_sms_header(msgs[i], "From"); | ||
+ | if (rcpt) { | ||
+ | id = nb_sms_send(rcpt, nb_status_summary()); | ||
+ | if (!id) { | ||
+ | nb_syslog("unable to send status message to %s", rcpt); | ||
+ | } else { | ||
+ | nb_syslog("successfully queued status message to %s (ID %s)", rcpt, id); | ||
+ | } | ||
+ | } | ||
+ | } else if (left(cmd,6) == "output") { | ||
+ | nb_syslog("dio out command received"); | ||
+ | setdio(cmd); | ||
+ | } else if (left(cmd,5) == "sleep") { | ||
+ | nb_syslog("dio out command received"); | ||
+ | setsleep(right(cmd,strlen(cmd)-5)); | ||
+ | } else { | ||
+ | nb_syslog("ignoring invalid message"); | ||
+ | } | ||
+ | |||
+ | /* delete message */ | ||
+ | ret = nb_sms_delete(msgs[i]); | ||
+ | if (ret == 0) nb_syslog("deleted message %s", msgs[i]); | ||
+ | } | ||
+ | |||
+ | if (reboot == 1) { | ||
+ | /* trigger reboot */ | ||
+ | nb_syslog("rebooting system"); | ||
+ | nb_reboot(); | ||
+ | } | ||
+ | |||
+ | exit(0); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ |