打印
[应用方案]

ESP8266 wifi模块上传数据到yeelink平台

[复制链接]
2367|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Bermanrep|  楼主 | 2016-11-22 21:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
上期我们实现了局域网内的wifi控制,基于上面的库文件,我们今天给大家带来使用yeelink平台的upload教程,小伙伴还可以用手机随时随地观看数据变化情况。

所需配件:

步骤:

1. 硬件连接



接线示意图



硬件实物连接图



2. 注册Yeelink账号

在烧写程序之前,我们必须做一些准备工作:注册Yeelink账号,并获取属于自己的API KEY和对应设备、传感器的ID地址值。ESP8266通过地址信息将DHT11的温度信息上传到服务器系统中唯一的标示图上,这样可以在Yeelink的网页上实时观察到该传感器当前环境温度信息。

步骤如下:

第一步,注册用户。Yeelink的地址为:http://www.yeelink.net/

第二步,点击“账号”目录下的“我的账户设置”按钮,记下当前的API KEY,在本例中,API KEY为“7bf75e6c2c8a17351d557f206f89fa2e”。



第三步,点击“我的设备”目录下的“添加新设备”按钮,并按照如下内容范例填写,然后点击保存按钮。




第四步,添加新设备完毕后还需要为该设备增加一个或多个传感器(一个设备可以同时支持多个传感器)。点击“传感器”栏下的“+增加一个传感器”按钮,进入添加传感器的页面。按照如下内容范例填写,然后点击保存。









沙发
Bermanrep|  楼主 | 2016-11-22 21:02 | 只看该作者
第五步,保存后,在管理设备页面的下方会出现一个数据曲线的图表。设备的ID值为数据曲线的URL中device后面那串数字,而传感器的ID值为数据曲线的URL中sensor后面那串数字,记下该设备和传感器的ID值,在本例中温度的设备ID值为3047,传感器ID值为4285; 湿度的设备ID值为3047,传感器ID值为26142。




完成上面的操作后,会得到相应的API KEY,设备的ID值和传感器的ID值。如果想了解更具体的步骤,请查看以下网页:http://www.yeelink.net/developer/doc/42 。

3.烧写程序,我们需要用到ESP8266库,下载地址为:http://pan.baidu.com/s/1sj2nUyT 。库跟第一章的一样,已下载的小伙伴不需要重新下载了。

温馨提示: ESP8266 wifi库文件是基于arduino IDE1.0.5版本编译的,如遇编译不过,请尝试其他版本。

把ESP8266库解压缩到\arduino\libraries目录下。

因为以太网的数据包比较大,我们需要把串口的环形缓冲区的大小改大才能正常显示所有数据。
用记事本打开arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp文件,把第59行的SERIAL_BUFFER_SIZE数值改为500.如下:


#if (RAMEND< 1000)
  #define SERIAL_BUFFER_SIZE 16
#else
  #define SERIAL_BUFFER_SIZE 500
#endif
保存修改。

打开下载的库文件里面的uartWIFI.H文件。确认以下两处是否如下:

    #define _DBG_RXPIN_ 2  //使用UNO的时候改为9
    #define _DBG_TXPIN_ 3  //使用UNO的时候改为10
    #define debugBaudRate 9600
   //#define UNO  //使用UNO的时候取消这行的注释
     #define MEGA //使用mega的时候注释掉这行

确认无误,保存。

复制以下demo代码到arduino IDE。

使用特权

评论回复
板凳
Bermanrep|  楼主 | 2016-11-22 21:04 | 只看该作者
#define PASSWORD   "27955416"                                //type your own WIFI password


#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

extern int chlID;        //client id(0-4)

// for yeelink api
#define APIKEY         "3a362e99d6f1daf974561163a8c99a85" // replace your yeelink api key here

//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0       15483 // replace y**ice ID
#define SENSORID0       26660 // replace your sensor ID

//replace the device ID and sensor ID for humidity sensor.
#define DEVICEID1       15483 // replace y**ice ID
#define SENSORID1       26661 // replace your sensor ID

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 = 5*1000; // delay between 2 datapoints, 5s
String returnValue = "";
boolean ResponseBegin = false;


int DHT11PIN=25;                        //Connect D25 to data pin of DHT11


int humidity;
int temperature;

int post_number;

void setup()
{

  wifi.begin();
  bool b = wifi.Initialize(STA, SSID, PASSWORD);
  if(!b)
  {
    DebugSerial.println("Init error");
  }
  delay(8000);  //make sure the module can have enough time to get an IP address
  String ipstring  = wifi.showIP();
  DebugSerial.println(ipstring);                //show the ip address of module
  

        

}
void loop()
{
  char message[400];
   // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if((millis() - lastConnectionTime > postingInterval)) {
  
  //read dht11
  int chk = dht11_read(DHT11PIN);
  if(chk==0)
  {
        if(post_number==0)
        {
                sendData(DEVICEID0,SENSORID0,temperature);
                post_number++;
        }
        else
        {
                post_number = 0;
                sendData(DEVICEID1,SENSORID1,humidity);
        }
        
  }
  
  }
  
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if(wifi.ReceiveMessage(message))
  {
      DebugSerial.println(message);   
  }


  delay(10);

}

使用特权

评论回复
地板
Bermanrep|  楼主 | 2016-11-22 21:05 | 只看该作者
// this method makes a HTTP connection to the server:
void sendData(int device_id,int sensor_id,int thisData) {
  // if there's a successful connection:
  if (wifi.ipConfig(TCP,server, 80)) {
    DebugSerial.println("connecting...");
    // send the HTTP PUT request:
    String cmd;
        cmd = "POST /v1.0/device/";
        cmd += String(device_id);
        cmd += "/sensor/";
        cmd += String(sensor_id);
        cmd += "/datapoints";
        cmd += " HTTP/1.1\r\n";
        cmd += "Host: api.yeelink.net\r\n";
        cmd += "Accept: *";
        cmd += "/";
        cmd += "*\r\n";
        cmd += "U-ApiKey: ";
        cmd += APIKEY;
        cmd += "\r\n";
        cmd += "Content-Length: ";
        int thisLength = 10 + getLength(thisData);
    cmd += String(thisLength);
        cmd += "\r\n";
        cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
        cmd += "Connection: close\r\n";
        cmd += "\r\n";
        cmd += "{\"value\":";
        cmd += String(thisData);
        cmd += "}\r\n";
        
        
        DebugSerial.println(cmd);
        
    wifi.Send(cmd);
    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    DebugSerial.println("connection failed");
    DebugSerial.println("disconnecting.");
    wifi.closeMux();
  }
}

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;
}




int dht11_read(int pin)
{
        // BUFFER TO RECEIVE
        int bits[5];
        int cnt = 7;
        int idx = 0;

        // EMPTY BUFFER
        for (int i=0; i< 5; i++)
        {bits= 0;}

        // REQUEST SAMPLE
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delay(18);
        digitalWrite(pin, HIGH);
        delayMicroseconds(40);
        pinMode(pin, INPUT);

        // ACKNOWLEDGE or TIMEOUT
        unsigned int loopCnt = 10000;
        while(digitalRead(pin) == LOW)
                if (loopCnt-- == 0) return -2;

        loopCnt = 10000;
        while(digitalRead(pin) == HIGH)
                if (loopCnt-- == 0) return -2;

        // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
        for (int i=0; i<40; i++)
        {
                loopCnt = 10000;
                while(digitalRead(pin) == LOW)
                        if (loopCnt-- == 0) return -2;

                unsigned long t = micros();

                loopCnt = 10000;
                while(digitalRead(pin) == HIGH)
                        if (loopCnt-- == 0) return -2;

                if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;    // restart at MSB
                        idx++;      // next byte!
                }
                else cnt--;
        }

        // WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
        humidity    = bits[0];
        temperature = bits[2];

        int sum = bits[0] + bits[2];  

        if (bits[4] != sum) return -1;
        return 0;
}

使用特权

评论回复
5
Bermanrep|  楼主 | 2016-11-22 21:06 | 只看该作者
在SSID和PASSWORD宏定义中修改成自己的WIFI名称和密码。

#define SSID      "Itead_1(Public)"        //type your own SSID name
#define PASSWORD  "27955416"            //type your own WIFI password

接着,把刚才所取得的APIKEY,设备的ID值和传感器的ID值替换到程序相应的位置中。设备的ID值替换代码中DEVICEID的值,传感器的ID值替换代码中SENSORID的值。

// for yeelink api
#define APIKEY         "7bf75e6c2c8a17351d557f206f89fa2e"// replace your yeelink api key here
//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0       3047 //replace y**ice ID
#define SENSORID0       4285 //replace your sensor ID
//replace the device ID and sensor ID for humidity sensor.
#define DEVICEID1       3047 // replacey**ice ID
#define SENSORID1       26142 //replace your sensor ID
修改完后,把该程序烧写到mega中去。

然后打开Serial monitor,等待一段时间后,会看到串口会显示它的ip地址,接着他就会每隔10s钟上传一次数据。



3. 查看数据图表

用网页打开yeelink平台,输入账号密码后,在用户中心的“我的设备”里可以看到温度和湿度的数据曲线。

手机还可以下载yeelink的客户端:http://www.yeelink.net/developer/tools





使用特权

评论回复
6
643757107| | 2016-11-22 21:52 | 只看该作者
如果用自己的服务器该如何玩呢

使用特权

评论回复
7
643757107| | 2016-11-23 19:33 | 只看该作者
我就想知道怎么通信的。

使用特权

评论回复
8
popoton| | 2016-12-4 22:47 | 只看该作者
项目源代码发一下就更赞了.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

132

帖子

2

粉丝