原创 如何用arduino+ethernet shield(W5100)与yeelink结合5分钟实现传感器数据web上传

2013-1-14 15:36 1417 18 19 分类: 消费电子

 

转自Yeelink博客。http://blog.yeelink.net/?p=34

本文介绍如何在Arduino上实现自己的Yeelink客户端,以便将采集到的传感器数据上传到Yeelink平台上。该例程会每隔30s采集一次传感器数据并发送到yeelink服务器。
硬件需求
Arduino主板
Ethernet板(W5100)
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 
#include 
#include 
#include 

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 = 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();
}

:WIZnet在线转载作品均尽可能注明出处,该作品所有人的一切权利均不因本站转载而转移。作者如不同意转载,即请通知本站予以删除或改正。转载的作品可能在标题或内容上或许有所改动。

文章评论1条评论)

登录后参与讨论

用户1647523 2013-1-14 15:37

arduino+ethernet shield(W5100)与yeelink结合5分钟实现传感器数据web上传
相关推荐阅读
用户1647523 2018-01-09 16:09
教你如何通过MCU配置S2E为TCP Client的工作模式
在上两篇文章中分别讲述了通过MCU配置S2E为UDP、TCP Server的工作模式”,相信小伙伴们已经对S2E的基本功能了解了一些,现在就让我们再接再厉、更上一层楼吧:首先我们先了解一下MCU与S2...
用户1647523 2018-01-09 15:53
教你如何通过MCU配置S2E为TCP Server的工作模式
在上一篇文章中讲了“如何通过MCU配置S2E为UDP模式”,相信小伙伴们已经了解了S2E的基本功能,在这一篇中,我们再接再厉,继续学习一下“如何通过MCU配置S2E为TCP Server的工作模式”,...
用户1647523 2018-01-09 14:44
教你如何通过MCU将S2E配置为UDP的工作模式
​​W5500S2E-S1是一款工业级串口转以太网模块,支持TCP Server、TCP Client和UDP三种工作模式,串口波特率最高可达1.152Mbps,并提供配套的上位机配置软件,也可通过网...
用户1647523 2015-12-09 13:53
第四届韩合集成电路技术交流会成功举办
2015年12月2日,第四届韩合集成电路技术交流会在WESTIN酒店深圳世界之窗店顺利举办。本届交流会与会嘉宾主要是韩合集成电路项目的主要领导、驻在深圳的9家韩合IC企业以及相关企业代表,主要围绕...
用户1647523 2015-12-09 13:46
第四届韩合集成电路技术交流会成功举办
2015年12月2日,第四届韩合集成电路技术交流会在WESTIN酒店深圳世界之窗店顺利举办。本届交流会与会嘉宾主要是韩合集成电路项目的主要领导、驻在深圳的9家韩合IC企业以及相关企业代表,主要围绕...
用户1647523 2015-10-16 14:47
WIZnet芯片在智能家居市场的应用
2014年是智能家居和可穿戴设备集中发力的一年。传统家电厂商、互联网企业和智能终端企业都虎视眈眈的盯着这个巨大的蛋糕,初创企业也借助各种众筹平台将自己的新奇创意转换为实际产品并推进市场,各种智能家...
我要评论
1
18
关闭 站长推荐上一条 /2 下一条