• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

MQTT with AysncWebserver

Q&A Forum › Category: ESP32 › MQTT with AysncWebserver
0 Vote Up Vote Down
Hamish Low asked 4 years ago

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 ?

Question Tags: AyncWebserver, ESP32, MQTT
10 Answers
0 Vote Up Vote Down
Steve Mercer answered 4 years ago

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.

0 Vote Up Vote Down
Hamish Low answered 4 years ago

Thanks Steve,
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. 
 
Hamish

0 Vote Up Vote Down
Steve Mercer answered 4 years ago

For reboots I’d look at power supply first. 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Hamish.
Reboots, as Steve mentioned, may be related to the power supply. However, it may also be related to some conflicts in the code.
Regards,
Sara

0 Vote Up Vote Down
Hamish Low answered 4 years ago

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.
 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Ok.
Then, let us know if you were able to solve the issue.
Regards,
Sara

0 Vote Up Vote Down
Hamish Low answered 4 years ago

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 ?

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Hamish.
I’m not sure how to handle that.
Have you considered trying the AsyncMQTT library? https://github.com/marvinroger/async-mqtt-client
Regards,
Sara

0 Vote Up Vote Down
Hamish Low answered 4 years ago

Sara, 
I have just downloaded that, need to see changes needed to use in my code, but it should be an improvement.
Thnks for this
 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

You can take a  look at this tutorial that uses that library in a simple example:

ESP32 MQTT – Publish BME680 Temperature, Humidity, Pressure, and Gas Readings (Arduino IDE)


I hope this helps.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.