标签:pre 新浪 sleep setup 接下来 cleanup 无线网卡 输入 gpio
本文由树莓派爱好者ITJoker 编辑,转载请注明出处。本人也有新浪博客同样是树莓派爱好者ITJoker可能名字少了IT。。。
sudo nano start.py
然后Ctrl+x再输入Y加回车即可。
输入命令:
sudo python start.py
即可运行!
1 ########################################### 2 #Writer: Joker # 3 #Time: 2017.6.25 # 4 ########################################### 5 # -*- coding: utf-8 -*- 6 import RPi.GPIO as GPIO 7 from socket import * 8 import time 9 def t_init(): 10 GPIO.setmode(GPIO.BOARD) 11 GPIO.setwarnings(False) 12 GPIO.setup(11,GPIO.OUT) 13 GPIO.setup(12,GPIO.OUT) 14 GPIO.setup(13,GPIO.OUT) 15 GPIO.setup(15,GPIO.OUT) 16 def t_stop(): 17 GPIO.output(11, False) 18 GPIO.output(12, False) 19 GPIO.output(13, False) 20 GPIO.output(13, False) 21 GPIO.output(15, False) 22 time.sleep(1) 23 GPIO.cleanup() 24 def t_up(): 25 t_init() 26 GPIO.output(11, True) 27 GPIO.output(12, False) 28 GPIO.output(13, True) 29 GPIO.output(15, False) 30 time.sleep(1) 31 GPIO.cleanup() 32 33 def t_down(): 34 t_init() 35 GPIO.output(11, False) 36 GPIO.output(12, True) 37 GPIO.output(13, False) 38 GPIO.output(15, True) 39 time.sleep(1) 40 GPIO.cleanup() 41 42 def t_right(): 43 t_init() 44 GPIO.output(11, False) 45 GPIO.output(12, True) 46 GPIO.output(13, True) 47 GPIO.output(15, False) 48 time.sleep(1) 49 GPIO.cleanup() 50 def t_left(): 51 t_init() 52 GPIO.output(11, True) 53 GPIO.output(12, False) 54 GPIO.output(13, False) 55 GPIO.output(15, True) 56 time.sleep(1) 57 GPIO.cleanup() 58 ######################################################### 59 #Writer: Joker # 60 #Time: 2017.6.25 # 61 ######################################################### 62 def commands (cmd): 63 if cmd == ‘d‘: 64 t_down() 65 elif cmd == ‘s‘: 66 t_stop() 67 elif cmd == ‘u‘: 68 t_up() 69 elif cmd == ‘l‘: 70 t_left() 71 elif cmd == ‘r‘: 72 t_right() 73 def execute(command): 74 print command 75 commands(command) 76 HOST =‘192.168.1.103‘ #the ip of raspberry pi 77 PORT = 8888 #the port of raspberry pi 78 s= socket(AF_INET, SOCK_STREAM) 79 s.bind((HOST, PORT)) 80 s.listen(1000) 81 print (‘listening on 8888‘) 82 while 1: 83 conn, addr = s.accept() 84 print (‘Connected by:‘, addr) 85 while 1: 86 command= conn.recv(20).replace(‘\n‘,‘‘) 87 if command == ‘g‘: 88 conn.close 89 break 90 elif not command:break 91 a=bytearray(command) 92 execute(a) #command 93 conn.close() 94 ############################################################################
树莓派小车By 树莓派爱好者ITJoker(通过python socket通信实现树莓派视频小车)
标签:pre 新浪 sleep setup 接下来 cleanup 无线网卡 输入 gpio
原文地址:http://www.cnblogs.com/ITJoker/p/7279390.html