Konfiguration über http
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Web_Net_Setup.ino 23KB

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