Warning: session_start(): open(/tmp/sess_c329degcpvjs54ahg4nfjb67t2, O_RDWR) failed: No space left on device (28) in /data/www/yeelink.net/blog/wp-content/themes/yeelink/functions.php on line 11 物联网跟我动手做系列教程—第三篇 实验二如何用arduino+ethernet shield与yeelink结合5分钟实现传感器数据web上传 : Yeelink

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

作者: · 39条评论 标签:

本文介绍如何在Arduino上实现自己的Yeelink客户端,以便将采集到的传感器数据上传到Yeelink平台上。该例程会每隔30s采集一次传感器数据并发送到yeelink服务器。

硬件需求
Arduino主板
Ethernet板
BH1750光强传感器模块(你可以替换成自己的传感器)

软件需求
Yeelink帐号(注册新用户
增加一个设备和一个传感器(参考快速开始)

电路
Ethernet板和主板的插接略去。
传感器插接线:
VCC-5v
GND-GND
SCL-SCL(analog pin 5)
SDA-SDA(analog pin 4)
ADD-NC

实物图

Yeelink网站上生成的数据曲线
 

源码
同时也被开源至了Github
https://github.com/dapingliu/arduino_client_4_yeelink 

/*
 Yeelink sensor client example
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

int BH1750address = 0x23;
byte buff[2];

// for yeelink api
#define APIKEY         "9cdf51696fa9ddfacdf819033a5f2f63" // replace your yeelink api key here
#define DEVICEID       4 // replace your device ID
#define SENSORID       7 // replace your sensor ID

// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net";   // name address for yeelink API

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

void setup() {
  Wire.begin();
  // start serial port:
  Serial.begin(57600);
  // start the Ethernet connection with DHCP:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
  }
  else {
    Serial.println("Ethernet configuration OK");
  }
}

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

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

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    // read sensor data, replace with your code
    int sensorReading = readLightSensor();
    //send data to server
    sendData(sensorReading);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("POST /v1.0/device/");
    client.print(DEVICEID);
    client.print("/sensor/");
    client.print(SENSORID);
    client.print("/datapoints");
    client.println(" HTTP/1.1");
    client.println("Host: api.yeelink.net");
    client.print("Accept: *");
    client.print("/");
    client.println("*");
    client.print("U-ApiKey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for {"value":} + number of digits of the data:
    int thisLength = 10 + getLength(thisData);
    client.println(thisLength);

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

    // here's the actual content of the PUT request:
    client.print("{\"value\":");
    client.print(thisData);
    client.println("}");
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}

// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten,
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}

///////////////////////////////////////////////////////////////////////////
// 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[i] = 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();
}

 

02
2012-06

未分类

评论列表

  1. Mackintosh 说:

    太好了,一直在琢磨如何让AUDUINO自己发微博呢!自从新浪宣布只支持OAUTH2.0以后我的计划就没戏了,郁闷。

  2. qiushui_007 说:

    HTTP中的详细的数据流程, 很好的参考.

  3. zhangdeyue1 说:

    请问一下
    发送数据中
    int thisLength = 10 + getLength(thisData);
    是表示什么意思啊?

  4. zhlgcc 说:

    感兴趣,关注中!

  5. xl_1120 说:

    我用自己的开发板。发送post connection:keep-alive可以实现几分钟传送,如果用close,就需不断的连接服务器,怎么解决连续上传

  6. xuebao579 说:

    我想请教各位前辈,对于一个对物联网丁点不了解的人,怎样能够快速入门,掌握Yeelink的使用

  7. Robot_bay 说:

    按照这种方法能不能一次多连接几个传感器上传(只要一个Arduino板和一个w5100板) 新手

  8. ardypro 说:

    参照这个范例的童鞋请注意,里面的getLength不能处理负数,要自己重新修改代码。

  9. radiocat 说:

    怎么修改这个代码啊?

  10. radiocat 说:

    大家帮忙看看,为什么找不到网页啊?我用的yeelink的样例,串口输出如下:

    Ethernet configuration OK
    Sensor value is: 0
    connecting…
    HTTP/1.1 404 Not Found
    Server: nginx
    Date: Wed, 12 Dec 2012 19:03:55 GMT
    Content-Type: text/html
    Content-Length: 162
    Connection: close

    404 Not Found

    404 Not Found
    nginx

    disconnecting.
    Sensor value is: 0
    connecting…
    HTTP/1.1 404 Not Found
    Server: nginx
    Date: Wed, 12 Dec 2012 19:04:26 GMT
    Content-Type: text/html
    Content-Length: 162
    Connection: close

  11. haomiaoE 说:

    请问如何通过Yeelink控制已经添加的传感器?比如调整电机转速

  12. lindx 说:

    一个设备多连几个传感器,数据是连着送,还是需要间隔30秒。

  13. lindx 说:

    两个传感器单独送都可以,但是连着送出现以下问题:
    在sendData()后加Delay()后再sendData(),
    结果程序成功送了一次数据,以后一直在
    connecting—
    connection failed
    disconnecting
    中循环,
    connecting—和connection failed是互斥的,结果走到一起了,不知道用法是否对,问题出哪了,请指点。

  14. lindx 说:

    在sendData()后加Delay()再sendData()没有成功,一直在connecting—,connection failed,disconnection中循环,不知问题出在哪?
    而且conncting和connection是互斥的竟然在一个循环中。

    两个单独的sendData都没有问题,请给予指点。

  15. lindx 说:

    两次调用sendData中间delay,每次send Data 就是一次http post, 但是还不行,至于如何查error还不会弄,是否能给这段代码,或在上述案例中给出示意,谢谢。

  16. Charles_li_2013 说:

    请问如果是用其他平台,比如STM32。我已经完成了网卡驱动和TCP/IP协议。下一步该怎么做呢?

  17. lixianyu 说:

    Ethernet板是什么?

  18. lihaobit 说:

    我也提示以下报错,需要在例程基础上做什么改动吗
    HTTP/1.1 404 Not Found
    Server: nginx
    Date: Wed, 29 May 2013 11:15:09 GMT
    Content-Type: text/html
    Content-Length: 162
    Connection: close

    404 Not Found

    404 Not Found
    nginx

  19. ahe1 说:

    我写了个程序,每隔15秒采集几个传感器的值。
    为什么过了8分钟左右 ,就不在向yeelink网页传送数据了呢,是我发快了或者慢了吗

  20. smartmos 说:

    怎么弄啊

  21. wanlizhang 说:

    client.print(“Accept: *”);
    client.print(“/”);
    client.println(“*”);
    client.print(“U-ApiKey: “);
    client.println(APIKEY);
    client.print(“Content-Length: “);
    请问这几句是什么意思?

  22. wuhaitian 说:

    具体页面怎么操作的

  23. ldc1309056536 说:

    很好

  24. kiven 说:

    终于修改成功了,刚开始用SDK,怎么样数据都没法上传,用你的代码,加上我自己的修改,终于可以上传LM35的温度数据了。感谢

  25. Vacat 说:

    请问W5100的库文件在哪里有下载啊?找了好久,谢谢好心人

  26. lfjkxjh 说:

    怎么没有数据呢,接路由器需要设置什么吗

  27. Qiu_SaMa 说:

    W5100怎么和PC连接上网的,要设置网络连接么?还是说直接和路由器连接就行?

  28. lu502 说:

    我用的代码和上面一样为什么返回这样啊??

    400 Bad Request

    400 Bad Request
    nginx/1.1.19

  29. czw74 说:

    谢谢分享,学习一下,不知道,怎么上传LM35的数据。

Trackbacks

Check out what others are saying about this post...
  1. […] 如何用arduino+ethernet shield(W5100)与yeelink结合5分钟实现传感器数据web上传 […]



我也来说两句

Warning: Unknown: open(/tmp/sess_c329degcpvjs54ahg4nfjb67t2, O_RDWR) failed: No space left on device (28) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0