2017-10-10 7 views
0

J'utilise une carte IoT Orange Pi 2g, pas d'interface graphique et une distribution Ubuntu 16.04. La carte a un modem 2G qui fonctionne principalement bien pour envoyer une URL à mon application Firebase par un script Python, mais parfois la connexion ne s'établit pas. C'est une connexion pppd via wvdial. Je voudrais être au courant en termes de matériel (LED avulse on/off) si mon modem 2G est connecté ou non.Comment puis-je identifier via un script python si j'ai une connexion PPP et si oui, allumer une LED?

Quelqu'un pourrait-il m'aider?

Merci beaucoup!

+0

Il y a un vaste ensemble de paquets de python sur l'index paquet python (https://pypi.python.org/pypi?%3Aaction=search&term= framboise & submit = recherche) liée à la framboise pi. peut-être trouvez-vous quelque chose qui correspond à vos besoins? – Matthias

Répondre

0

Je ne connais pas les fonctionnalités de python pour cela. Mais je propose que vous utilisiez python (si vous devez) pour faire un fork avec un des utilitaires système qui vous donne l'état actuel des périphériques réseau. Vous pouvez suivre cette ligne: Calling an external command in Python et appeler par exemple "ifconfig". Votre appareil ppp devrait apparaître là-bas.

+0

Merci pour votre contribution Matthias. Il est correct d'appeler une commande externe en Python. J'ai fait cela en utilisant la bibliothèque d'importation os et écrire OS.System ('mycommand') sur le code. Je peux écrire par exemple "ifconfig". Mais le problème est comment puis-je l'analyser afin que je puisse identifier une connexion ppp dans cette sortie? –

+0

Vous pourriez vous contenter de «grepping» pour l'appareil. – Matthias

0

Si vous pouvez utiliser un package python externe: pip install netifaces.

Avec ce package, vous pouvez tester si l'interface existe, puis tester si vous pouvez accéder à google. Ce code n'a pas été testé, mais devrait vous être très proche.

import netifaces 
import requests 

ppp_exists = False 
try: 
    netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
    ppp_exists = True 
except: 
    ppp_exists = False 

# you have an interface, now test if you have a connection 
has_internet = False 
if ppp_exists == True: 
    try: 
     r = requests.get('http://www.google.com', timeout=10) # timeout is necessary if you can't access the internet 
     if r.status_code == requests.codes.ok: 
      has_internet = True 
     else: 
      has_internet = False 
    except requests.exceptions.Timeout: 
     has_internet = False 

if ppp_exists == True and has_internet == True: 
    # turn on LED with GPIO 
    pass 
else: 
    # turn off LED with GPIO 
    pass 

MISE À JOUR

Vous pouvez connecter la sortie de ifconfig dans un fichier texte à l'aide

os.system('ifconfig > name_of_file.txt') 

Vous pouvez ensuite analyser cela de toute façon vous le souhaitez. Voici une façon de vérifier également que l'interface ppp existe.

import os 
import netifaces 

THE_FILE = './ifconfig.txt' 

class pppParser(object): 
    """ 
    gets the details of the ifconfig command for ppp interface 
    """ 

    def __init__(self, the_file=THE_FILE, new_file=False): 
     """ 
     the_file is the path to the output of the ifconfig command 
     new_file is a boolean whether to run the os.system('ifconfig') command 
     """ 
     self.ppp_exists = False 
     try: 
      netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
      self.ppp_exists = True 
     except: 
      self.ppp_exists = False 
     if new_file: 
      open(the_file, 'w').close() # clears the contents of the file 
      os.system('sudo ifconfig > '+the_file) 
     self.ifconfig_text = '' 
     self.rx_bytes = 0 
     with open(the_file, 'rb') as in_file: 
      for x in in_file: 
       self.ifconfig_text += x 

    def get_rx_bytes(self): 
     """ 
     very basic text parser to gather the PPP interface data. 
     Assumption is that there is only one PPP interface 
     """ 
     if not self.ppp_exists: 
      return self.rx_bytes 
     ppp_text = self.ifconfig_text.split('ppp')[1] 
     self.rx_bytes = ppp_text.split('RX bytes:')[1].split(' ')[0] 
     return self.rx_bytes 

Il suffit d'appeler pppParser(). Get_rx_bytes()

+0

Merci pour la contribution, Matt! En fait, j'ai une consommation de données limitée. Il ne serait pas possible pour mon appareil d'essayer une requête toutes les deux minutes par exemple. Cela compromettrait mon produit. Je voudrais passer par un moyen d'analyser la sortie 'ifconfig' et analyser si il y a des données RX (xx.xx) envoyées. Pensez-vous que c'est possible? –

+0

Oui, c'est possible. J'ai mis à jour la réponse avec une méthode simple d'analyseur de texte. –