这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 物联网技术 » 通过USB-I2C适配器实现物联网功能(源码下载)!

共1条 1/1 1 跳转至

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

菜鸟
2019-05-08 15:00:45     打赏

偶然一个机会看到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. #include "stdafx.h"

  4. #include "ControlI2C.h"

  5. #include "yeelink.h"



  6. int _tmain(int argc, _TCHAR* argv[])

  7. {

  8.         int ret,i;

  9.         VII_INIT_CONFIG I2C_Config;

  10.         uint8_t write_buffer[8]={0};

  11.         uint8_t        read_buffer[8]={0};

  12.         ret = Yeelink_GetApiKey("viewtool","viewtool2013");//输入用户名和密码

  13.         if(ret != ERR_SUCCESS){

  14.                 printf("Get api key error!");

  15.                 return ret;

  16.         }

  17.         //扫描已经连接的设备

  18.         ret = VII_ScanDevice(1);

  19.         if(ret <= 0)

  20.         {

  21.                 printf("No device connect!\n");

  22.                 return ret;

  23.         }

  24.     //打开设备

  25.     ret = VII_OpenDevice(VII_USBI2C, 0, 0);

  26.     if (ret != ERR_SUCCESS)

  27.     {

  28.         printf("Open device error!\n");

  29.         return ret;

  30.     }

  31.     //初始化设备(硬件控制模式)

  32.     I2C_Config.AddrType = VII_ADDR_7BIT;

  33.     I2C_Config.ClockSpeed = 100000;

  34.     I2C_Config.ControlMode = VII_HCTL_MODE;

  35.     I2C_Config.MasterMode = VII_MASTER;

  36.     I2C_Config.SubAddrWidth = VII_SUB_ADDR_NONE;

  37.     ret = VII_InitI2C(VII_USBI2C, 0, 0, &I2C_Config);

  38.     if (ret != ERR_SUCCESS)

  39.     {

  40.         printf("Initialize device error!\n");

  41.         return ret;

  42.     }

  43.         //循环读取温湿度数据

  44.         while(1)

  45.         {

  46.                 uint8_t write_buffer[8] = {0};

  47.         //Wake up AM2311 sensor

  48.         VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 1);

  49.         //Send out read temperature and huminity command

  50.         write_buffer[0] = 0x03;

  51.         write_buffer[1] = 0x00;

  52.         write_buffer[2] = 0x04;

  53.         ret = VII_WriteBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, write_buffer, 3);

  54.         if (ret != ERR_SUCCESS)

  55.         {

  56.             printf("Write data error!\n");

  57.             return ret;

  58.         }

  59.         // Read out temperature and huminity

  60.                 uint8_t read_buffer[8] = {0};

  61.         ret = VII_ReadBytes(VII_USBI2C, 0, 0, 0xB8, 0x00, read_buffer, 8);

  62.         if (ret != ERR_SUCCESS)

  63.         {

  64.             printf("Read data error!\n");

  65.             return ret;

  66.         }

  67.         else

  68.         {

  69.             double t = ((read_buffer[4] << 8) | read_buffer[5]) / 10.0;

  70.             system("cls");

  71.             printf("温度值:%.1f ℃\n",t);

  72.             double h = ((read_buffer[2] << 8) | read_buffer[3]) / 10.0;

  73.             printf("湿度值:%.1f %\n",h);

  74.                         Sleep(10000);

  75.                         char StrTmp[1024]={0};

  76.                         sprintf(StrTmp,"%.1f",t);

  77.                         ret = Yeelink_PostData("9433","14860",StrTmp);//输入设备ID和传感器ID,以及传感器数据

  78.                         if(ret != ERR_SUCCESS){

  79.                                 printf("Post data error!");

  80.                         }

  81.                         sprintf(StrTmp,"%.1f",h);

  82.                         ret = Yeelink_PostData("9433","14861",StrTmp);//输入设备ID和传感器ID,以及传感器数据

  83.                         if(ret != ERR_SUCCESS){

  84.                                 printf("Post data error!");

  85.                         }

  86.         }

  87.         }

  88.         return 0;

  89. }



复制代码

通过Yeelink获取到的数据截图如下:
温度.jpg 
湿度.jpg 
程序完整工程下载(VS2010):
 VC_USB_I2C_AM2321B_Yeelink.rar





关键词: 适配器    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]