junyi12 发表于 2019-5-17 10:13

通过USB-I2C适配器实现物联网功能(源码下载)!

偶然一个机会看到Yeelink这个平台,感觉不错,利用Ginkgo USB-I2C适配器可以读写控制AM2311温湿度传感器以获取环境温湿度,以前已经实现对这个适配器读写控制并在上位机上显示温湿度数据,今天看了下Yeelink的API,不是很复杂,于是就准备将它测的数据上传到Yeelink上;
我将数据上传部分程序封装了下,用起来更简单了,上传数据或者获取数据需要用到的函数如下:
[*]int32_t WINAPI Yeelink_GetApiKey(const char *pUserName,const char *pPassword);
[*]int32_t WINAPI Yeelink_PostData(const char *pDeviceId,const char *pSensorId,const char *pValue);
[*]int32_t WINAPI Yeelink_GetData(const char *pDeviceId,const char *pSensorId,char *pValue);

复制代码
你只需要做以下工作就可以使用这些函数了:
1、在Yeelink注册一个账户,这个是必须的哈;
2、新建设备和传感器,找到设备ID和传感器ID,这个在设备管理里面的URL可以看到;
3、通过Ginkgo USB-I2C适配器获取环境中的温湿度值;

完成以上3个步骤后就可以调用这3个函数,实现将数据上传到Yeelink服务器了。
完整程序如下:
[*]// USB_I2C_AM2321B.cpp : 定义控制台应用程序的入口点。
[*]//
[*]
[*]#include "stdafx.h"
[*]#include "ControlI2C.h"
[*]#include "yeelink.h"
[*]
[*]
[*]int _tmain(int argc, _TCHAR* argv[])
[*]{
[*]      int ret,i;
[*]      VII_INIT_CONFIG I2C_Config;
[*]      uint8_t write_buffer={0};
[*]      uint8_t      read_buffer={0};
[*]      ret = Yeelink_GetApiKey("viewtool","viewtool2013");//输入用户名和密码
[*]      if(ret != ERR_SUCCESS){
[*]                printf("Get api key error!");
[*]                return ret;
[*]      }
[*]      //扫描已经连接的设备
[*]      ret = VII_ScanDevice(1);
[*]      if(ret <= 0)
[*]      {
[*]                printf("No device connect!\n");
[*]                return ret;
[*]      }
[*]    //打开设备
[*]    ret = VII_OpenDevice(VII_USBI2C, 0, 0);
[*]    if (ret != ERR_SUCCESS)
[*]    {
[*]      printf("Open device error!\n");
[*]      return ret;
[*]    }
[*]    //初始化设备(硬件控制模式)
[*]    I2C_Config.AddrType = VII_ADDR_7BIT;
[*]    I2C_Config.ClockSpeed = 100000;
[*]    I2C_Config.ControlMode = VII_HCTL_MODE;
[*]    I2C_Config.MasterMode = VII_MASTER;
[*]    I2C_Config.SubAddrWidth = VII_SUB_ADDR_NONE;
[*]    ret = VII_InitI2C(VII_USBI2C, 0, 0, &I2C_Config);
[*]    if (ret != ERR_SUCCESS)
[*]    {
[*]      printf("Initialize device error!\n");
[*]      return ret;
[*]    }
[*]      //循环读取温湿度数据
[*]      while(1)
[*]      {
[*]                uint8_t write_buffer = {0};
[*]      //Wake up AM2311 sensor
[*]      VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 1);
[*]      //Send out read temperature and huminity command
[*]      write_buffer = 0x03;
[*]      write_buffer = 0x00;
[*]      write_buffer = 0x04;
[*]      ret = VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 3);
[*]      if (ret != ERR_SUCCESS)
[*]      {
[*]            printf("Write data error!\n");
[*]            return ret;
[*]      }
[*]      // Read out temperature and huminity
[*]                uint8_t read_buffer = {0};
[*]      ret = VII_ReadBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, read_buffer, 8);
[*]      if (ret != ERR_SUCCESS)
[*]      {
[*]            printf("Read data error!\n");
[*]            return ret;
[*]      }
[*]      else
[*]      {
[*]            double t = ((read_buffer << 8) | read_buffer) / 10.0;
[*]            system("cls");
[*]            printf("温度值:%.1f ℃\n",t);
[*]            double h = ((read_buffer << 8) | read_buffer) / 10.0;
[*]            printf("湿度值:%.1f %\n",h);
[*]                        Sleep(10000);
[*]                        char StrTmp={0};
[*]                        sprintf(StrTmp,"%.1f",t);
[*]                        ret = Yeelink_PostData("9433","14860",StrTmp);//输入设备ID和传感器ID,以及传感器数据
[*]                        if(ret != ERR_SUCCESS){
[*]                              printf("Post data error!");
[*]                        }
[*]                        sprintf(StrTmp,"%.1f",h);
[*]                        ret = Yeelink_PostData("9433","14861",StrTmp);//输入设备ID和传感器ID,以及传感器数据
[*]                        if(ret != ERR_SUCCESS){
[*]                              printf("Post data error!");
[*]                        }
[*]      }
[*]      }
[*]      return 0;
[*]}
[*]
[*]

复制代码
通过Yeelink获取到的数据截图如下:


程序完整工程下载(VS2010):
http://www.viewtool.com/bbs/static/image/filetype/rar.gif VC_USB_I2C_AM2321B_Yeelink.rar
页: [1]
查看完整版本: 通过USB-I2C适配器实现物联网功能(源码下载)!