Skip to content

13. Networking and communications

Group assignment:

  • Send a message between two projects

To see our group assignment click here

Individual assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses

For this week’s assignment, we have chosen to communicate the electronic board we developed in Week 8 (Electronics production) based on an ESP32 MCU with a board using the ESP8266 MCU. These two electronic boards will use WiFi as a means of communication with the MQTT protocol. This work is related to our final project.

To carry out our assignment this week we have broken it down into 4 steps: the manufacture of an electronic board for the ESP8266, the implementation of an MQTT server with a Raspberry pi 3, the programming of electronic boards and the test of message exchange.

The manufacture of an electronic board for the ESP8266 (ESP-01)

To build the electronic board of the ESP8266 (ESP-01), we have been inspired by the hello.ESP-01 PCB on the fab academy website as shown below.

For the design of our PCB we used Eagle, here are the results below. for more details on the use of Eagle software refer to Week 6 (Electronics design).

After having generated the png files of the traces and the outline of our printed circuit as indicated below. we proceeded to the generation of the toolpaths with Mods CE and the machining with the Roland SRM-20. For more details on the process refer Week 8 (Electronics production).

for the download of the source files go to the bottom of the web page in the Files section.

Here are the results we obtained.

After having soldered the different electronic components we realized during the verification of the continuity of the electronic circuit that we had made a mistake in the design on the positioning of the pins of the ESP01 as shown below.

it was probably tiredness, I drank a big glass of water and I went to bed. the next day we designed another electronic board to reorganize the pin order as shown below.

after making this new electronic board we proceeded to the test as shown below

The implementation of an MQTT server with a Raspberry pi 3

MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport, ideal for connecting remote devices with a small code footprint and minimal network bandwidth. for more details click here

Before moving on to the implementation of the MQTT server, here is a graphic that summarizes the communication topology.

For the creation of a communication network based on the MQTT protocol we need 4 main elements:

  • the MQTT server = mosquitto
  • the MQTT clients = ESP01 && ESP32
  • the means of communication = WiFi
  • the Topic = cmd_mpass

For the MQTT server we chose the open source option mosquitto. The server will be hosted on a rapberry pi 3.

For the installation of the server we first proceeded to the installation of the operating system raspberry pi OS.
We downloaded and installed Raspberry Pi OS.

Then we inserted the SD card of the Raspberry Pi in our computer and proceeded to the installation of the operating system as shown below.

Sorry for the interface of the application in French my operating system is in French.😉

Since we used the lite version of raspebrry pi os, we need to communicate with the pi using the ssh protocol from where we configured the ssh connection settings as shown below.

To finish the installation we inserted the SD card in the raspberry pi 3, we connected it to the network of our Fablab thanks to an Ethernet RJ45 cable and we powered it as indicated below.

To connect to the raspberry pi with ssh we used windows powershell.

  • connection to the Raspberry pi

1
2
3
4
ssh <username>@<host_ip_address> -P <port_number>

# For example
ssh pi@192.168.1.210 -P 1022
results

  • now that we are connected to the raspberry pi we will update the pi before any other command.
sudo apt-get update 
sudo apt-get upgrade
  • Installation of mosquitto

sudo apt-get install mosquitto
Now that our mosquitto server is installed we need to move on to its configuration. To do this we will enter command lines to modify the connection parameters to the server for more details click here

  • Run the following command, but replace “YOUR_USERNAME” with the username you want to use

sudo mosquitto_passwd -c /etc/mosquitto/passwd YOUR_USERNAME
results

  • Run the following command to edit the configuration file
sudo nano /etc/mosquitto/mosquitto.conf
  • Add the following line at the top of the file (make sure it is at the top of the file, otherwise it won’t work)
per_listener_settings true
  • Add the following three lines to allow connection for authenticated users and tell Mosquitto where the username/password file is located
1
2
3
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
  • Your configuration file will look as follows :
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

per_listener_settings true

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
allow_anonymous false 
listener 1883  
password_file /etc/mosquitto/passwd
  • Press CTRL-X, then Y, and finally press Enter to exit and save the changes.

  • Restart Mosquitto for the changes to take effect.

sudo systemctl restart mosquitto

Now that we are done we will check if our server works as shown below

sudo systemctl status mosquitto

the commissioning of our mqtt server is finished we move to the programming of our MCU (mqtt clients).

MCU(Microcontroller) programming

For programming our MCU, we will use Arduino IDE with the PubSubClient library. The PubSubClient library provides a client to perform simple publishing and subscription operations with a server that supports MQTT.

For the operation, we will write two programs, one for the ESP01 and the other for the ESP32. The ESP32 will send messages to the ESP01 through a topic.

For the different codes, you have to modify the code with your own SSID, password and Raspberry Pi IP address.

Programs

ESP32_mqtt.ino
#include <WiFi.h>
#include <PubSubClient.h>

// Replace the next variables with your SSID/Password combination
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "YOUR_MQTT_BROKER_IP_ADDRESS";

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);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

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

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

  randomSeed(micros());

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


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect("ESP32","YOUR_USERNAME","PASSWORD")) {
      Serial.println("connected");
     } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

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

}

void loop() {

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

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("cmd_mpass", msg);
  }

}

ESP01_mqtt.ino
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "........";
const char* password = "........";
const char* mqtt_server = "broker.mqtt-dashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);


void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

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

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

  randomSeed(micros());

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

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect("ESP01","YOUR_USERNAME","PASSWORD")) {
      Serial.println("connected");
      client.subscribe("cmd_mpass");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

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

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}
Explanation of the essential elements of the program

  • The first step in the program is to connect to the Wifi network and to the mqtt server with the lines below by specifying the ssid, the password of the Wifi network and the IP address of the Mqtt server.
1
2
3
const char* ssid = "........";
const char* password = "........";
const char* mqtt_server = "......";
  • We need to specify the connection parameters to the Mqtt server in the reconnect() function as shown below.
client.connect("client_id","YOUR_USERNAME","PASSWORD")
  • We need to subscribe to MQTT topics by specifying the topic name in the reconnect() function, as shown below.
client.subscribe("topic");
  • to read the messages published on the topic to which we have subscribed we use the callback() function as shown below.

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}
- to publish a message on the topic of our choice we use the function client.publish(topic_name, message). note: the variable message must be of type char.

client.publish("cmd_mpass", msg);

Test

For the test it would be a question of visualizing the messages sent by the ESP32 and received by ESP01.

Files


Last update: May 21, 2023