1

I am using a Orange Pi 2g IoT board, no graphical interface and distro Ubuntu 16.04. The board has a modem 2G that mostly works fine to send an URL to my Firebase application by a Python script, but sometimes the connection does not establish. It is a pppd connection through wvdial. I would like to be aware in terms of hardware (avulse LED on/off) if my modem 2G is connected or not.

Could anyone help me out with this?

Many thanks!

K3---rnc
  • 5,445
  • 2
  • 29
  • 41
Lucas Bastos
  • 99
  • 10
  • There is an extensive set of python packages on the python package index ( https://pypi.python.org/pypi?%3Aaction=search&term=raspberry&submit=search ) related to raspberry pi. maybe you find something that fits your needs? – Matthias Oct 12 '17 at 18:46

2 Answers2

1

If you can use an external python package: pip install netifaces.

With this package, you can test if the interface exists, and then test if you can get to google. This code is untested, but should get you very close.

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

UPDATE

You can log the output of ifconfig to a text file using

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

You can then parse this anyway you like. Here's one way that also checks to make sure that the ppp interface exists.

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

Just call pppParser().get_rx_bytes()

Matt Dale
  • 121
  • 7
  • Thanks for the input, Matt! Actually I have a limited data consumption. It would be not feasible for my device to try a request every couple minutes for example. It would compromise my product. I would like to go through a way to analyze the 'ifconfig' output and parse if there is RX(xx.xx) data sent. Would you think it is possible? – Lucas Bastos Oct 24 '17 at 18:39
  • Yes it's possible. I updated the answer with a simple text parser method. – Matt Dale Oct 25 '17 at 15:14
0

I don't know about python capabilities for this. But I propose that you use python (if you have to) to fork a process with one of the system utilities that give you the current state of the network devices. You could follow this line: Calling an external command in Python and call for example "ifconfig". Your ppp device should show up there.

Matthias
  • 3,228
  • 2
  • 23
  • 40
  • Thanks for your input Matthias. It is ok to call an external command in Python. I got this done by using import os library and write OS.System('mycommand') on the code. I can write for example "ifconfig". But the problem is how can I parse it so I can identify a ppp connection in this output? – Lucas Bastos Oct 21 '17 at 02:52
  • You could get away with just 'grepping' for the device. – Matthias Oct 21 '17 at 04:36