【ST B-L475E-IOT01A1蓝牙Wi-Fi开发板】7-利用wifi向服务端发送温度等传感器数据
<div class='showpostmsg'> 本帖最后由 qiao--- 于 2024-4-20 15:30 编辑<p>在上期测评中成功的用ISM43362模组连接到了手机热点,这期测评就用它向服务端发送我们之前已经测出来的数据。本期测评的代码均建立在上期测评的基础上。</p>
<p> </p>
<p>我们上期测评链接到了手机wifi,这期测评我们可以用板子连接电脑的wifi,这样方便我们用电脑看板子的ip。代码还是和上期的一样。</p>
<p> </p>
<p>这里我用电脑观察到板子的ip如下所示:</p>
<p> </p>
<p> </p>
<p>于是我用板子作为客户端,调用我们上期测评移植好的库,代码如下所示</p>
<p> </p>
<p>其中第一参数是封装的连接的句柄num,这里我选择1,方式为TCP连接,名字命名为“iot”,然后写上ipaddr和远程服务端的端口号和本地的端口号。</p>
<p>ipaddr为一个四个字节的数组,如下所示,这个根据自己的实际情况设置。</p>
<p> </p>
<p>这两部做好了以后可以打开网络调试助手看看能否通信</p>
<p> </p>
<p>这里我打算在主循环里循环发送字符串看看效果</p>
<p> </p>
<p>效果,服务端这边也是成功接收到了客户端发来的数据,如下所示:</p>
<p>0477b2396b071990cb0d832bce2cf15c<br />
</p>
<p>接下来我打算用六轴传感器发送传感器的温度值和二轴的偏移角。</p>
<p>首先对传感器进行初始化,代码如下所示:</p>
<p> </p>
<pre>
<code> /*
*Initialize mems driver interface
*/
lsm6dsl_ctx_t lsm6dsl_ctx;
lsm6dsl_ctx.write_reg = platform_write;
lsm6dsl_ctx.read_reg = platform_read;
lsm6dsl_ctx.handle = &hi2c2;
/*
*Check device ID
*/
static axis3bit16_t data_raw;
static axis3bit32_t acceleration_mg;
static axis3bit32_t angular_rate_mdps;
static axis1bit16_t temperature_deg_c;
static uint8_t whoamI, rst;
static uint8_t USBbuffer;
char str;
lsm6dsl_device_id(&lsm6dsl_ctx, &whoamI);
printf("%d\r\n",1);
printf("%d",whoamI);
if ( whoamI != LSM6DSL_ID )
while(1); /*manage here device not found */
/*
*Restore default configuration
*/
lsm6dsl_reset_set(&lsm6dsl_ctx, PROPERTY_ENABLE);
printf("%d\r\n",2);
do {
lsm6dsl_reset_get(&lsm6dsl_ctx, &rst);
} while (rst);
/*
*Set xl Full Scale
*/
lsm6dsl_xl_full_scale_set(&lsm6dsl_ctx, FS_4g);
/*
*Set gyro Full Scale
*/
lsm6dsl_gyro_full_scale_set(&lsm6dsl_ctx, FS_2000dps);
/*
*Enable Acc Block Data Update
*/
lsm6dsl_block_data_update_set(&lsm6dsl_ctx, PROPERTY_ENABLE);
/*
* Set xl Output Data Rate
*/
lsm6dsl_xl_data_rate_set(&lsm6dsl_ctx, XL_12Hz5);
/*
* Set gyro Output Data Rate
*/
lsm6dsl_gyro_data_rate_set(&lsm6dsl_ctx, GY_26Hz);
/*
* Read samples in polling mode (no int)
*/
float xl_X,xl_Y,xl_Z;
float gy_X,gy_Y,gy_Z;</code></pre>
<p> </p>
<p>然后在while中不断解析数据,并向服务端发送数据,如下所示:</p>
<p> </p>
<pre>
<code> /* USER CODE END WHILE */
lsm6dsl_status_reg_t status_reg;
lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_STATUS_REG, (uint8_t *)&status_reg, 1);
if (status_reg.gda)
{
lsm6dsl_angular_rate_raw(&lsm6dsl_ctx, &data_raw);
angular_rate_mdps.i32bit = FROM_FS_2000dps_TO_mdps( data_raw.i16bit );
angular_rate_mdps.i32bit = FROM_FS_2000dps_TO_mdps( data_raw.i16bit );
angular_rate_mdps.i32bit = FROM_FS_2000dps_TO_mdps( data_raw.i16bit );
gy_X = angular_rate_mdps.i32bit/ 1000.0;
gy_Y = angular_rate_mdps.i32bit / 1000.0;
gy_Z = angular_rate_mdps.i32bit / 1000.0;
sprintf((char*)USBbuffer, "GY:%lf\t%lf\t%lf\r\n", gy_X,
gy_Y, gy_Z);
printf("%s",USBbuffer);
//CDC_Transmit_FS( USBbuffer, strlen((char const*)USBbuffer) );
}
if (status_reg.xlda)
{
lsm6dsl_acceleration_raw(&lsm6dsl_ctx, &data_raw);
acceleration_mg.i32bit = FROM_FS_4g_TO_mg( data_raw.i16bit );
acceleration_mg.i32bit = FROM_FS_4g_TO_mg( data_raw.i16bit );
acceleration_mg.i32bit = FROM_FS_4g_TO_mg( data_raw.i16bit );
xl_X =acceleration_mg.i32bit / 100.0;
xl_Y =acceleration_mg.i32bit / 100.0;
xl_Z =acceleration_mg.i32bit / 100.0;
sprintf((char*)USBbuffer, "XL:%.3f\t %.3f\t %.3f\r\n", xl_X,
xl_Y, xl_Z);
printf("%s",USBbuffer);
//CDC_Transmit_FS( USBbuffer, strlen( (char const*)USBbuffer) );
}
/*
* If the gyroscope is not in Power-Down mode, the temperature data rate
* is equal to 52Hz, regardless of the accelerometer and gyroscope
* configuration. (Ref AN5040)
*/
if (status_reg.tda)
{
lsm6dsl_temperature_raw(&lsm6dsl_ctx, &temperature_deg_c);
temperature_deg_c.i16bit = FROM_LSB_TO_degC( temperature_deg_c.i16bit );
sprintf((char*)USBbuffer, "temp:%3d\r\n", temperature_deg_c.i16bit);
printf("%s",USBbuffer);
WIFI_SendData(1, USBbuffer, a, &a, HAL_TIMEOUT);
//CDC_Transmit_FS( USBbuffer, strlen( (char const*)USBbuffer) );
}
IMUupdate(gy_X,gy_Y,gy_Z,xl_X,xl_Y,xl_Z);
sprintf(str,"pose calculating :%.3f\t\t%.3f\t\t \r\n",Pitch,Roll);
printf("pose calculating :%.3f\t\t%.3f\t\t \r\n",Pitch,Roll);
WIFI_SendData(1, str, a, &a, HAL_TIMEOUT);
HAL_Delay(100);
/* USER CODE BEGIN 3 */
}</code></pre>
<p>最后将代码烧录进板子里就OK了。</p>
<p>效果:</p>
<p>44957a21ad611bce208fe8ca5f9da6c3<br />
</p>
<p>可以看到当我将板子进行翻转,服务端接受到的角度也在发生变化,并且和板子实际的角度挺相近的;温度也和我现在的室温差不多,传感器测得数据还是挺准确的。</p>
<p> </p>
<p><strong><span style="font-size:18px;">总结:</span></strong>通过本期测评成功的将我们之前测得的温度等的传感器值通过tcp通信传到了服务端,这样如果我们自己做一个上位机,就可以用这套代码进行可视化展示了。通过板子测得数据经模组传输到服务端,传输的数据准确无误。</p>
<div></div>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>再整套上位机就更加完美啦</p>
Jacktang 发表于 2024-4-23 07:32
再整套上位机就更加完美啦
<p>有道理,整C#<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/lol.gif" width="48" /></p>
damiaa 发表于 2024-4-23 14:36
有道理,整C#
<p>是的,上位机用这个很合适</p>
<p> </p>
页:
[1]