I have an ESP32 powered device in two versions, one is controlled through MQTT using PubSubClient as part of an Openhab install, the second running Asyncwebserver and hosting webpages that can control the device.
Can I combine them ? How can I publish and subscribe to a MQTT server when AsyncWebserver is running ? The problem is there is no Wificlient instance to connect the pubsubClient instance to. Is there another MQTT library I should be using ?
I don’t see why you can’t combine them. MQTT is a protocol (Default port 1883). HTTP is a protocol (Default port 80). You can use multiple protocols over the same transport (WiFi in this case). Try including the AsyncTCP and AsyncWebServer libraries and creating a server object (I have seen if you include a library and then don’t use it you get errors). You should be able to do the standard “server.on()”.
I say give it a try and see what happens. About the only issue I could foresee is possibly a lack of memory.
Yes it works ! Mostly – there are errors if websockets and pubsubclient are active at the same time, some conflict I need to look at causing reboots. But better than I expected.
Reboots, as Steve mentioned, may be related to the power supply. However, it may also be related to some conflicts in the code.
Yes, I think it is code related, console shows ‘Core 1 paniced’ which usual means code accessed the wrong memory. It seems to happen if a browser window has been inactive for some time and I then refresh it. The log shows opening a new websocket and then crashes.
I know I need to make a change to the pubsubclient calls to always check the connection is active before publishing, I will fix that and then look at the crash problem again, maybe that it part of the issue.
I fixed the MQTT related issues and others but this crashing remains – related to websockets. Typical terminal messages
18:28:01.973 > WebSocket client #1 connected from 10.0.4.53
18:28:51.414 > Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.
18:28:51.415 > Core 1 register dump:
This usually happens is browser window was opened, server has rebooted, and when browser window is refreshed the server crashes as above. Something to do with browser windows thinking websocket is already open and sends an inappropriate request ? Is there something to put in the .js to prevent this ?
I’m not sure how to handle that.
Have you considered trying the AsyncMQTT library? https://github.com/marvinroger/async-mqtt-client
I have just downloaded that, need to see changes needed to use in my code, but it should be an improvement.
Thnks for this
You can take a look at this tutorial that uses that library in a simple example:
I hope this helps.