10

I am making a home automation project based on star topology. What I am trying to achieve is that one of the nodemcu/ESP8266 acts as a server which is accessible for the outside world and other nodemcu/ESP8266 acts as clients which are connected to relays or sensors.

Upon receiving the command from the server, the relays must be triggered accordingly and update the status back to the server. I read lots of tutorial via different methods. MQTT seems good but I don't want to use any third party broker like Adafruit. I want to host the web server either on my nodemcu or my web host. The sad part is I don't own a Raspberry Pi.

Can I use one of my ESP8266 devices as an MQTT broker, or is there a suitable alternative?

14
  • 2
    To use MQTT you need a broker and a esp8266 is just not capable of acting as a broker. You need something "bigger"
    – hardillb
    Commented Sep 17, 2017 at 8:10
  • 1
    You can host an MQTT broker on your PC/Laptop though running it 0/24 might increase your electricity bill a bit. Commented Sep 17, 2017 at 9:00
  • 1
    @hardillb --> does something bigger means raspberry pi itself? or Arduino Mega could also be used for the same purpose Commented Sep 17, 2017 at 11:08
  • 1
    It means a real computer, so not a Arduino Mega, a Pi Zero would be enough
    – hardillb
    Commented Sep 17, 2017 at 11:09
  • 1
    @BenceKaulics --> can I use my web hosting instead of leaving my laptop on for the whole of its life. I have a shared web hosting. Commented Sep 17, 2017 at 11:28

7 Answers 7

10

Technically speaking, yes, an ESP8266 could act as an MQTT broker. In fact, someone has already tried it! By the end of their project, they claimed to have a broker that can bridge to a cloud MQTT broker, with a web interface and a decent amount of uptime. In the comments, they say that their code is proprietary, so you won't be able to use their code exactly, but it does serve as a proof of concept if you really wanted to use an ESP8266.

However, using an ESP8266 as a broker is likely to be a lot of effort. Using a Pi, as suggested by MatsK, would be far easier, and although Raspberry Pi units are a little more expensive (between £5 and £25 + postage, depending on which model you choose), you'll save a lot of time. With a Pi, you can just use an established broker like Mosquitto. On an ESP8266, there's no chance that Mosquitto would run, and you'd probably have to write your own broker or use a far less reliable one.

Alternatively, you could just connect all your ESP8266s directly to a cloud service like AWS IoT. This simplifies your setup, but does mean that every request is routed through the Internet, and you cannot control devices locally. If your Internet connection breaks using this approach, you'll also have no control over the devices.

7
  • 1
    I would recommend to use "Node-red" a graphical web interface where you can create logic for automation.
    – MatsK
    Commented Sep 17, 2017 at 9:46
  • @Aurora - Thank you for explaining the thing with such depth. It brought clarity to my thinking. I understand that Pi is necessary for stand-alone servers. But if I want to take this thing over the cloud. Is it necessary to use the third party IoT services? Can I use my web host for it? If not too difficult. I'm just 14. So, I don't have much of technical knowledge. Commented Sep 17, 2017 at 11:56
  • @RohitMathur Yeah, you could use your own web host, depending on what type of web host you have—if it's something like a VPS or a dedicated server, yes; if it's just a traditional web host for web pages only, no. If you share which host you're using, I can try to point you in the right direction. Also, be sure to read What should I do when someone answers my question? so you can reward any helpful answers you get.
    – Aurora0001
    Commented Sep 17, 2017 at 11:58
  • I have a shared host. For now, I think I have to compromise with Pi. VPS cost is high. How many nodes can be connected to a Pi as clients? Like ESP8266 can manage up to 4 clients. My project can be of n no. of nodes. Some connected to a relay, some with sensors. Commented Sep 17, 2017 at 12:05
  • @RohitMathur I don't know the exact number, but I would imagine that a Pi could probably handle hundreds of messages a second, particularly the Pi 3, which is pretty powerful. Almost certainly you won't reach the limit of the Pi's capabilities with your use case.
    – Aurora0001
    Commented Sep 17, 2017 at 12:09
9

My suggestion would be a mosquitto MQTT broker on a Raspberry Pi.

There is an article here https://tech.scargill.net/a-christmas-script/ where Peter Scargill have made a script that installs all necessary components and dependencies.

Take a look at Node-red. With it you can create logic to complement your project.

Updates:

I just found a ESP8266 MQTT broker, freely available. This I have to try....

https://www.youtube.com/watch?v=0K9q4IuB_oA

https://github.com/martin-ger/esp_mqtt

3
  • Yes, I think I need to purchase a Pi. And this article was really helpful. Can you please share some more tutorials for my project? I will go through them once I get a Pi. Commented Sep 17, 2017 at 11:50
  • Rui Santos have many nice tutorials, like: randomnerdtutorials.com/… and many more and his book "home automation" is recommendable.
    – MatsK
    Commented Sep 17, 2017 at 11:57
  • I have been following a tutorial. randomnerdtutorials.com/…. Raspberry Pi is working absolutely fine. But I believe esp8266 is not receiving messages. So I cannot control the relay wired with esp8266. Commented Sep 23, 2017 at 2:53
6

In addition to the answers and comments that recommend using a Raspberry Pi because of the ESP8266’s limited resources, it would generally make sense to use the ESP32’s, the unofficial successor to the ESP8266. Due to being dual-core + 3x more RAM, it fixes WiFi connection issues that increased when user programs increased in size.

On the price range it is only a few dollars more expensive than ESP8266 and significantly cheaper than RaspberryPi’s (at least until the zero comes down in price). The other answers should have you covered on the software side.

5

Adding to what the other excellent answers have already stated, using an ESP8266 as your MQTT broker is less than ideal for the simple reason that it lacks the system requirements to perform up to speed. Someone on Github was trying a similar feat, and was told:

It is not impossible but it does not make much sense. The broker needs some resources like CPU, memory and number of TCP connections (one for each client), maybe non-volatile storage (for persistent messages), configuration for user access rights and many other details.

I echo the other great answers: get yourself a Raspberry Pi or some other cheap computer to act as your broker. The ESP will simply be lacking the specs to perform well.

3
  • 1
    Yes. It is very much clear now that I would be needing a Pi to accomplish this project Commented Sep 17, 2017 at 11:57
  • This is mostly a scaling issue - it doesn't really show up until the number of nodes grows. The issue that is present right from the start is that the software must be rewritten, likely from scratch, to create broker functionality in a minimal resources way. Commented Sep 17, 2017 at 14:19
  • So bigger MCU would be adequate, but wouldn't be any cheaper than a low end SBC? Commented Sep 17, 2017 at 15:22
3

There is a nice and functional arduino compatible library with a tiny MQTT-broker that runs on ESP8266:

1
  • This one should be a chosen answer )
    – podarok
    Commented Jun 28, 2019 at 14:55
2

You can use an ESP8266 as MQTT broker. Its support 20 devices, websockets and bridge connection to a cloud MQTT instance that provide MQTT websockets and SSL.

In three minutes you can have a fully functioning MQTT broker. Check out the iotcentral.eu platform. Make an account and you are ready.

2
  • thanks for the suggestion Dan. But now i am using Raspberry Pi as the MQTT broker. but now the problem is with esp8266. Sometimes it connects to the Wifi but sometimes it just says "Connecting................." and the dots go on. I tried using external power supply as well it isn't working. Any idea how to do that? Commented Oct 16, 2017 at 8:36
  • @RohitMathur: have checked your reception on wifi?
    – woodz
    Commented Jun 9, 2020 at 9:07
0

Check this out: https://github.com/martin-ger/uMQTTBroker

It probably won't be as powerful as an Raspberry pi but it gets the job done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.