Edge Workflow - Run Python Script like "normal"


#1

Hello,

I am evaluating Losant since a few days, now working with the edge compute feature on my Raspberry Pi 3.

The original intention was quite simple: use an edge workflow to execute a python script giving me the actual CPU temperature every 5 seconds. Just for testing.

What I have accomplished up to now: installed relevant packages, activated the Losant edge agent and connected it to my Losant application. Everything went fine. At least it seemed like that.

Where do I struggle: in using the “run executable”-node…! Unfortunately.

The first approach was to define the “command” of the “run executable”-node exactly as I would do when being in a SHH session connected to my Raspberry Pi with the standard user “pi”. So I tried “sudo python3 ~/Losant/Edge/get_CPU_Temp.py”. The Python script should write the CPU temperature to the stdout.

Based on the debug messages I realized that the edge agent is not the user “pi” but “losant”, so not knowing anything about my Python script in the home directory of “pi” and not capable of executing the “python3” command.
I tried many different variants (also with using "bash -c “…” ", changing the “current working directory”, …) but did not succeed.

I hope someone is able to help me out and propose a way forward.

Best regards and many thanks


#2

Remember the edge agent is running inside the docker container so any executable you need to run (and it’s supporting environment) must be installed in your running container.

This means you effectively need to build you own image with theses dependancies based on the Losant docker image.

Alternately run the python script in base os or another container and either trigger the edge agent via mqtt/http and send it the temp, or put it to redis and the edge agent can get the value from redis.


#3

As Tim said, our agent is inside a Docker container, which is an isolated environment within your Raspberry Pi. You can kind of think of the Docker container like a Virtual Machine with little-to-no knowledge or access to the host OS. This means it can’t access any files by default unless you explicitly mount them in.

The Edge Agent container does have python 2.7.9 pre-installed. If your script can run under python 2.7, you can mount your script into the container using the Docker run command and then execute it using the Run Executable Node.

Assuming your script is in the pi’s home directory on the host OS, modify the Docker run command to look like this:

docker run -d --restart always --name losant-edge-agent \
  -v /var/lib/losant-edge-agent/data:/data \
  -v /var/lib/losant-edge-agent/config.toml:/etc/losant/losant-edge-agent-config.toml \
  losant/edge-agent
  -v /home/pi/get_CPU_temp.py:/mnt/get_CPU_temp.py \
  losant/edge-agent

The above example comes from our documentation on using a configuration file.

Notice the second -v where your script is being mounted from the host OS to /mnt/get_CPU_temp.py inside the container.

You can now use a Run Executable node with the command:

python /mnt/get_CPU_temp.py

#4

Thank you both very much for providing an answer to my question!

I will try this on my R-Pi!

Unfortunately I don’t know much about docker. I will study it in more depth.

Is it also possible to install new python modules / dependencies inside the container?
Eventually the R-Pi will be used to talk to some peripherals with Modbus RTU via RS485.
So I will need the corresponding modules python extensions.

Best regards


#5

Hi

We have edge agent communicating via RS485. We install https://github.com/3cky/mbusd Mbusd Modbus serial proxy. Then the edge agent can talk directly to the device via MODBUS TCP.

mbusd when built and installed set’s itself up as systemd process, and requires simple config file.

We also run separate (pre-existing agents written in python) in the parent (not under docker). These talk directly to losant. (reading modbus - these where written before we started using losant)

So we are running a mixed environment of edge agent and other agents on the same pi.

I find one off workflows quicker to build in edge (mainly as the edge agent is more accessible programming wise to a wider audience) so I don’t become a bottleneck.