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

Web_Net_Setup.ino 25KB

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