Convert byte to hex. Please help!

Hello guys! In my project I want to eliminate the process of seting-up network parameters in sketch, then compile it, then upload it, every time I change the network (or router).

So, I wrote a code that takes a string from Serial Monitor, converts and stores it in eeprom as a series of 6 bytes, then in setup() function, reads back bytes from eeprom.

So far everythig works just fine, the bytes are read corectly, and according to their hex correspondents.

But after I initialize the " byte mac[6]" with those 6 bytes, compile, upload, I can't connect my arduino.

That hapens only when I allocate in my router ip address of my arduino as static. I need static ip to connect to arduino from internet.

It seems to me that, the "Ethernet.begin()", when called with all parameters, accepts as mac adress only bytes declared as HEX.

So I need your help please! How can I convert those 6 bytes (or mark them) as hex values in order to make my connection work?

Is there other solution?

So I need your help please!

Does it really need to be stated that we need to see your code?

No, I just hope that solution si something very simple...

Yes I'm sure the solution is something simple.

If only we knew what the problem was.

Thank you for your patience AWOL!
Serial Monitor shold be seted to send newline.

#include <SPI.h>
#include <Ethernet.h>  
#include <EEPROM.h>

EthernetServer server(8888);

byte mac[6];
byte ip[4];
byte gateway[4];
byte subnet[] = {255, 255, 255, 0};
                         
int pins[] =  {4, 5, 6, 7, 8, 9};       
int pinState[] = {0, 0, 0, 0, 0, 0};  
boolean newData = false; 
char parLine[100];


void setup()
  {
   Serial.begin(9600);
   
   for(int i=0;i<6; i++) {pinMode(pins[i], OUTPUT);}

   setMAC();  
   setIP(); 

   byte j = 0;
   while(j<3)
     {
      gateway[j]= ip[j];
      j++; 
     } 
   gateway[j] = 1;
   
   byte x= EEPROM.read(0);
   if(x!='#'){Serial.print("Restart device to set-up the network!");return;}

   readEEPROM(mac, 1, 6);
   Ethernet.begin(mac, ip, gateway, gateway, subnet);    
   server.begin();
   Serial.print("\n The network was successfuly initiated!");
   Serial.print("\n The PC session is finished!");
  }


void loop()
 {
  EthernetClient client= server.available();
  if(client) 
  {
    while(client.connected()) 
    {
      readHeader(client);
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      client.println("<html><body>");
      client.println("<h3>&nbspArduino Control Panel</h3>");
      client.println("<form method= 'GET'>");  

      setParams();
      setPinStates();
      for (int i=0; i<6; i++)
      {
       htmlPins(client, i);
      }
      client.println("<input type= 'submit' value= 'Update'/>");
      client.println("</form>");
      client.println("</body></html>");
      delay(1);
      client.stop();            
    }
  }
}


void setMAC()
  {
   char tmac[18];
   char tmp[4];
   char tmp2[6];
   
   Serial.print("\n Input the MAC address,then press Enter:  ");
   while(!getString(tmac, 18)){;}
   newData= false;
   byte i= 0, k= 0, l= strlen(tmac);

   while(i<=l)
      {
       tmp2[0]= '0';
       tmp2[1]= 'x';
       tmp2[2]= '\0';
       byte j= 0;

       while(tmac[i]!=':' && tmac[i]!='-' && tmac[i]!='\0')
          {
           tmp[j++]= tmac[i++];
          }
       
       tmp[j]= '\0';
       Serial.print(tmp);
       if(k!=5)Serial.print(":");
       strcat(tmp2, tmp);
       mac[k++]= ((byte)strtol(tmp2,NULL,0)); 
       i++;  
      }
   EEPROM.write(0,'#');
   byte x=0;
   for(int i=1; i<7; i++)         
     { 
      byte b= mac[x++];                        
      EEPROM.write(i,b); 
      delay(3); 
     }   
  }


void setIP()
  {
   char tip[16];
   char tmp[4];
   
   Serial.print("\n Input IP address then press Enter:  ");
   while(!getString(tip, 16)){;}
   newData= false;
   byte i= 0, k= 0, l=strlen(tip);

   while(i<=l)
      {
       byte j= 0;

       while(tip[i]!='.' && tip[i]!='\0')
          {
           tmp[j++]= tip[i++];
          }
       
       tmp[j]='\0';
       ip[k++]= ((byte)strtol(tmp,NULL,0));
       i++;  
      }
    
    for(int i=0;i<4;i++)
       {
        Serial.print(ip[i]);
        if(i!=3)Serial.print(".");
       }
    byte x= 0;
    for(int i= 7;i<= 10; i++)        
     {     
      byte b= ip[x++];                    
      EEPROM.write(i,b); 
      delay(3);
     }    
 
  }


boolean getString(char *s, byte l)
   {
    static byte x= 0;
    char n='\n';
    char ch;
    while(Serial.available()>0 && newData==false)
       {
        ch= Serial.read();
        if(ch!= n)
          {
           s[x]= ch;
           x++;
           if(x>= l)
             {
              x= l-1;
              }
           }
        else
           {
            s[x]='\0';
            x= 0;
            newData= true;
            return true;
            }
        }
     return false;     
    }



void readEEPROM(byte *arr,int s,int e)
 {
  byte b, x= 0;
  int i=s;
  
  while(i<=e)
    {
     b=EEPROM[i++];
     arr[x++]= b;
    }
 }
 

void readHeader(EthernetClient client)
{
  char ch;
  int i = 0;
  while (ch!='\n')
  {
    if (client.available())
    {
     ch = client.read();
     parLine[i] = ch;
     i ++;
    }
  }
  parLine[i] ='\0';
}


void setParams()
{
 if(parLine[5]=='?')
   {
    for (int i=0; i<6; i++)
       {
        pinState[i] = valueOfParam(i + '0');
       }
   }
}


int valueOfParam(char param)                       
{
  int l= strlen(parLine);
  for (int i = 6; i<l; i++)
  {
    if (parLine[i]==param && parLine[i+1]== '=')  
    {                                            
      return(parLine[i+2]- '0');                 
    }                                            
  }                                              
  return 0;                                      
}


void setPinStates()
 {
  for (int i=0; i<6; i++) {digitalWrite(pins[i], pinState[i]);}
 }


void htmlPins(EthernetClient client,int i)
{
  client.print("<p>&nbspD");
  client.print(pins[i]);  
  client.print("&nbsp");
  client.print(" <select name='");
  client.print(i);
  client.println("'>");
  client.print("<option value='0'");
  if (pinState[i] == 0)
  {
    client.print(" selected");
  }
  client.println(">Off</option>");
  client.print("<option value='1'");
  if (pinState[i] == 1)
  {
    client.print(" selected");
  }
  client.println(">On</option>");
  client.println("</select></p>");  
}

Serial Monitor shold be seted to send newline.

No. YOU need to show us what YOU sent to the Arduino (or, more importantly, what it thinks it received), what you stored in EEPROM, what you read from EEPROM,etc. AND explain what the problem is.

Your readEEPROM() thinks that EEPROM is an array. It never actually calls the EEPROM.read() method. Why not?

you enter the numbers as decimal or with 0x prefix?

If \c base is zero or 16, the string may then include a \c "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is \c '0', in which case it is taken as 8 (octal).

Hello guys! I appolagize for my absence, I couldn't login to my account, and in weekend I've been extremely bussy.

After upload above sketch, I open Serial Monitor, first I input the mac address in this standard format: "DE:AD:BE:EF:FE:ED" then ip address in this format: 192.168.100.125

The string "DE:AD:BE:EF:FE:ED" is splitted in subsrings, like "DE", "AD", etc. each substring is concatenated with "0x" so each looks like: "0xDE", "0xAD", etc. then I use this statement: "mac[k++]= ((byte)strtol(tmp2,NULL,0)); " to convert each new subtring to its decimal value.

Then I store these values as bytes in EEPROM, I read them back to initialize each mac[] element, then I initialize the network.

If I setup a static address in my router with above mac and ip address, I can't connect with my arduino.

If I use just DHCP, it works. Something's wrong with mac values

Still, I used some online hex to decimal converters and I found that all the values read back from EEPROM were correct according with the data from Serial Monitor. Is my conversion wrong?

If I use just DHCP, it works. Something's wrong with mac values

The MAC values define a unique address for the shield. As long as there is only one device with a given MAC address connected to a network at one time, the actual MAC addresses do not matter.

The difference between the static IP address and the dynamic IP address is that the device that assigned the dynamic IP address knows that the device it assigned it to exists and can inform other devices.

With a static IP address, the router may, or may not, know that the Arduino exists, and is using that IP address. If it knows, you can connect to the Arduino from any other device on the LAN. It it does not know that the Arduino exists, with that IP address, it will NOT route packets to that IP address, even if it exists and is working.

why do you copy the entered IP address into gateway IP address?

Thank you PaulS! That meas that everything that matters is the port used for port forwarding, am I wright? If my home's router allows outband connections on the port I chose, data packets will be sent just for device configured on that port, no matter IP and MAC ?

If is so, I lost so much time, be cause of some tutorials about port forwarding, where they say that first thing to do is to set-up a static IP address if you want your device to be accessed from internet.

Juraj, I've copied just first 3 bytes of IP, then the last is "1". (that's my router's gateway).