Skip to content

14. Embedded Networking and Communications

This week I am going to learn something about MQTT, and I will do some corresponding experiments about it. So, let’s get started!!

Group assignment:

Send a message between two projects Document your work to the group work page and reflect on your individual page what you learned

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for use in situations where a small code footprint is required and/or network bandwidth is at a premium. It is commonly used for “machine-to-machine” (M2M) communication and Internet of Things (IoT) applications.

How Does MQTT Work?

MQTT operates on a publish/subscribe model:

Broker: A server that receives all messages from the clients and then routes the messages to the appropriate destination clients.
Clients: Devices that connect to the broker. Clients can be publishers, subscribers, or both.
Publisher: A client that sends messages.
Subscriber: A client that receives messages.
Topics: A hierarchical structure to organize messages. Clients publish to and subscribe to topics. A topic might look like sensor/temperature/room1.

alt text

Broker can name as Server;
Client can be Publisher or Subscriber;
A client can be publisher or sever the same time.

Reflection on our group work

Click here to group work.

alt text

My board added a temperature and humidity sensor, and it acts as a server & publisher by connecting to Wi-Fi. The IP address is given by Wi-Fi, and it keeps sending temperature and humidity data under this Wi-Fi address;

Matthew’s board is connected to Wi-Fi as a client, obtains an IP address, keeps reading the content of my board’s IP address, and keeps displaying the received temperature and humidity data on the monitor

Individual assignment:

design, build and connect wired or wireless node(s) with network or bus addresses and a local interface

Messaging with MQTT between Two (Different/same) PCs

At the front

Steps at a Glance:
Understand MQTT: MQTT is a lightweight, publish/subscribe messaging protocol.
How It Works: Uses a broker to manage messages between publishers and subscribers.
Messaging Setup:
Same Network: Use mosquitto_pub and mosquitto_sub commands. Different Networks: Ensure broker accessibility, use the same commands.
Use WebSocket if Needed: Use libraries like paho-mqtt for WebSocket connections.
Following these steps will allow you to set up and test MQTT messaging between two PCs, whether they are on the same network or different networks.

Steps

Prerequisites MQTT Broker:

alt text

MQTT Client Tools: Install Mosquitto client tools or use a client library.

MQTT Dashboard For Android

Same/Different Network Setup

Step 1: Open Command Prompt: Press Win + R, type cmd, and hit Enter.

Step 2: Connect Clients to the Broker Publisher on PC1:

Open Command Prompt and publish a message using the mosquitto_pub command:

mosquitto_pub -h <BROKER_IP> -p <PORT> -u <USERNAME> -P <PASSWORD> -t <TOPIC> -m "Hello from PC1"

Subscriber on PC2:

Open Command Prompt and subscribe to the topic using the mosquitto_sub command:

mosquitto_sub -h <BROKER_IP> -p <PORT> -u <USERNAME> -P <PASSWORD> -t <TOPIC>

Result: The message “Hello from PC1” should appear on PC2.

Results/HeroShots:

My PC1 (acted as a publisher now): alt text

code share as bellow:

mosquitto_pub -h mqtt.fabcloud.org -u fabacademy -P fabacademy -t fabacademy/Dion -m "Hi Dear!Are you feeling happy now? ;-)code:myTrial1"

Another PC2 (acted as a subscriber now):

mosquitto_sub -h mqtt.fabcloud.org -u fabacademy -P fabacademy -t fabacademy/Dion

Now, PC2 received this;(this is also my own laptop,I just open anther Terminal and run as a subscriber ) alt text

Notice,PC2 can be your own laptop, or other PC (not at the same network);

Since, a client could be a Publisher/Subsriber. And MQTT can be work at the same network area/ a different one.

alt text

See , this is my classmate Matthew, he is far away from me at Chaihuo now, he seems so happy to receive my message after run the command:mosquitto_sub -h mqtt.fabcloud.org -u fabacademy -P fabacademy -t fabacademy/Dion

alt text

Matthew ‘s compliments 😁 alt text

Additional Note

Mosquitto

Name mosquitto — an MQTT broker Description mosquitto is a broker for the MQTT protocol version 5.0/3.1.1/3.1. To Download You can download Mosquitto base on your using operating system, or you can download Source file of it.

MQTT Support Mosquitto supports MQTT v5.0, v3.1.1, and v3.1.

mosquitto_pub

Name mosquitto_pub — an MQTT version 5/3.1.1/3.1 client for publishing simple messages

Description mosquitto_pub is a simple MQTT version 5/3.1.1 client that will publish a single message on a topic and exit.

Synopsis

mosquitto_pub { [-h hostname] [--unix socket path] [-p port-number] [-u username] [-P password] -t message-topic... | -L URL } [-A bind-address] [-c] [-d] [-D command identifier value] [-i client-id] [-I client-id-prefix] [-k keepalive-time] [--nodelay] [-q message-QoS] [--quiet] [-r] [--repeat count] [--repeat-delay seconds] [-S] [-V protocol-version] [-x session-expiry-interval] { -f file | -l | -m message | -n | -s } [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] [[ { --cafile file | --capath dir } [--cert file] [--key file] [--ciphers ciphers] [--tls-version version] [--tls-alpn protocol] [--tls-engine engine] [--keyform { pem | engine }] [--tls-engine-kpass-sha1 kpass-sha1] [--tls-use-os-certs] [--insecure] ] | [ --psk hex-key --psk-identity identity [--ciphers ciphers] [--tls-version version] ]] [--proxy socks-url]

mosquitto_pub [--help]

Annotation of the example CMD mentioned above

mosquitto_pub -h mqtt.fabcloud.org -u fabacademy -P fabacademy -t fabacademy/Dion -m "Hi Dear!Are you feeling happy now? ;-)code:myTrial1"

In this case :

-h,
--host
Specify the host to connect to. Defaults to localhost.

-h mqtt.fabcloud.org
It indicates the host is mqtt.fabcloud.org

-P, (Pay attention, it’s capitalize ‘P’!)
--pw
Provide a password to be used for authenticating with the broker. Using this argument without also specifying a username is invalid when using MQTT v3.1 or v3.1.1. See also the –username option.

-u,
--username
Provide a username to be used for authenticating with the broker. See also the –pw argument.

-u fabacademy -P fabacademy

It specifies the username and password we use for Publisher client.

-t,
--topic
The MQTT topic on which to publish the message.

-t fabacademy/Dion
It specifies that fabacademy/Dion is the topic on which to publish messages. Notice this is a sub-topic withfabacademy/ of MQTT broker at the front.

-m, --message Send a single message from the command line.

-m “Hi Dear!Are you feeling happy now? ;-)code:myTrial1”
This is the message I wanna send to the broker.

mosquitto_sub

Name mosquitto_sub — an MQTT version 5/3.1.1/3.1 client for subscribing to topics

Description
mosquitto_sub is a simple MQTT version 5/3.1.1 client that will subscribe to topics and print the messages that it receives.

In addition to subscribing to topics, mosquitto_sub can filter out received messages so they are not printed (see the-T option) or unsubscribe from topics (see the -U option). Unsubscribing from topics is useful for clients connecting with clean session set to false.

Synopsis

mosquitto_sub { [-h hostname] [--unix socket path] [-p port-number] [-u username] [-P password] -t message-topic... | -L URL [-t message-topic...] } [-A bind-address] [-c] [-C msg-count] [-d] [-D command identifier value] [-E] [-i client-id] [-I client-id-prefix] [-k keepalive-time] [-N] [--nodelay] [--pretty] [-q message-QoS] [--random-filter chance] [--remove-retained] [ -R | --retained-only ] [--retain-as-published] [-S] [-T filter-out...] [-U unsub-topic...] [-v] [-V protocol-version] [-W message-processing-timeout] [-x session-expiry-interval] [--proxy socks-url] [--quiet] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] [[ { --cafile file | --capath dir } [--cert file] [--key file] [--tls-version version] [--tls-alpn protocol] [--tls-engine engine] [--keyform { pem | engine }] [--tls-engine-kpass-sha1 kpass-sha1] [--tls-use-os-certs] [--insecure] ] | [ --psk hex-key --psk-identity identity [--tls-version version] ]]

mosquitto_sub [--help]

mosquitto_sub -h mqtt.fabcloud.org -u fabacademy -P fabacademy -t fabacademy/Dion

In this command, it’s the same function as the Publisher’s.

Remarks

So, reading above, you should notice that when using mosquitto relevant cmd, commands is capital sensitive: -P differs from -p; so does -T and -t, etc. Just make sure spelling carefully when you coding.

Example - To realize Datavisulization with XIAO and DHT11 via MQTT Protocol

For more cases, you can visit my week15

To showcase networking and communications, I will use these components list as below:
alt text

Embedded Program

Diagrams and Designs

Hardware overview - Pinout diagram alt text

Schematic Diagram: alt text

PCB Design:
alt text

Grove DHT11

alt text

Configuration on MQTT Dash

You can download it in Google Play. It also support Android.

In my tiral this time, I need to use the information that MQTT broker provided by Pro. Neil Gershenfeld: alt text

Create a broker : alt text

Create Metrics for both text and range displaying: In addition to text display, I also want to use percentage to display temperature and humidity values. So I create metrics for both text and range displaying on MQTT Dash:

Configurations:

  • This Termrature_text metric is intended for displaying payload text( temperature displaying)

alt text

-Humid_text: alt text

-Temprature_range: alt text

-Hmid_range: alt text

Embedded Programming:

Make sure you have these headers files, if not, click ‘Library Manager’ to install. alt text

Select the board & port: alt text

Upload the code: alt text

Upload Succeed!!! alt text

Switch to Serial Monitor, if no message display, then press ‘Reset’ button on XIAO.
alt text

You will find data returned to the Serial Monitor; There is also real-time visualization of data on the MQTT Dash

alt text alt text

You can see that the temperature and humidity values ​​of the sensor are displayed in real time on MQTT Dash. Note that the local printing of temperature and humidity values ​​is very fast, but there is a delay when using MQTT.

Refference Code

/*

Originally from MQTT ESP8266 Example 

Basic XIAO-ESP32 MQTTS example

 Before starting , You should have
 - MQTT Broker Name 
 - MQTT Broker Username 
 - MQTT Borker Password 

*/



#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <Adafruit_NeoPixel.h>

#define DHTPIN D2
#define DHTTYPE DHT11
#define NUMPIXELS 80
#define PIN D1
#define DELAYVAL 20
#define BRIGHTNESS 50

DHT dht(DHTPIN, DHTTYPE);
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

const char* ssid = "AndroidAP26ab";
const char* password = "tawd7050";
const char* mqtt_server = "mqtt.fabcloud.org";

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500); // Reduce blocking delay
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "XIAO-ESP32-Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str(), "fabacademy", "fabacademy")) {
      Serial.println("connected");
      client.subscribe("fabacademy/myLampSwitch");
      client.subscribe("fabacademy/LampColor");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000); // Reduce blocking delay
    }
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  dht.begin();

}

void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();
  // delay(20); // Reduce blocking delay

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  char tempra[100];
  snprintf(tempra, 100, "%f", t);
  Serial.println(tempra);
  client.publish("fabacademy/Temprature", tempra);
  char humidity[100];
  snprintf(humidity, 100, "%f", h);
  client.publish("fabacademy/Humidity", humidity);
  Serial.println(humidity);
}