查看: 2044|回复: 0

[资料] 通过纬图USB-I2C适配器实现物联网功能(源码下载)!

[复制链接]

该用户从未签到

发表于 2019-7-22 14:33:59 | 显示全部楼层 |阅读模式
分享到:
偶然一个机会看到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);

[color=rgb(51, 102, 153) !important]复制代码

你只需要做以下工作就可以使用这些函数了:
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[8]={0};
  •         uint8_t        read_buffer[8]={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[8] = {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[0] = 0x03;
  •         write_buffer[1] = 0x00;
  •         write_buffer[2] = 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[8] = {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[4] << 8) | read_buffer[5]) / 10.0;
  •             system("cls");
  •             printf("温度值:%.1f ℃\n",t);
  •             double h = ((read_buffer[2] << 8) | read_buffer[3]) / 10.0;
  •             printf("湿度值:%.1f %\n",h);
  •                         Sleep(10000);
  •                         char StrTmp[1024]={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;
  • }


[color=rgb(51, 102, 153) !important]复制代码

通过Yeelink获取到的数据截图如下:


程序完整工程下载(VS2010):
VC_USB_I2C_AM2321B_Yeelink.rar
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-3-19 10:00 , Processed in 0.118169 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.