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.

934 lines
26KB

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