Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« on: March 11, 2011, 06:08:16 pm » |
When you use #include <SPI.h> #include <Ethernet.h> byte ip[] = { 192, 168, 14, 85}; in your sketch for a web page. How do I get the byte ip[] shown in the web page? client.print(ip[]) is not the right way, but I can't figure out what is should be
|
|
|
|
« Last Edit: March 12, 2011, 02:28:24 pm by JO3RI »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #1 on: March 11, 2011, 07:32:39 pm » |
Have you tried something like this: client.print(ip[0]); client.print("."); client.print(ip[1]); client.print("."); client.print(ip[2]); client.print("."); client.print(ip[3]);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6546
Arduino rocks
|
 |
« Reply #2 on: March 11, 2011, 07:50:34 pm » |
Not sure what you are trying to do. Do you want an arduino web server to include the IP address in the returned web page?
|
|
|
|
|
Logged
|
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #3 on: March 12, 2011, 03:36:11 am » |
Do you want an arduino web server to include the IP address in the returned web page? Yep that's what I'm trying to do. When you visit the webpage on the arduino, it should show in html the IP-address of the Arduino webserver (ethernet Shield) The webpage I'm working on will be a setup-page where you can change the IP address of you Ethernet Shield, but for now I want to know how you can show the actual IP. http://arduino.cc/forum/index.php/topic,55044.0.html
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6546
Arduino rocks
|
 |
« Reply #4 on: March 12, 2011, 10:39:17 am » |
You may want to look at the DHCP code at the below site to see how info is obtained from a router. http://gkaindl.com/software/arduino-ethernet
|
|
|
|
|
Logged
|
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #5 on: March 12, 2011, 10:44:41 am » |
Ok, Understand your code (I think  ) But, what I simply want to do is, print the IP-address the arduino webservers has, on the webpage, or a 16x2 LCD screen, or de serial console.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6546
Arduino rocks
|
 |
« Reply #6 on: March 12, 2011, 10:55:07 am » |
May be too simple, but couldn't you also set the ip address as a string variable in the setup part of the code if you have already set it as the ip address to use?
|
|
|
|
|
Logged
|
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #7 on: March 12, 2011, 10:57:09 am » |
Yes true, but how? can't I just print the IP have set in byte ip[] = { 192, 168, 14, 85}; ?
|
|
|
|
|
Logged
|
|
|
|
|
Papendrecht, NL
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #8 on: March 12, 2011, 11:20:47 am » |
No, you cant display: byte ip[] = { 192, 168, 14, 85} out of the box. This is an array of 4 bytes. To make them human readable, you have to translate them to characters.
The Serial.print() function knows how to translate most simple variables to characters, i.e. byte, int, float, char, etc.
The most simple solution - already mentioned - is to store the IP address 2 times, one time as a byte array for the code and one time as a char array (i.e. string) for humans.
Another way is to write a function which translates the byte array to a string.
PaulS has shown you even another way.
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 20
Posts: 2308
|
 |
« Reply #9 on: March 12, 2011, 11:56:23 am » |
Have you tried using itoa()??
char[4] printIP;
itoa((int)ip[0], printIP, 10); Serial.print(printIP); Serial.print("."); //rinse repeat
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #10 on: March 12, 2011, 02:27:45 pm » |
YES victory. thanks bubulindo (just had to change char[4] printIP; into char printIP[4]; This sketch shows a webpage with the webservers IP-address showing. #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 14, 85}; char printIP[4]; Server server(80); void setup() { Ethernet.begin(mac, ip); server.begin(); } void loop() { Client client = server.available(); if (client) { boolean current_line_is_blank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == 'n' && current_line_is_blank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); itoa((int)ip[0], printIP, 10); client.print(printIP); client.print("."); itoa((int)ip[1], printIP, 10); client.print(printIP); client.print("."); itoa((int)ip[2], printIP, 10); client.print(printIP); client.print("."); itoa((int)ip[3], printIP, 10); client.print(printIP); break; } if (c == 'n') { current_line_is_blank = true; } else if (c != 'r') { current_line_is_blank = false; } } } delay(1); client.stop(); } }
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 20
Posts: 2308
|
 |
« Reply #11 on: March 12, 2011, 04:46:34 pm » |
Java is destroying my poor knowledge of C. I'm sorry. LOL There's an example on the Networking section of the forum, showing how to print the IP address: for (int n = 0; n<=3; n++) { client.print((int)client_ip[n]); client.print("."); }
The thread is this one: http://arduino.cc/forum/index.php/topic,54378.0.htmlGive it a try and see which is smaller.
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #12 on: March 12, 2011, 05:12:52 pm » |
The above doesn't seem to work, it shows numbers, but not the ip address. Your other solution does work.  Now I'm trying to show the MAC-address, but byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; gives me 222:173:190:239:254:237 which is not hexadecimal, but decimal. I know this is basic stuff, but it's late. Good night 
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 20
Posts: 2308
|
 |
« Reply #13 on: March 13, 2011, 01:42:27 am » |
use itoa(mac[0], string_mac, 16); but you need to add the 0x to the string before you print to the screen.
I haven't got my arduino here with me, but it worked for me at home. :S
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Hamme, Belgium
Offline
Sr. Member
Karma: 3
Posts: 383
|
 |
« Reply #14 on: March 13, 2011, 07:05:29 am » |
|
|
|
|
|
Logged
|
|
|
|
|
|