极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 44850|回复: 15

树莓派+python做的智能远程控制原型

[复制链接]
发表于 2012-10-26 17:48:16 | 显示全部楼层 |阅读模式
本帖最后由 qtsharp 于 2012-10-27 10:49 编辑

原文:http://www.cnblogs.com/qtsharp/archive/2012/10/26/2740924.html

使用的工具:

树莓派+python+django+pyserial+STC89C52单片机。


树莓派通过usb转串口与单片机,linux上自带usb转串口芯片的驱动,真爽!

网页与后台使用django,通过django调用pyserial库向单片机发送命令。

基于这种原型,可以很方便的利用手机浏览器控制家里的电器,好好利用django的账户安全功能,又可以做到远程控制需要的安全保密性!

树莓派+python+django+pyserial这个方案,是我认为在学习成本、搭建速度、开发速度、安全性、易用度、客户端无关性方面比较完美的集合。

视频里使用的手机QQ浏览器,由于手机网络不给力,并且又通过qq服务器压缩转发,所以能看出延迟,如果用电脑操作效果更好。





1、这个方案其实完全可以拿掉单片机,直接将python+django+rpi.gpio结合可以做同样的事,一个树莓派结合继电器就可以做远程网页控制了。

2、但是直接通过单片机利用蓝牙芯片进行串口连接,可以降低无线控制系统的成本。

3、由于网页使用python语言编写,所以可以十分方便的进行本地操作,更有超多的现成库,可实现的功能远远比网页脚本语言强大得多。

4、估计没几个能有比django的admin模块更简单的健全的账户安全系统了。

5、其实整个系统的核心就在django上,所以这个系统运行在任何一个linux终端上上均可以,不单单是树莓派。

6、但是树莓派最大的优势就是低功耗,高度优化的系统和rpi.gpio,作为一个日常低负载的服务器没有人会不喜欢——如果CPU能再给力点的话。



相关资料:

1、django教程:http://djangobook.py3k.cn/2.0/ 如果碰到无法理解的问题,别急,去https://docs.djangoproject.com/en/1.4/找找答案。

2、51单片机的串口例程:http://www.elecfans.com/emb/danpianji/20110509197678.html

3、PySerial方面完全可以用这里的代码:http://www.shumeipai.net/read.php?tid=1118&ds=1#11689

4、下面贴一下django里的部分app代码:


[pre lang="ython" line="1"]control.py:

#!/usr/bin/env python
import serial

class Control():
     def __init__(self,device='/dev/ttyUSB0',BAUD=4800):
         self.client = serial.Serial(device,BAUD,timeout=1)
     def command(self,CMD):
         try:
             self.client.write(CMD)
             self.client.close()
         except:
             pass


views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect, Http404
from control import Control

def control(request):
     DEMO=None
     if request.method == 'POST':
         if 'CMD' in request.POST:
             CON = Control()
             CMD = request.POST['CMD']
             CON.command(CMD)
         if 'demo' in request.POST:
             DEMO=request.POST['demo']
         if 'not_demo' in request.POST:
             DEMO=None
     return render_to_response('controller.html', {'demo'EMO}, context_instance=RequestContext(request))[/code]

再贴上有点偏离主题的视频里示范网页的html模板代码:


[pre lang="html" line="1"]{% extends "basepage.html" %}

{% block title %}远程控制{% endblock %}
{% block content %}
{% if not demo %}
<form action="" method="post">{% csrf_token %}
<p><input type="submit" name='demo' title='视频演示' accesskey="y" value="视频演示"></button></p>
</form>
<form action="" method="post">{% csrf_token %}
<p><input type="submit" name='CMD' title='开' accesskey="k" value="01">开</button></p>
<p><input type="submit" name='CMD' title='关' accesskey="g" value="00">关</button></p>
<p><input type="submit" name='CMD' title='闪' accesskey="s" value="88">闪</button></p>
</form>
{% endif %}
{% if demo %}
<form action="" method="post">{% csrf_token %}
<p><input type="submit" name='not_demo' title='返回控制' accesskey="f" value="返回控制"></button></p>
</form>
<br>
<embed src="http://player.youku.com/player.php/sid/XNDY2Nzg2NjI0/v.swf" allowFullScreen="true" quality="high" width="480" height="400" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>
{% endif %}
{% endblock%}[/code]
回复

使用道具 举报

发表于 2012-10-26 23:05:12 | 显示全部楼层
MARK,感谢楼主分享
回复 支持 反对

使用道具 举报

发表于 2012-10-27 08:16:36 | 显示全部楼层
貌似又要学新语言了
回复 支持 反对

使用道具 举报

发表于 2012-10-27 11:01:21 | 显示全部楼层
我还是建议尝试用主流的东西搭建吧!
rasp-pi +web.py+pyserial+arduino

建议放弃python的django框架,选取精干web.py直接可以集成进去pyserial和arduino通讯
放弃51单片机选用arduino。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-27 11:19:51 | 显示全部楼层
本帖最后由 qtsharp 于 2012-10-27 12:16 编辑
gaoshine 发表于 2012-10-27 11:01
我还是建议尝试用主流的东西搭建吧!
rasp-pi +web.py+pyserial+arduino


一开始也考虑了,但是arduino类似的东西太多了,没必要再翻出来。

而且51的成本小太多了,扩展也容易,晶振、电阻、蓝牙芯片同时焊到一个DIP40插座上,插上个51芯片,无线控制板就出来了。

这玩意的前台这么简单用任何框架都可以做。

但是django的session和admin能让用户验证功能迅速做起来,我这里使用django做就是图个方便。
回复 支持 反对

使用道具 举报

发表于 2012-10-27 12:33:56 | 显示全部楼层
其实有没有人在 Raspberry Pi 上写JAVA呢?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-27 13:04:44 | 显示全部楼层
wing 发表于 2012-10-27 12:33
其实有没有人在 Raspberry Pi 上写JAVA呢?

你写不就有了
回复 支持 反对

使用道具 举报

发表于 2012-10-28 15:57:13 | 显示全部楼层
wing 发表于 2012-10-27 12:33
其实有没有人在 Raspberry Pi 上写JAVA呢?

有一个基于java的 Raspi项目,感兴趣的朋友可以参考:
https://bitbucket.org/sbub/raspberry-pi-gpio-web-control/overview
也做成web方式的,简单说明如下(具体见上面链接):

Getting Started
Install Java, e.g. sudo apt-get install openjdk-6-jdk
Check that you can call java from the command line, e.g. java -version
Install Maven2 sudo apt-get install maven2
Install Mercurial sudo apt-get install mercurial
Get the project hg clone https://bitbucket.org/sbub/raspberry-pi-gpio-web-control
Get Winstone Servlet Container at http://winstone.sourceforge.net/ and put the jar in the same directory as the start.sh.
Build the project mvn package (It takes about 3-7 minutes to build on Raspberry Pi).
Check the location and name of WINSTONE_JAR in start.sh
Copy gpio.conf.MUST_BE_CHANGED to gpio.conf and configure it.
Start the servlet container sudo ./start.sh. You may have to mark the scripts executable chmod a+x start.sh stop.sh first.
Make a request, e.g. http://raspberrypi:8080/handle?g0=1&g1=0
It will return {"g1":0,"g0":1} (and you may check the log).
Stop the servlet container sudo ./stop.sh. Make sure that there are no old Winstone processes running with old configurations! You can check that with ps -ef| grep java.
To enable cronjobs, you need to copy cron.conf.MAY_BE_CHANGED to cron.conf and configure it
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-29 07:04:19 | 显示全部楼层
gaoshine 发表于 2012-10-28 15:57
有一个基于java的 Raspi项目,感兴趣的朋友可以参考:
https://bitbucket.org/sbub/raspberry-pi-gpio-w ...

好东西!                              
回复 支持 反对

使用道具 举报

发表于 2012-11-1 21:29:37 | 显示全部楼层
没有可用的显示器
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-2 11:16:43 | 显示全部楼层
萧芸凤 发表于 2012-11-1 21:29
没有可用的显示器

这个用不到显示器吧
回复 支持 反对

使用道具 举报

发表于 2012-11-3 10:02:17 | 显示全部楼层
感觉国内的树莓派玩家不多啊
回复 支持 反对

使用道具 举报

发表于 2012-11-4 18:10:09 | 显示全部楼层
萧芸凤 发表于 2012-11-1 21:29
没有可用的显示器

不要显示器也可以玩,直接SSH远程登录,当服务器用吧
回复 支持 反对

使用道具 举报

发表于 2013-2-14 23:16:22 | 显示全部楼层
最近才发现原来ORACLE官网有介绍
http://www.oracle.com/technetwor ... errypi-1704896.html
回复 支持 反对

使用道具 举报

发表于 2013-6-14 02:10:45 | 显示全部楼层
gaoshine 发表于 2012-10-27 11:01
我还是建议尝试用主流的东西搭建吧!
rasp-pi +web.py+pyserial+arduino

这点赞同,django搞这个太笨重了
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 09:29 , Processed in 0.041909 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表