Konfiguration über http
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
23KB

  1. /* Web_Net_Setup.pde - example for a webinterface to set the network configuration
  2. Author: Matthias Maderer
  3. Date: 07.03.2013
  4. Version: 1.0.1
  5. web: www.edvler-blog.de/arduino_networksetup_webinterface_with_eeprom
  6. This is a sample Sketch for Webduino!
  7. More informations about Webduino can be found at https://github.com/sirleech/Webduino
  8. For more informations about EEPROMAnything.h look at http://playground.arduino.cc/Code/EEPROMWriteAnything
  9. */
  10. /*
  11. * With this example its possible to configure the network configuration of the
  12. * Arduino Ethernet Shield with a webinterface. Imagine like your router setup.
  13. *
  14. * It's possible to configure the following network settings:
  15. * - MAC address
  16. * - IP address
  17. * - Subnet
  18. * - Gateway
  19. * - DNS Server
  20. * - Webserver port
  21. * - USE DHCP YES/NO (if you use DHCP connect per serial port - 9600 baud - on powerup to see which ip address is assigned)
  22. * - DHCP renew interval
  23. *
  24. * Other functions:
  25. * - Display DHCP renew status
  26. * - Display DHCP renew timestamp
  27. * - Display Arduino uptime
  28. * - Display used RAM
  29. *
  30. * You can configure default settings. This settings are used wenn no configuration is present.
  31. * Look at the function set_EEPROM_Default().
  32. *
  33. * It is possible to connect a RESET button. If the button is pressed and the Arduino is turned on
  34. * the default values would be restored too.
  35. * See #define RESET_PIN.
  36. *
  37. *
  38. * All settings are stored in EEPROM. This means they are permanent.
  39. * Please look at http://arduino.cc/en/Reference/EEPROM for a short description.
  40. *
  41. *
  42. * To setup your arduino upload this sketch.
  43. *
  44. * If you don't change the sourcecode the default IP address is http://192.168.0.111/
  45. * Don't forget to change the IP of your network adapter to a suitable address (e.g. to IP 192.168.0.1, NETMASK 255.255.255.0)!
  46. *
  47. * Enter the following URL for the setup page:
  48. * http://192.168.0.111/setupNet.html
  49. *
  50. * Please note that no input checks are done!!
  51. * This means that no error would be thrown if you type a wrong IP address or other failures.
  52. * Keep this in mind.
  53. *
  54. * Resources:
  55. * There are many Strings for the HTML site. The compiled size is about 27.000 Bytes. Aprox. 2000 byte of SRAM is used.
  56. * On smaller Arduinos this may cause problems. I've tested it on a MEGA 2560.
  57. *
  58. * BUGS:
  59. * - After uploading your sketch the arduino is not reachable. --> Reset your Arduino!!
  60. */
  61. #define WEBDUINO_FAVICON_DATA "" // no favicon
  62. //#define DEBUG //uncomment for serial debug output
  63. #define USE_SYSTEM_LIBRARY //comment out if you want to save some space (about 1 Byte). You wouldn't see uptime and free RAM if it's commented out.
  64. #define SERIAL_BAUD 9600
  65. #include "SPI.h" // new include
  66. #include "avr/pgmspace.h" // new include
  67. #include "Ethernet.h"
  68. #include "WebServer.h"
  69. /* #############################################################################################################################################################
  70. * Code for the EEPROM related things
  71. *
  72. */
  73. #include <EEPROM.h>
  74. #include "EEPROMAnything.h"
  75. #define RESET_PIN 40 //Connect a button to this PIN. If the button is hold, an the device is turned on the default ethernet settings are restored.
  76. /* structure which is stored in the eeprom.
  77. * Look at "EEPROMAnything.h" for the functions storing and reading the struct
  78. */
  79. struct config_t
  80. {
  81. byte config_set;
  82. byte use_dhcp;
  83. byte dhcp_refresh_minutes;
  84. byte mac[6];
  85. byte ip[4];
  86. byte gateway[4];
  87. byte subnet[4];
  88. byte dns_server[4];
  89. unsigned int webserverPort;
  90. } eeprom_config;
  91. /**
  92. * set_EEPROM_Default() function
  93. *
  94. * The default settings.
  95. * This settings are used when no config is present or the reset button is pressed.
  96. */
  97. void set_EEPROM_Default() {
  98. eeprom_config.config_set=1; // dont change! It's used to check if the config is already set
  99. eeprom_config.use_dhcp=0; // use DHCP per default
  100. eeprom_config.dhcp_refresh_minutes=60; // refresh the DHCP every 60 minutes
  101. // set the default MAC address. In this case its DE:AD:BE:EF:FE:ED
  102. eeprom_config.mac[0]=0xDE;
  103. eeprom_config.mac[1]=0xAD;
  104. eeprom_config.mac[2]=0xBE;
  105. eeprom_config.mac[3]=0xEF;
  106. eeprom_config.mac[4]=0xFE;
  107. eeprom_config.mac[5]=0xED;
  108. // set the default IP address for the arduino. In this case its 192.168.0.111
  109. eeprom_config.ip[0]=192;
  110. eeprom_config.ip[1]=168;
  111. eeprom_config.ip[2]=0;
  112. eeprom_config.ip[3]=111;
  113. // set the default GATEWAY. In this case its 192.168.0.254
  114. eeprom_config.gateway[0]=192;
  115. eeprom_config.gateway[1]=168;
  116. eeprom_config.gateway[2]=0;
  117. eeprom_config.gateway[3]=254;
  118. // set the default SUBNET. In this case its 255.255.255.0
  119. eeprom_config.subnet[0]=255;
  120. eeprom_config.subnet[1]=255;
  121. eeprom_config.subnet[2]=255;
  122. eeprom_config.subnet[3]=0;
  123. // set the default DNS SERVER. In this case its 192.168.0.254
  124. eeprom_config.dns_server[0]=192;
  125. eeprom_config.dns_server[1]=168;
  126. eeprom_config.dns_server[2]=0;
  127. eeprom_config.dns_server[3]=254;
  128. // set the default Webserver Port. In this case its Port 80
  129. eeprom_config.webserverPort=80;
  130. #ifdef DEBUG
  131. Serial.println("Config reset");
  132. #endif
  133. }
  134. /**
  135. * read_EEPROM_Settings function
  136. * This function is used to read the EEPROM settings at startup
  137. *
  138. * Overview:
  139. * - Set the PIN for the RESET-button to input and activate pullups
  140. * - Load the stored data from EEPROM into the eeprom_config struct
  141. * - Check if a config is stored or the reset button is pressed. If one of the conditions is ture, set the defaults
  142. */
  143. void read_EEPROM_Settings() {
  144. pinMode(RESET_PIN, INPUT);
  145. digitalWrite(RESET_PIN, HIGH);
  146. // read the current config
  147. EEPROM_readAnything(0, eeprom_config);
  148. // check if config is present or if reset button is pressed
  149. if (eeprom_config.config_set != 1 || digitalRead(RESET_PIN) == LOW) {
  150. // set default values
  151. set_EEPROM_Default();
  152. // write the config to eeprom
  153. EEPROM_writeAnything(0, eeprom_config);
  154. }
  155. }
  156. /**
  157. * print_EEPROM_Settings() function
  158. *
  159. * This function is used for debugging the configuration.
  160. * It prints the actual configuration to the serial port.
  161. */
  162. #ifdef DEBUG
  163. void print_EEPROM_Settings() {
  164. Serial.print("IP: ");
  165. for(int i = 0; i<4; i++) {
  166. Serial.print(eeprom_config.ip[i]);
  167. if (i<3) {
  168. Serial.print('.');
  169. }
  170. }
  171. Serial.println();
  172. Serial.print("Subnet: ");
  173. for(int i = 0; i<4; i++) {
  174. Serial.print(eeprom_config.subnet[i]);
  175. if (i<3) {
  176. Serial.print('.');
  177. }
  178. }
  179. Serial.println();
  180. Serial.print("Gateway: ");
  181. for(int i = 0; i<4; i++) {
  182. Serial.print(eeprom_config.gateway[i]);
  183. if (i<3) {
  184. Serial.print('.');
  185. }
  186. }
  187. Serial.println();
  188. Serial.print("DNS Server: ");
  189. for(int i = 0; i<4; i++) {
  190. Serial.print(eeprom_config.dns_server[i]);
  191. if (i<3) {
  192. Serial.print('.');
  193. }
  194. }
  195. Serial.println();
  196. Serial.print("MAC: ");
  197. for (int a=0;a<6;a++) {
  198. Serial.print(eeprom_config.mac[a],HEX);
  199. if(a<5) {
  200. Serial.print(":");
  201. }
  202. }
  203. Serial.println();
  204. Serial.print("Webserver Port: ");
  205. Serial.println(eeprom_config.webserverPort);
  206. Serial.print("USE DHCP: ");
  207. Serial.println(eeprom_config.use_dhcp);
  208. Serial.print(" DHCP renew every ");
  209. Serial.print(eeprom_config.dhcp_refresh_minutes);
  210. Serial.println(" minutes");
  211. Serial.print("Config Set: ");
  212. Serial.println(eeprom_config.config_set);
  213. }
  214. #endif
  215. // #############################################################################################################################################################
  216. /* START Network section #######################################################################################################################################
  217. * Code for setting up network connection
  218. */
  219. unsigned long last_dhcp_renew;
  220. byte dhcp_state;
  221. /**
  222. * renewDHCP() function
  223. * Renew the DHCP relase in a given interval.
  224. *
  225. * Overview:
  226. * - Check if interval = 0 and set it to 1
  227. * - Check if renew interval is reached and renew the lease
  228. */
  229. void renewDHCP(int interval) {
  230. unsigned long interval_millis = interval * 60000;
  231. if (interval == 0 ) {
  232. interval = 1;
  233. }
  234. if (eeprom_config.use_dhcp==1) {
  235. if((millis() - last_dhcp_renew) > interval_millis) {
  236. last_dhcp_renew=millis();
  237. dhcp_state = Ethernet.maintain();
  238. }
  239. }
  240. }
  241. /**
  242. * setupNetwork() function
  243. * This function is used to setupup the network according to the values stored in the eeprom
  244. *
  245. * Overview:
  246. * - First of all read the EEPROM settings
  247. * - Display a link to the ethernet setup
  248. * - Check if DHCP should be used, if not create instaces of IPAddress for ip, gateway, subnet and dns_server
  249. * - Invoke Ethernet.begin with all parameters if no dhcp is active (Ethernet.begin(mac, ip, dns_server, gateway, subnet);).
  250. * - If DHCP is used invoke only with mac (Ethernet.begin(mac);) and display the ip on the serial console.
  251. */
  252. void setupNetwork() {
  253. read_EEPROM_Settings();
  254. #ifdef DEBUG
  255. print_EEPROM_Settings();
  256. #endif
  257. // byte mac[] = { eeprom_config.mac[0], eeprom_config.mac[1], eeprom_config.mac[2], eeprom_config.mac[3], eeprom_config.mac[4], eeprom_config.mac[5] };
  258. if (eeprom_config.use_dhcp != 1) {
  259. IPAddress ip(eeprom_config.ip[0], eeprom_config.ip[1], eeprom_config.ip[2], eeprom_config.ip[3]);
  260. IPAddress gateway (eeprom_config.gateway[0],eeprom_config.gateway[1],eeprom_config.gateway[2],eeprom_config.gateway[3]);
  261. IPAddress subnet (eeprom_config.subnet[0], eeprom_config.subnet[1], eeprom_config.subnet[2], eeprom_config.subnet[3]);
  262. IPAddress dns_server (eeprom_config.dns_server[0], eeprom_config.dns_server[1], eeprom_config.dns_server[2], eeprom_config.dns_server[3]);
  263. Ethernet.begin(eeprom_config.mac, ip, dns_server, gateway, subnet);
  264. } else {
  265. if (Ethernet.begin(eeprom_config.mac) == 0) {
  266. Serial.print("Failed to configure Ethernet using DHCP");
  267. }
  268. Serial.println(Ethernet.localIP());
  269. }
  270. }
  271. // END Network section #########################################################################################################################################
  272. /* WEB-Server section #######################################################################################################################################
  273. * Webserver Code
  274. */
  275. #ifdef USE_SYSTEM_LIBRARY
  276. #include "system.h"
  277. System sys;
  278. #endif
  279. /* Store all string in the FLASH storage to free SRAM.
  280. The P() is a function from Webduino.
  281. */
  282. P(Page_start) = "<html><head><title>Web_EEPROM_Setup</title></head><body>\n";
  283. P(Page_end) = "</body></html>";
  284. P(Http400) = "HTTP 400 - BAD REQUEST";
  285. P(Index) = "<h1>index.html</h1><br>This is your main site!<br>The code is found in the indexHTML() function.<br>You can add more sites if you need. Please see the well documented source code.<br><br>Use the following link to setup the network.<br><a href=\"setupNet.html\">NETWORK SETUP</a>";
  286. P(Form_eth_start) = "<FORM action=\"setupNet.html\" method=\"get\">";
  287. P(Form_end) = "<FORM>";
  288. P(Form_input_send) = "<INPUT type=\"submit\" value=\"Set config\">";
  289. P(Form_input_text_start) = "<input type=\"text\" name=\"";
  290. P(Form_input_value) = "\" value=\"";
  291. P(Form_input_size2) = "\" maxlength=\"2\" size=\"2";
  292. P(Form_input_size3) = "\" maxlength=\"3\" size=\"3";
  293. P(Form_input_end) = "\">\n";
  294. P(MAC) = "MAC address: ";
  295. P(IP) = "IP address: ";
  296. P(SUBNET) = "Subnet: ";
  297. P(GW) = "GW address: ";
  298. P(DNS_SERVER) = "DNS server: ";
  299. P(WEB_PORT) = "Webserver port (1-65535): ";
  300. P(DHCP_ACTIVE) = "Use DHCP: ";
  301. P(DHCP_REFRESH) = "Renew interval for DHCP in minutes (1 - 255): ";
  302. P(Form_cb) = "<input type=\"radio\" name=\"23\" value=\"";
  303. P(Form_cb_checked) = " checked ";
  304. P(Form_cb_on) = ">On";
  305. P(Form_cb_off) = ">Off";
  306. P(br) = "<br>\n";
  307. P(table_start) = "<table>";
  308. P(table_tr_start) = "<tr>";
  309. P(table_tr_end) = "</tr>";
  310. P(table_td_start) = "<td>";
  311. P(table_td_end) = "</td>";
  312. P(table_end) = "</table>";
  313. P(Config_set) = "<font size=\"6\" color=\"red\">New configuration stored! <br>Please turn off and on your Arduino or use the reset button!</font><br>";
  314. P(DHCP_STATE_TIME) = "DHCP last renew timestamp (sec)";
  315. P(DHCP_STATE) = "DHCP renew return code (sec)";
  316. P(UPTIME) = "Uptime: ";
  317. #ifdef USE_SYSTEM_LIBRARY
  318. P(RAM_1) = "RAM (byte): ";
  319. P(RAM_2) = " free of ";
  320. #endif
  321. /* This creates an pointer to instance of the webserver. */
  322. WebServer * webserver;
  323. /**
  324. * indexHTML() function
  325. * This function is used to send the index.html to the client.
  326. */
  327. void indexHTML(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
  328. {
  329. /* this line sends the standard "we're all OK" headers back to the
  330. browser */
  331. server.httpSuccess();
  332. /* if we're handling a GET or POST, we can output our data here.
  333. For a HEAD request, we just stop after outputting headers. */
  334. if (type == WebServer::HEAD)
  335. return;
  336. server.printP(Page_start);
  337. server.printP(Index);
  338. server.printP(Page_end);
  339. }
  340. /**
  341. * setupNetHTML() function
  342. * This function is used to send the setupNet.html to the client.
  343. *
  344. * Overview:
  345. * - Send a HTTP 200 OK Header
  346. * - If get parameters exists assign them to the corresponding variable in the eeprom_config struct
  347. * - Print the configuration
  348. *
  349. * Parameters are simple numbers. The name of the parameter is converted to an int with the atoi function.
  350. * This saves some code for setting the MAC and IP addresses.
  351. */
  352. #define NAMELEN 5
  353. #define VALUELEN 7
  354. void setupNetHTML(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
  355. {
  356. URLPARAM_RESULT rc;
  357. char name[NAMELEN];
  358. char value[VALUELEN];
  359. boolean params_present = false;
  360. byte param_number = 0;
  361. /* this line sends the standard "we're all OK" headers back to the
  362. browser */
  363. server.httpSuccess();
  364. /* if we're handling a GET or POST, we can output our data here.
  365. For a HEAD request, we just stop after outputting headers. */
  366. if (type == WebServer::HEAD)
  367. return;
  368. server.printP(Page_start);
  369. // check for parameters
  370. if (strlen(url_tail)) {
  371. while (strlen(url_tail)) {
  372. rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
  373. if (rc != URLPARAM_EOS) {
  374. params_present=true;
  375. // debug output for parameters
  376. #ifdef DEBUG
  377. Serial.print(name);
  378. server.print(name);
  379. Serial.print(" - ");
  380. server.print(" - ");
  381. Serial.println(value);
  382. server.print(value);
  383. server.print("<br>");
  384. #endif
  385. param_number = atoi(name);
  386. // read MAC address
  387. if (param_number >=0 && param_number <=5) {
  388. eeprom_config.mac[param_number]=strtol(value,NULL,16);
  389. }
  390. // read IP address
  391. if (param_number >=6 && param_number <=9) {
  392. eeprom_config.ip[param_number-6]=atoi(value);
  393. }
  394. // read SUBNET
  395. if (param_number >=10 && param_number <=13) {
  396. eeprom_config.subnet[param_number-10]=atoi(value);
  397. }
  398. // read GATEWAY
  399. if (param_number >=14 && param_number <=17) {
  400. eeprom_config.gateway[param_number-14]=atoi(value);
  401. }
  402. // read DNS-SERVER
  403. if (param_number >=18 && param_number <=21) {
  404. eeprom_config.dns_server[param_number-18]=atoi(value);
  405. }
  406. // read WEBServer port
  407. if (param_number == 22) {
  408. eeprom_config.webserverPort=atoi(value);
  409. }
  410. // read DHCP ON/OFF
  411. if (param_number == 23) {
  412. eeprom_config.use_dhcp=atoi(value);
  413. }
  414. // read DHCP renew interval
  415. if (param_number == 24) {
  416. eeprom_config.dhcp_refresh_minutes=atoi(value);
  417. }
  418. }
  419. }
  420. EEPROM_writeAnything(0, eeprom_config);
  421. }
  422. //print the form
  423. server.printP(Form_eth_start);
  424. if(params_present==true) {
  425. server.printP(Config_set);
  426. }
  427. server.printP(table_start);
  428. // print the current MAC
  429. server.printP(table_tr_start);
  430. server.printP(table_td_start);
  431. server.printP(MAC);
  432. server.printP(table_td_end);
  433. server.printP(table_td_start);
  434. for (int a=0;a<6;a++) {
  435. server.printP(Form_input_text_start);
  436. server.print(a);
  437. server.printP(Form_input_value);
  438. server.print(eeprom_config.mac[a],HEX);
  439. server.printP(Form_input_size2);
  440. server.printP(Form_input_end);
  441. }
  442. server.printP(table_td_end);
  443. server.printP(table_tr_end);
  444. // print the current IP
  445. server.printP(table_tr_start);
  446. server.printP(table_td_start);
  447. server.printP(IP);
  448. server.printP(table_td_end);
  449. server.printP(table_td_start);
  450. for (int a=0;a<4;a++) {
  451. server.printP(Form_input_text_start);
  452. server.print(a+6);
  453. server.printP(Form_input_value);
  454. server.print(eeprom_config.ip[a]);
  455. server.printP(Form_input_size3);
  456. server.printP(Form_input_end);
  457. }
  458. server.printP(table_td_end);
  459. server.printP(table_tr_end);
  460. // print the current SUBNET
  461. server.printP(table_tr_start);
  462. server.printP(table_td_start);
  463. server.printP(SUBNET);
  464. server.printP(table_td_end);
  465. server.printP(table_td_start);
  466. for (int a=0;a<4;a++) {
  467. server.printP(Form_input_text_start);
  468. server.print(a+10);
  469. server.printP(Form_input_value);
  470. server.print(eeprom_config.subnet[a]);
  471. server.printP(Form_input_size3);
  472. server.printP(Form_input_end);
  473. }
  474. server.printP(table_td_end);
  475. server.printP(table_tr_end);
  476. // print the current GATEWAY
  477. server.printP(table_tr_start);
  478. server.printP(table_td_start);
  479. server.printP(GW);
  480. server.printP(table_td_end);
  481. server.printP(table_td_start);
  482. for (int a=0;a<4;a++) {
  483. server.printP(Form_input_text_start);
  484. server.print(a+14);
  485. server.printP(Form_input_value);
  486. server.print(eeprom_config.gateway[a]);
  487. server.printP(Form_input_size3);
  488. server.printP(Form_input_end);
  489. }
  490. server.printP(table_td_end);
  491. server.printP(table_tr_end);
  492. // print the current DNS-SERVER
  493. server.printP(table_tr_start);
  494. server.printP(table_td_start);
  495. server.printP(DNS_SERVER);
  496. server.printP(table_td_end);
  497. server.printP(table_td_start);
  498. for (int a=0;a<4;a++) {
  499. server.printP(Form_input_text_start);
  500. server.print(a+18);
  501. server.printP(Form_input_value);
  502. server.print(eeprom_config.dns_server[a]);
  503. server.printP(Form_input_size3);
  504. server.printP(Form_input_end);
  505. }
  506. server.printP(table_td_end);
  507. server.printP(table_tr_end);
  508. // print the current webserver port
  509. server.printP(table_tr_start);
  510. server.printP(table_td_start);
  511. server.printP(WEB_PORT);
  512. server.printP(table_td_end);
  513. server.printP(table_td_start);
  514. server.printP(Form_input_text_start);
  515. server.print(22);
  516. server.printP(Form_input_value);
  517. server.print(eeprom_config.webserverPort);
  518. server.printP(Form_input_end);
  519. server.printP(table_td_end);
  520. server.printP(table_tr_end);
  521. //print the current DHCP config
  522. server.printP(table_tr_start);
  523. server.printP(table_td_start);
  524. server.printP(DHCP_ACTIVE);
  525. server.printP(table_td_end);
  526. server.printP(table_td_start);
  527. server.printP(Form_cb);
  528. server.print("0\"");
  529. if(eeprom_config.use_dhcp != 1) {
  530. server.printP(Form_cb_checked);
  531. }
  532. server.printP(Form_cb_off);
  533. server.printP(Form_cb);
  534. server.print("1\"");
  535. if(eeprom_config.use_dhcp == 1) {
  536. server.printP(Form_cb_checked);
  537. }
  538. server.printP(Form_cb_on);
  539. server.printP(table_td_end);
  540. server.printP(table_tr_end);
  541. //print the current DHCP renew time
  542. server.printP(table_tr_start);
  543. server.printP(table_td_start);
  544. server.printP(DHCP_REFRESH);
  545. server.printP(table_td_end);
  546. server.printP(table_td_start);
  547. server.printP(Form_input_text_start);
  548. server.print(24);
  549. server.printP(Form_input_value);
  550. server.print(eeprom_config.dhcp_refresh_minutes);
  551. server.printP(Form_input_size3);
  552. server.printP(Form_input_end);
  553. server.printP(table_td_end);
  554. server.printP(table_tr_end);
  555. //print DHCP status
  556. if(eeprom_config.use_dhcp == 1) {
  557. server.printP(table_tr_start);
  558. server.printP(table_td_start);
  559. server.printP(DHCP_STATE);
  560. server.printP(table_td_end);
  561. server.printP(table_td_start);
  562. server.print(dhcp_state);
  563. server.printP(table_td_end);
  564. server.printP(table_tr_end);
  565. server.printP(table_tr_start);
  566. server.printP(table_td_start);
  567. server.printP(DHCP_STATE_TIME);
  568. server.printP(table_td_end);
  569. server.printP(table_td_start);
  570. server.print(last_dhcp_renew/1000);
  571. server.printP(table_td_end);
  572. server.printP(table_tr_end);
  573. }
  574. #ifdef USE_SYSTEM_LIBRARY
  575. //print uptime
  576. server.printP(table_tr_start);
  577. server.printP(table_td_start);
  578. server.printP(UPTIME);
  579. server.printP(table_td_end);
  580. server.printP(table_td_start);
  581. server.print(sys.uptime());
  582. server.printP(table_td_end);
  583. server.printP(table_tr_end);
  584. server.printP(table_tr_start);
  585. server.printP(table_td_start);
  586. server.printP(RAM_1);
  587. server.print(sys.ramFree());
  588. server.printP(RAM_2);
  589. server.print(sys.ramSize());
  590. server.printP(table_td_end);
  591. server.printP(table_tr_end);
  592. #endif
  593. server.printP(table_end);
  594. //print the send button
  595. server.printP(Form_input_send);
  596. server.printP(Form_end);
  597. server.printP(Page_end);
  598. }
  599. /**
  600. * errorHTML() function
  601. * This function is called whenever a non extisting page is called.
  602. * It sends a HTTP 400 Bad Request header and the same as text.
  603. */
  604. void errorHTML(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
  605. {
  606. /* this line sends the standard "HTTP 400 Bad Request" headers back to the
  607. browser */
  608. server.httpFail();
  609. /* if we're handling a GET or POST, we can output our data here.
  610. For a HEAD request, we just stop after outputting headers. */
  611. if (type == WebServer::HEAD)
  612. return;
  613. server.printP(Http400);
  614. server.printP(Page_end);
  615. }
  616. // END WEBCODE ######################################################################################################################################################
  617. /**
  618. * setup() function
  619. * This function is called whenever the arduino is turned on.
  620. */
  621. void setup()
  622. {
  623. Serial.begin(SERIAL_BAUD);
  624. /* initialize the Ethernet adapter with the settings from eeprom */
  625. delay(200); // some time to settle
  626. setupNetwork();
  627. delay(200); // some time to settle
  628. #define PREFIX ""
  629. webserver = new WebServer(PREFIX, eeprom_config.webserverPort);
  630. /* setup our default command that will be run when the user accesses
  631. * the root page on the server */
  632. webserver->setDefaultCommand(&indexHTML);
  633. /* setup our default command that will be run when the user accesses
  634. * a page NOT on the server */
  635. webserver->setFailureCommand(&errorHTML);
  636. /* run the same command if you try to load /index.html, a common
  637. * default page name */
  638. webserver->addCommand("index.html", &indexHTML);
  639. /* display a network setup form. The configuration is stored in eeprom */
  640. webserver->addCommand("setupNet.html", &setupNetHTML);
  641. /* start the webserver */
  642. webserver->begin();
  643. }
  644. /**
  645. * loop() function
  646. * Runs forver ....
  647. *
  648. * Overview:
  649. * - Renew the DHCP lease
  650. * - Serve web clients
  651. *
  652. */
  653. void loop()
  654. {
  655. // renew DHCP lease
  656. renewDHCP(eeprom_config.dhcp_refresh_minutes);
  657. char buff[200];
  658. int len = 200;
  659. /* process incoming connections one at a time forever */
  660. webserver->processConnection(buff, &len);
  661. }