11.Networking and Communications.¶
Networking and Networking deals with the exchange of data and information between the devices, systems or software over a network.It involves the transmission of data through various protocols and technologie which allow different devices to communicate with each other.Networking and communication happens either with Wired communication using cables and ethernet or with Wireless communication like Wi-Fi or Bluetooth.
Assignments for week 11:¶
1. Group Assignment
- send a message between two projects.
2. Individual Assignment
- design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)
1. Group Assignment.¶
Our group Assignment link is here
Learnings outcomes from the group assignment:¶
To meet the requirement of this assignment, we had used XIAO RP2040 and XIAO SAMD21 microcontrollers which were fabricated with PCB by individuals during electronic design and production weeks.XIAO RP2040 MCU with board was our controller board(Master) and XIAO SAMD21 MMCU board was as peripheral board(Slave), using I2C connections to communcate between them.
While running both codes, The master code program helped to type a messaage message into serial monitor which sent the message to an I2C Slave at the set address.The Slave board will respond to the message transmitted, letting the LED and speaker to play audio for a second.There was time delay of 3 seconds to sent message from Master to Slave.
2. Individual Assignment.¶
Before i go on with the assignment, it was important to understand about Node, Network or Bus.
Node - refers to a device that has ability to send or receive data or communicate or interact with other devices in a network.
Network or Bus - the method by which the nodes communicate.Example of network is Wi-Fi/bluetooth and I2C as Bus system.
Components Required for my assignment:¶
- XIAO ESP32-C3 as microcontroller as it has facility to communicate via Bluetooth/wireless.
Seeed Studio XIAO ESP32C3 is an IoT mini development board based on the Espressif ESP32-C3 WiFi/Bluetooth dual-mode chip, featuring a 32-bit RISC-V CPU that delivers powerful computing performance with its efficient architecture. It supports WiFi, and Bluetooth 5 (BLE) protocols.It consists of an external antenna to increase the signal strength for wireless applications.It is equipped 11 digital I/O pins and supports four serial interfaces such as UART, I2C and SPI.It also contain a small reset button and a bootloader mode button on the board.
This microcontroller is positioned as a high-performance, low-power, cost-effective IoT mini development board, suitable for low-power IoT applications and wireless applications.
This is my reference source about XIAO ESP32-C3.
Workflow of the assignment.¶
The Detailed workflow:
1. System Diagram¶
My final project requires the ESP NOW as the protocol for communication between the two XIAO ESP32C-3 PCB.Here is my system diagram that will show communication between my two fabricated PCB.
2. Designing two PCB to communicate.¶
As my final project consists of two parts, i have designed two PCB, one for the chirp device(MCU-1) and Clean(Soap dispenser).I designed using KICAD.Here is the design for CHIRPING bird(PCB-1)
Then i milled it using the SRM-2O Milling machine and soldered and check for the continuity after aech soldering actions.
Similarly, I also made my second PCB where it can be named as CLEAN(MCU-2).
After milling, i cxhecked for connections and it was working.
For more detailed, you can refer to my final project site under Electronics Design and Production
My final project titled CHIRP and Clean Device consists of two parts, CHIRP(MCU-1) with DF player mini, speaker, VL53L1X motion sensor and XIAO ESP32-C3.The other CLEAN(MCU-2) consists of DF player mini, speaker, VL53L1X distance sensor, mini water pump and XIAO ESP32 C-3.
The MCU-1(CHIRP) part will act as a master and MCU-2(CLEAN) part as receiver device where they can communicate using the ESP NOW protocol.
About ESP NOW¶
It is a protocol that can be used to exchange data between ESP32 boards programmed with Arduino IDE.This protocol was developed by Espressif which is featured with short package transmission.It can also be used to communicate between multiple devices ineasy way.
While using the ESP NOW to communicate between my two XIAO ESP32 C-3, i need to add the MAC address.MAC address stands for Media Access Control Address.It is a hardware identifier that identify each device on a network.MAC address are made of six group of two hexadecimal digits, separated by colons.
I do not need other library for ESP NOW as i have already installed the ESP32 library package by Espressif System. In order to add the MAC ADDRESS, i referred this website and i have used that example code to get my MAC ADDRESS.
here is the sample code i used from this website.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <esp_wifi.h>
void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.STA.begin();
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}
void loop(){
}
i have compiled and uploded that above code in the new sketch of Arduino IDE and opened the serial monitor to copy and paste the mac address.
i had added the mac address to the receiver code and let the transmitter and receiver to communicate.It was working.
i have added the audio file into the SD card for both the boards.My intructor Tshering Wangzom helped me to convert the mp3 files using FreeTTS.
After adding the mac address, i give the connections and uploaded the MCU-1(sender) by selecting the board and port.Meanwhile, i had also compiled and uploaded the receiver(MCU-2) by selecting the board and its port.
here are the codes:
Sender code(MCU-1)¶
#include <WiFi.h>
#include <esp_now.h> // - esp now library
#include <Wire.h>
#include <VL53L1X.h> // - Person_Approaching sensor library
#include <DFRobotDFPlayerMini.h>
#include <HardwareSerial.h>
VL53L1X sensor;
uint8_t receiverMac[] = {0x84, 0xFC, 0xE6, 0x00, 0xCF, 0x78}; // - recievers Address
#define SDA_PIN 4
#define SCL_PIN 6
#define DF_RX_PIN 9
#define DF_TX_PIN 10
#define THRESHOLD 800
#define DEBOUNCE_COUNT 3
HardwareSerial dfSerial(1);
DFRobotDFPlayerMini dfplayer;
bool personPresent = false;
uint8_t debounceCounter = 0;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed");
return;
}
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, receiverMac, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
Wire.begin();
Wire.setClock(400000); // use 400 kHz I2C
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
// information on range and timing limits.
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);
sensor.startContinuous(50);
//dfplayer
dfSerial.begin(9600, SERIAL_8N1, DF_RX_PIN, DF_TX_PIN);
if (!dfplayer.begin(dfSerial)) while (1);
dfplayer.volume(25);
}
void loop() {
sensor.read();
int Person_Approaching = sensor.ranging_data.range_mm;
Serial.print("Range: ");
Serial.print(Person_Approaching);
if (Person_Approaching < THRESHOLD && !personPresent) {
personPresent = true;
dfplayer.play(1); // Entry bird sound
//delay(5000);
}else if(Person_Approaching > THRESHOLD && personPresent) {
const char *message = "Person_Exited"; // - SMS like message
esp_err_t result = esp_now_send(receiverMac, (uint8_t *)message, strlen(message)); // - send SMS
if (result == ESP_OK) {
Serial.println("Message sent");
} else {
Serial.println("Failed to send");
}
dfplayer.stop();
personPresent = false;
}
Serial.println();
delay(2000); // Send every 5 seconds
}
Receiver code(MCU-2)¶
//-----final soap dispensor code------------
#include <WiFi.h>
#include <esp_now.h>
#include <Wire.h>
#include <VL53L1X.h>
#include <DFRobotDFPlayerMini.h>
#include <HardwareSerial.h>
// ---------- Configuration ----------
#define PUMP_PIN D2
#define SDA_PIN D4
#define SCL_PIN D5
#define DF_RX_PIN D9
#define DF_TX_PIN D10
#define THRESHOLD 80 // mm
// ---------- Globals ----------
VL53L1X sensor;
HardwareSerial dfSerial(1);
DFRobotDFPlayerMini dfplayer;
bool monitoring = false;
bool soapDispensed = false;
bool handDetected = false;
unsigned long triggerTime = 0;
int reminderStage = 0; // 0 = none, 1 = first msg, 2 = second msg, 3 = final msg
void resetFlags() {
monitoring = false;
soapDispensed = false;
handDetected = false;
triggerTime = 0;
reminderStage = 0;
}
// ---------- ESP-NOW Callback ----------
void onDataReceive(const uint8_t *mac, const uint8_t *incomingData, int len) {
String received = "";
for (int i = 0; i < len; i++) {
received += (char)incomingData[i];
}
Serial.print("Received: ");
Serial.println(received);
if (received == "Person_Exited") {
resetFlags();
monitoring = true;
triggerTime = millis();
Serial.println("Monitoring started after person exit");
}
}
// ---------- Setup ----------
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, LOW);
// ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed");
return;
}
esp_now_register_recv_cb(onDataReceive);
// VL53L1X
Wire.begin(SDA_PIN, SCL_PIN);
Wire.setClock(400000);
sensor.setTimeout(500);
if (!sensor.init()) {
Serial.println("VL53L1X not found");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);
sensor.startContinuous(50);
// DFPlayer
dfSerial.begin(9600, SERIAL_8N1, DF_RX_PIN, DF_TX_PIN);
if (!dfplayer.begin(dfSerial)) {
Serial.println("DFPlayer init failed");
while (1);
}
dfplayer.volume(30); // Adjust as needed
}
// ---------- Loop ----------
void loop() {
sensor.read();
int distance = sensor.ranging_data.range_mm;
if (!monitoring) return;
unsigned long elapsed = millis() - triggerTime;
// Hand detected
if (distance < THRESHOLD && !soapDispensed) {
Serial.println("Hand detected - dispensing soap");
digitalWrite(PUMP_PIN, HIGH);
delay(2000);
digitalWrite(PUMP_PIN, LOW);
dfplayer.play(4); // Thank you
soapDispensed = true;
monitoring = false;
return;
}
// Reminders based on time
if (elapsed > 5000 && reminderStage == 0) {
Serial.println("Playing reminder 1: Don't forget to wash your hands");
dfplayer.play(1); // Don't forget
reminderStage = 1;
} else if (elapsed > 10000 && reminderStage == 1) {
Serial.println("Playing reminder 2: Please wash your hands");
dfplayer.play(2); // Please wash
reminderStage = 2;
} else if (elapsed > 15000 && reminderStage == 2) {
Serial.println("Playing reminder 3: Unhygienic...");
dfplayer.play(3); // Unhygienic
reminderStage = 3;
}
delay(100);
}
My Learning outcomes from this individual assignment.¶
This assignment helped me understand the use of Wi-Fi to communicate and monitor the program of Arduino IDE by using ESP NOW as the protocol.