<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wechat &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/wechat/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:26:06 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]基于微信和Raspberry Pi的远程监控系统 / A Remote Monitoring System Based on WeChat &#038; Raspberry Pi</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9f%ba%e4%ba%8e%e5%be%ae%e4%bf%a1%e5%92%8craspberry-pi%e7%9a%84%e8%bf%9c%e7%a8%8b%e7%9b%91%e6%8e%a7%e7%b3%bb%e7%bb%9f-a-remote-monitoring-system-based-on-wechat-raspberry-pi/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9f%ba%e4%ba%8e%e5%be%ae%e4%bf%a1%e5%92%8craspberry-pi%e7%9a%84%e8%bf%9c%e7%a8%8b%e7%9b%91%e6%8e%a7%e7%b3%bb%e7%bb%9f-a-remote-monitoring-system-based-on-wechat-raspberry-pi/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 16 Mar 2014 14:47:19 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[remote monitoring system]]></category>
		<category><![CDATA[webcam]]></category>
		<category><![CDATA[wechat]]></category>
		<category><![CDATA[微信]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[远程监控系统]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5374</guid>

					<description><![CDATA[<p>
<a href="http://www.raspberrypi.org/" target="_blank" rel="noopener noreferrer">Raspberry Pi</a>是什么？<br />
引用维基百科的一句话：</p>
<blockquote>
<p>
		The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.</p>
</blockquote>
<p>简单地说，它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。<br />
<span style="color: rgb(0, 0, 255); ">In short, Pi is an ARM-based mini computer which has a credit card size.</span>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9f%ba%e4%ba%8e%e5%be%ae%e4%bf%a1%e5%92%8craspberry-pi%e7%9a%84%e8%bf%9c%e7%a8%8b%e7%9b%91%e6%8e%a7%e7%b3%bb%e7%bb%9f-a-remote-monitoring-system-based-on-wechat-raspberry-pi/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<a href="http://www.raspberrypi.org/" target="_blank" rel="noopener noreferrer">Raspberry Pi</a>是什么？<br />
引用维基百科的一句话：</p>
<blockquote>
<p>
		The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.</p>
</blockquote>
<p>简单地说，它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。<br />
<span style="color: rgb(0, 0, 255); ">In short, Pi is an ARM-based mini computer which has a credit card size.</span></p>
<p>前段时间公司举行hackathon比赛，我做了一套如题所示的系统，整个过程太累了，连续30个小时没睡，简直要了我半条命，现在我打算把它的主要实现记下来。<br />
<span id="more-5374"></span><br />
在阅读本文之前，请确保你已经阅读过我的另外几篇<a href="http://www.codelast.com/?p=5013" target="_blank" rel="noopener noreferrer"><span style="background-color: rgb(255, 160, 122); ">关于Pi的文章</span></a>，因为本文与它们有或多或少的关系。<br />
<span style="color: rgb(0, 0, 255); ">Before reading this article, please make sure that you&#39;ve read&nbsp;<a href="http://www.codelast.com/?p=5013" target="_blank" rel="noopener noreferrer"><span style="background-color: rgb(255, 160, 122); ">some other articles</span></a>&nbsp;of mine on Pi</span><span style="color: rgb(0, 0, 255); ">, because this article is more or less based on them.</span></p>
<p><span style="background-color: rgb(0, 255, 0); ">【1】</span>本文主要内容 <span style="color:#ff0000;">/</span> <span style="color:#0000ff;">Main content of this article</span><br />
构建一个具有旋转自由度的远程监控系统，使用微信（WeChat）向树莓派发送指令，再由树莓派直接控制步进电机带动摄像头旋转、拍照，并将照片回传到微信上。<br />
将这样一套设备放在某个地方，就可以通过微信监控其周围的情况了。<br />
先看演示效果（收到了从微信发送的指令之后，摄像头就转动了）：<br />
<embed align="middle" allowfullscreen="true" allowscriptaccess="always" height="400" quality="high" src="http://player.youku.com/player.php/sid/XNjg1NzQ5NzM2/v.swf" type="application/x-shockwave-flash" width="480"></embed><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【2】</span>实现方案 <span style="color:#ff0000;">/</span> <span style="color:#0000ff;">The implementation</span><br />
用下图表示：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/wechat_pi_monitoring_system_1_1.png" style="width: 400px; height: 323px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
最上方是用户，你就是你所处的位置。通过微信，用户添加了一个公众账号之后，可以向其发送消息。微信Server（腾讯的服务器）收到消息之后，会根据你在微信公众账号后台的设置，决定是直接回复这条消息（以固定内容），还是将其转发到另一台你自己指定的服务器上。显然，由于有树莓派的存在，我们必须要有另一台服务器（图中的&ldquo;Web Server&rdquo;）的存在，因为我们不可能将树莓派挂在微信Server上。</p>
<p>有了那台&ldquo;Web Server&rdquo;，Pi就可以与之通信，间接地收到来自微信Server（其实也就是用户）的指令了。收到指令后，Pi会执行它，要么拍照，要么控制步进电机带动摄像头转动。<br />
有人会说，为什么我们不能让微信Server直接与Pi交互？例如，我在Pi上架设一个Web Server，微信Server就可以直接和它通信了啊！<br />
没错，但是这样的条件要求太高了。这是因为：<br />
1）微信Server不接受任何服务器主动向它发送请求，只能是微信Server向其他服务器发送请求，其他服务器再回复；<br />
2）在中国的网络环境下，一个家庭宽带里接入的设备，通常都是没有公网IP的。如果你非要通过配置端口映射等方法来让外面的服务器可以直接访问到家里的服务器的话，配置太麻烦。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">【3】</span>中转服务器的角色<br />
中转服务器即图中的&ldquo;Web Server&rdquo;，它负责与微信Server交互、与Pi交互，将来自微信的指令转达到Pi那里，接收来自Pi的数据，等等。<br />
之所以叫它&ldquo;Web Server&rdquo;，是因为运行在其上的，是一个web应用程序。<br />
有人说，我自己从来都没有一台具有公网IP的服务器，怎么搭建这样一个web应用？不用急，有大量免费的互联网资源可以用！例如<a href="https://developers.google.com/appengine/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Google App Engine</span></a>（GAE），<a href="http://sae.sina.com.cn/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Sina App Engine</span></a>（SAE），<a href="http://developer.baidu.com/service" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Baidu App Engine</span></a>（BAE），等等。<br />
这些*AE是什么东西？它们的学名可以统称为&ldquo;公有云计算平台&rdquo;，用人话说就是&ldquo;为你提供一台虚拟服务器，可以在上面搭建web应用程序&rdquo;。<br />
GAE在国内被墙了，SAE和BAE是仿GAE搞起来的，SAE比BAE先出世，于是我选了SAE。<br />
SAE支持在上面用Python、PHP、Java来开发web应用，像我开发的这个应用，SAE的免费配额够用好久了。<br />
另外我要提的是，SAE只是在最近，才开放了一个实时服务&mdash;&mdash;Channel。这个东西是个什么概念？你在SAE上的程序是不支持bind()的，也就是你不能写一个程序监听某个特定端口，来实现一个服务，SAE出于安全性考虑才做了这种限制。为了弥补这种缺陷，SAE推出了一种名为Channel的服务：</p>
<blockquote>
<p>
		&ldquo;Channel是SAE提供的实时消息推送服务。通过在浏览器和SAE服务端之间建立长连接，使得应用可以方便的向Javascript客户端实时的推送消息。开发者可以利用Channel服务开发自己的实时性要求比较高的应用，如游戏、在线聊天室、在线直播等。&rdquo;</p>
</blockquote>
<p>但是如果我没记错的话，它的文档中写过，最好不要通过Channel服务传送超过4K的数据&mdash;&mdash;这就有点弱了，要知道，摄像头拍摄的一张照片几乎是必须要超过4K的，所以，按我的理解，想通过SAE Channel服务来实时化中转服务器和Pi之间的交互，还是不现实的。<br />
因此，我干脆就把中转服务器弄得简单一点，不去想完全实时化的情景，而是在收到微信Server发来的指令后，存储在MySQL中，由Pi周期性轮循中转服务器来获取指令。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">【4】</span>微信Server是怎么将用户指令发到中转服务器上的<br />
首先你要创建一个微信公众账号，并申请成为一名微信开发者。然后，你在微信中查找并添加这个公众账号，就可以向它发送文本消息（即本文所说的指令）了。这个消息其实是发送到了微信Server上，至于微信Server是将该消息转发出去，还是直接以固定内容回复，这是由你在微信公众平台的后台设置的。在我的这个项目中，当然是让微信Server发送到我的中转服务器去处理，按微信Server的要求，必须要在5秒内给它回复（否则就无效了）。<br />
我个人觉得，微信的开发文档写得不好，有些至关重要的东西说得不明不白，很多人在微信开发者论坛中发帖问，才找到了答案。例如，一个&ldquo;订阅号&rdquo;是没有权限将图片之类的素材上传到微信Server上的，只有&ldquo;服务号才行，但是对我这样的个人来说，申请到&ldquo;服务号&rdquo;是完全不可能的事情，这就决定了我的技术方案要适应它&mdash;&mdash;我不能把Pi拍摄的照片发送到微信Server上，而只能放在中转服务器（SAE）上，只回传图片的public URL给微信。这也就导致了本系统的安全性得不到保证&mdash;&mdash;一个用户控制自己的Pi拍摄的照片，不应该可以在公网上被检索到。虽然SAE可以限制指定域名才能访问其Storage中的文件，但是我觉得这样安全性仍然不够。<br />
无论如何，在Hackathon比赛时间有限，我无法完善这个系统，能让它按设想的&ldquo;动起来&rdquo;，我就心满意足了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">【5】</span>Pi控制步进电机以及摄像头拍照<br />
Pi控制摄像头拍照最终使用了V4L2来实现（具体可以看<a href="http://www.codelast.com/?p=7873" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这里</span></a>），在Hackathon的过程中，我为了拍照程序不稳定的头疼问题折腾了一个多小时，最终达到了一个&ldquo;可接受&rdquo;的结果。<br />
至于Pi如何控制步进电机，这里也不详述了。</p>
<p><span style="background-color:#00ff00;">【6】</span>系统物理结构搭建<br />
这里说的物理结构搭建，是指我怎样把上面林林总总的各种东西用一个外壳包装起来？<br />
先看成品的实拍图：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/wechat_pi_monitoring_system_4.jpg" style="width: 450px; height: 431px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
近距离，仔细看摄像头和步进电机的连接方式：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/wechat_pi_monitoring_system_7.jpg" style="width: 486px; height: 309px;" /></div>
<p>俯视图：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/wechat_pi_monitoring_system_11.jpg" style="width: 470px; height: 610px;" /></div>
<p>你一下就明白了：这不是用乐高积木搭建起来的嘛！<br />
准确地说，是用山寨的乐高积木搭建起来的。淘宝上买的仿乐高货，积木个头很大一块，可以把树莓派、步进电机驱动板、步进电机很好地固定在中间，动弹不得，并且拆卸方便，对于一个实验性的系统来说，堪称完美包装器材。<br />
仔细看中间那张图，步进电机和摄像头是怎么连接在一起的？看起来简单，其实它是我花费了相当大的功夫，以损失了相当大的身体健康为代价才做出来的：<br />
首先，摄像头买来的时候有一个比较大的底座（市面上的非特殊定制的摄像头都差不多吧），由于两块&ldquo;乐高&rdquo;积木中间的空间容不下那么大的底座（并且底座也不是平的，会影响转动），所以必须要切割底座。切割是个痛苦的过程，电锯与塑料高速摩擦产生了很多有毒气体，味道很臭，我戴着PM 2.5的口罩仍然感觉无防护作用（真应该把3M的活性炭防毒面具带过去的）。<br />
但这只是第一步！<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
就算切割好了摄像头的底座，要把它粘贴到步进电机上那根细细的转轴上，也不是件容易的事情。为了让这个粘贴更牢靠，我的设计是：先切割一块圆的小亚克力板出来，中间钻个洞，刚好有电机轴那么大，然后先把这块小圆板固定、粘贴到电机转轴上，最后再把摄像头底座粘贴到这块亚克力板上。<br />
事实证明这样的设计粗陋却非常有效，最后摄像头被牢牢地固定在了步进电机上，并且不影响转动。</p>
<p><span style="background-color:#00ff00;">【7】</span>总结<br />
Hackathon是一种精神，或许我做的东西和牛人们的发明相比什么也不算，或许我做的东西和很多科技产品相比简陋无比，但是，它毕竟是我一点一滴做出来的，它的每根导线、每个流动的字节之间都流淌着我的血液，看到它出生的那一刻，那种激动的感觉无法形容！<br />
这也是我热爱技术的原因之一&mdash;&mdash;尽管我渺小，但是它却给了我想像发挥的空间，每个人都能在它里面找到自己的位置。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9f%ba%e4%ba%8e%e5%be%ae%e4%bf%a1%e5%92%8craspberry-pi%e7%9a%84%e8%bf%9c%e7%a8%8b%e7%9b%91%e6%8e%a7%e7%b3%bb%e7%bb%9f-a-remote-monitoring-system-based-on-wechat-raspberry-pi/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
