capacious
capacious

Reputation: 13

How can I get a device name from within a IoT Edge Custom module (python)

SUMMARY

I don't get the expected hostname from the device when requesting it via python code; I get some container ID

BACKGROUND

When running the following code on Raspbian in python 3:

import socket
print(socket.gethostname())

or:

import platform
platform.node()

...you get the hostname of the machine you're running the code on. (That's what I expect)

When doing the same from a Custom IoT Edge module you get some kind of identifier for I think the container?

How can you get the hostname of the system the container is running on within the container module itself?

SOLUTION

As suggested the Device ID and Module ID are exposed as environment variables: IOTEDGE_DEVICEID and IOTEDGE_MODULEID.

So now in python you can do the following:

DEVICEID = os.environ["IOTEDGE_DEVICEID"]
MODULEID = os.environ["IOTEDGE_MODULEID"]

And then use the variables like that in your (python) code down the line.

Upvotes: 1

Views: 2008

Answers (1)

silent
silent

Reputation: 16208

See here for a similar question. Short answer: do a docker inspect on your custom module container and see the different environment variables that are available. One of them is the hostname of your Edge device.

Upvotes: 1

Related Questions