Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

websocket - Why does this MQTT client works fine through MicroPython's webREPL but fails in the main.py of the ESP32?

I'm currently working an IOT project requiring the transfer of sensor data between an ESP32 (a wESP32 to be exact) and a Raspberry Pi configured as a broker. From what I've read so far the MQTT protocol seems to fit my needs perfectly, I'm thus running a Mosquitto broker on the Pi as well as the MQTT simple client library provided on the micropython's GitHub repository.

The first tests performed in the MicroPython WebREPL have been successful as I've been able to receive data published from the ESP using the following code:

Welcome to MicroPython!
Password:
WebREPL connected
>>> from umqtt.simple import MQTTClient
>>> c = MQTTClient("umqtt_client", "rapsberrypi")
>>> c.connect()
0
>>> c.publish(b"sensors/temperature", "{:.1f}".format(21.35))
>>> c.disconnect()
>>>

However as soon as I try running the same code on boot in the main.py file or through the serial port using ether screen or rshell I get the following error.

Started webrepl in normal mode
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
>>> I (4379) ethernet: LAN cable connected
I (5359) event: eth ip: 192.168.1.62, mask: 255.255.255.0, gw: 192.168.1.1
I (5359) ethernet: Got IP
from umqtt.simple import MQTTClient
>>> c = MQTTClient("umqtt_client", "rapsberrypi")
>>> c.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "umqtt/simple.py", line 57, in connect
IndexError: list index out of range
>>>

For some context here is the 57th line of the umqtt/simple.py file:

55  def connect(self, clean_session=True):
56      self.sock = socket.socket()
57      addr = socket.getaddrinfo(self.server, self.port)[0][-1]
58      self.sock.connect(addr)

If tou have any clue of what's going on here please let me know!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In this line of code:

c = MQTTClient("umqtt_client", "rapsberrypi")

the second argument to the constructor - "raspberrypi" - identifies the broker (server).

This is likely to be defined in such a way that only software running on your Raspberry Pi will be able to resolve it. The name won't be visible to software running in places other than the Pi.

In the code running on your ESP32, replace "raspberrypi" with the IP address or a resolvable (fully qualified domain name) name for your Raspberry Pi. Note that 127.0.0.1 is the loopback interface's IP address and is not accessible by software that's not running on the Pi.

You can use the ifconfig command to list your network interfaces; look for one named something like wlan0 for wifi or eth0 for wired ethernet and use the IP address associated with that interface.

Once you've done that if you still can't reach the broker then the broker is likely to be configured to not respond to requests that don't originate on the same machine as it's running.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...