MQTT on NodeMCU

I´m trying to set up some sensors and switches by MQTT using a NodeMCU to do this. But I want to optimize the ports on NodeMCU using the most I can. My doubt is how to programing this on NodeMCU to read a sensor and turn on a switch in diferent port on the same device.

Have you tried espeasy? Makes it really simple.

Check espeasy or Bruh’s multisensor if you want to do it directly in code.

Thanks guys. I´m using Arduino IDE to programing. I´m gonna try espeasy, but my doubt is how to put tha things on MQTT together. I know how to do it separately but I want to put this on the same code.

that what bruh’s multisensor do… It gets value from homidity, temperature, led, light sensor and put all in MQTT using JSON formatting.

If you know how operate things separately, then I guess the main challenge would be to ensure that sensor readings are not impacting your ability to interact with actuators and instant sensors.
In the simplest case with several sensors (like temperature, humidity, luminance) connected to a NodeMCU you can just sequentially poll each sensor’s status, then send it over to the MQTT broker, and then sleep the node for 5-10 minutes or so. As soon as you have instant sensors (like motion or door) or actuators (like relays) you have to ensure that these always come first in your loop. And even then, choose wisely which sensors and actuators you combine.
For example I was planning to combine a gas sensor with a motion sensor, and then realised that reading a gas sensor can involve various cycles of pre-heating, reading multiple time, taking the average, etc. whilst holding up reading any other sensor data.

1 Like

Another vote for ESPEasy. I have 5 nodemcu’s all with a bunch of different sensors hooked up and it works great.

Thank you guys. I could put all together. Just a DHT sensor and a simple led for now. I´m just had to adjust the topics for the MQTT broker. I´m just starting to work with MQTT and have a lot of doubts.
@exxamalte, thnks for the tips, maybe a had to combine the sensors and switches separately.
So for now I just have 1 NodeMCU and an Arduino UNO to do my automations.
Thank you again.