【BearPi-HM Nano,玩转鸿蒙“碰一碰”】-7-“伪碰一碰”LED控制
<div class='showpostmsg'> 本帖最后由 sun63312 于 2021-8-16 13:30 编辑<p style="text-align: center; margin-top: 0; margin-bottom: 0;"><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">小熊派</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">HarmonyOS</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">“</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">伪</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">碰一碰</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">”</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">LED控制</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">实现</span></p>
<p><span style="font-family: '宋体';">一、简介:</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">拖了有点久,有几个B</span><span style="font-family: '宋体';">UG</span><span style="font-family: '宋体';">一直没有解决,现在算是初步实现了我之前想要实现的效果:基于小熊派[</span><span style="font-family: '宋体';">D4_iot_tcp_server</span><span style="font-family: '宋体';">]</span><span style="font-family: '宋体';">例程实现“</span><span style="font-family: '宋体';">碰一碰</span><span style="font-family: '宋体';">”控制L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">灯的想法。</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">使用的并不是真正的</span><span style="font-family: '宋体';">“碰一碰”</span><span style="font-family: '宋体';">技术,只是基于tcp实现了webserver功能,然后手机端通过N</span><span style="font-family: '宋体';">FC</span><span style="font-family: '宋体';">能读取到这个</span><span style="font-family: '宋体';">webserver</span><span style="font-family: '宋体';">的H</span><span style="font-family: '宋体';">TTP</span><span style="font-family: '宋体';">并打开网页进行控制</span><span style="font-family: '宋体';">,所以我称之为“伪碰一碰”。</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">代码主要结合以下功能:</span></p>
<p style="margin-left: 80px;"><span style="font-family: '宋体';">1、wifi连接-静态I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">配置(D</span><span style="font-family: '宋体';">HCP</span><span style="font-family: '宋体';">后我一直获取不到准确的I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">,这个卡了很久最后只能妥协先用静态I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">了)</span></p>
<p style="margin-left: 80px;"><span style="font-family: '宋体';">2</span><span style="font-family: '宋体';">、基于</span><span style="font-family: '宋体';">tcp</span><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">server</span><span style="font-family: '宋体';">的</span><span style="font-family: '宋体';">webserver</span><span style="font-family: '宋体';">(提示:端口号必须是8</span><span style="font-family: '宋体';">0</span><span style="font-family: '宋体';">)</span></p>
<p style="margin-left: 80px;"><span style="font-family: '宋体';">3</span><span style="font-family: '宋体';">、P</span><span style="font-family: '宋体';">WM</span></p>
<p style="margin-left: 80px;"><span style="font-family: '宋体';">4</span><span style="font-family: '宋体';">、I</span><span style="font-family: '宋体';">2C+NFC</span></p>
<p><span style="font-family: '宋体';">二、</span><span style="font-family: '宋体';">实现功能</span><span style="font-family: '宋体';">演示</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">使用带N</span><span style="font-family: '宋体';">FC</span><span style="font-family: '宋体';">功能的手机,碰一碰可以获取到写入N</span><span style="font-family: '宋体';">FC</span><span style="font-family: '宋体';">的网页信息(我这里用iPhone XR演示)</span></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">N</span><span style="font-family: '宋体';">FC</span><span style="font-family: '宋体';">成功感应后,</span><span style="font-family: '宋体';">弹出如</span><span style="font-family: '宋体';">上</span><span style="font-family: '宋体';">信息</span><span style="font-family: '宋体';">,电机进入就能看到我写的非常简易的web了,如下图:</span></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p><span style="font-family: '宋体';"> 最上面显示当前L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">的状态和亮度百分比,下面一行则是点击对应的</span><span style="font-family: '宋体'; color: #0070C0;">here</span><span style="font-family: '宋体';">,</span><span style="font-family: '宋体';">可以实现后面对应描述的功能。第一个</span><span style="font-family: '宋体'; color: #0070C0;">here</span><span style="font-family: '宋体';">是关闭L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">灯,第二个</span><span style="font-family: '宋体'; color: #0070C0;">here</span><span style="font-family: '宋体';">是打开L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">并且是1</span><span style="font-family: '宋体';">00</span><span style="font-family: '宋体';">%的亮度。第三个</span><span style="font-family: '宋体'; color: #0070C0;">here</span><span style="font-family: '宋体';">就是N</span><span style="font-family: '宋体';">FC</span><span style="font-family: '宋体';">读取到的网页内容,切换L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">灯的开关状态。第四个</span><span style="font-family: '宋体'; color: #0070C0;">here</span><span style="font-family: '宋体';">点击后开关呼吸灯模式。</span></p>
<p><span style="font-family: '宋体';"> 原理解释:</span></p>
<p><span style="font-family: '宋体';"> 其实就是</span><span style="font-family: '宋体';">设置一个简单的类似</span><span style="font-family: '宋体';">http的服务器</span><span style="font-family: '宋体';">,</span></p>
<p><span style="font-family: '宋体';"> 服务器将根据请求设置</span><span style="font-family: '宋体';">LED</span><span style="font-family: '宋体';">的状态</span></p>
<p> <a href="http://server_ip/gpio/000">http://server_ip/gpio/0</a><a href="http://server_ip/gpio/000">00</a><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">关闭L</span><span style="font-family: '宋体';">ED </span></p>
<p> <a href="http://server_ip/gpio/050">http://server_ip/gpio/0</a><a href="http://server_ip/gpio/050">50</a><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">打开L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">,P</span><span style="font-family: '宋体';">WM</span><span style="font-family: '宋体';">输出</span><span style="font-family: '宋体';">50</span><span style="font-family: '宋体';">%</span></p>
<p> <a href="http://server_ip/gpio/100">http://server_ip/gpio/</a><a href="http://server_ip/gpio/100">100</a><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">全开L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">,P</span><span style="font-family: '宋体';">WM</span><span style="font-family: '宋体';">输出1</span><span style="font-family: '宋体';">00</span><span style="font-family: '宋体';">%</span><span style="font-family: '宋体';"> </span></p>
<p><span style="font-family: '宋体';"> (说明:输入数值必须是3位,输入数值范围0</span><span style="font-family: '宋体';">00</span><span style="font-family: '宋体';">~</span><span style="font-family: '宋体';">100</span><span style="font-family: '宋体';">)</span></p>
<p> <a href="http://server_ip/gpio/F">http://server_ip/gpio/</a><a href="http://server_ip/gpio/F">F</a><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">切换L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">开关状态</span><span style="font-family: '宋体';"> </span></p>
<p><span style="font-family: '宋体';"> (说明:</span><span style="font-family: '宋体';">关闭的时候就打开</span><span style="font-family: '宋体';">,比如上次关闭前打开P</span><span style="font-family: '宋体';">WM50</span><span style="font-family: '宋体';">%,那么再次打开还是保持5</span><span style="font-family: '宋体';">0</span><span style="font-family: '宋体';">%)</span></p>
<p> <a href="http://server_ip/lwip/B">http://server_ip/</a><a href="http://server_ip/lwip/B">lwip</a><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">打开一个测试界面</span></p>
<p><span style="font-family: '宋体';"> 其他错误输入,会打开一个4</span><span style="font-family: '宋体';">00</span><span style="font-family: '宋体';">的网站</span></p>
<p><span style="font-family: '宋体';"> server_ip</span><span style="font-family: '宋体';">就是连接的I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">,我这里用了静态I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">地址1</span><span style="font-family: '宋体';">92.168.1.200</span></p>
<p><span style="font-family: '宋体';"> 所以其实用电脑或者手机浏览器直接输入对应的网址就能控制L</span><span style="font-family: '宋体';">ED</span><span style="font-family: '宋体';">(注意必须在同一个局域网内哦)</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">演示视频:</span></p>
<p> <iframe allowfullscreen="true" frameborder="0" height="450" src="http://player.youku.com/embed/XNTE5NDI0MTMwOA" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p><span style="font-family: '宋体';">三、</span><span style="font-family: '宋体';">部分修改过的</span><span style="font-family: '宋体';">代码简单说明</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">1、wifi连接静态I</span><span style="font-family: '宋体';">P</span><span style="font-family: '宋体';">分配</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample\my_WPYP_LED\src\wifi_connect.c</span><span style="font-family: '宋体';">下</span></p>
<pre>
<code class="language-bash">//配置静态IP
if (g_lwip_netif)
{
hi_sta_set_addr(g_lwip_netif);
osEventFlagsSet(wifi_connect_evt_id, FLAGS_MSK1);
}</code></pre>
<p><span style="font-family: '宋体';">2、基于tcp</span><span style="font-family: '宋体';"> </span><span style="font-family: '宋体';">server</span><span style="font-family: '宋体';">的webserver</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample\my_WPYP_LED\wpyp_led_demo.c</span><span style="font-family: '宋体';">下:</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">这是接收tcp消息:</span></p>
<pre>
<code class="language-bash">if ((ret = lwip_recv(conn, recvbuf, sizeof(recvbuf), 0)) == -1) //等待tcp消息
{
printf("recv error \r\n");
lwip_close(conn);
return;
}</code></pre>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">这是</span><span style="font-family: '宋体';">部分接收信息后进行数据判断,正确后发送相应网页信息的代码</span><span style="font-family: '宋体';">:</span></p>
<pre>
<code class="language-bash">else if (strncmp((char *)recvbuf, "GET /lwip", 9) == 0)
{
lwip_write(conn, htmldata, sizeof(htmldata) - 1);
}</code></pre>
<p><span style="font-family: '宋体';">3、P</span><span style="font-family: '宋体';">WM</span><span style="font-family: '宋体';">呼吸灯效果</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample\my_WPYP_LED\wpyp_led_demo.c</span><span style="font-family: '宋体';">下:</span></p>
<pre>
<code class="language-bash">if (LED_BLN_flag == 1)
{
for (i = 0; i < 40000; i += 100)
{
if (LED_BLN_flag == 0)
break;
//输出不同占空比的PWM波
PwmStart(WIFI_IOT_PWM_PORT_PWM2, i, 40000); //0时LED灭,40000的分频160Mhz/40000 = 4Kh
usleep(10);
}
}</code></pre>
<p><span style="font-family: '宋体';">4、</span><span style="font-family: '宋体';">NFC</span><span style="font-family: '宋体';">写入</span><span style="font-family: '宋体';">web</span><span style="font-family: '宋体';">信息</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample\my_WPYP_LED\src\i2c_nfc.c</span><span style="font-family: '宋体';">下</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">其中【</span><span style="font-family: '宋体';">storeUrihttp</span><span style="font-family: '宋体';">】是有点点小修改哦</span><span style="font-family: '宋体';">,并且使用等待wifi连接成功后的事件</span></p>
<pre>
<code class="language-bash">while (1)
{
flags = osEventFlagsWait(wifi_connect_evt_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
printf("NFC EVEN Flags is %d\n", flags);
ret = storeUrihttp(NDEFFirstPos, (uint8_t *)WEB);
if (ret != 1)
{
printf("NFC Write Data Falied :%d ", ret);
}
usleep(10000); //延迟10ms
}</code></pre>
<p> </p>
<p><span style="font-family: '宋体';">四、移植说明</span></p>
<p><span style="font-family: '宋体';">1、源码分享</span></p>
<p> </p>
<p><span style="font-family: '宋体';">2、移植说明</span></p>
<p><span style="font-family: '宋体';"> 解压压缩包,后直接把文件夹添加到</span><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample</span><span style="font-family: '宋体';">路径下,然后修改</span><span style="font-family: '宋体';">applications\BearPi\BearPi-HM_Nano\sample\BUILD.gn</span><span style="font-family: '宋体';">文件,如下图就能使用了:</span></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p><span style="font-family: '宋体';">五、还存在的问题</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">1、没法使用D</span><span style="font-family: '宋体';">HCP</span><span style="font-family: '宋体';">,非常的遗憾</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">2、没有写W</span><span style="font-family: '宋体';">IFI</span><span style="font-family: '宋体';">断开重连的功能</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">3、配置W</span><span style="font-family: '宋体';">IFI</span><span style="font-family: '宋体';">时代码默认死的,没法使用手机配置</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">4、web写的有点low</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">后面将继续更新,不过要再学习华为云和M</span><span style="font-family: '宋体';">QTT</span><span style="font-family: '宋体';">后,希望能解决上面这些B</span><span style="font-family: '宋体';">UG</span><span style="font-family: '宋体';">,并且实行真的“</span><span style="font-family: '宋体';">碰一碰</span><span style="font-family: '宋体';">”。</span></p>
</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>
<p>没法使用DHCP,确实比较遗憾,这个没有写WIFI断开重连的功能是为什么呢</p>
<p>有那么点意思了!</p>
Jacktang 发表于 2021-8-16 22:39
楼主的列的几个存在问题
没法使用DHCP,确实比较遗憾,这个没有写WIFI断开重连的功能是为什么呢
<p>WIFI断开重连这个功能还没来得及写</p>
<p>被动刷新时用的定时轮询?</p>
<p>了解更多智能硬件相关产品~请戳:【华为开发者联盟生态市场智能硬件专区https://developer.huawei.com/consumer/cn/market/prod-list?categoryIdL1=7e857be8d4b24a7e97ddf5d941b38ca1&ha_source=mkt-thirdforum】</p>
页:
[1]