ESP32 + Mongoose server + MQTT broker and client - http

I have a system already working, which consist in a Mongoose web server running in a ESP32. It is also capable of publishing messagges trough MQTT, using Mongoose's mqtt libraries, when connected trugh an external MQTT broker. The idea is that there could be many of those systems, let's say 5 units, connected to the same LAN whith an extra hardware running a mqtt broker.
The broker is the one who manage some features of the individual units.
The thing is, what if the broker goes down? Or, if the broker is not in the LAN but is connected through internet, what if the internet connection goes down?
If that happens, the idea is that one of those units became a mqtt broker, so it can manage its own features and also other unit's.
In that way, I would have one unit working as a broker (only with basic functionalities) but also as a client.
I would like to know if anyone of you have already worked with a similar setup, or what do you think about it.
Any other idea?
I know there are Mongoose libraries to run a MQTT broker in the device, that wouldn't be an issue... the thing is... Isn't it better to setup an HTTP communication between the units, since they are all already web servers?

Related

How to do OTA via http, using ESP32 as AP instead of as STA, and using ESP-IDF?

I need to write a program, using ESP-IDF, which is able to update the firmware of an ESP32-S3-based device wirelessly, without having on-field access to Internet.
I googled and found the example project about OTA:
OTA: https://github.com/espressif/esp-idf/tr ... system/ota
But this project seems to use the ESP32 as an HTTP client, connecting to a server running on a computer, and both the ESP32 and the computer being connected to the same local network created by a router acting as AP. For the firmware update on-field, I won't have access to Internet or a local network via a router, hence I will need to set up a direct connection between the ESP32 and my computer. I thought that I could do this by using the ESP32 as AP and then connect with my PC to the ESP32-created network.
For using ESP32 as AP, I found the following example project:
WiFI as AP: https://github.com/espressif/esp-idf/tr ... ted/softAP
and for using ESP32 as HTTP server I found the following example project:
HTTP server: https://github.com/espressif/esp-idf/tr ... ver/simple
My idea was to try to combine code from the above 3 example projects in order to use the ESP32 as AP instead of as STA, run an HTTP server on ESP32, use a computer as HTTP client to connect to the ESP32 server, and from the computer browser to upload the new firmware file to the ESP32 server, and then copy the new firmware from a buffer to the ota flash partition.
As I don't have experience with OTA at all and less experience with ESP32, I wanted to first ask if the above idea is possible at all or there exists a simpler way to accomplish the OTA. If there is no simpler method, how can I use the OTA code with ESP32 being an HTTP server instead of HTTP client (as the OTA example seems to be coupled to an HTTP client) ?
P.S. Using the ESP32 as HTTP client seems not applicable to me because I will need to know in advance a certain permanent IP address for the computer acting as an HTTP server which I think I cannot have, but I am not sure if I am thinking correctly ?

How can I use mqtt to connect hono to ditto?

Is it possible to use mqtt to send data from a device (arduino in my case) to hono and then send these data to ditto via mqtt?
in my case I have arduino that send a value via mqtt to hono, but now i need the data to be received by ditto.
For the moment i have only arduino that sends data to hono via mqtt.
Eclipse Ditto can either consume messages directly from an MQTT broker using MQTT 3.1.1 or MQTT 5:
https://www.eclipse.org/ditto/connectivity-protocol-bindings-mqtt.html
https://www.eclipse.org/ditto/connectivity-protocol-bindings-mqtt5.html
Or, utilising Eclipse Hono as device connectivity layer, consume messages via Apache Kafka or AMQP 1.0 (which Hono uses for "northbound" applications ingesting telemetry data).
If you need Eclipse Hono, I suggest to have a look at the "Cloud 2 Edge" package in the Eclipse IoT Packages project which already automatically sets up and configures both Eclipse Hono and Eclipse Ditto to work with each other:
https://www.eclipse.org/packages/packages/cloud2edge/
If you only need to send data via MQTT, I would suggest to use an MQTT broker (like Eclipse Mosquitto) as this simplifies the setup a lot.
If you have requirements towards supporting multiple IoT protocols or massive scalability requirements, Eclipse Hono is a good choice.

Why use mqtt server for BLE gateway?

I have a BLE temperature sensor, which should send measurements to an ESP32 via a BLE connection and my plan was that the ESP32 could send the measurements via WiFi to my database (Firebase Realtime Database). However, I have seen from a few tutorials that a mqtt server would be neccesary (see the screenshot below - link to the video: https://www.youtube.com/watch?v=PBYCG-ypvRY). Could someone please explain to me why the mqtt component is neccesary? Currently I am measuring temperatures using my ESP32 (without the BLE temperautre sensors) and sending them directly to the database, so I am confused as to why with BLE I would need a mqtt server.
Thanks for your help in advance!
You don't need MQTT. It's just one of the IoT-friendly protocol alternatives (like HTTPS, AMQP, CoAP, homebrew, etc) available to you for device-to-server communication. There are pros and cons to each one. It's up to you to choose whatever solution is relevant to your system.
If you've already implemented the Firebase HTTPS interface and it does the job, you're done.
If you are having problems, note that the Google IoT Core also prescribes MQTT (or, alternatively HTTPS) as an underlying messaging protocol - used together with the Device SDK for Embedded C. But this is for people who want to have a large-scale authentication and management solution for their devices. By all means, stick to your existing solution if it works.

Can an MQTT Client in principle publish two different topics?

Background: I'm currently drawing up a project using a simple Arduino and a GPRS module that connects with UDP protocol to a RPi configured as an MQTT client, with an online MQTT Broker.
Arduino--GPRSMod--RPIMQTTClientPubl--Broker--WebclientSubscrib
I have not done anything yet, except gathering information about the plausibility of this project and figuring out the time and effort (The GPRS Module with UDP protocol to RPi is based on somebody else's project, and I'm not sure yet how it works, I thought in theory I could just send it directly to the Broker, but not sure which protocol I'm suppsed to use) and familiarized myself with Windows Mosquitto, just to try out MQTT.
Question: Is it possible to have two (or more) different sensors connected to the Arduino (Say, US, Temp and a normal Switch) and have them all sent as three different topics to the MQTT broker through one MQTT client?
US -------|
Temp ----|--Arduino--GPRSMod--RPIMQTTClientPubl--Broker--WebclientSubscrib
Switch ---|
I have a feeling that although in theory the three different inputs at the Arduino side, can be recognized and sent as three different signals (sent at different times) to the GPRSModule, however the GPRS Module||MQTT Client, can only publish to one topic, isn't it? Then do I really have to have three different GPRSModule||MQTTClients so I can send three different topics?
Thanks in advance!
Any given MQTT client can publish to as many topics as they like. You should only need 1 GSM/GPRS module

Using MQTT between Raspberry Pi, Arduino and JavaScript

A mate and I are doing a project at the moment that hosts a webpage on a raspberry pi which I would like to send a message via MQTT to the arduino which reads sensors and then sends the values back to the pi to be displayed on the web page.
I am hoping to use Mosquito on the pi as a broker and treat the arduino and the webpage as clients.
I am just having a lot of trouble working out how it all fits together.
A lot of examples I have seen use web sockets and node.js and MQTT.io and I'm just so confused..
Could someone please help me piece it all together and explain how everything fits and what it does?
Thanks in advance!
Websockets allows a web browser to open a bi-directional communication pipe to a server.
For websockets to work you need a websocket enabled mqtt broker. At the moment you have to install this seperately, some examples of what is out there are here. Mosquitto will have websockets enabled in the next version, 1.4, you can compile it if you wish.
An example of what can be done, and what I am doing atm with mqtt and websockets is;
publish temperature data to mqtt broker from an arduino.
subscribe to the topic that the arduino is publishing to and populate the data into a mysql database using python scripts.
have a live website subscribe to the topic and show the current temperature as a line graph.
We did tutorials on building applications with MQTT and websockets in the past. Slides (+ Code) can be found on Slideshare.
If you're interested in how MQTT over websockets works in detail, this blog post may help you. Also, a full example websockets/MQTT application tutorial can be found in this blog post.

Resources