Connect to MQTT Broker with .env variables 使用.env变量连接到MQTT代理

huangapple go评论70阅读模式
英文:

Connect to MQTT Broker with .env variables

问题

I wrote a python code to connect to a MQTT Broker, which worked completely fine until I tried working with environment variables. The library used is paho-mqtt.

At first, the broker, port, topic, password and username were declared in the code, and the code worked well. Then, I created a .env file which stores those variables, and now it cannot connect to the broker anymore.

Here's how the python code "loads" the environment variables:

from dotenv import load_dotenv
...
if private_lorawan == True:
    load_dotenv('mqtt-private.env') # loads file
    broker = os.getenv('BROKER')
    port = int(os.getenv('PORT')) # convert in integer
    topic = os.getenv('TOPIC')
    username = os.getenv('USERNAME')
    password = os.getenv('PASSWORD')

Here's the mqtt-private.env file:

BROKER='xxx.xx.xx.xxx' # IP address
PORT=1883
TOPIC="v3/<topic>/devices/<device-eui>/up"
USERNAME="<username>"
PASSWORD="<password>"

And here's the output when running the code:

Connect_mqtt
my-client-0
Loop started
Failed to connect, return code 5

When searching what the return code means:

5: Connection Refused. The client is not authorized to connect.

I tried:

  • username and password in double quotes

  • username and password without double quotes

But none of these options seem to work.

英文:

I wrote a python code to connect to a MQTT Broker, which worked completely fine until I tried working with environment variables. The library used is paho-mqtt.

At first, the broker, port, topic, password and username were declared in the code, and the code worked well. Then, I created a .env file which stores those variables, and now it cannot connect to the broker anymore.

Here's how the python code "loads" the environment variables :

from dotenv import load_dotenv
...
if private_lorawan == True:
    load_dotenv(&#39;mqtt-private.env&#39;) # loads file
    broker = os.getenv(&#39;BROKER&#39;)
    port = int(os.getenv(&#39;PORT&#39;)) # convert in integer
    topic = os.getenv(&#39;TOPIC&#39;)
    username = os.getenv(&#39;USERNAME&#39;)
    password = os.getenv(&#39;PASSWORD&#39;)

Here's the mqtt-private.env file :

BROKER=&#39;xxx.xx.xx.xxx&#39; # IP address
PORT=1883
TOPIC=&quot;v3/&lt;topic&gt;/devices/&lt;device-eui&gt;/up&quot;
USERNAME=&quot;&lt;username&gt;&quot;
PASSWORD=&quot;&lt;password&gt;&quot;

And here's the output when running the code :

Connect_mqtt
my-client-0
Loop started
Failed to connect, return code 5

When searching what the return code means :

> 5 : Connection Refused. The client is not authorized to connect.

I tried :

  • username and password in double quotes

  • username and password without double quotes

But none of these options seem to work.

答案1

得分: 0

根据文档

默认情况下,load_dotenv 不会覆盖现有的环境变量。

USERNAME 在许多操作系统中设置为登录名,所以你看到的结果是预期的。解决方案包括使用不同的变量名,将数据加载到字典中(dotenv_values),并指定 override - load_dotenv(dotenv_path='mqtt-private.env', override=True)

英文:

As per the docs:

>By default, load_dotenv doesn't override existing environment variables.

USERNAME is set to the login name in many operating systems, so the result you are seeing is to be expected. Solutions include using a different variable name, loading the data into a dict (dotenv_values), and specifying override - load_dotenv(dotenv_path=&#39;mqtt-private.env&#39;, override=True).

huangapple
  • 本文由 发表于 2023年5月17日 15:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定