diff --git a/examples/Web_Net_Setup/System.cpp b/examples/Web_Net_Setup/System.cpp new file mode 100644 index 0000000..25ef78f --- /dev/null +++ b/examples/Web_Net_Setup/System.cpp @@ -0,0 +1,59 @@ +#include "Arduino.h" +#include "System.h" + +char* System::uptime() +{ + char buffer[65]; + + long days=0; + long hours=0; + long mins=0; + long secs=0; + + secs = millis()/1000; //convect milliseconds to seconds + mins=secs/60; //convert seconds to minutes + hours=mins/60; //convert minutes to hours + days=hours/24; //convert hours to days + secs=secs-(mins*60); //subtract the coverted seconds to minutes in order to display 59 secs max + mins=mins-(hours*60); //subtract the coverted minutes to hours in order to display 59 minutes max + hours=hours-(days*24); //subtract the coverted hours to days in order to display 23 hours max + + if (days > 0) { + ltoa(days,buffer,10); + strcpy(retval,buffer); + } + else { + strcpy(retval,"0"); + } + + strcat(retval,":"); + ltoa(hours,buffer,10); + strcat(retval,buffer); + + strcat(retval,":"); + ltoa(mins,buffer,10); + strcat(retval,buffer); + + strcat(retval,":"); + ltoa(secs,buffer,10); + strcat(retval,buffer); + + strcat(retval,'\0'); + + return retval; +} + +int System::ramFree () { + extern int __heap_start, *__brkval; + int v; + int a = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); + return a; +} + +int System::ramSize() { + int v; + int a = (int) &v; + return a; +} + + diff --git a/examples/Web_Net_Setup/System.h b/examples/Web_Net_Setup/System.h new file mode 100644 index 0000000..13397c4 --- /dev/null +++ b/examples/Web_Net_Setup/System.h @@ -0,0 +1,39 @@ +#ifndef System_h +#define System_h + +#include + +/** +* System Class. +* +* @author Matthias Maderer +* @version 1.1.7 +*/ +class System { +public: + /** + * Returns the uptime of the arduino with a char pointer. + * Format: DAYS:HOURS:MINUTES:SECONDS + * Sample: 1:20:23:50 = 1 day, 20 hours, 23 minutes and 50 seconds + * @return char *: pointer! + */ + char * uptime(); + + /** + * Returns the free RAM + * @return int: free RAM + */ + int ramFree(); + + /** + * Returns the size of the RAM + * @return int: RAM size + */ + int ramSize(); + +private: + char retval[25]; +}; + +#endif + diff --git a/examples/Web_Net_Setup/Web_Net_Setup.pde b/examples/Web_Net_Setup/Web_Net_Setup.pde index 3ad54c7..2425433 100644 --- a/examples/Web_Net_Setup/Web_Net_Setup.pde +++ b/examples/Web_Net_Setup/Web_Net_Setup.pde @@ -1,8 +1,8 @@ /* Web_Net_Setup.pde - example for a webinterface to set the network configuration Author: Matthias Maderer Date: 07.03.2013 -Version: 1.0 -web: www.edvler-blog.de +Version: 1.0.1 +web: www.edvler-blog.de/arduino_networksetup_webinterface_with_eeprom This is a sample Sketch for Webduino! More informations about Webduino can be found at https://github.com/sirleech/Webduino @@ -12,16 +12,23 @@ For more informations about EEPROMAnything.h look at http://playground.arduino.c /* * With this example its possible to configure the network configuration of the -* Arduino board over a webinterface. Imagine like your router setup. +* Arduino Ethernet Shield with a webinterface. Imagine like your router setup. * * It's possible to configure the following network settings: +* - MAC address * - IP address * - Subnet * - Gateway * - DNS Server * - Webserver port -* - USE DHCP YES/NO +* - USE DHCP YES/NO (if you use DHCP connect per serial port - 9600 baud - on powerup to see which ip address is assigned) * - DHCP renew interval +* +* Other functions: +* - Display DHCP renew status +* - Display DHCP renew timestamp +* - Display Arduino uptime +* - Display used RAM * * You can configure default settings. This settings are used wenn no configuration is present. * Look at the function set_EEPROM_Default(). @@ -48,7 +55,8 @@ For more informations about EEPROMAnything.h look at http://playground.arduino.c * Keep this in mind. * * Resources: -* 27.000 Bytes +* There are many Strings for the HTML site. The compiled size is about 27.000 Bytes. Aprox. 2000 byte of SRAM is used. +* On smaller Arduinos this may cause problems. I've tested it on a MEGA 2560. * * BUGS: * - After uploading your sketch the arduino is not reachable. --> Reset your Arduino!! @@ -57,6 +65,8 @@ For more informations about EEPROMAnything.h look at http://playground.arduino.c #define WEBDUINO_FAVICON_DATA "" // no favicon //#define DEBUG //uncomment for serial debug output +#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. +#define SERIAL_BAUD 9600 #include "SPI.h" // new include @@ -90,7 +100,10 @@ struct config_t unsigned int webserverPort; } eeprom_config; -/* The default Ethernet settings. +/** +* set_EEPROM_Default() function +* +* The default settings. * This settings are used when no config is present or the reset button is pressed. */ void set_EEPROM_Default() { @@ -99,21 +112,21 @@ void set_EEPROM_Default() { eeprom_config.use_dhcp=0; // use DHCP per default eeprom_config.dhcp_refresh_minutes=60; // refresh the DHCP every 60 minutes - // set the default MAC address. In this case its DE:AD:BE:EF:11:22 + // set the default MAC address. In this case its DE:AD:BE:EF:FE:ED eeprom_config.mac[0]=0xDE; eeprom_config.mac[1]=0xAD; eeprom_config.mac[2]=0xBE; eeprom_config.mac[3]=0xEF; - eeprom_config.mac[4]=0x11; - eeprom_config.mac[5]=0x11; + eeprom_config.mac[4]=0xFE; + eeprom_config.mac[5]=0xED; - // set the default IP address for the arduino. In this case its 172.31.0.111 + // set the default IP address for the arduino. In this case its 192.168.0.111 eeprom_config.ip[0]=192; eeprom_config.ip[1]=168; eeprom_config.ip[2]=0; eeprom_config.ip[3]=111; - // set the default GATEWAY. In this case its 172.31.0.254 + // set the default GATEWAY. In this case its 192.168.0.254 eeprom_config.gateway[0]=192; eeprom_config.gateway[1]=168; eeprom_config.gateway[2]=0; @@ -125,7 +138,7 @@ void set_EEPROM_Default() { eeprom_config.subnet[2]=255; eeprom_config.subnet[3]=0; - // set the default DNS SERVER. In this case its 172.31.0.254 + // set the default DNS SERVER. In this case its 192.168.0.254 eeprom_config.dns_server[0]=192; eeprom_config.dns_server[1]=168; eeprom_config.dns_server[2]=0; @@ -166,6 +179,12 @@ void read_EEPROM_Settings() { } } +/** +* print_EEPROM_Settings() function +* +* This function is used for debugging the configuration. +* It prints the actual configuration to the serial port. +*/ #ifdef DEBUG void print_EEPROM_Settings() { Serial.print("IP: "); @@ -233,21 +252,27 @@ void print_EEPROM_Settings() { /* START Network section ####################################################################################################################################### -* Webserver Code +* Code for setting up network connection */ -long last_dhcp_renew; +unsigned long last_dhcp_renew; byte dhcp_state; /** -renewDHCP() function -Renew the DHCP relase in a given interval. +* renewDHCP() function +* Renew the DHCP relase in a given interval. +* +* Overview: +* - Check if interval = 0 and set it to 1 +* - Check if renew interval is reached and renew the lease */ void renewDHCP(int interval) { + unsigned long interval_millis = interval * 60000; + if (interval == 0 ) { interval = 1; } if (eeprom_config.use_dhcp==1) { - if((millis() - last_dhcp_renew) > (interval*60*1000)) { + if((millis() - last_dhcp_renew) > interval_millis) { last_dhcp_renew=millis(); dhcp_state = Ethernet.maintain(); } @@ -256,7 +281,7 @@ void renewDHCP(int interval) { /** -* setupNetwork() function (webpage) +* setupNetwork() function * This function is used to setupup the network according to the values stored in the eeprom * * Overview: @@ -291,7 +316,18 @@ void setupNetwork() { // END Network section ######################################################################################################################################### +/* WEB-Server section ####################################################################################################################################### +* Webserver Code +*/ +#ifdef USE_SYSTEM_LIBRARY +#include "system.h" +System sys; +#endif + +/* Store all string in the FLASH storage to free SRAM. +The P() is a function from Webduino. +*/ P(Page_start) = "Web_EEPROM_Setup\n"; P(Page_end) = ""; @@ -333,25 +369,25 @@ P(table_end) = ""; P(Config_set) = "New configuration stored!
Please turn off and on your Arduino or use the reset button!

"; -/* - - - .. mehr auf http://w-w-w.ms/a1v0si#3127029 -*/ +P(UPTIME) = "Uptime: "; -//P(Form_input_cb_start) = "begin(); } +/** +* loop() function +* Runs forver .... +* +* Overview: +* - Renew the DHCP lease +* - Serve web clients +* +*/ void loop() { + // renew DHCP lease + renewDHCP(eeprom_config.dhcp_refresh_minutes); + char buff[200]; int len = 200;