[新人帖]yeelink平台下,LM35温度上传+开关控制LED
本帖最后由 code-AR 于 2014-3-26 18:20 编辑在淘宝上淘到了ENC28J60以太网盾模块,加上看到了张老师的ENC28J60利用ethercard库httpPost函数发送数据到yeelink或者乐联网
http://www.geek-workshop.com/thread-1824-1-1.html
,以及【第二弹】基于dht22+dsm501A+enc28J60+arduino+lcd1602+yeelink+实现家居环境监控
http://www.geek-workshop.com/thread-1747-1-1.html
。
对于yeelink平台感兴趣,想用现有资源自己尝试做一个智能温度监测+远程控制开关(现在只有控制LED,下一步想弄个继电器,尝试大电器)。网络我是使用的无线路由器的LAN接口,自动获取IP地址。
先上几张完成的图吧:
实物图
监控页面
本来想上个面包板图的可惜用的软件没有ENC28J60以太网盾模块样板,就没做了。
现在上源代码:
#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;
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;
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);
}
以上代码,有参考张老师的。同时在制作该项目中也有老师的帮助,在这里感谢帮助O(∩_∩)O!!!
程序编写可能还不够简洁,希望大家可以提出建议。
应要求添加库文件 棒!
可以试试现在的APP,随时随地从iOS和Android两个平台上面,查看你的作品表现。 erjiang 发表于 2013-6-5 08:14 static/image/common/back.gif
棒!
可以试试现在的APP,随时随地从iOS和Android两个平台上面,查看你的作品表现。
我试过Android平台的APP,可以使用。但是感觉界面比较简单,希望早点升级。 android功能正在不断的增加中!
对照着楼主的有问题,只能上传开关状态,温度无法上传 报错了,,,,
cc1plus.exe: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://sourceforge.net/tracker/?atid=520074&group_id=68108&func=browse> for instructions.
sketch_mar23a:27: error: 'Ethernet' has not been declared
sketch_mar23a.ino: In function 'void my_result_cb2(byte, word, word)':
sketch_mar23a:40: error: 'Ethernet' was not declared in this scope
sketch_mar23a.ino: In function 'void my_result_cb1(byte, word, word)':
sketch_mar23a:48: error: 'Ethernet' was not declared in this scope
sketch_mar23a:48: error: expected ',' or ';' before '::' token
sketch_mar23a.ino: In function 'void setup()':
sketch_mar23a:70: error: 'ether' was not declared in this scope
sketch_mar23a:70: error: 'Ethernet' has not been declared
sketch_mar23a:76: error: 'ether' was not declared in this scope
sketch_mar23a:81: error: 'ether' was not declared in this scope
sketch_mar23a.ino: In function 'void loop()':
sketch_mar23a:100: error: 'ether' was not declared in this scope
Freddy_ 发表于 2014-3-23 20:07 static/image/common/back.gif
报错了,,,,
cc1plus.exe: internal compiler error: Segmentation fault
Please submit a full bug r ...
你看下库文件,有没问题。 楼主能提供下你的库吗?我用张老师的库老是报错 本帖最后由 code-AR 于 2014-3-26 18:20 编辑
samwin 发表于 2014-3-26 14:03 static/image/common/back.gif
楼主能提供下你的库吗?我用张老师的库老是报错
我看看下,有的。已经更新 code-AR 发表于 2014-3-23 23:09 static/image/common/back.gif
你看下库文件,有没问题。
以太网哭问题
LM35的数据接口接哪。。 我接的A0 没有数据上传上去呀。。 Freddy_ 发表于 2014-3-29 22:28 static/image/common/back.gif
我接的A0 没有数据上传上去呀。。
你看下串口,有没数据信息。 code-AR 发表于 2014-3-30 19:57 static/image/common/back.gif
你看下串口,有没数据信息。
9600么????????? 本帖最后由 x852770 于 2014-4-2 11:57 编辑
不好意思大大~我串口看switchStatus切換可以看到0 跟 1 但是我板子上沒有反應耶:Q:Q:Q第一要查哪裡呢!
線我以經查過應該沒問題~!
页:
[1]
2