chzhewl 发表于 2013-7-26 13:02
使用中感觉这款zigbee 还是不错的, 在家里隔两堵墙也可以用,重启后可以迅速进入网络. 很稳定.至今未发现 ...
嗯,不错,购买时是买的1个主机(协调器),几个路由的方案吗? 还是别的? 他家的zigbee没用过,不太了解。语音模块,之前想买这款来着:http://item.taobao.com/item.htm?spm=a1z10.1.w4004-1912740700.43.1x4yEO&id=25039504519 主要看着挺小巧的,听过例子音频,感觉还行。我现在是用的树莓派,跑的下边这个python角本,iphone运行了一个netio的软件,通过socket和这个角本通讯,基本能达到控制灯的效果,不过还有很多不尽如人意的地方,期待也能完整读读chzhewl 兄的python:)
- # coding=utf-8
- import socket
- import SocketServer
- import os
- import RPi.GPIO as GPIO
- import time
- import serial
- # Setup serial connection to arduino
- DEVICE = "/dev/ttyAMA0"
- BAUD = 9600
- ser = serial.Serial(DEVICE, BAUD)
- #Variables
- comingHomeStatus = 0
- class MyTCPHandler(SocketServer.BaseRequestHandler):
- """
- The RequestHandler class for our server.
- It is instantiated once per connection to the server, and must
- override the handle() method to implement communication to the
- client.
- """
- def handle(self):
- global comingHomeStatus, ser
- while 1:
- # self.request is the TCP socket connected to the client
- self.data = self.request.recv(1024).strip()
- if not self.data: break
- print "Incoming connection from: {}".format(self.client_address[0])
- print "Received data:", self.data
- #LED1
- if self.data == 'led1 on':
- ser.write('{"ID":1,"things":"light","command":"on"}')
- print 'received on'
- self.request.sendall('led1 open')
- elif self.data == 'led1 off':
- ser.write('{"ID":1,"things":"light","command":"off"}')
- print 'received off'
- self.request.sendall('led1 close')
- #LED2
- if self.data == 'led2 on':
- ser.write('{"ID":2,"things":"light","command":"on"}')
- print 'received on'
- self.request.sendall('led2 open')
- elif self.data == 'led2 off':
- ser.write('{"ID":2,"things":"light","command":"off"}')
- print 'received off'
- self.request.sendall('led2 close')
- ### GROUPED LIGHTS
- #Coming-home-lights
- if self.data == 'comingHomeOn':
- self.request.sendall('On')
- GPIO.output(5, True)
- GPIO.output(11, True)
- GPIO.output(12, True)
- comingHomeStatus = 1
- elif self.data == 'comingHomeOff':
- self.request.sendall('Off')
- GPIO.output(5, False)
- GPIO.output(11, False)
- GPIO.output(12, False)
- comingHomeStatus = 0
-
- #Turn all indoor lights off
- if self.data == 'allIndoorLightsOff':
- print "Turning all indoor lights off"
- GPIO.output(5, False) #Garage roof light
- GPIO.output(7, False) #Workshop roof light
- GPIO.output(8, False) #Storage roof light
- self.request.sendall('.')
- #Turn all outdoor lights off
- if self.data == 'allOutdoorLightsOff':
- print "Turning all outdoor lights off"
- GPIO.output(11, False) #Driveway light
- GPIO.output(12, False) #Carport light
- GPIO.output(13, False) #Workshop light
- GPIO.output(16, False) #Storage light
- self.request.sendall('.')
-
- ### INDOOR LIGHTS
- #Garage roof light
- if self.data == 'garageRoofOn':
- print "Turning garage roof light on"
- GPIO.output(5, True)
- self.request.sendall('.')
- elif self.data == 'garageRoofOff':
- print "Turning garage roof light off"
- GPIO.output(5, False)
- self.request.sendall('.')
-
- #Workshop roof light
- if self.data == 'workshopRoofOn':
- print "Turning workshop roof light on"
- GPIO.output(7, True)
- self.request.sendall('.')
- elif self.data == 'workshopRoofOff':
- print "Turning workshop roof light off"
- GPIO.output(7, False)
- self.request.sendall('.')
- #Storage roof light
- if self.data == 'storageRoofOn':
- print "Turning storage roof light on"
- GPIO.output(8, True)
- self.request.sendall('.')
- elif self.data == 'storageRoofOff':
- print "Turning storage roof light off"
- GPIO.output(8, False)
- self.request.sendall('.')
- ### OUTDOOR LIGHTS
- #Driveway light
- if self.data == 'drivewayOn':
- print "Turning driveway light on"
- GPIO.output(11, True)
- self.request.sendall('.')
- elif self.data == 'drivewayOff':
- print "Turning driveway light off"
- GPIO.output(11, False)
- self.request.sendall('.')
- #Carport light
- if self.data == 'carportOn':
- print "Turning carport light on"
- GPIO.output(12, True)
- self.request.sendall('.')
- elif self.data == 'carportOff':
- print "Turning carport light off"
- GPIO.output(12, False)
- self.request.sendall('.')
-
- #Workshop light
- if self.data == 'workshopOn':
- print "Turning workshop light on"
- GPIO.output(13, True)
- self.request.sendall('.')
- elif self.data == 'workshopOff':
- print "Turning workshop light off"
- GPIO.output(13, False)
- self.request.sendall('.')
-
- #Storage light
- if self.data == 'storageOn':
- print "Turning storage light on"
- GPIO.output(16, True)
- self.request.sendall('.')
- elif self.data == 'storageOff':
- print "Turning storage light off"
- GPIO.output(16, False)
- self.request.sendall('.')
- ### STATUS SENDS
- #Garagedoor button text
- if self.data == 'door_button':
- if GPIO.input(15):
- self.request.sendall('Close')
- #GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- print "Sending buttontext: Close"
- else:
- self.request.sendall('Open')
- #GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- print "Sending buttontext: Open"
- #Garagedoor position
- if self.data == 'door_status':
- #print "Sending Door status: ", GPIO.input(15)
- if GPIO.input(15):
- print "Sending that garagedoor is open"
- self.request.sendall('open')
- else:
- print "Sending that garagedoor is closed"
- self.request.sendall('closed')
- #Coming-home-lights status
- if self.data == 'comingHomeStatus':
- if comingHomeStatus == 1:
- self.request.sendall('On')
- print "Sending that coming home lights are on"
- else:
- self.request.sendall('Off')
- print "Sending that coming home lights are off"
-
- if __name__ == "__main__":
- HOST, PORT = "", 54321
- # Create the server, binding to localhost on port 54321
- server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
- # Activate the server; this will keep running until you
- # interrupt the program with Ctrl-C
- server.serve_forever()
复制代码 |