Connection Status Return of 0 with connection

Hey folks,
I am using a Raspberry Pi running Raspberry Pi OS with a SARA-U201 2G/3G Nova.

I am using the .getConnectionStatus() function in my script, found here: https://www.hologram.io/references/python-sdk

However, the return code of this function is a 0, which means “cloud disconnected”, when in fact I am connected to the cellular network. When I boot my Pi, I run a systemd service that executes: /etc/bin/systemd/system/hologram network connect.The LED is solid blue, also I check ppp0 with ifconfig and I am connected, I can also ping servers.

If I issue sudo hologram network disconnect and then sudo hologram network connect and then run my .getConnectionStatus() function I get a return int of 1 not 0. Where 1, is connected.

Code:
from Hologram.HologramCloud import HologramCloud
hologram = HologramCloud(dict(), network=‘cellular’)
connection_status = hologram.network.getConnectionStatus()
print (“Connection status: {}”.format(connection_status))

Summary:
Is the CLOUD DISCONNECTED (return 0) result unrelated to a cellular network connection?

Hmm, im surpised it returns a 1 if you run the CLI since that status isn’t a global variable and no modem actually implements the is_connected function. Those variables are set with each instance and default to 0 unless you connect within the same script.

I can run some tests but just from looking at the code the only thing that changes the connection status is the connect and disconnect function… so honestly im a little stumped at how you got it to give you a 1 at all.

so if you do:

from Hologram.HologramCloud import HologramCloud

hologram = HologramCloud(dict(), network=‘cellular’)
hologram.network.connect()
connection_status = hologram.network.getConnectionStatus()
print (“Connection status: {}”.format(connection_status))

does it still return 0?

Dom, let me explain further…

At boot a systemd service runs and it looks like so:

[Unit]
Description=Connect Hologram service
After=network-online.target sys-subsystem-net-devices-ppp0.device

[Service]
Type=oneshot
RemainAfterExit=yes
EexecStart=/usr/local/bin/hologram network connect

[Install]
WantedBy=multi-user.target

So the connection is initially started by the CLI and not the Python SDK. From what I understand, any Python SDK commands will not provide any useful information unless it is done in the same script.

Like I’ve found previously, running your code you suggested does not run fully because it breaks at line 4.

hologram.network.connect()

Error:

Expections.HologrgamError.PPPError: Existing PPP session(s) are established by pid(s)

So, I guess the point here, which is fine, is that I can’t interrogate the connection status using the Python SDK when a connection was established using the CLI. No worries.

Have you guys found a best practice to establish a connection at boot?

Cheers

I’ll open an issue on the github about it because that state machine should be something it can determine from the status of the modem and we currently aren’t handling it well.

I would recommend against connecting at boot like that. Not because there is anything inherently wrong with it but other modems, like the R410, only have a single serial port and so no commands would be able to be sent to the modem once that PPP connection is started. If you only intend to ever pass data than its fine but with other modems it would effectively lock you out from getting SMS and other AT based/Serial connection things.

The approach you take using a systemd service is the same method we use though we do it slightly differently. Our entrypoint looks like this:

import sys
from pathlib import Path
from tunneler.tunnel import Tunneler

def install_upstart():
    python_path = sys.executable
    module_path = Path(__file__).parent.parent.absolute()
    script = (f"""
[Unit]
Description=Hologram Tunneler Daemon
StartLimitInterval=30
StartLimitBurst=5

[Service]
WorkingDirectory={module_path}
ExecStart={python_path} -m tunneler
Restart=on-failure
RestartSec=2
TimeoutStopSec=120

[Install]
WantedBy=multi-user.target
            """)
    with open('/lib/systemd/system/tunneler.service', 'w') as f:
        f.write(script)
    print('Script installed')

if '--install' in sys.argv[1:]:
    install_upstart()
else:
    tunnel = Tunneler()
    tunnel.run()

you don’t have to use a module like we do but this has worked pretty well for us. This tunneler script is for us to do remote diagnostics so its a very simple daemon.

import sys
import time
import logging

from tunneler.config import TunnelConfig

# SDK
from Hologram.HologramCloud import HologramCloud
from Hologram.Network import NetworkScope

class Tunneler:
    def __init__(self):
        self.logger = logging.getLogger()
        self.sms_messages = []

        self.hologram = HologramCloud({}, authentication_type='totp', network='cellular')

    def run(self):
        self.logger.info('Starting up tunneler daemon')
        try:
            while True:
                # Get all received sms
                sms = self.hologram.popReceivedSMS()
                while sms:
                    self.sms_messages.append(sms)
                    sms = self.hologram.popReceivedSMS()
                self.parseAndHandleSMS()
                time.sleep(10)
        except KeyboardInterrupt:
            pass
        self.logger.info('Shutting down tunneler daemon')

    def parseAndHandleSMS(self):
        """
        Check our incoming SMS messages, check the most recent and then clear them
        """
        if len(self.sms_messages) > 0:
            most_recent = self.sms_messages[-1]
            self.logger.info(f'Got SMS: {most_recent}')
            if 'enable' in most_recent.message:
                # Bring up PPP for us to tunnel into
                self.hologram.network.scope = NetworkScope.HOLOGRAM
                self.hologram.network.connect()
            elif 'disable' in most_recent.message:
                self.hologram.network.disconnect()
            self.sms_messages = []

Instead of listening for SMS you could start your connection and then it should return the proper status if you query for it in the run function.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.