About String Expressions for IP Adressess

Hello, I'm using Leonardo ETH for web server applications, for temperature monitoring. You know, device is using like this codes;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xE3, 0x29 };

IPAddress ip(192,168,1,149); EthernetServer server(80);

When I write this IP number to the browser, I can see the HTML file and on-line temperature value. But I don't want to write IP number to the browser, I want to any small string expression (like ; 9A) for the browser. Is that possible? This string expressions are the local name for the arduino board. In this way, local name is more useful then IP number.Thank you.

It’s not clear to me what you’re asking, but maybe this function will be of use:

// convert IPAddress to char array and put it in the passed buffer
void IPtoa(const IPAddress & IP, char IPcharBuffer[]) {
#if defined(__ARDUINO_X86__)
  sprintf (IPcharBuffer, "%u", IP[0]);
#else  //defined(__ARDUINO_X86__)
  utoa(IP[0], IPcharBuffer, 10);  //convert the first octet
#endif  //defined(__ARDUINO_X86__)
  for (byte octetCount = 1; octetCount < 4; octetCount++) {  //convert the other 3 octets
    strcat(IPcharBuffer, ".");
    char octetChar[3 + 1];  //3 digit byte + null terminator
#if defined(__ARDUINO_X86__)
    sprintf (octetChar, "%u", IP[octetCount]);
#else  //defined(__ARDUINO_X86__)
    utoa(IP[octetCount], octetChar, 10);  //convert the octet
#endif  //defined(__ARDUINO_X86__)
    strcat(IPcharBuffer, octetChar);
  }
}

I left in all the preprocessor conditionals for support of architectures other than AVR so it’s kind of ugly.

To use it, you create a char array large enough to hold the IP address string, plus one for the terminator, and pass it to the function. After that, the array will contain the string representation of the IP address:

#include <IPAddress.h>

void setup() {
  Serial.begin(9600);
  IPAddress ip(192, 168, 1, 149);
  char IPstring[16];
  IPtoa(ip, IPstring);
  Serial.println(IPstring);
}

void loop() {}

// convert IPAddress to char array and put it in the passed buffer
void IPtoa(const IPAddress & IP, char IPcharBuffer[]) {
#if defined(__ARDUINO_X86__)
  sprintf (IPcharBuffer, "%u", IP[0]);
#else  //defined(__ARDUINO_X86__)
  utoa(IP[0], IPcharBuffer, 10);  //convert the first octet
#endif  //defined(__ARDUINO_X86__)
  for (byte octetCount = 1; octetCount < 4; octetCount++) {  //convert the other 3 octets
    strcat(IPcharBuffer, ".");
    char octetChar[3 + 1];  //3 digit byte + null terminator
#if defined(__ARDUINO_X86__)
    sprintf (octetChar, "%u", IP[octetCount]);
#else  //defined(__ARDUINO_X86__)
    utoa(IP[octetCount], octetChar, 10);  //convert the octet
#endif  //defined(__ARDUINO_X86__)
    strcat(IPcharBuffer, octetChar);
  }
}

Hello,
Thank you, ok, I’ll ask again more clear; My application code is like this (Arduino Leonardo ETH+LM35 temp measurement) with web server application;

#include <SPI.h>
#include <Ethernet2.h>

byte mac[] = {
   0x90, 0xA2, 0xDA, 0x10, 0xE3, 0x29
};
IPAddress ip(192, 168, 1, 149);
EthernetServer server(80);


int sicaklik=0;                                     
const int sicaklik_pin=A0;

void setup() {
  
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void olcum()                                            
{

sicaklik=analogRead(sicaklik_pin);
sicaklik=sicaklik*0.48828125;
delay(1000);

}

void loop() {

   olcum();
   
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
 
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);

        if (c == '\n' && currentLineIsBlank) {
        
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  
          client.println("Refresh: 10");  
          client.println();
          client.println("<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>");
          client.println("<html xmlns=http://www.w3.org/1999/xhtml lang=tr-TR>");
          client.println("<head profile=http://gmpg.org/xfn/11>");
         
client.println("<META http-equiv=content-type content=text/html; charset=windows-1254>");
client.println("<style type=text/css>");
client.println(".style1{text-align: center;font-size: xx-large;}");
client.println("</style>");
client.println("</head>");

client.println("<body>");
client.println("<div class=style1>");
client.println(sicaklik);
client.println("</div>");
client.println("</body>");   
client.println("</html>");
          break;
        }
        if (c == '\n') {
         
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
       
          currentLineIsBlank = false;
        }
      }
    }
    
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Where can I change string expression instead of IP adress. I don’t want to write 192.168.1.149 IP number to web browser, I want to write 9A string expression to webbrowser. Thanks…

Are you saying that you want to access the Arduino from the URL bar of your browser using a domain name (e.g. http://9a.com) rather than an IP address?

Hello, If I write any domain like (http://9a.com) it is not different (192.168.1.149) from its confusion. I want too basic description like 9A. Is that possible? When I enter this 9A to web browser, it must be automatically connect 192.168.1.149 IP device and show the temperature value.

From your own computer you could add an entry to the hosts file eg 192.168.1.149 9a.com

However this only works from your own computer when connected to your own network.

If you want everyone to be able to access it you would need to register a domain name (9a.com is already taken, as are all the 2 and 3 letter .com domains) then set up port-forwarding on your router to direct your external IP address to the 192.168.1.149 internal address.