Advertisement
Guest User

Untitled

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