zcbzjx 发表于 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;   // 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");

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;
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");

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和前面的变量定义部分。

JUST_DO_IT 发表于 2012-8-31 21:05:32

网络环境

本帖最后由 JUST_DO_IT 于 2012-8-31 21:16 编辑

rt..............

zcbzjx 发表于 2012-8-31 23:35:23

本帖最后由 zcbzjx 于 2012-8-31 23:40 编辑

更新一楼代码为自动获取ip地址和用dns访问服务器,郝老板能不能做个和github功能一样的东西啊,让我们可以开源并更新代码啊,那个维基是不是有这个功能呢,能不能开放点权限嘞,我把沙盒更改了,郝老板记得去看看哦,有好东西看哦!

弘毅 发表于 2012-8-31 23:44:26

zcbzjx 发表于 2012-8-31 23:35 static/image/common/back.gif
更新一楼代码为自动获取ip地址和用dns访问服务器,郝老板能不能做个和github功能一样的东西啊,让我们可以开 ...

:loveliness:WIKI就可以实现部分要求。。。我看看WIKI怎么逐步开放编辑权限哈

zcbzjx 发表于 2012-9-4 13:33:51

1楼更新几张实物图片

hankjlu12 发表于 2012-9-15 21:19:57

好好学习一下

JUST_DO_IT 发表于 2012-9-18 21:46:24

那怎么获取上面的开关的数据来控制LED灯呢

zhangdeyue1 发表于 2012-9-23 15:57:21

楼主的MAC地址是随意输入的吗

zcbzjx 发表于 2012-9-24 01:53:40

zhangdeyue1 发表于 2012-9-23 15:57 static/image/common/back.gif
楼主的MAC地址是随意输入的吗

对的,随便输入。

zhangdeyue1 发表于 2012-9-24 17:48:56

请问dataLength = sprintf(sensorData,"{\"value\":%d.%d}",whole,fract);
这一句是什么意思啊?:(

zcbzjx 发表于 2012-9-25 07:37:49

就是一个sprintf函数,你可以百度下。它的返回值是sensorData的长度。

darkorigin 发表于 2012-11-13 17:13:58

用LM35的话,是不是直接改你下面传感器数据采集函数就好?
我已经去掉了你上面的传感器IIC口定义和库文件

darkorigin 发表于 2012-11-13 17:33:41

提问下语法方面
代码1的 40行 Stash stash;
按我理解应该是定义一个 Stash 型的变量(或实例)stash 但是好像没看到哪里定义了这个变量(或实例)?求教。

zcbzjx 发表于 2012-11-13 21:29:19

darkorigin 发表于 2012-11-13 17:13 static/image/common/back.gif
用LM35的话,是不是直接改你下面传感器数据采集函数就好?
我已经去掉了你上面的传感器IIC口定义和库文件

恩,去掉就行,不过lm35属于模拟传感器,受电源影响比较大,鉴于和18b20价格差不多,建议还是用18b20,我开始也选择了lm35,后来果断放弃了。

zcbzjx 发表于 2012-11-13 21:31:07

darkorigin 发表于 2012-11-13 17:33 static/image/common/back.gif
提问下语法方面
代码1的 40行 Stash stash;
按我理解应该是定义一个 Stash 型的变量(或实例)stash 但是 ...

stash是在库函数中声明的,主要用于构建数据包。建议不要用stash,用httppost比较好。我有个例子,你看看。不过最近没研究这个库了,好似又更新了不少。
页: 1 2 [3] 4 5 6
查看完整版本: 基于18B20+enc28j60+arduino+yeelink的远程温度监控