查看: 3500|回复: 0

[项目] 通过Socket通信控制LED

[复制链接]
  • TA的每日心情
    开心
    2014-2-20 10:23
  • 签到天数: 45 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2017-8-14 13:44:46 | 显示全部楼层 |阅读模式
    分享到:
    对于树莓派来说,可以通过ssh和电脑进行互联,所以一般的应用来说,不需要socket通信,只要打开ssh在树莓派上运行写好的Python或者C程序即可。但是,有的project需要多个sensor,如果仅仅通过打开多个ssh来运行不同程序,这样sensor搜集的数据就不直观,因此就需要在PC上建立一个Server来和树莓派通信,从而可以把所有sensor以及其他devices需要的信号都集中显示在一个PC端的程序上。如果会网页编程的话,在树莓派上建立一个WEB服务器应该也很好实现这个功能。如果不会网页编程,那只好使用socket通信了。最简单的开始,也就是通过socket控制LED的亮与熄。
    下面是Server端的程序:# TCP-Serverimport socket
    # 1. 创建 socket 对象
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 2. 将 socket 绑定到指定地址 (本机地址)address = ('192.168.1.102', 8889)
    s.bind(address)
    # 3. 接收连接请求# listen(backlog), The backlog argument specifies the maximum number of queued connections and should be at least 0;
    # the maximum value is system-dependent (usually 5), the minimum value is forced to 0.
    s.listen(5)
    print 'Waiting for connection...'
    # 4. 等待客户请求一个连接# 调用 accept 方法时,socket 会进入 "waiting" 状态。
    # accept方法返回一个含有两个元素的元组 (connection, address)。
    # 第一个元素 connection 是新的 socket 对象,服务器必须通过它与客户通信;
    # 第二个元素 address 是客户的 Internet 地址。
    new_s, addr = s.accept()
    print 'Got connect from', addr
    # 5. 处理:服务器和客户端通过 send 和 recv 方法通信
    command = ' '
    print ('Please enter 1 to turn on the led, 2 to turn off the led,3 to disconnect the communication:')
    while True:
        command = raw_input()
        new_s.send(command)
        data = new_s.recv(512)
        print data    if data == 'Communication is disconnected':
           break         
    # 6. 传输结束,关闭连接
    new_s.close()
    s.close()
    接下来是Client端程序:
    # TCP-Clientimport RPi.GPIO as GPIO
    import time
    import socket
    address = ('192.168.1.102', 8889)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(address)
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(27, GPIO.OUT)
    GPIO.setup(17, GPIO.OUT)
    GPIO.output(17, False)
    GPIO.output(27, False)
    while True:
        command = s.recv(512)
        if command == '1':
            GPIO.output(27, True)
            GPIO.output(17, False)
            s.send('Command 1 received by client')
            print "Command is ", command
        elif command == '2':
            GPIO.output(27, False)
            GPIO.output(17, True)
            s.send('Command 2 received by client')
             print "Command is ", command
        elif command == '3':
            s.send('Communication is disconnected')
            print "Command is ", command
            break
        else:
            s.send('Please enter 1, 2 or 3')
            print "Command is ", commands.close()
    我们只需要在PC上运行Server的程序,在Raspberry Pi上运行Client的程序便可以控制LED的亮与熄了,很简单。

    回复

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-3-28 19:50 , Processed in 0.118740 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.