11. Embedded Networking and Communications
Here you can find a recording of the lecture from the 2th of april.
This week's assignments and learning outcomes, see here:
Group assignment:
- Send a message between two projects.
- Document your work on the group work page and reflect on your individual page what you learned.
You can find the documentation for our group assignments here.
Individual assignment:
- design, build and connect wired or wireless node(s) with network or bus addresses and a local input and/or output devices.
Questions to be answered/from Nueval:
Have you answered these questions?
- Linked to the group assignment page
- Documented your project and what you have learned from implementing networking and/or communication protocols.
- Explained the programming processes you used.
- Ensured and documented that your addressing for boards works.
- Outlined problems and how you fixed them.
- Included design files (or linked to where they are located if you are using a board you have designed and fabricated earlier) and original source code.
- Included a ‘hero shot’ of your network and/or communications setup.
Hero shot
Hero shot from group project
MQTT
Video showing how we used a MQTT broker, two microcontrollers as clients, a temperature reader and an OLED screen to send the response to a heat change of the sensor in Ísafjörður live on the OLED screen in Neskaupstaður. The distance between these two places is 445km by air see here.
Host and client communicating
Hero shot from individual project
Summary
This week, as a group, we tested the design rules for our 3D printers. We documented the results on our group page and you can find it here.
Work process detail
The group project
Me and Jóhannes Andrésson began working on this project in Fab Lab Ísafjörður during the Machine week.The idea was to use a temperature sensor that a microcontroller would read and then send the reading to another microcontroller which would display the temperature reading on a LCD screen.
Jóhannes set up a Wifi host with a XIAO ESP32-C3 and provide a wireless access point to connect to. The sensor is a One-Wire digital Temperature sensor. Then I set up a XIAO ESP32-C3 as Wifi client and connect to the access point and receive data from it. Then the readings should have been displayed on an I2C LCD display. This did not work out as it should have. We created an access point (Soft AP) with one device (which served as host). Then the other microcontroller served as a client and connected to it.
We used this page as a reference.
When Jóhannes had set up his host, I used Arduino IDE and ran this code on my microcontroller, which was the client:
#include <WiFi.h>
const char* ssid = "ESP32C3_Johannes";
const char* password = "****";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {}
As you can see
No communication between host and client
Then the host was supposed to send the temperature reading to the client, but the temperature did not appear on the LCD screen as it should have.
MQTT Communication
I travelled from Ísafjörður and home to Neskaupstaður. Since we could not finish our group assignment, we had to figure out a way to do that. Because of the long distances between the microcontrollers we couldn't use the same wireless network, so Jóhannes did some research and suggested we tried MQTT. He looked into the directions and explained all steps to me. We followed this tutorial, which used the arduino-mqtt library. We used public.cloud.shiftr.io and it worked in this project.
In the middle of all this planning I suddenly realized that the XIAO ESP32C3 microcomputer I had been using had been left behind in Ísafjörður so I had to find another XIAO ESP32C3 and solder pins to it. This did not take a long time because I am getting more used to soldering. That's good.
MQTT
Both ESP32-C3 work as client and there is no host, but a MQTT broker. The broker receives messages Every client can send messages to the broker, but it has to be under a certain topic. Client can then subscribes to a certain topic. In our case one client is publishing and the other is subscribing. The topic we created was called /fabisa
.
First we tested this example called ESP32DevelopmentBoard from the MQTT library and added our networking information and text into it:
// This example uses an ESP32 Development Board
// to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt
#include <WiFi.h>
#include <MQTT.h>
const char ssid[] = "fablab";
const char pass[] = "******";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("arduino", "public", "public")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/fabisa");
// client.unsubscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
// by Arduino. You need to set the IP address directly.
client.begin("public.cloud.shiftr.io", net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/fabisa", "Fab Lab Austurland");
}
}
Then Ólöf opened the serial monitor and there this text was displayed:
The next step was to change this code and add to it what we needed. Together we figured out, how to display the message from the topic on the screen. These are the codes after changes:
Code for Temperature Client
// Code based on example
// by Joël Gähwiler. Changes added by Jóhannes and Ólöf
// https://github.com/256dpi/arduino-mqtt
#include <WiFi.h>
#include <MQTT.h>
#include <DS18B20.h>
String temp;
DS18B20 ds(D1);
const char ssid[] = "ssid";
const char pass[] = "****";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
String readTemp() {
ds.selectNext();
temp = ds.getTempC();
return temp;
}
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("ESP32-C3-IFJ", "public", "public")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/fabisa");
// client.unsubscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
// by Arduino. You need to set the IP address directly.
client.begin("public.cloud.shiftr.io", net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
temp = readTemp();
client.publish("/fabisa", temp);
}
}
Jóhannes measured the temperature with the one-wire sensor and I connected the OLED screen and ran the code. Then Jóhannes measured the temperature.
This video shows the response to a heat change of the sensor in Ísafjörður live on the OLED screen in Neskaupstaður.
Host and client communicating
Learning outcome
Learning outcome from group assignment
I learned how a microcontroller can serve as a wifi host and another microcontroller can serve as a client that connects to the host's wifi. I also learned that with the MQtt protocol you can use two microcontrollers as clients where one of them publishes data under a certain topi and the other one subscribes to this topic.