|
|
本帖最后由 code-AR 于 2014-3-26 18:20 编辑
在淘宝上淘到了ENC28J60以太网盾模块,加上看到了张老师的,以及。
对于yeelink平台感兴趣,想用现有资源自己尝试做一个智能温度监测+远程控制开关(现在只有控制LED,下一步想弄个继电器,尝试大电器)。网络我是使用的无线路由器的LAN接口,自动获取IP地址。
先上几张完成的图吧:
实物图
监控页面
本来想上个面包板图的可惜用的软件没有ENC28J60以太网盾模块样板,就没做了。
现在上源代码:
[pre lang="arduino" line="1" file="LM-OC1"]#include <OneWire.h>
#include <EtherCard.h>
#define OUT
#define TIME1 2000
#define TIME2 15000
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x31 };//MAC地址只要与网络上设备不冲突就行
char website[] PROGMEM = "api.yeelink.net";
char urlBuf2[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";//填写自己的传感器和设备信息
char urlBuf1[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/";
char apiKey[] PROGMEM = "U-ApiKey:xxxxxxxxxxxxxxxxxxx";//填写自己的API key
byte Ethernet::buffer[700];
static long timer;
static void my_result_cb2 (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
String switchStatus;
static void my_result_cb1 (byte status, word off, word len) {
String reply=(const char*)Ethernet::buffer + off + 354;
Serial.println(reply);
switchStatus = reply.substring(reply.length()-2,reply.length()-1);
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.print("Switch Status:");
Serial.println(switchStatus);
Serial.println();
}
void setup () {
Serial.begin(57600);
if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)) //自动获取IP地址
Serial.println( "Failed to access Ethernet controller");
else
Serial.println("Ethernet controller initialized");
Serial.println();
if (!ether.dhcpSetup())
Serial.println("Failed to get configuration from DHCP");
else
Serial.println("DHCP configuration done");
ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Netmask:\t", ether.mymask);
ether.printIp("Gateway:\t", ether.gwip);
Serial.println();
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
else
Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
timer = - TIME1;
timer = - TIME2;
}
void loop () {
digitalWrite(4,LOW);
ether.packetLoop(ether.packetReceive());
String reply;
if (millis() > timer + TIME1) {
timer = millis()+1000;
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
Serial.println("\n>>> REQ");
ether.browseUrl(urlBuf1, "datapoints", website,apiKey, my_result_cb1);
}
if(switchStatus=="0")
{
digitalWrite(4,LOW);
}
if(switchStatus=="1")
{
digitalWrite(4,HIGH);
}
ether.packetLoop(ether.packetReceive());
if (millis() > timer + TIME2) {
timer = millis();
Serial.println("\n>>> REQ");
static char buf[20];
get_send_string(buf);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
ether.httpPost (urlBuf2, website, apiKey, buf, my_result_cb2);
}
}
void get_send_string(OUT char *p){ //处理LM35的数据
int val=analogRead(0);
int data=(125*val)>>8;
uint16_t Tc_100 = data;
uint8_t whole, fract;
whole = Tc_100/1;
fract = Tc_100 % 1;
sprintf(p,"{\"value\":%d.%d}",whole,fract);
}
[/code]
以上代码,有参考张老师的。同时在制作该项目中也有老师的帮助,在这里感谢帮助O(∩_∩)O!!!
程序编写可能还不够简洁,希望大家可以提出建议。
应要求添加库文件 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|