Set/Change IP Address Web Form - Ethernet Shield

Tried doing someting like this, but I do not think it does anything at this point?

int ipA_READ = EEPROM.read( ip[0] );
int ipB_READ = EEPROM.read( ip[1] );
int ipC_READ = EEPROM.read( ip[2] );
int ipD_READ = EEPROM.read( ip[3] );

int subnetA_READ = EEPROM.read( subnet[0] );
int subnetB_READ = EEPROM.read( subnet[1] );
int subnetC_READ = EEPROM.read( subnet[2] );
int subnetD_READ = EEPROM.read( subnet[3] );

int gwA_READ = EEPROM.read( gateway[0] );
int gwB_READ = EEPROM.read( gateway[1] );
int gwC_READ = EEPROM.read( gateway[2] );
int gwD_READ = EEPROM.read( gateway[3] );

  //if default IP address is not set then overwrite???
  // would need to be if LAST IP address set is different

  if ( ip[0] != 172 ) {
       EEPROM.write( ip[0], ipA ); 
  }
  if ( ip[1] != 31 ) {
       EEPROM.write( ip[1], ipB ); 
  }
  if ( ip[2] != 1 ) {
       EEPROM.write( ip[2], ipC ); 
  }    
  if ( ip[3] != 50 ) {
       EEPROM.write( ip[3], ipD ); 
  }
  
  if ( subnet[0] != 255 ) {
       EEPROM.write( subnet[0], subnetA ); 
  }
  if ( subnet[1] != 255 ) {
       EEPROM.write( subnet[1], subnetB ); 
  }
  if ( subnet[2] != 255 ) {
       EEPROM.write( subnet[2], subnetC ); 
  }    
  if ( subnet[3] != 0 ) {
       EEPROM.write( subnet[3], subnetD ); 
  }
  
  if ( gateway[0] != 172 ) {
       EEPROM.write( gateway[0], gwA ); 
  }
  if ( gateway[1] != 31 ) {
       EEPROM.write( gateway[1], gwB ); 
  }
  if ( gateway[2] != 1 ) {
       EEPROM.write( gateway[2], gwC ); 
  }    
  if ( gateway[3] != 1 ) {
       EEPROM.write( gateway[3], gwD ); 
  }

To be continued and ideas most welcome...

Snapshot of Network Settings Web Form

Snapshot - Form IP Changer.JPG

For anyone following this, I managed to complete the form and it is fully operable now, setting IP/Subnet/Gateway from a web from, storing it to EEPROM.

The EEPROM, I was struggling with b/c it has to be placed precisely in specific areas in order to overwrite the existing values

Here is an example of setting the IP address, I will post the entire form code later with the hopes that someone (an expert, maybe Arduino) will improve upon it.

Example reading/writing eeprom:

 int ipA_READ = EEPROM.read( ip[0] );
  int ipB_READ = EEPROM.read( ip[1] );
  int ipC_READ = EEPROM.read( ip[2] );
  int ipD_READ = EEPROM.read( ip[3] );
 
         ipA=nA_IP; 
           EEPROM.write( ip[0], ipA );        
         ipB=nB_IP;  
           EEPROM.write( ip[1], ipB );       
         ipC=nC_IP;
           EEPROM.write( ip[2], ipC );         
         ipD=nD_IP;
           EEPROM.write( ip[3], ipD );

Hi, I'm very interested in the complete code of this sketch. As you can see in my own post, I'm working on this too:
http://arduino.cc/forum/index.php/topic,55044.0.html

Now maybe we can merge both our sketch. You'll get credit for the code and I'll get credit for the layout :smiley:
The only thing I'm struggling with, is the forms and the way to submit them.

As promised, attached is the pde. I have tested this against an UNO and a DFRduino ATMega328 (Duemilanove) . If anyone improves upon this keep me posted..

Network_Settings_Web_Control_Form.pde (21 KB)

Ok, got your code, reading it right now. :astonished:

Did you change Ethernet.cpp to get this working? And what exactly did you change?

The first thing I'll be doing is to try remaking you sketch, so I understand everything happening. Then I'll put all your HTML in Flash memory, so we have more SRAM. Also I want to have a separate SETUP page, which won't be to hard, because I bought the Arduino Cookbook (amazone kindle for ipad). It has a sketch that just does that.

This way you can have the SETUP page at http://IP-address/setup and your normal webpage at http://IP-address.

If you don't mind, I'll be posting this in my own Topic and If I make progress I'll post it here too.

So this is the idea I would use (this is not a sketch):

READ EEPROM "TYPE"
IF "type" = "" (empty)
THEN USE PRESET (IP=192.168.1.2, MASK=255.255.255.0,GW=192.168.1.2,MAC=DE:AD:BE:EF:FE:ED)
ELSE
IF "type" = "STATIC"
IF "IP" = "" (empty)
USE PRESET AND SET TO USE AND PUBLISH
ELSE READ EEPROM "IP" AND SET TO USE AND PUBLISH

IF "MASK" ="" (empty)
THEN USE PRESET AND SET TO USE AND PUBLISH
ELSE READ EEPROM "MASK" AND SET TO USE AND PUBLISH

IF "GW" = "" (empty)
THEN USE PRESET AND SET TO USE AND PUBLISH
ELSE READ EEPROM "GW" AND SET TO USE AND PUBLISH

IF "MAC" = ""(empty)
THEN USE PRESET AND SET TO USE AND PUBLISH
ELSE READ EEPROM "MAC" AND SET TO USE AND PUBLISH

ELSE USE DHCP AND SET TO USE AND PUBLISH (on the setuppage)

START WEBPAGE
IF URL= IP
GOTO WEBPAGE (made by user, and what if user wants multiple pages?)
ELSE
IF URL = IP/setup (like in http://192.168.1.2/setup)
GOTO SETUPPAGE
ELSE SHOW ERROR PAGE

SETUPPAGE
USE TYPE, IP, MASK, GW, MAC AND PUBLISH
SHOW FILLOUT FORM FOR TYPE, IP, MASK, GW, MAC AND PUBLISH
SHOW SUBMIT BUTTON

WEBPAGE
USE TYPE, IP, MASK, GW, MAC
SHOW WEBPAGE (made by user)

SUBMIT BUTTON
WRITE TYPE, IP, MASK, GW, MAC AND PUBLISH TO EEPROM
RESET ARDUINO

I had to change the Ethernet.cpp file b/c I noticed gateway was being set as IP then changing last octet only and subnet was statically set here as well.

Before:
//uint8_t gateway[4];
//gateway[0] = ip[0];
//gateway[1] = ip[1];
//gateway[2] = ip[2];
//gateway[3] = 1;

//uint8_t subnet[] = {
//255, 255, 255, 0 };

After:

#include "w5100.h"
#include "Ethernet.h"

uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
  0, 0, 0, 0 };
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 
  0, 0, 0, 0 };

void EthernetClass::begin(uint8_t *mac, uint8_t *ip, uint8_t *gateway)
{
  begin(mac, ip, gateway);
}

void EthernetClass::begin(uint8_t *mac, uint8_t *ip, uint8_t *gateway, uint8_t *subnet)
{
  W5100.init();
  W5100.setMACAddress(mac);
  W5100.setIPAddress(ip);
  W5100.setGatewayIp(gateway);
  W5100.setSubnetMask(subnet);
}

EthernetClass Ethernet;

@Jo3ri,
Fun an Arduino "basic" sketch :slight_smile:

@robtillaart

I know :slight_smile: But it really helps to write down this kinds of things. Otherwise I get lost in my own coding :~

Update: 1st through 3rd IP octet is not saving correctly. Working on a fix.

Meanwhile I found out your working on a different Arduino version (mine is 0.22) also I have the new Arduino Ethernet Shield. But the Idea remains the same.

FYI: I thanks to your sketch, I managed to use EEPROM for storing mac, ip, gw, sub and type (static, dhcp or unknown) and showing this info in the webpage.

The sketch starts with setting the pre-sets the first time and writes a knowing bit. The next time it runs, the sketch doesn't use pre-sets, but reads EEPROM: when type is stored as static, it takes static otherwise it takes dhcp.

Now I need to work on the GET forms in html. I haven't found out how to do that yet, but I'll get there.

I am using 022? Sounds like good progress.

Ah, then it must be a different Ethernet Shield. :slight_smile:

Now you're sending HTML in a different way then I do, I use:

Client.print("some text or other HTML code");

Now I was hoping to get help out of your sketch for the forms part, but I can't :~

I'll have to figure it out myself :frowning:

I started with client.print as well and used all of the SRAM doing so.

My sketch is using the Flash library which easily puts all that html into program memory, dramatically improving performance.

Aha, I See. Well I found a way to do the same, but I'm using the avr/pgmspace.h library. This way I can put every thing in The Flash Memory with PROGMEM and I let it flow through a buffer of let's say 100 bytes (SRAM).

This should mean I could use your HTML code for the forms. Could you filter them out and post them? Especially t he part where you get the info out of the forms.

Thanks.

Here is the basic form, you will see from my code that these are simply wrapped in

client << F("       ") << "\n";

You can accomplish the same with Progmem, not sure which is more memory efficient. I struggled freeing memory for a while and this worked best for me, at least it seemed the easier method. I will be interested to see your take on this as well.

When the form is posted, a get string is created such as this:

http://172.31.1.50/?i1=172&i2=31&i3=4&i4=50&s1=255&s2=255&s3=255&s4=0&g1=172&g2=31&g3=1&g4=1&END=END

That string is saved to inString initially then to stringOne:

String stringOne = inString;

For substring processing I am using indexOf to find the number between all of the & and = symbols.

//////////////     CHANGE IP ADDRESS FORM /////////////////////////////////////////////////////



<B>Change IP Address:</B>

<form name=\"changeipaddress\" action=\"\" method=\"get\">
<TABLE width=\"325\">
<tr>     
<td>IP Address:</td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"i1\" value=\"172\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"i2\" value=\"31\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"i3\" value=\"1\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"i4\" value=\"50\"></td>     
</tr>

<tr>
<td>Subnet Mask:</td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"s1\" value=\"255\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"s2\" value=\"255\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"s3\" value=\"255\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"s4\" value=\"0\"></td>     
</tr>

<tr>
<td>Gateway:</td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"g1\" value=\"172\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"g2\" value=\"31\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"g3\" value=\"1\"></td>     
<td><input type=\"text\" size=\"3\" maxlength=\"3\" name=\"g4\" value=\"1\"></td>     
<td><input type=\"hidden\" size=\"3\" maxlength=\"3\" name=\"END\" value=\"END\"></td>     
</tr>
         //notice the hidden form element placed in order to create a final "&" character in the get string

<tr>
<td align=\"center\" colspan=\"5\"><input type=\"submit\" value=\"Update Network Configuration\"></td>
</tr>     

<tr>
<td align=\"center\" colspan=\"5\">*After changing IP address
change url in browser</td>
</tr>     
</TABLE>     
</form>

For any followers, this project is in collaboration with this one: http://arduino.cc/forum/index.php/topic,55044.0.html

If your sending your project to someone with no development experience, maybe your dad, or a potential client you've built a prototype for, or your using your Arduino in multiple locations (and not all have DHCP servers), then you just want to be able use the product out of the box on a network, like a home router, plug and play, this will be the solution.

The revised web form code is now working properly (for me at least), see attachments.

Network_Settings_Form.pde (21 KB)

techadmin:
The revised web form code is now working properly (for me at least), see attachments.

To bad it doesn't work on my Uno and latest ethershield (microSD reader).
Can't ping the IP adres.

If I Disabling these settings I can ping the arduino but can't access the webpages:

//EEPROM address to array
// byte mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// byte ip[4] = { ipAaddress_READ, ipBaddress_READ, ipCaddress_READ, ipDaddress_READ };
// byte gateway[4] = { gwAaddress_READ, gwBaddress_READ, gwCaddress_READ, gwDaddress_READ };
// byte subnet[4] = { subnetAaddress_READ, subnetBaddress_READ, subnetCaddress_READ, subnetDaddress_READ };

byte mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

Do you have multiple Arduino's with the same MAC address? if so change them asap as they should be unique,

just a thought.