9102| 2
|
[项目教程] micro:bit + Obloq 打造物联网智能提醒喝水器 |
本帖最后由 于鑫 于 2018-10-21 00:15 编辑 <br /><br /><b><font size="6" face="Helvetica">micro:bit + Obloq 打造物联网智能水杯</font></b><div><font face="微软雅黑" size="5"><b><br></b></font><div></div><div><br><b><font size="5" face="Helvetica">项目背景</font></b></div><div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000">大家好,我叫于儿,今年8岁。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000">爸爸工作很忙,经常忘记喝水,所以我开发一个智能提醒喝水器提醒爸爸喝水。为了让更好地让爸爸养成喝水的习惯,我加入了物联网模块,实时监控爸爸的喝水数据。晚上等爸爸回家我就可以拿着数据批评爸爸。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000"><br></font></font></div><b><font size="5" face="Helvetica">实现思路</font></b></div><div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000">我6岁开始学习编程,我会Scratch,现在在用micro:bit学习Python。我基于micro:bit和Obloq物联网模块来开发这个物联网智能提醒喝水器。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000">大致思路如下:</font></font></div><ul><li>设置一个计数器,初始值为0</li><li>用一个重量传感器来称水杯的重量,每秒钟读取一次数据</li><li>如果重量<=0,说明水杯不在称重台上,发送消息“水杯拿起”到IoT;</li><li>如果重量>0, 则比较这次重量与上一次重量的差值<ul><li>如果为正,说明水杯中水多了,发送消息“加水”到IoT;</li><li>如果为负,说明水杯中水少了,发送少的重量到IoT,计数器设置为0;</li><li>上次重量 <- 本次重量<br></li></ul></li><li>计数器+1</li><li>如果计数器>60*30 (30分钟),则播放音乐提醒喝水</li><li>sleep(1000)</li></ul><div><br></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" size="3" color="#000000">流程图如下:</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="flowchart.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">如何实现称重?</font></b></div><div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" color="#000000" size="3">这个项目的难点是如何实现称重。一开始想用<font style=""><a href="https://www.dfrobot.com.cn/goods-832.html" target="_blank">重量传感器模块</a></font>,研究了很久发现micro:bit上没有可用的驱动,都是用Arduino来读取数据然后通过串口再传递给micro:bit,可是我没有Arduino,并且似乎micro:bit不能2个设备同时工作在串口模式下,也就是说无法跟Obloq放在一起工作。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" color="#000000" size="3"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif"><font style="" color="#000000" size="3">于是我想到我有薄膜压力传感器,水杯的重量可以转变成压力,然后通过薄膜压力传感器来读到重量。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif"><font style="" color="#000000" size="3"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" color="#000000" size="3">薄膜压力传感器的缺点是精度差,不过我的这个项目关心的是重量的差值,对精度要求不高,可以接受。【后来我加入了多次测量求平均值的方法,提高了读数的精度和稳定性】</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" color="#000000" size="3"><br></font></font></div><b><font size="5" face="Helvetica">所需配件</font></b></div><div><ul><li><font face="Helvetica">micro:bit + micro:Mate</font></li><li><font face="Helvetica">RFP612薄膜压力传感器</font></li><li><font face="Helvetica">RFP-ZHII电阻电压转换模块</font></li><li><font face="Helvetica">OBLOQ - IoT物联网模块</font></li><li><font face="Helvetica">数字蜂鸣模块<br></font></li></ul><div><br></div><b><font size="5" face="Helvetica">配件资料</font></b></div><div><b><br></b></div><div><b><font size="4" face="Helvetica">RFP612薄膜压力传感器</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="" color="#000000" size="2">RFP薄膜式压力传感器可对任何接触面的压力进行静态和动态测量,薄膜压力传感器将施加在传感器感应区域的压力转换成电阻信号,然后根据力~电阻的标定关系曲线获得外界所施压力的变化信息,压力越大,传感器输出的电阻越小。</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="RFP612.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">RFP-ZHII电阻电压转换模块</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">RFP-ZHII电阻电压转换模块将传感器的电阻信号转化为模拟电压信号或高低电平。它配合RFP薄膜式压力传感器一起使用,将压力转换为模拟信号供micro:bit读取。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">功能引脚见下图</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="RFP-ZHII.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">通过实际称重测试,我测得了如下重量-模拟信号读数:</font></font></div><br><table class="t_table"><tbody><tr><td><div align="right">重量(g)</div></td><td><div align="right">模拟读数</div></td></tr><tr><td><div align="right">0</div></td><td><div align="right">1020</div></td></tr><tr style="background-color: rgb(248, 248, 248)"><td><div align="right">83</div></td><td><div align="right">968</div></td></tr><tr><td><div align="right">162</div></td><td><div align="right">935</div></td></tr><tr style="background-color: rgb(248, 248, 248)"><td><div align="right">229</div></td><td><div align="right">893</div></td></tr><tr><td><div align="right">311</div></td><td><div align="right">873</div></td></tr><tr style="background-color: rgb(248, 248, 248)"><td><div align="right">396</div></td><td><div align="right">840</div></td></tr><tr><td><div align="right">460</div></td><td><div align="right">827</div></td></tr><tr style="background-color: rgb(248, 248, 248)"><td><div align="right">538</div></td><td><div align="right">808</div></td></tr><tr><td><div align="right">620</div></td><td><div align="right">797</div></td></tr><tr style="background-color: rgb(248, 248, 248)"><td><div align="right">722</div></td><td><div align="right">782</div></td></tr><tr><td><div align="right">878</div></td><td><div align="right">750</div></td></tr></tbody></table><br><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">将上面的数据做到图上(见下图),可以发现重量和模拟信号呈线性关系。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="w-a.png" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">通过线性回归,可以得到重量~模拟信号的函数关系为:</font></font></div><div align="left">[mw_shl_code=python,true]import numpy as np weight = [0, 83, 162, 229, 311, 396, 460, 538, 620, 722, 878] analog = [1020, 968, 935, 893, 873, 840, 827, 808, 797, 782, 750] analog_fit = np.polyfit(weight, analog, 1) print(analog_fit)[/mw_shl_code]</div><br><blockquote><div align="left"><font face="Helvetica" color="#008080"><b>输出 [-0.295, 981]</b></font></div><div align="left"><font face="Helvetica" color="#008080"><b><br></b></font></div><div align="left"><div class="quote"><blockquote>analog = −0.295 ∗ weight + 981</blockquote></div></div></blockquote><div align="left"><br></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">实际使用时我们是读到模拟信号量然后换算重量,所以模拟信号~重量的函数关系为:</font></font></div>[mw_shl_code=python,true]weight_fit = np.polyfit(analog, weight, 1) print(weight_fit)[/mw_shl_code]</div><div><font color="#008080" face="Helvetica"><br></font><blockquote><div align="left"><font color="#008080" face="Helvetica">输出 [-3.175, 3140]</font></div><div align="left"><font color="#008080" face="Helvetica"><br></font></div></blockquote><div align="left"><div class="quote"><blockquote>weight = −3.175 ∗ analog + 3140</blockquote></div><br></div><div align="left"><br></div><b><font size="4" face="Helvetica">OBLOQ - IoT物联网模块</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">OBLOQ是一款基于ESP8266设计的串口转WIFI物联网模块,用以接收和发送物联网信息。它尺寸紧凑,价格低,接口简单,即插即用,搭配DFRobot自有的物联网平台,大大降低了物联网的使用门槛,无需复杂的基础知识,就能迅速搭建出一套物联网应用。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;"><font color="#000000">详细资料请参考</font><font color="rgb(65, 131, 196)" style="color: rgb(0, 0, 0);"><a href="https://wiki.dfrobot.com.cn/index.php?title=(SKU:_TEL0118)Gravity:_OBLOQ_-_IoT_Module_%E7%89%A9%E8%81%94%E7%BD%91%E6%A8%A1%E5%9D%97" target="_blank">Gravity: OBLOQ - IoT Module 物联网模块</a></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;"><br></font></font></div><b><font size="4" face="Helvetica">数字蜂鸣模块</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">数字蜂鸣器是一款简单的发声装置,可以通过频率来控制音调。它结构简单、应用丰富,能够模拟我们生活中许多声音和音乐。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;"><font color="#000000">详细资料请参考</font><font color="rgb(65, 131, 196)" style="color: rgb(0, 0, 0);"><a href="https://wiki.dfrobot.com.cn/index.php/(SKU:DFR0032)%E6%95%B0%E5%AD%97%E8%9C%82%E9%B8%A3%E5%99%A8%E6%A8%A1%E5%9D%97" target="_blank">数字蜂鸣器模块</a></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;"><br></font></font></div><b><font size="5" face="Helvetica">设备接线</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="connection.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="5" face="Helvetica">代码实现</font></b></div><div><b><br></b></div><div><b><font size="4" face="Helvetica">注册物联网</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">登陆<font style=""><a href="https://iot.dfrobot.com.cn/index.html" target="_blank">https://iot.dfrobot.com.cn</a></font>完成用户注册,即可添加设备。</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">我是用微信小程序注册的,在微信小程序中搜索EasyIoT,点击小程序,完成授权即可。</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="IoT0.png" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><i>Iot_id</i> 和 <i>Iot_pwd</i> 可以在 <b style="">我的</b> 里查看</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="IoT2.png" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">点击 <b style="">创建</b> 添加设备</font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="IoT3.png" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">添加后在 <b style="">设备</b> 里可以查看到刚刚添加的设备及其 <i>topic</i></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000"><br></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="IoT1.png" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><b><font size="4" face="Helvetica">添加Obloq软件包</font></b><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">进入<font style=""><a href="https://makecode.microbit.org/" target="_blank">https://makecode.microbit.org/</a></font> --> 点击“添加软件包” --> 在搜索框中输入Obloq,点击搜索 --> 添加Obloq软件包。</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="add-package.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">Obloq初始化</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="init_obloq.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">模拟读数转化为重量</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="analog2weight.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">根据重量判断喝水行为</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="weight.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">定时提醒喝水</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="timer.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">接收到“water”信息提醒喝水</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="water.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="4" face="Helvetica">完整代码如下</font></b></div><div></div><div><br><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="microbit-code.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><b><font size="5" face="Helvetica">系统演示</font></b></div><div><font face="Helvetica" size="5"><b><br></b></font><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">先来张系统全家福</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="demo.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" style=""><font style="font-size: 14px;" color="#000000">喝水、加水的数据可以在EasyIot小程序里看到</font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"></font></font></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><br></font></font></font></div><div align="left"><font face="Helvetica, arial, sans-serif" color="#000000"><span style="font-size: 14px;">详细演示请看文章开头的视频。</span></font></div><div align="left"><font color="rgb(0, 0, 0)"><font face="Helvetica, arial, sans-serif"><font style="font-size: 14px"><img src="iot.jpg" border="0" alt="" style="max-width:400px"></font></font></font></div><br></div></div> |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed