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.

852 lines
25KB

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