This project was originally done in 2012. The original version had all of the parameters hard coded in the Arduino sketch. The new version adds a setup shell that is accessible via the Arduino's serial port. This allows the user to enter their own parameters which get stored in the Arduino's EEPROM. I've also added an LCD display that shows the current IP address and connection state. The LCD isn't super useful but it looks cool.
How it works…..
The Arduino starts and enters the setup() function. The serial port is initialized then the existing parameters are read from the EEPROM and copied into string variables. Since the parameters vary in length the starting address for each parameter in EEPROM is dynamic. To address this, the first seven bytes of EEPROM contain the length of each parameter. The length of each parameter is first read out of EEPROM. Then the start address of each parameter is calculated and stored in RAM so the Arduino know what portion of EEPROM each parameter occupies.
The parameters are then spit out of the serial port and the user is prompted to edit the parameters. If the user sends an “S" (for setup) , the Arduino will enter a configuration function that prompts the user to change the parameters one at a time. After all of the new parameters are recorded, they are displayed via the serial port and the user is prompted to save them to EEPROM. If the user selects no, the changes are discarded and the values from EEPROM are reloaded . If the user selects yes, the new values are written to the EEPROM. The length of each parameter is calculated and written to the first seven bytes of EEPROM. From here the rest of the setup function is executed, an Ethernet connection is established and a local IP address is obtained from the LAN DHCP server.
The loop function is entered, a connection to checkip.dyndns.org is attempted. If connection is successful an HTTP request is made. The Textfinder library is used to search the returned text and identify the external IP address. The newly read IP address is compared to the last recorded IP address. If the addresses are different, the sendmail() function is evoked.
The sendmail() function establishes a connection to the SMTP server, login commands are sent. The saved username and password are BASE64 encoded and submitted to the SMTP server. Commands are sent to the SMTP server to send an email based on the saved configuration values. The new external IP address is included in the email. Once the email is sent, the connection to the SMTP server is severed. The sendmail() function ends and the sketch returns to the loop() function. After each command is sent to the SMTP server, the response is pushed out of the Arduino serial port to help with troubleshooting. The sketch waits for a reply from the SMTP server before moving to the next command. If the server does not respond, a timeout will occur. The sketch will continue to run if a timeout occurs. A configurable delay occurs and the loop repeats.
An LCD display is attached to the Arduino to display the current external IP address and various connection states. The LCD is driven by a 74HC595 shift register to conserve pins on the Arduino.
I didn't put any effort into protecting the user from themselves. It is possible that if significantly long strings are entered for the parameters, a memory overflow could occur or the entire EEPROM could be filled up. These scenarios shouldn't occur under normal use but are possible.
If I were to start this project today, I would use a Raspberry Pi or ESP8266 instead of an Arduino with an Ethernet Shield. The Raspberry Pi is about half the cost and offers some other interesting functionality the ESP8266 is even cheaper but a bit more difficult to program. At the time this project was originally done, the Raspberry Pi was fairly new and a somewhat difficult to obtain, the ESP8266 didn't exist (at least I wasn't aware of it). One day in the future I may port this application...Read more »