Close

ESP32 communicating with Mosquitto

A project log for Haptic Sleeve

Sleeve worn on the arm to provide haptic feedback while performing handwriting exercises.

grant-stankaitisGrant Stankaitis 03/23/2020 at 06:080 Comments

Read this before continuing!

I spent a few hours trying to figure out why my ESP32 could not communicate with Mosquitto even though they were both on the same private, home network. I tried everything from reconfiguring the ESP32 to checking my network settings and routing tables. I knew it was a private network and I thought my firewall might have been limiting. It turns out, my home network was set as a public network in my wireless settings. Be sure to set your network to private so that your PC uses your private network communication settings instead of more restricted public network settings. This can be changed if you right-click on your active network and select 'Properties.'

Once I had that sorted out, my ESP32 instantly communicated with Mosquitto!

Below is some test code that I used to connect my ESP32 to my network.

import network

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect("<WiFi SSID>", "<Password>")
		
if(station.isconnected()):
  print("Station connected!")
  print(station.ifconfig()) # Print IP address

 ESP32 message passing

After tinkering with the main.py code, I was able to publish a message just as I did with the console windows in the previous post! In order to use the simple.py script to allow the ESP32 to communicate using the MQTT protocol, you need to create a new directory within the ESP32 to allow you to store simple.py within memory. To do this, right click 'device' in the file browser on the left-hand side of uPyCraft, and click 'New Dir'. Name this directory 'umqtt,' then drag and drop the simple.py script into the folder. It will then download to the ESP32. As you can see from the screenshot below, I was able to send messages to Mosquitto. In the console window on the right, you can see the ESP32 connecting and sending the messages to Mosquitto. On the left, the ESP32 is printing out the messages after they are sent.

Now that I worked out message passing, I will test it in the reverse direction as the project states, with messages being passed from the PC to the ESP32.

Discussions