极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: zcbzjx

基于18B20+enc28j60+arduino+yeelink的远程温度监控

[复制链接]
 楼主| 发表于 2012-11-13 21:31:07 | 显示全部楼层
darkorigin 发表于 2012-11-13 17:33
提问下语法方面
代码1的 40行 Stash stash;
按我理解应该是定义一个 Stash 型的变量(或实例)stash 但是 ...

stash是在库函数中声明的,主要用于构建数据包。建议不要用stash,用httppost比较好。我有个例子,你看看。不过最近没研究这个库了,好似又更新了不少。
回复 支持 反对

使用道具 举报

发表于 2012-11-19 15:40:39 | 显示全部楼层
JUST_DO_IT 发表于 2012-9-18 21:46
那怎么获取上面的开关的数据来控制LED灯呢

使用HTTP GET的方式就可以了,教程可以参考 http://blog.yeelink.net/?p=94
回复 支持 反对

使用道具 举报

发表于 2012-11-20 15:25:06 | 显示全部楼层
DFRobot 发表于 2012-8-11 13:22
使用W5100 制作的3种传感器 数据 上传yeelink成功,http://www.yeelink.net/devices/242

困惑于enc28j0,w5100在手,新手求代码
回复 支持 反对

使用道具 举报

发表于 2012-11-20 18:02:36 | 显示全部楼层
代码密集分布在bbs.yeelink.net和blog.yeelink.net上,本坛子里面也有不少的
回复 支持 反对

使用道具 举报

发表于 2012-11-20 19:50:48 | 显示全部楼层
yeelink.net的博客和论坛,都是基于enc28j60的,唯一一个5100还是测试光强的。我想先拿成熟的代码测试硬件,然后再慢慢学习和改进。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-20 19:52:18 | 显示全部楼层
5100代码例程就有的吧。
回复 支持 反对

使用道具 举报

发表于 2012-11-20 20:12:00 | 显示全部楼层
yeelink官方的,不是5100+BH1750光强传感器模块,就是enc28j60+18B20,唯独没有5100+18b20的代码,而且例程质量远不如cosm
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-20 21:13:15 | 显示全部楼层
明天我写个例程给你。还好俺饿了好几顿,花了100块买了个5100
回复 支持 反对

使用道具 举报

发表于 2012-11-20 21:24:21 | 显示全部楼层
我是昏头呆脑,彻底迷茫在yeelink的例程,转投cosm已经小有斩获。
http://www.geek-workshop.com/thread-2563-1-1.html
至少说明,硬件是没有问题的,还是希望能回归yeelink.

我参照的是这个例程:http://arduino.cc/en/Tutorial/PachubeClient

其实更希望用下面这个例程,传输多个参数:http://arduino.cc/en/Tutorial/PachubeClientString
但是搞不明白读取18b20传感器部分,能不能援手点拨如何读取18b20。

yeelink这个官方例程,网络是通的,就是传感器部分不知道如何改写。
http://blog.yeelink.net/?p=34

DS18B20温度传感器的例程,两个都已经通过,可供参考
http://www.arduino.cn/thread-1345-1-1.html
回复 支持 反对

使用道具 举报

发表于 2012-11-20 21:31:23 | 显示全部楼层
问题是,此贴的网卡怎么修改为5100?

或者,下面这个官方例程,怎么把传感器部分,修改为单个18b20或者多个

物联网跟我动手做系列教程—第三篇 实验二
如何用arduino+ethernet shield与yeelink结合5分钟实现传感器数据web上传
http://blog.yeelink.net/?p=34

///////////////////////////////////////////////////////////////////////////
// get data from light sensor
// you can replace this code for your sensor
int readLightSensor()
{
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;
  }

  Serial.print("Sensor value is: ");
  Serial.println((int)val);

  return val;
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-20 21:54:56 | 显示全部楼层
家里没arduino,没法调试,明天我到办公室给你弄吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-21 10:27:46 | 显示全部楼层
本帖最后由 zcbzjx 于 2012-11-21 10:34 编辑
  1. /*
  2. Yeelink sensor client example
  3. */

  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <OneWire.h>

  7. #define ONEWIRE_PIN A0

  8. OneWire ds(ONEWIRE_PIN);


  9. byte buff[2];

  10. // for yeelink api
  11. #define APIKEY         "xxxxxxxxxxxxxxxxxxxxxxxxxxx" // replace your yeelink api key here
  12. #define DEVICEID       xx // replace your device ID
  13. #define SENSORID       xxx // replace your sensor ID

  14. // assign a MAC address for the ethernet controller.
  15. byte mac[] = {
  16.   0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  17. IPAddress myip(10,21,0,195);
  18. IPAddress mydns(10,11,5,25);
  19. IPAddress mygateway(10,21,0,1);

  20. // initialize the library instance:
  21. EthernetClient client;
  22. char server[] = "api.yeelink.net";   // name address for yeelink API

  23. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  24. boolean lastConnected = false;                 // state of the connection last time through the main loop
  25. const unsigned long postingInterval = 15*1000; // delay between 2 datapoints, 30s

  26. // Some global variables
  27. char sensorData[20];
  28. uint8_t dataLength;

  29. void setup() {
  30.   // Start up the dallas library

  31.   // start serial port:
  32.   Serial.begin(57600);
  33.   // start the Ethernet connection with DHCP:
  34.   Ethernet.begin(mac,myip,mydns,mygateway);
  35. }

  36. void loop() {
  37.   // if there's incoming data from the net connection.
  38.   // send it out the serial port.  This is for debugging
  39.   // purposes only:
  40.   if (client.available()) {
  41.     char c = client.read();
  42.     Serial.print(c);
  43.   }

  44.   // if there's no net connection, but there was one last time
  45.   // through the loop, then stop the client:
  46.   if (!client.connected() && lastConnected) {
  47.     Serial.println();
  48.     Serial.println("disconnecting.");
  49.     client.stop();
  50.   }

  51.   // if you're not connected, and ten seconds have passed since
  52.   // your last connection, then connect again and send data:
  53.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  54.     // read sensor data, replace with your code
  55.     get_Send_String();
  56.     Serial.println(sensorData);
  57.     //send data to server

  58.     sendData(sensorData,dataLength);
  59.   }
  60.   // store the state of the connection for next time through
  61.   // the loop:
  62.   lastConnected = client.connected();
  63. }

  64. // this method makes a HTTP connection to the server:
  65. void sendData(char* thisData,byte thisLength) {
  66.   // if there's a successful connection:
  67.   if (client.connect(server, 80)) {
  68.     Serial.println("connecting...");
  69.     // send the HTTP PUT request:
  70.     client.print("POST /v1.0/device/");
  71.     client.print(DEVICEID);
  72.     client.print("/sensor/");
  73.     client.print(SENSORID);
  74.     client.print("/datapoints");
  75.     client.println(" HTTP/1.1");
  76.     client.println("Host: api.yeelink.net");
  77.     client.print("Accept: *");
  78.     client.print("/");
  79.     client.println("*");
  80.     client.print("U-ApiKey: ");
  81.     client.println(APIKEY);
  82.     client.print("Content-Length: ");


  83.     client.println(thisLength);

  84.     client.println("Content-Type: application/x-www-form-urlencoded");
  85.     client.println("Connection: close");
  86.     client.println();

  87.     // here's the actual content of the PUT request:
  88.     client.print(thisData);
  89.   }
  90.   else {
  91.     // if you couldn't make a connection:
  92.     Serial.println("connection failed");
  93.     Serial.println();
  94.     Serial.println("disconnecting.");
  95.     client.stop();
  96.   }
  97.   // note the time that the connection was made or attempted:
  98.   lastConnectionTime = millis();
  99. }


  100. ///////////////////////////////////////////////////////////////////////////
  101. // get data from light sensor
  102. // you can replace this code for your sensor
  103. ///////////////////////////////////////////////////////////////////////////
  104. // get data from temperature sensor
  105. // you can replace this code for your sensor
  106. void get_Send_String(){
  107.   uint16_t Tc_100 = get_Current_Temp();
  108.   uint8_t i,whole, fract;
  109.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  110.   fract = Tc_100 % 10;
  111.   dataLength = sprintf(sensorData,"{"value":%d.%d}",whole,fract);  
  112. }

  113. uint16_t get_Current_Temp(){ //0.1C
  114.   //returns the temperature from one DS18S20 in DEG Celsius
  115.   byte data[12];
  116.   byte addr[8];
  117.   if ( !ds.search(addr)) {
  118.     //no more sensors on chain, reset search
  119.     ds.reset_search();
  120.     return 0;
  121.   }
  122.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  123.     return 1000;
  124.   }
  125.   if ( addr[0] != 0x28) {
  126.     return 1000;
  127.   }
  128.   ds.reset();
  129.   ds.select(addr);
  130.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  131.   delay(1000);
  132.   ds.reset();
  133.   ds.select(addr);  
  134.   ds.write(0xBE); // Read Scratchpad

  135.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  136.     data[i] = ds.read();
  137.   }
  138.   ds.reset_search();
  139.   uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
  140.   return tempRead*10/16;
  141. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2012-11-21 10:48:00 | 显示全部楼层
哥们,W5100的使用,远远比enc的那个简单多啦!
回复 支持 反对

使用道具 举报

发表于 2012-11-21 11:14:24 | 显示全部楼层
收到,谢谢

回家就试,传感器A0,模拟输入么?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-21 11:40:02 | 显示全部楼层
A0-A5也可以是数字,A6-A7是纯模拟
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-20 03:20 , Processed in 0.042373 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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