极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 31055|回复: 22

[新人帖]yeelink平台下,LM35温度上传+开关控制LED

[复制链接]
发表于 2013-6-4 22:24:16 | 显示全部楼层 |阅读模式
本帖最后由 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以太网盾模块样板,就没做了。

现在上源代码:
[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
回复

使用道具 举报

发表于 2013-6-5 08:14:02 | 显示全部楼层
棒!
可以试试现在的APP,随时随地从iOS和Android两个平台上面,查看你的作品表现。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-5 12:04:59 | 显示全部楼层
erjiang 发表于 2013-6-5 08:14
棒!
可以试试现在的APP,随时随地从iOS和Android两个平台上面,查看你的作品表现。

我试过Android平台的APP,可以使用。但是感觉界面比较简单,希望早点升级。
回复 支持 反对

使用道具 举报

发表于 2013-6-5 14:41:38 | 显示全部楼层
android功能正在不断的增加中!
回复 支持 反对

使用道具 举报

发表于 2014-2-24 10:57:26 | 显示全部楼层
对照着楼主的有问题,只能上传开关状态,温度无法上传
回复 支持 反对

使用道具 举报

发表于 2014-3-23 20:07:30 | 显示全部楼层
报错了,,,,
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
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-23 23:09:06 | 显示全部楼层
Freddy_ 发表于 2014-3-23 20:07
报错了,,,,
cc1plus.exe: internal compiler error: Segmentation fault
Please submit a full bug r ...

你看下库文件,有没问题。
回复 支持 反对

使用道具 举报

发表于 2014-3-26 14:03:22 | 显示全部楼层
楼主能提供下你的库吗?我用张老师的库老是报错
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-26 15:36:22 | 显示全部楼层
本帖最后由 code-AR 于 2014-3-26 18:20 编辑
samwin 发表于 2014-3-26 14:03
楼主能提供下你的库吗?我用张老师的库老是报错


我看看下,有的。已经更新
回复 支持 反对

使用道具 举报

发表于 2014-3-29 21:57:20 | 显示全部楼层
code-AR 发表于 2014-3-23 23:09
你看下库文件,有没问题。

以太网哭问题
回复 支持 反对

使用道具 举报

发表于 2014-3-29 22:15:17 | 显示全部楼层
LM35的数据接口接哪。。
回复 支持 反对

使用道具 举报

发表于 2014-3-29 22:28:42 | 显示全部楼层
我接的A0 没有数据上传上去呀。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-30 19:57:30 | 显示全部楼层
Freddy_ 发表于 2014-3-29 22:28
我接的A0 没有数据上传上去呀。。

你看下串口,有没数据信息。
回复 支持 反对

使用道具 举报

发表于 2014-4-1 16:00:17 | 显示全部楼层
code-AR 发表于 2014-3-30 19:57
你看下串口,有没数据信息。

9600么?????????
回复 支持 反对

使用道具 举报

发表于 2014-4-2 11:52:46 | 显示全部楼层
本帖最后由 x852770 于 2014-4-2 11:57 编辑

不好意思大大~我串口看switchStatus  切換可以看到0 跟 1 但是我板子上沒有反應耶第一要查哪裡呢!

線我以經查過應該沒問題~!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 17:51 , Processed in 0.038814 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表