|
楼主 |
发表于 2012-8-31 20:07:59
|
显示全部楼层
本帖最后由 zcbzjx 于 2012-8-31 22:37 编辑
这个例子比较适合开始用enc28J60的。你最好介绍下你的网络环境,是固定ip,还是自动获取ip地址。从你的代码来看,你对网络好似一点不懂。我贴的代码只是针对我批量的嘶吼用的,所以我只要改每个设备的mac和ip地址,而你要用的话,则mac ip gateway都要根据自己的实际情况更改了。
建议你看下这2个例子:
第一个例子是固定ip地址的。- // This demo does web requests via DNS lookup, using a fixed gateway.
- // 2010-11-27 <[email protected]> http://opensource.org/licenses/mit-license.php
- #include <EtherCard.h>
- #define REQUEST_RATE 5000 // milliseconds
- // ethernet interface mac address
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- // ethernet interface ip address
- static byte myip[] = { 192,168,1,203 };
- // gateway ip address
- static byte gwip[] = { 192,168,1,1 };
- //dns ip address
- static byte mydns[] = { 192,168,1,1};
- // remote website name
- char website[] PROGMEM = "google.com";
- byte Ethernet::buffer[300]; // a very small tcp/ip buffer is enough here
- static long timer;
- // called when the client request is complete
- static void my_result_cb (byte status, word off, word len) {
- Serial.print("<<< reply ");
- Serial.print(millis() - timer);
- Serial.println(" ms");
- Serial.println((const char*) Ethernet::buffer + off);
- }
- void setup () {
- Serial.begin(57600);
- Serial.println("\n[getViaDNS]");
-
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
- ether.staticSetup(myip, gwip, mydns);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
-
- timer = - REQUEST_RATE; // start timing out right away
- }
- void loop () {
- ether.packetLoop(ether.packetReceive());
-
- if (millis() > timer + REQUEST_RATE) {
- timer = millis();
- Serial.println("\n>>> REQ");
- ether.browseUrl(PSTR("/foo/"), "bar", website, my_result_cb);
- }
- }
复制代码 第二个例子是自动获取ip地址的- // This demo does web requests via DHCP and DNS lookup.
- // 2011-07-05 <[email protected]> http://opensource.org/licenses/mit-license.php
- #include <EtherCard.h>
- #define REQUEST_RATE 5000 // milliseconds
- // ethernet interface mac address
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- // remote website name
- char website[] PROGMEM = "google.com";
- byte Ethernet::buffer[700];
- static long timer;
- // called when the client request is complete
- static void my_result_cb (byte status, word off, word len) {
- Serial.print("<<< reply ");
- Serial.print(millis() - timer);
- Serial.println(" ms");
- Serial.println((const char*) Ethernet::buffer + off);
- }
- void setup () {
- Serial.begin(57600);
- Serial.println("\n[getDHCPandDNS]");
-
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
- if (!ether.dhcpSetup())
- Serial.println("DHCP failed");
-
- ether.printIp("My IP: ", ether.myip);
- // ether.printIp("Netmask: ", ether.mymask);
- ether.printIp("GW IP: ", ether.gwip);
- ether.printIp("DNS IP: ", ether.dnsip);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
-
- timer = - REQUEST_RATE; // start timing out right away
- }
- void loop () {
- // DHCP expiration is a bit brutal, because all other ethernet activity and
- // incoming packets will be ignored until a new lease has been acquired
- if (!ether.dhcpValid() && !ether.dhcpSetup())
- Serial.println("DHCP failed");
-
- ether.packetLoop(ether.packetReceive());
-
- if (millis() > timer + REQUEST_RATE) {
- timer = millis();
- Serial.println("\n>>> REQ");
- ether.browseUrl(PSTR("/foo/"), "bar", website, my_result_cb);
- }
- }
复制代码 主要看setup和前面的变量定义部分。 |
|