在代码中,有一个无需设置即可帮助获取动态IP的库。但是问题是他们没有指定如何正确使用它,而且我不知道如何解决它,因为在arduino和C上我是新手..
可能这是一个愚蠢的问题。
代码的源代码在一本书中:“ Arduino Cookbook”
这里是我尝试并遵循的部分:
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(;;)
;
}
}
“如果要在串行监视器上查看从DHCP服务器返回的值,请使用以下设置功能:”
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");
}
}
这是我在包含图书馆的网站上收到的说明:
有关完整内容,可以使用此链接: LINK
PS:下载文件为最新版本(v0.4)
这是代码:
#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();
}
错误问题是:
/*
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}'
*/
对不起,我英语不好。我是BR
已编辑:
我从DHCP开发人员那里找到了一些代码,但是仍然有同样的问题。.Dhcp没有在他的代码中声明。
#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(;;)
;
}
我认为Arduino代码获得了一些更改,这些更改使该库无效,而且我认为我无法解决该问题。
这是2009年创建的。