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?

Aurora0001
  • 18,520
  • 13
  • 55
  • 169
Rohit Mathur
  • 369
  • 1
  • 2
  • 10

7 Answers7

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.

Aurora0001
  • 18,520
  • 13
  • 55
  • 169
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

anonymous2
  • 4,902
  • 3
  • 22
  • 49
MatsK
  • 951
  • 6
  • 18
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.

Moritz
  • 161
  • 1
  • 4
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.

anonymous2
  • 4,902
  • 3
  • 22
  • 49
3

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

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
Joern
  • 39
  • 2
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.

Helmar
  • 8,450
  • 6
  • 36
  • 84
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.