0

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:

  1. Add "#include "Dhcp.h" to the beginning of your Arduino sketch.
  2. Declare a variable of type Dhcp (e.g. "Dhcp dhcp;").
  3. 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.

  • 1
    You should contact the author of the library and ask _him_. Not us. There's a contact link right on that page. – Lightness Races in Orbit Apr 09 '19 at 23:46
  • _"Declare a variable of type Dhcp (e.g. "Dhcp dhcp;")."_ But you did not do that. Why not? – Lightness Races in Orbit Apr 09 '19 at 23:47
  • `server.available();` What's that supposed to do? `server` is an array of `byte`. Though this error comes from code you did not even show us. – Lightness Races in Orbit Apr 09 '19 at 23:48
  • @LightnessRacesinOrbit Well.. i follow the instruction in a book called "Arduino Cookbook". The code is just a copy and i know that he did not declares a variable called Dhcp and for that reason comes the problem. I just tried multiple things to fix it but i can't find how. – Henrique Fonseca Veloso Apr 09 '19 at 23:52
  • 1
    But you didn't follow the instruction. The instruction says to declare a variable. You did not do that. You can read about how to declare a variable in your C++ book. – Lightness Races in Orbit Apr 09 '19 at 23:53
  • @LightnessRacesinOrbit i just cut the code because it's too big to show.. The problem comes from the early stages of it with the library created by a random user from internet and used by a book. I really tried to fix this for a few hours but my C language isn't good – Henrique Fonseca Veloso Apr 09 '19 at 23:54
  • i already know that but i don't know too well the language. Like i say im new at programming C. The code really is missing the variable of Dhcp but i dont know how to put it. I already tried the things like: "ïnt", "char", "word","byte".. Stuffs like that but it didn't. – Henrique Fonseca Veloso Apr 09 '19 at 23:58
  • You're going to have to learn the basics of the language before programming in it; there's no way around that. For starters, C and C++ are two different languages; this is the latter, not the former. You can pick up a book on C++ programming [here](https://stackoverflow.com/q/388242/560648). Good luck! – Lightness Races in Orbit Apr 10 '19 at 00:29
  • _" i just cut the code because it's too big to show"_ That is not what was instructed in the documentation about "[MCVE]". You need to follow instructions! – Lightness Races in Orbit Apr 10 '19 at 00:30
  • I can show the proggraming with no problems.. i just think its useless because it can't even start and not reproduce.. This is for an arduino so its kind diferent. I know that this is a basic thing but i cant find anywhere to say how to fix it. – Henrique Fonseca Veloso Apr 10 '19 at 00:34
  • You just need to declare a variable. If you begin reading one of the introductory C++ books I linked you to, it'll be taught to you in the first few chapters. I highly recommend it. – Lightness Races in Orbit Apr 10 '19 at 00:37
  • The thing is i already tried that.. i tried every single combination that i know and do not work.. Can you please suggest something. i already out of ideas – Henrique Fonseca Veloso Apr 10 '19 at 00:41
  • Learn the language, from a book. Good luck. – Lightness Races in Orbit Apr 10 '19 at 00:46

0 Answers0