Using MQTT between Raspberry Pi, Arduino and JavaScript - arduino

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.

Related

ESP32 + Mongoose server + MQTT broker and client

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?

Can I use HTTPS and SSE client libraries on Arduino PORTENTA Machine Control board?

I am looking for simple board for IoT professional projects. I bumped into the Arduino PORTENTA Machine Control board a while back, but I cannot get some things sorted out:
What libraries can I use on this board (mainly SSE and HTTPS libraries)?
Does it have enough computational power to do HTTPS requests (not HTTP but HTTPS)?
What am I trying to achieve:
I am trying to build a project around Google’s Firestore / Firebase.
I need to communicate to it via REST API.
Google enables only HTTPS communication with their databases – and
normal Arduino boards can’t do HTTPS requests because they do not
have enough power.
So, I need either SSE client + HTTPS requests library to run on the
PORTENTA board OR Firebase Realtimedatabase library (I know the
library is built on HTTPS requests – but if it’s not compatible I can
recreate it with SSE client and HTTPS requests).
Other info:
By SSE I mean Server Send Events for Realtime communication and
updates to the board :)
I have no problem programming it in native Arduino language or in Micropython.
The answer below states that I can use those libraries if the board is supported - but I don't know how to know if the board is supported :/
I have never heard that Arduino lacks the power for HTTPS requests and Arduino have documentation on how to make HTTPS requests for many of their official boards: https://arduinogetstarted.com/tutorials/arduino-https-request
If you are using an unsupported board you will have to route the traffic to another host first. Either another Arduino or an ESP to act as the main host/client that it can communicate to access external resources such as Firebase.

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.

Recommendations for MQTT with Arduino and Android Clients

I am fairly new to Arduino, and for a project I want to use an UNO to push/publish notifications to an MQTT broker, then have the broker push a notification to a subscribing Android app I am developing.
I have an ESP8266 for my Arduino incoming. I've done a fair bit of research but am having some trouble piecing things together. I've read articles with the Arduino as the subscriber, or as the server, or the Android as the publisher, but none for this specific implementation. What I (think) I know so far is:
1: Since this is just a fun DIY, test.mosquitto.org would suffice as a decent free broker. So for a #define mqtt_server field, it'd be test.mosquitto.org.
2: Programmatically, I can push notifications from my Arduino to this broker via an mqtt library such as PubSubClient.
3. In Android Studio, I can use an mqtt client (Paho?) to subscribe to the topics being pushed to the broker.
My questions at this point are:
Is there anything else I would need to install on the Arduino besides PubSubClient and the library for my ESP?
I saw that on some code examples, there were 'mqtt_password' and 'mqtt_user' fields. Do all brokers generate these fields, and if so, where can I access them from test.mosquitto.org.
Are there better brokers for a job like this (that are also free) or would mosquitto be enough? I'll only be sending short messages of < 100 characters, but do some brokers limit the amount of data they receive/send?
I just need a jump start in understanding the overall architecture. As usual, any help is appreciated.
Is there anything else I would need to install on the Arduino besides
PubSubClient and the library for my ESP?
You are on the right way... if your Arduino libs are working then you can pub topics and see if the broker can deal with it (I suggest to use mqttFx as a client and subs to those topics too)
I saw that on some code examples, there were 'mqtt_password' and
'mqtt_user' fields. Do all brokers generate these fields, and if so,
where can I access them from test.mosquitto.org.
every broker can deal with anonymous clients, M2m.eclipse.org is one of those...
Are there better brokers for a job like this (that are also free) or
would mosquitto be enough? I'll only be sending short messages of <
100 characters, but do some brokers limit the amount of data they
receive/send?
yes, you can search in the web for those, I have been playing with:
HiveQM: broker.mqttdashboard.com
and
Eclipse: iot.eclipse.org
and they allow you more or lesss the same features you need for the test...
For the andoir part, Paho is a pretty good way to go...
but fisrt step fisrt, pub things with arduino and check that the broker is understanding you + that another client(mqttfx) can get the info and send you something too :)

web service communication over nRF24L01

I have two peaces of nRFO24L01, one of them is connected to arduino and second one is connected to RPi. For communication between them I used this library https://github.com/tmrh20/RF24 and this communication working very good. Now I want to have web services on RPi which have to be able to communicate with arduino through the nRF24L01. The best option should be some java web services on tomcat server but I can't manage to find some kind of wrapper which will provide me easily connection between c++ code from mentioned library and web services. Is there any solution for this?
For communication between arduino and RPi I used getting started example from mentioned library from github.
See the RF24Ethernet lib from the same guy:
http://tmrh20.github.io/RF24Ethernet/

Resources