In the code, theres a library that helps to get dynamic IP without having to set it.. But the problem is they dont specify how to use it properly and i dont know how to fix it because im new at arduino and C..
Probally this gonna be a silly question.
The source of the code is in the book called: "Arduino Cookbook"
Heres the part where i tried and follow:
15.2 Obtaining Your IP Address Automatically
Problem
The IP adress you use for the Ethernet shield must be unique on your network and you would like this to be allocated automatically. You want the Ethernet shield to obtain an IP adress from a DHCP server.
Solution
This is the sketch from Recipe 15.1 with the bolded lines added to use the DHCP library:
#if ARDUINO > 18
#include <SPI.h> // needed for Arduino versions later than 0018
#endif
#include <Ethernet.h>
#include "Dhcp.h" // add this for the DHCP library
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// IP addressing lines removed
byte server[] = { 209,85,229,104 }; // Google
Client client(server, 80);
void setup()
{
Serial.begin(9600);
if(Dhcp.beginWithDHCP(mac) == 1) // begin method returns 1 if successful
{
Serial.println("got IP address, connecting...");
delay(5000);
}
else
{
Serial.println("unable to acquire ip address!");
while(true)
; // do nothing
}
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
"If you want to see the values returned from the DHCP server on the Serial Monitor, use the following setup function:"
void setup()
{
Serial.begin(9600);
if(Dhcp.beginWithDHCP(mac) == 1) // begin method returns 1 if successful
{
byte buffer[6];
Serial.println("ip acquired...");
// show the values returned from the DHCP server
Dhcp.getLocalIp(buffer);
Serial.print("ip address: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getSubnetMask(buffer);
Serial.print("subnet mask: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getGatewayIp(buffer);
Serial.print("gateway ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDhcpServerIp(buffer);
Serial.print("dhcp server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDnsServerIp(buffer);
Serial.print("dns server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
delay(5000);
}
else
{
Serial.println("unable to acquire ip address!");
while(true)
; // do nothing
}
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
This is the instructon i receive in the website that contains the library:
- Add "#include "Dhcp.h" to the beginning of your Arduino sketch.
- Declare a variable of type Dhcp (e.g. "Dhcp dhcp;").
- Call the beginWithDHCP() method, while providing it a MAC address.
For full content, you can use this link: LINK
PS: The download file is the latest version (v0.4)
This is the code:
#include <SPI.h>
#include <Ethernet.h>
#include "Dhcp.h"
#include <string.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 209,85,229,104 };
EthernetClient client(80);
const int ledPin = 9;
String readString = String(30);
int status = 0;
void setup(){
Serial.begin(9600);
if(Dhcp.beginWithDHCP(mac) == 1){
Serial.println("got IP address, connecting...");
byte buffer[6];
Serial.println("ip acquired...");
// show the values returned from the DHCP server
Dhcp.getLocalIp(buffer);
Serial.print("ip address: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getSubnetMask(buffer);
Serial.print("subnet mask: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getGatewayIp(buffer);
Serial.print("gateway ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDhcpServerIp(buffer);
Serial.print("dhcp server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDnsServerIp(buffer);
Serial.print("dns server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
delay(5000);
} else {
Serial.println("unable to acquire ip address!");
while(true)
}
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100)
{
readString += c; // "readstring" VAI RECEBER OS CARACTERES LIDO
}
if (c == '\n') { //SE ENCONTRAR "\n" É O FINAL DO CABEÇALHO DA REQUISIÇÃO HTTP
if (readString.indexOf("?") <0){ //SE ENCONTRAR O CARACTER "?", FAZ
}
else //SENÃO,FAZ
if(readString.indexOf("ledParam=1") >0)
{
digitalWrite(ledPin, HIGH);
status = 1;
}else{ //SENÃO, FAZ
digitalWrite(ledPin, LOW);
status = 0;
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<link rel='icon' type='image/png' href='http://blogmasterwalkershop.com.br/arquivos/artigos/sub_wifi/logo_mws.png'/>");
client.println("<title>Led por Net</title>");
client.println("</head>");
client.println("<body style=background-color:#ADD8E6>");
client.println("<center><font color='blue'><h1>Test</font></center></h1>");
client.println("<h1><center>CONTROLE DE LED</center></h1>");
if (status == 1){
client.println("<center><form method=get name=LED><input type=hidden name=ledParam value=0 /><input type=submit value=APAGAR></form></center>");
}else{
client.println("<center><form method=get name=LED><input type=hidden name=ledParam value=1 /><input type=submit value=ACENDER></form></center>");
}
client.println("<center><font size='5'>Status atual do LED: </center>");
if (status == 1){
client.println("<center><font color='green' size='5'>LIGADO</center>");
}else{ //SENÃO, FAZ
client.println("<center><font color='red' size='5'>DESLIGADO</center>");
}
client.println("<hr/>");
client.println("<hr/>");
client.println("</body>");
client.println("</html>");
readString=""; //A VARIÁVEL É REINICIALIZADA
client.stop();
}
}
}
}
}
void printArray(Print *output, char* delimeter, byte* data, int len, int base){
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < len; i++){
if(i != 0)
output->print(delimeter);
output->print(itoa(data[i], buf, base));
}
output->println();
}
The error problem is:
/*
In function 'void loop()':
Rede_VPN:70:34: error: request for member 'available' in 'server', which is of non-class type 'byte [4] {aka unsigned char [4]}'
EthernetClient client = server.available();
^
exit status 1
request for member 'beginWithDHCP' in 'Dhcp', which is of non-class type 'word {aka unsigned int}'
*/
Sorry for my bad english.. Im BR
EDITED:
I find some code from the DHCP developer and yet has the same problem.. Dhcp was not declared in his code.
#include <Ethernet.h>
#include "Dhcp.h"
#include <string.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 64, 233, 187, 99 }; // Google
boolean ipAcquired = false;
Client client(server, 80);
void setup()
{
Serial.begin(9600);
Serial.println("getting ip...");
int result = Dhcp.beginWithDHCP(mac);
if(result == 1)
{
ipAcquired = true;
byte buffer[6];
Serial.println("ip acquired...");
Dhcp.getMacAddress(buffer);
Serial.print("mac address: ");
printArray(&Serial, ":", buffer, 6, 16);
Dhcp.getLocalIp(buffer);
Serial.print("ip address: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getSubnetMask(buffer);
Serial.print("subnet mask: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getGatewayIp(buffer);
Serial.print("gateway ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDhcpServerIp(buffer);
Serial.print("dhcp server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDnsServerIp(buffer);
Serial.print("dns server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
delay(3000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
else
Serial.println("unable to acquire ip address...");
}
void printArray(Print *output, char* delimeter, byte* data, int len, int base)
{
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < len; i++)
{
if(i != 0)
output->print(delimeter);
output->print(itoa(data[i], buf, base));
}
output->println();
}
void loop()
{
if(ipAcquired)
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
spinForever();
}
}
else
spinForever();
}
void spinForever()
{
for(;;)
;
}
What i think is the Arduino code gained some changes that invalid this library and i don't think i can fix that..
This was created in 2009.