Advertisement
Guest User

Untitled

a guest
May 26th, 2017
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.71 KB | None | 0 0
  1. /* $Id:  UPS_ARDUINO 2009-01-01 22:08:21Z robynhub $ */
  2. /*
  3.  * Copyright (C) 2008-2009 Antonio Bartolini (robynhub@gmail.com) - Software Design
  4.  * Copyright (C) 2008-2009 Marco Antonini (marcomail.anto@gmail.com) - Hardware Design
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  */
  20.  
  21. // Software Version
  22. #define VERSION "1.12"
  23.  
  24. // Enable watchdog (adaboot needed! comment it out otherwise!)
  25. #define WATCHDOG
  26.  
  27. // Enable sounds
  28. #define SOUNDS
  29.  
  30. // Define Hardware version 1.1
  31. #define HARDWARE_1_1
  32.  
  33. #include <Ethernet.h>
  34. #include <EEPROM.h>
  35. #include <MsTimer2.h>
  36. #ifdef WATCHDOG
  37. #include <avr/io.h>
  38. #include <avr/wdt.h>
  39. #endif
  40.  
  41. ////////////////////////////////////// Arduino port listing
  42.  
  43. #define UPS_220V_INT 0 /* Interrupt on state of AC line. */
  44. #define UPS_220V 2  /* State of AC line.  LOW if on-line HIGH otherwise */
  45. #define UPS_DEFAULT_CONFIG 3  /* Read pin for default settings. Always HIGH, with pin goes to LOW */
  46. #define UPS_LED_ONLINE 4  /* Led 2 "Online" */
  47. #define UPS_LED_BATTERY 5  /* Led 1 "On Battery" PWM */
  48. #define UPS_BEEPER 6  /* PWM Beeper */
  49. #define UPS_BUTTON 7  /* Read Button, always HIGH, LOW if pushed =Used to bypass= */
  50. #define UPS_DISABLE_CHARGER 8 /* Charger always ON, if HIGH disconnect charger  */
  51. #define UPS_FORCE_TO_BATTERY 9  /* Force to inverter  */
  52.  
  53. // ANALOG
  54. #define UPS_ANALOG_VINV 5
  55. #define UPS_ANALOG_AINV 4
  56. #define UPS_ANALOG_VBATT 3
  57. #define UPS_ANALOG_ABATT 2
  58.  
  59. /* Sensors Constants: 63.69mV / Volt o 36.60mV / Amp  for ver. 1.0
  60.  (analogread()*4.9) / sensors constant(mV) */
  61. #define CONST_V 63.69
  62. #define CONST_A 36.60
  63.  
  64. /* Ticks between next battery check  360/12 = 30 days */
  65. #define TICKS_TO_NEXT_BATTERY_CHECK 360
  66.  
  67. /* Battery Dead Voltage in Volts */
  68. #define LOW_BATTERY_VOLTAGE 21
  69.  
  70. /* max value for unsigned long */
  71. #define MAX_UNSIGNED_LONG 4294967295
  72.  
  73. ////////////////////////////////////// Global Default Config
  74. byte mac[] = { 0xCA, 0xFE, 0xBA, 0xBE, 0x22, 0x22 };
  75. byte ip[] = { 192, 168, 1, 1 };
  76. byte mask[] = { 255 , 255, 255, 0 };
  77. byte gw[] = { 192 , 168, 1, 1 };
  78.  
  79. // telnet defaults to port 4949 (munin client compatible)
  80. Server server = Server(4949);
  81.  
  82. ////////////////////////////////////////////////////////////
  83.  
  84. Client client = NULL;
  85. boolean superuser = false;
  86.  
  87. volatile char buffer[64];
  88. volatile boolean on_battery_check = false;
  89. volatile unsigned long test_start_time;
  90. volatile unsigned long test_stop_time;
  91. volatile int ticks;
  92.  
  93. // CONIFIGURATION
  94. struct config_t {
  95.     // ip
  96.     byte ip[4];
  97.     // mask
  98.     byte mask[4];
  99.     // gw
  100.     byte gw[4];
  101.     // password
  102.     char password[32];
  103.     // days to next battery check (12 ticks = 1 day)
  104.     int ticks;
  105.     // running time on battery
  106.     unsigned long running_time;
  107. }
  108. configuration;
  109.  
  110.  
  111. void banner(){
  112.     Serial.println("=== System Started ===");
  113.     Serial.println("Network Config:");
  114.     Serial.print("Ip: ");
  115.     Serial.print((int)configuration.ip[0]);
  116.     Serial.print(".");
  117.     Serial.print((int)configuration.ip[1]);
  118.     Serial.print(".");
  119.     Serial.print((int)configuration.ip[2]);
  120.     Serial.print(".");
  121.     Serial.println((int)configuration.ip[3]);
  122.     Serial.print("Netmask: ");
  123.     Serial.print((int)configuration.mask[0]);
  124.     Serial.print(".");
  125.     Serial.print((int)configuration.mask[1]);
  126.     Serial.print(".");
  127.     Serial.print((int)configuration.mask[2]);
  128.     Serial.print(".");
  129.     Serial.println((int)configuration.mask[3]);
  130.     Serial.print("Gateway: ");
  131.     Serial.print((int)configuration.gw[0]);
  132.     Serial.print(".");
  133.     Serial.print((int)configuration.gw[1]);
  134.     Serial.print(".");
  135.     Serial.print((int)configuration.gw[2]);
  136.     Serial.print(".");
  137.     Serial.println((int)configuration.gw[3]);
  138.     Serial.print("Enable Pass: ");
  139.     Serial.println(configuration.password);
  140.     Serial.print("Running Time: ");
  141.     Serial.println((int)configuration.running_time / 60000 );
  142.     Serial.print("Days to check: ");
  143.     Serial.println(ticks / 12);
  144.         Serial.print("Software Version: ");
  145.         Serial.println(VERSION);
  146.     Serial.println("=== Up and Running ===");
  147.     return;  
  148. }
  149.  
  150.  
  151. // 220V Interrupt Handling
  152. void interrupt(){
  153.     // quit battery check
  154.     if (digitalRead(UPS_220V) == HIGH && on_battery_check){
  155.         digitalWrite(UPS_FORCE_TO_BATTERY,LOW);
  156.         on_battery_check = false;
  157. #ifdef HARDWARE_1_1
  158.                 pinMode(UPS_DISABLE_CHARGER,INPUT);
  159. #else
  160.                 digitalWrite(UPS_DISABLE_CHARGER, LOW);
  161. #endif
  162.     }
  163.     init_eth0();
  164. }
  165.  
  166.  
  167. // Ethernet Initialization
  168. void init_eth0(){  
  169.     Ethernet.begin(mac, configuration.ip, configuration.gw, configuration.mask);
  170.     // start listening for clients
  171.     server.begin();
  172. }
  173.  
  174.  
  175. void reset(){
  176.     // Watchdog Abuse
  177. #ifdef WATCHDOG
  178.     while(1) {
  179.     };
  180. #endif
  181.    
  182. }
  183.  
  184. // Set values to default
  185. void apply_default(){
  186.     Serial.println("=== Default Config Applyed ===");
  187.     configuration.ip[0] = ip[0];
  188.     configuration.ip[1] = ip[1];
  189.     configuration.ip[2] = ip[2];
  190.     configuration.ip[3] = ip[3];
  191.     configuration.mask[0] = mask[0];
  192.     configuration.mask[1] = mask[1];
  193.     configuration.mask[2] = mask[2];
  194.     configuration.mask[3] = mask[3];
  195.     configuration.gw[0] = gw[0];
  196.     configuration.gw[1] = gw[1];
  197.     configuration.gw[2] = gw[2];
  198.     configuration.gw[3] = gw[3];
  199.     strcpy(configuration.password,"arduino");
  200.     configuration.ticks = TICKS_TO_NEXT_BATTERY_CHECK;
  201.     configuration.running_time = 0;
  202.     save_config();
  203. #ifdef SOUNDS
  204.     // bip bip bip!
  205.     tone(UPS_BEEPER,2048,500);
  206.     delay(500);
  207.     tone(UPS_BEEPER,2048,500);
  208.     delay(500);
  209.     tone(UPS_BEEPER,2048,500);
  210.     delay(500);
  211.     noTone(UPS_BEEPER);
  212. #endif
  213.     return;
  214. }
  215.  
  216.  
  217. // Bypass button handling
  218. void bypass_button(){
  219.     // do nothing if on battery
  220.     if (on_battery())
  221.         return;
  222.     // Bypass
  223.     if (digitalRead(UPS_BUTTON) == LOW)
  224.         force_to_battery(HIGH);
  225.     else
  226.         force_to_battery(LOW);
  227. }
  228.  
  229. // Return true if the load it's on battery
  230. boolean on_battery(){
  231.     if (digitalRead(UPS_220V) == HIGH)
  232.         return true;
  233.     else if (on_battery_check)
  234.         return true;
  235.     else if (digitalRead(UPS_BUTTON) == LOW)
  236.         return true;
  237.     else
  238.         return false;
  239. }
  240.  
  241. // Force the load to go on battery
  242. void force_to_battery(int value){
  243.     // avoid problems
  244.     if (digitalRead(UPS_220V) == HIGH && value == HIGH)
  245.         return;
  246.     if (digitalRead(UPS_220V) == LOW)
  247.         digitalWrite(UPS_FORCE_TO_BATTERY,value);
  248.     else
  249.         // UPS_220 == HIGH
  250.         digitalWrite(UPS_FORCE_TO_BATTERY,LOW);
  251. }
  252.  
  253. // battery check timer
  254. void battery_check_timer(){
  255.     // reinit eth0
  256.     init_eth0();
  257.     if (!on_battery_check){
  258.        
  259.         ticks--;
  260.        
  261.         if (ticks <= 0){
  262.             ticks = TICKS_TO_NEXT_BATTERY_CHECK;
  263.                         save_config();
  264.             battery_check_start();
  265.         }
  266.        
  267.     }
  268. }
  269.  
  270. // start battery check
  271. void battery_check_start(){
  272.     on_battery_check = true;
  273.     test_start_time = millis();
  274.     force_to_battery(HIGH);
  275. #ifdef HARDWARE_1_1
  276.     pinMode(UPS_DISABLE_CHARGER,OUTPUT);
  277.     digitalWrite(UPS_DISABLE_CHARGER, LOW);
  278. #else
  279.     digitalWrite(UPS_DISABLE_CHARGER, HIGH);
  280. #endif
  281.    
  282. }
  283.  
  284. // stop battery check
  285. void battery_check_stop(){
  286. #ifdef HARDWARE_1_1
  287.     pinMode(UPS_DISABLE_CHARGER,INPUT);
  288. #else
  289.     digitalWrite(UPS_DISABLE_CHARGER, HIGH);
  290. #endif
  291.     force_to_battery(LOW);
  292.     test_stop_time = millis();
  293.     if (test_start_time > test_stop_time)
  294.         configuration.running_time = (test_stop_time + (MAX_UNSIGNED_LONG - test_start_time));
  295.     else
  296.         configuration.running_time = test_stop_time - test_start_time;
  297.     save_config();
  298.     on_battery_check = false;
  299.     init_eth0();
  300. }
  301.  
  302. // Write a struct in eeprom
  303. template <class T> int EEPROM_writeAnything(int ee, const T& value)
  304. {
  305.     const byte* p = (const byte*)(const void*)&value;
  306.     int i;
  307.     for (i = 0; i < sizeof(value); i++)
  308.         EEPROM.write(ee++, *p++);
  309.     return i;
  310. }
  311.  
  312. // Read a struct in eeprom
  313. template <class T> int EEPROM_readAnything(int ee, T& value)
  314. {
  315.     byte* p = (byte*)(void*)&value;
  316.     int i;
  317.     for (i = 0; i < sizeof(value); i++)
  318.         *p++ = EEPROM.read(ee++);
  319.     return i;
  320. }
  321.  
  322. // Routine to save config
  323. void save_config(){
  324.         configuration.ticks = ticks;
  325.     EEPROM_writeAnything(0, configuration);
  326.     return;
  327. }
  328.  
  329. // Sensors values convertion
  330. float convert(int value, float constant){
  331.     return ((value * 4.9)/constant);
  332. }
  333.  
  334. // Return the voltage of the inverter in Volts
  335. float read_inverter_voltage(){
  336. #ifdef HARDWARE_1_1
  337.         return convert(analogRead(UPS_ANALOG_VBATT), CONST_V);
  338. #else
  339.     return convert(analogRead(UPS_ANALOG_VINV), CONST_V);
  340. #endif
  341. }
  342.  
  343. // Return the current of the inverter in Ampere
  344. float read_inverter_ampere(){
  345.     return convert(analogRead(UPS_ANALOG_AINV), CONST_A);
  346. }
  347.  
  348. // Return the voltage of the batteries in Volts
  349. float read_battery_voltage(){
  350.     return convert(analogRead(UPS_ANALOG_VBATT), CONST_V);
  351. }
  352.  
  353. // Return the current of the batteries in Ampere
  354. float read_battery_ampere(){
  355.     return convert(analogRead(UPS_ANALOG_ABATT), CONST_A);
  356. }
  357.  
  358. int check_login(volatile char * buffer) {
  359.     // superuser login
  360.     if (buffer[0] == 'e' &&
  361.         buffer[1] == 'n' &&
  362.         buffer[2] == 'a' &&
  363.         buffer[3] == 'b' &&
  364.         buffer[4] == 'l' &&
  365.         buffer[5] == 'e'){
  366.         for (int i = 0 ; i < 16 ; i++ ){
  367.             if (buffer[i+7] == 13 && configuration.password[i] == 0){
  368.                 server.println("# Login OK");
  369.                 server.print("$ ");
  370.                 superuser = true;
  371.                 return 1;
  372.             }
  373.             // check password
  374.             if (buffer[i+7] != configuration.password[i])
  375.                 break;
  376.         }
  377.         server.println("# Wrong login");
  378.         superuser = false;
  379.         return 0;
  380.     }
  381.     return 2;
  382. }
  383.  
  384. void parse_command(volatile char * buffer){
  385.     // test
  386.     if (buffer[0] == 't' &&
  387.         buffer[1] == 'e' &&
  388.         buffer[2] == 's' &&
  389.         buffer[3] == 't'){
  390.         if (superuser){
  391.             battery_check_start();
  392.             server.println("# Battery Test Started");
  393.         }
  394.         else not_allowed();
  395.     }
  396.     // setpass
  397.     else if (buffer[0] == 's' &&
  398.              buffer[1] == 'e' &&
  399.              buffer[2] == 't' &&
  400.              buffer[3] == 'p' &&
  401.              buffer[4] == 'a' &&
  402.              buffer[5] == 's' &&
  403.              buffer[6] == 's' &&
  404.              buffer[7] == ' '){
  405.         if (superuser){
  406.             int i = 8;
  407.             for ( ; i< 31 ; i++){
  408.                 if (buffer[i] == 13){
  409.                     configuration.password[i-8] = 0;
  410.                     break;
  411.                 }
  412.                 else
  413.                     configuration.password[i-8] = buffer[i];
  414.             }
  415.             server.print("# Saved password: ");
  416.             server.println(configuration.password);
  417.             save_config();
  418.         }
  419.         else not_allowed();
  420.     }
  421.     // fetch data
  422.     else if (buffer[0] == 'f' &&
  423.              buffer[1] == 'e' &&
  424.              buffer[2] == 't' &&
  425.              buffer[3] == 'c' &&
  426.              buffer[4] == 'h' &&
  427.              buffer[5] == ' ' &&
  428.              buffer[6] == 'd' &&
  429.              buffer[7] == 'a' &&
  430.              buffer[8] == 't' &&
  431.              buffer[9] == 'a'){
  432.         server.print("running_time.value ");
  433.         server.print(configuration.running_time / 60000);
  434.         server.print("\nnextcheck.value ");
  435.         server.print(ticks / 12);
  436.         server.print("\nonline.value ");
  437.         server.print(digitalRead(UPS_220V) == LOW);
  438.         server.print("\nbatt_v.value ");
  439.         server.print((int)(read_battery_voltage() * 100));
  440.         server.print("\nbatt_a.value ");
  441.         server.print((int)(read_battery_ampere() * 100));
  442.         server.print("\ninv_v.value ");
  443.         server.print((int)(read_inverter_voltage() * 100));
  444.         server.print("\ninv_a.value ");
  445.         server.print((int)(read_inverter_ampere() * 100));
  446.                 if (superuser){
  447.                   #ifdef HARDWARE_1_1
  448.                   server.print("\nhardwareversion.value 11");
  449.                   #else
  450.                   server.print("\nhardwareversion.value 1");
  451.                   #endif
  452.                   server.print("\nsoftwareversion.value ");
  453.                   server.print(VERSION);
  454.                   server.print("\non_battery_check.value ");
  455.           server.print(on_battery_check);
  456.                   server.print("\non_battery.value ");
  457.           server.print(on_battery());
  458.                 }
  459.         server.print("\n.\n");
  460.     }
  461.     // save
  462.     else if (buffer[0] == 's' &&
  463.              buffer[1] == 'a' &&
  464.              buffer[2] == 'v' &&
  465.              buffer[3] == 'e'){
  466.         if (superuser){
  467.             save_config();
  468.             server.println("# Configuration saved");
  469.         }
  470.         else   not_allowed();
  471.     }
  472.     // disable
  473.     else if (buffer[0] == 'd' &&
  474.              buffer[1] == 'i' &&
  475.              buffer[2] == 's' &&
  476.              buffer[3] == 'a' &&
  477.              buffer[4] == 'b' &&
  478.              buffer[5] == 'l' &&
  479.              buffer[6] == 'e' ){
  480.         superuser=false;
  481.         server.println("# Disable OK");
  482.        
  483.     }
  484.     // ipapply
  485.     else if (buffer[0] == 'i' &&
  486.              buffer[1] == 'p' &&
  487.              buffer[2] == 'a' &&
  488.              buffer[3] == 'p' &&
  489.              buffer[4] == 'p' &&
  490.              buffer[5] == 'l' &&
  491.              buffer[6] == 'y'){
  492.        
  493.         if (superuser){
  494.             server.println("# Apply new IP setting.");
  495.             server.println("# Please connect to the new ip address.");
  496.             client.stop();
  497.             save_config();
  498.             init_eth0();
  499.         }
  500.         else not_allowed();
  501.     }
  502.     // quit || exit
  503.     else if ((buffer[0] == 'q' &&
  504.               buffer[1] == 'u' &&
  505.               buffer[2] == 'i' &&
  506.               buffer[3] == 't') ||
  507.              (
  508.               buffer[0] == 'e' &&
  509.               buffer[1] == 'x' &&
  510.               buffer[2] == 'i' &&
  511.               buffer[3] == 't')){
  512.                  superuser = false;
  513.                  client.stop();
  514.              }
  515.     else if (buffer[0] == 'r' &&
  516.              buffer[1] == 'e' &&
  517.              buffer[2] == 'b' &&
  518.              buffer[3] == 'o' &&
  519.              buffer[4] == 'o' &&
  520.              buffer[5] == 't'){
  521.         if (superuser){
  522.             server.println("# Rebooting");
  523.             superuser = false;
  524.             client.stop();
  525.             reset();
  526.         }  
  527.     }
  528.     //ipconfigs
  529.     else if (buffer[0] == 'i' &&
  530.              buffer[1] == 'p' &&
  531.              buffer[2] == 'c' &&
  532.              buffer[3] == 'o' &&
  533.              buffer[4] == 'n' &&
  534.              buffer[5] == 'f' &&
  535.              buffer[6] == 'i' &&
  536.              buffer[7] == 'g'){
  537.         if (superuser){
  538.             server.println("# Network Config:");
  539.             server.print("# Ip: ");
  540.             server.print((int)configuration.ip[0]);
  541.             server.print(".");
  542.             server.print((int)configuration.ip[1]);
  543.             server.print(".");
  544.             server.print((int)configuration.ip[2]);
  545.             server.print(".");
  546.             server.println((int)configuration.ip[3]);
  547.             server.print("# Netmask: ");
  548.             server.print((int)configuration.mask[0]);
  549.             server.print(".");
  550.             server.print((int)configuration.mask[1]);
  551.             server.print(".");
  552.             server.print((int)configuration.mask[2]);
  553.             server.print(".");
  554.             server.println((int)configuration.mask[3]);
  555.             server.print("# Gateway: ");
  556.             server.print((int)configuration.gw[0]);
  557.             server.print(".");
  558.             server.print((int)configuration.gw[1]);
  559.             server.print(".");
  560.             server.print((int)configuration.gw[2]);
  561.             server.print(".");
  562.             server.println((int)configuration.gw[3]);
  563.         }
  564.         else  not_allowed();
  565.     }
  566.     //ip
  567.     else if (buffer[0] == 'i' &&
  568.              buffer[1] == 'p' ){
  569.         if (superuser){
  570.             int base = 3;
  571.             int ott[4];
  572.            
  573.             for (int i = 0 ; i < 4 ; i++){
  574.                 ott[i] = (atoi((char*)&buffer[base]));
  575.                 if (ott[i] > 255){
  576.                     server.println("# Wrong Format (size)");
  577.                     return;
  578.                 }
  579.                
  580.                 if (i < 3) {  
  581.                     if (buffer[base + 1] == '.') base = base + 2;
  582.                     else if (buffer[base + 2] == '.') base = base + 3;
  583.                     else if (buffer[base + 3] == '.') base = base + 4;
  584.                     else {
  585.                         server.println("# Wrong Format (dots)");
  586.                         return;
  587.                     }
  588.                 }
  589.             }
  590.             // apply
  591.             for (int i = 0 ; i < 4 ; i++)
  592.                 configuration.ip[i] = (byte)ott[i];
  593.             server.println("# Ip Address Set");
  594.         }
  595.         else not_allowed();
  596.     }
  597.     //mask
  598.     else if (buffer[0] == 'm' &&
  599.              buffer[1] == 'a' &&
  600.              buffer[2] == 's' &&
  601.              buffer[3] == 'k'){
  602.         if (superuser){
  603.             int base = 5;
  604.             int ott[4];
  605.            
  606.             for (int i = 0 ; i < 4 ; i++){
  607.                 ott[i] = (atoi((char*)&buffer[base]));
  608.                 if (ott[i] > 255){
  609.                     server.println("# Wrong Format (size)");
  610.                     return;
  611.                 }
  612.                
  613.                 if (i < 3) {  
  614.                     if (buffer[base + 1] == '.') base = base + 2;
  615.                     else if (buffer[base + 2] == '.') base = base + 3;
  616.                     else if (buffer[base + 3] == '.') base = base + 4;
  617.                     else {
  618.                         server.println("# Wrong Format (dots)");
  619.                         return;
  620.                     }
  621.                 }
  622.             }
  623.             // apply
  624.             for (int i = 0 ; i < 4 ; i++)
  625.                 configuration.mask[i] = (byte)ott[i];
  626.             server.println("# Network Mask Set");
  627.         }
  628.         else not_allowed();
  629.     }
  630.     //gw
  631.     else if (buffer[0] == 'g' &&
  632.              buffer[1] == 'w' ){
  633.         if (superuser){
  634.             int base = 3;
  635.             int ott[4];
  636.            
  637.             for (int i = 0 ; i < 4 ; i++){
  638.                 ott[i] = (atoi((char*)&buffer[base]));
  639.                 if (ott[i] > 255){
  640.                     server.println("# Wrong Format (size)");
  641.                     return;
  642.                 }
  643.                
  644.                 if (i < 3) {  
  645.                     if (buffer[base + 1] == '.') base = base + 2;
  646.                     else if (buffer[base + 2] == '.') base = base + 3;
  647.                     else if (buffer[base + 3] == '.') base = base + 4;
  648.                     else {
  649.                         server.println("# Wrong Format (dots)");
  650.                         return;
  651.                     }
  652.                 }
  653.             }
  654.             // apply
  655.             for (int i = 0 ; i < 4 ; i++)
  656.                 configuration.gw[i] = (byte)ott[i];
  657.             server.println("# Network Gateway Set");
  658.         }
  659.         else  not_allowed();
  660.     }
  661.     //help
  662.     else if (buffer[0] == 'h' &&
  663.              buffer[1] == 'e' &&
  664.              buffer[2] == 'l' &&
  665.              buffer[3] == 'p'){
  666.         server.println("# Available Commands are: ");
  667.         server.println("fetch data,\nenable [PASS],\nsetpass [PASS],\ndisable,\nsave,\ntest,\nipconfig,\nipapply,\nip [ADDR],\nmask [NMASK],\ngw [ADDR],\nquit,\nreboot,\nexit");
  668.     }
  669.     else server.println("# Unknown Command");
  670.    
  671. }
  672.  
  673. void not_allowed(){
  674.     server.println("# Not Allowed");
  675. }
  676.  
  677. void setup()
  678. {
  679. #ifdef WATCHDOG
  680.     wdt_enable(WDTO_2S);
  681. #endif
  682.    
  683.     pinMode(UPS_DEFAULT_CONFIG, INPUT);
  684.     pinMode(UPS_220V, INPUT);
  685.     pinMode(UPS_BUTTON, INPUT);
  686.    
  687. #ifdef HARDWARE_1_1
  688.     pinMode(UPS_DISABLE_CHARGER,INPUT);
  689. #else
  690.     pinMode(UPS_DISABLE_CHARGER, OUTPUT);
  691.     // sanity check
  692.     digitalWrite(UPS_DISABLE_CHARGER, LOW);
  693. #endif
  694.    
  695.     pinMode(UPS_FORCE_TO_BATTERY, OUTPUT);
  696.     pinMode(UPS_LED_ONLINE, OUTPUT);
  697.     pinMode(UPS_LED_BATTERY, OUTPUT);
  698.     pinMode(UPS_BEEPER, OUTPUT);
  699.    
  700.    
  701.     on_battery_check = false;
  702.     force_to_battery(LOW);
  703.    
  704.     // Force Defaults
  705.     if (digitalRead(UPS_DEFAULT_CONFIG) == LOW)
  706.         apply_default();
  707.    
  708.     // Read the config
  709.     EEPROM_readAnything(0, configuration);
  710.     ticks = configuration.ticks;
  711.  
  712.     // INTERRUPT
  713.     attachInterrupt(UPS_220V_INT, interrupt , CHANGE);
  714.    
  715.     // Set timer for self-check
  716.     MsTimer2::set(7200000, battery_check_timer);
  717.     // 900.000ms period (15 min)
  718.     // 3.600.000ms period (1h)
  719.     // 86.400.000ms period (24h)
  720.     MsTimer2::start();
  721.    
  722.     init_eth0();
  723.    
  724.     // tiny debug on serial
  725.     Serial.begin(9600);
  726.     banner();  
  727.    
  728. }
  729.  
  730.  
  731. void loop()
  732. {
  733. #ifdef WATCHDOG
  734.     wdt_reset();
  735. #endif
  736.    
  737.     if (on_battery()){
  738.         digitalWrite(UPS_LED_BATTERY,HIGH);
  739.         digitalWrite(UPS_LED_ONLINE,LOW);
  740.        
  741.         if (! on_battery_check){
  742.             // beep
  743. #ifdef SOUNDS
  744.             tone(UPS_BEEPER,2048);
  745. #endif         
  746.         }
  747.         else // we're on battery check
  748.             if(read_battery_voltage() < LOW_BATTERY_VOLTAGE) // check battery Status and stop if under limit
  749.                 battery_check_stop();
  750.     }
  751.     else {
  752.         // we're on line!
  753.         digitalWrite(UPS_LED_BATTERY,LOW);
  754.         digitalWrite(UPS_LED_ONLINE,HIGH);
  755. #ifdef SOUNDS
  756.         // shut up beep!
  757.         noTone(UPS_BEEPER);
  758. #endif
  759.     }
  760.     bypass_button();
  761.    
  762.     // wait for a new connection
  763.     client = server.available();
  764.     // new client connected
  765.     if (client) {
  766.         int i=0;
  767.         // connection estabiished
  768.         while (client.connected()) {
  769. #ifdef WATCHDOG
  770.             wdt_reset();
  771. #endif
  772.             // there is some data available
  773.             if (client.available()) {
  774.                 // check request length
  775.                 if (i >= 63) break;
  776.                
  777.                 // read a char
  778.                 buffer[i] = client.read();
  779.                
  780.                 // check if is the termination of the string
  781.                 if (buffer[i] == '\n'){
  782.                     if(check_login(buffer) < 2)
  783.                         break;
  784.                     else
  785.                         parse_command(buffer);
  786.                     if (superuser)
  787.                         server.print("$ ");
  788.                     break;
  789.                 }
  790.                 i++;
  791.             } // end if available
  792.         }  // end while connected  
  793.     } // end if client
  794. } // end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement