首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Raspberry PI -PI多个IP

Raspberry PI -PI多个IP
EN

Stack Overflow用户
提问于 2016-01-25 02:56:08
回答 1查看 191关注 0票数 0

想要的代码分析和修复或指出我在正确的方向,请。这么多的错误,有的克服了,有的没有。

程序运行在覆盆子PI2上,应该尝试和ping特定的IP地址,并返回结果。

这是编程的新手,你大概能看出来!不确定是否需要ping库,或者是否可以不使用ping库

代码语言:javascript
复制
import sys
import time
from pushbullet import Pushbullet
import serial

class Users(object):
    def __init__(self, name=None, ip=None):
        self.name = name
        self.ip = ip
        self.status = 'out'
pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed

userList = []
userList.append(Users("Ali", "192.18.1.14"))
userList.append(Users("Sophie", "192.18.1.9"))
userList.append(Users("TV", "192.18.1.7"))

try:
    while True: 
        print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
        for user in userList:
            result = os.system ("ping -n 1 " = user.ip)
            oldStatus = user.status
            if (result == 0):
                    #What we'll do if a device is detected
                if (oldStatus == 'out'):
                    push = pb.push_note("Home Pi", user.name + " is home")
                    user.status = 'in'
                print user.name + " is home"
            else:
                 #What we'll do if a device is NOT not detected
                if (oldStatus == 'in'):
                    push = pb.push_note("Home Pi", user.name + " has just left")
                    user.status = 'out'
                print user.name + " is out"

        print "Next check will be in 30 seconds"
        time.sleep(30)

        except (KeyboardInterrupt, SystemExit):
EN

回答 1

Stack Overflow用户

发布于 2016-01-25 03:28:45

我修改了你的代码才能工作,我没有pushbullet。将我的新代码与以前的代码进行比较,看看它们之间的区别和错误

代码语言:javascript
复制
import sys
import time
#from pushbullet import Pushbullet
#import serial
#you need import os
import os

class Users(object):
    def __init__(self, name=None, ip=None):
        self.name = name
        self.ip = ip
        self.status = 'out'
#pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed

userList = []
userList.append(Users("Notebook", "192.168.1.2"))
userList.append(Users("TV", "192.168.1.4"))

try:
    while True: 
        print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
        for user in userList:
            #result = os.system ("ping -n 1 " = user.ip)
        # to concatenate string you need + sign
            #result = os.system("ping -n 1 " + user.ip)
            # -n is a wrong option for ubuntu
            result = os.system("ping -c 1 " + user.ip)
            oldStatus = user.status
            if (result == 0):
                    #What we'll do if a device is detected
                if (oldStatus == 'out'):
                    #push = pb.push_note("Home Pi", user.name + " is home")
                    user.status = 'in'
                print user.name + " is home"
            else:
                 #What we'll do if a device is NOT not detected
                if (oldStatus == 'in'):
                    #push = pb.push_note("Home Pi", user.name + " has just left")
                    user.status = 'out'
                print user.name + " is out"

        print "Next check will be in 30 seconds"
        time.sleep(30)

#wrong identation
except (KeyboardInterrupt, SystemExit):
  sys.exit(0)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34980014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档