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

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

  1. int32_t WINAPI Yeelink_GetApiKey(const char *pUserName,const char *pPassword);
  2. int32_t WINAPI Yeelink_PostData(const char *pDeviceId,const char *pSensorId,const char *pValue);
  3. 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服务器了。
完整程序如下:

  1. // USB_I2C_AM2321B.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ControlI2C.h"
  6. #include "yeelink.h"
  7.  
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11.         int ret,i;
  12.         VII_INIT_CONFIG I2C_Config;
  13.         uint8_t write_buffer[8]={0};
  14.         uint8_t        read_buffer[8]={0};
  15.         ret = Yeelink_GetApiKey("viewtool","viewtool2013");//输入用户名和密码
  16.         if(ret != ERR_SUCCESS){
  17.                 printf("Get api key error!");
  18.                 return ret;
  19.         }
  20.         //扫描已经连接的设备
  21.         ret = VII_ScanDevice(1);
  22.         if(ret <= 0)
  23.         {
  24.                 printf("No device connect!\n");
  25.                 return ret;
  26.         }
  27.     //打开设备
  28.     ret = VII_OpenDevice(VII_USBI2C, 0, 0);
  29.     if (ret != ERR_SUCCESS)
  30.     {
  31.         printf("Open device error!\n");
  32.         return ret;
  33.     }
  34.     //初始化设备(硬件控制模式)
  35.     I2C_Config.AddrType = VII_ADDR_7BIT;
  36.     I2C_Config.ClockSpeed = 100000;
  37.     I2C_Config.ControlMode = VII_HCTL_MODE;
  38.     I2C_Config.MasterMode = VII_MASTER;
  39.     I2C_Config.SubAddrWidth = VII_SUB_ADDR_NONE;
  40.     ret = VII_InitI2C(VII_USBI2C, 0, 0, &I2C_Config);
  41.     if (ret != ERR_SUCCESS)
  42.     {
  43.         printf("Initialize device error!\n");
  44.         return ret;
  45.     }
  46.         //循环读取温湿度数据
  47.         while(1)
  48.         {
  49.                 uint8_t write_buffer[8] = {0};
  50.         //Wake up AM2311 sensor
  51.         VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 1);
  52.         //Send out read temperature and huminity command
  53.         write_buffer[0] = 0x03;
  54.         write_buffer[1] = 0x00;
  55.         write_buffer[2] = 0x04;
  56.         ret = VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 3);
  57.         if (ret != ERR_SUCCESS)
  58.         {
  59.             printf("Write data error!\n");
  60.             return ret;
  61.         }
  62.         // Read out temperature and huminity
  63.                 uint8_t read_buffer[8] = {0};
  64.         ret = VII_ReadBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, read_buffer, 8);
  65.         if (ret != ERR_SUCCESS)
  66.         {
  67.             printf("Read data error!\n");
  68.             return ret;
  69.         }
  70.         else
  71.         {
  72.             double t = ((read_buffer[4] << 8) | read_buffer[5]) / 10.0;
  73.             system("cls");
  74.             printf("温度值:%.1f ℃\n",t);
  75.             double h = ((read_buffer[2] << 8) | read_buffer[3]) / 10.0;
  76.             printf("湿度值:%.1f %\n",h);
  77.                         Sleep(10000);
  78.                         char StrTmp[1024]={0};
  79.                         sprintf(StrTmp,"%.1f",t);
  80.                         ret = Yeelink_PostData("9433","14860",StrTmp);//输入设备ID和传感器ID,以及传感器数据
  81.                         if(ret != ERR_SUCCESS){
  82.                                 printf("Post data error!");
  83.                         }
  84.                         sprintf(StrTmp,"%.1f",h);
  85.                         ret = Yeelink_PostData("9433","14861",StrTmp);//输入设备ID和传感器ID,以及传感器数据
  86.                         if(ret != ERR_SUCCESS){
  87.                                 printf("Post data error!");
  88.                         }
  89.         }
  90.         }
  91.         return 0;
  92. }
  93.  
  94.  

复制代码

通过Yeelink获取到的数据截图如下:
通过USB-I2C适配器实现物联网功能(源码下载)!_第1张图片 
通过USB-I2C适配器实现物联网功能(源码下载)!_第2张图片

你可能感兴趣的:(通过USB-I2C适配器实现物联网功能(源码下载)!)