Networking and Communications
Group assignments
- send a message between two projects
My group assignments
Individual Assignments
- design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or
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 process(es) 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
PC working environment
- PC: MacBook Pro(16-inch,2019)
- OS: Sonoma 14.7.2
- Terminal: zsh
hero video
trying wireless come wireless communication via ESP-NOW. ESP-NOW is used for wireless communication. I will be using piezo sensors and DFplayer for my final project. This test worked very well for me because I wanted to create a system that would play a sound when the piezo sensor responded.
connection
The instructor told me to connect the power from the receiver when wiring.
- sender
- receiver
ESP-NOW
This time I tried using two XIAO ESP32C3F.
- Pinout
1.DFplayer
- Pinout
programming
I asked ChatGPT the following question
SP-NOW to communicate wirelessly and play the sound on the receiving side DFplayer.
Sound when the value of the piezo element exceeds 1500.
IDE : Arduino IDE
Board : XIAO ESP32C3(both transmitter and receiver)
input devece : piezo device (connect to A0 on transmitter side)
output devece : DFplayer (connect to TX and RX on the receiving side)
MAC address
- 68:67:25:EC:27:B4 (transmitting side)
- 58:CF:79:EC:63:34 (receiving side)
Please write a program with the above conditions.
- IDE : Arduino IDE
- Board : XIAO ESP32C3
- library for Sender
library | role |
---|---|
WiFi.h | Library for using the Wi-Fi function of ESP32. |
esp_now.h | A library for using the ESP-NOW communication protocol. |
- library for Receiver
library | role |
---|---|
WiFi.h | Library for using the Wi-Fi function of ESP32. |
esp_now.h | A library for using the ESP-NOW communication protocol. |
SoftwareSerial.h | Instruction to load a library for serial communication in software |
DFRobotDFPlayerMini.h | Instruction to load library for FPlayer Mini MP3 player |
Sender(XIAO ESP32C3)
#include <WiFi.h>
#include <esp_now.h>
#define PIEZO_PIN A0
const int THRESHOLD = 100;
uint8_t peerAddress[6] = { 0xCC, 0xBA, 0x97, 0x16, 0x17, 0x98 };
struct __attribute__((packed)) Payload { uint16_t value; };
Payload tx;
// Callback function for send status
void onSend(const uint8_t*, esp_now_send_status_t st) {
Serial.printf("[SEND-CB] status=%d (%s)\n",
st, st == ESP_NOW_SEND_SUCCESS ? "OK" : "FAIL");
}
void setup() {
Serial.begin(115200);
// Wait for Serial to initialize (timeout after 2 seconds)
uint32_t t0 = millis();
while (!Serial && millis() - t0 < 2000) {}
WiFi.mode(WIFI_STA); // Start Wi-Fi driver in station mode
delay(200); // Wait for Wi-Fi to stabilize
Serial.printf("[MAC] %s\n", WiFi.macAddress().c_str());
// Initialize ESP-NOW
ESP_ERROR_CHECK(esp_now_init());
esp_now_register_send_cb(onSend);
// Register peer device
esp_now_peer_info_t p{};
memcpy(p.peer_addr, peerAddress, 6);
p.channel = 0; // Use current Wi-Fi channel
ESP_ERROR_CHECK(esp_now_add_peer(&p));
Serial.println("[Wi-Fi] Peer added");
}
void loop() {
uint16_t v = analogRead(PIEZO_PIN);
if (v > THRESHOLD) {
tx.value = v;
esp_now_send(peerAddress, (uint8_t*)&tx, sizeof(tx));
Serial.printf("[SEND] Piezo=%u HIT!\n", v);
delay(15);
} else {
Serial.printf("[SEND] Piezo=%u\n", v);
}
delay(100);
}
Receiver(XIAO ESP32C3)
#include <WiFi.h>
#include <esp_now.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#define RX_PIN 20 // XIAO ESP32C3 GPIO20 or XIAO ESP32S3 GPIO44
#define TX_PIN 21 // XIAO ESP32C3 GPIO21 or XIAO ESP32S3 GPIO43
SoftwareSerial dfSer(RX_PIN, TX_PIN);
DFRobotDFPlayerMini df;
uint8_t senderMac[6] = { 0xCC, 0xBA, 0x97, 0x15, 0x47, 0x70 };
const int THRESHOLD = 500;
struct __attribute__((packed)) Payload { uint16_t value; };
volatile bool playReq = false;
uint32_t lastPlay = 0;
// Callback function for receiving ESP-NOW data
void onRecv(const esp_now_recv_info_t* info,
const uint8_t* data, int len)
{
if (len == sizeof(Payload)) {
const Payload* p = reinterpret_cast<const Payload*>(data);
Serial.printf("[RECV] from %02X:%02X:%02X:%02X:%02X:%02X val=%u\n",
info->src_addr[0], info->src_addr[1], info->src_addr[2],
info->src_addr[3], info->src_addr[4], info->src_addr[5],
p->value);
if (p->value > THRESHOLD) playReq = true;
}
}
void setup() {
Serial.begin(115200);
// Wait for Serial to initialize (timeout after 2 seconds)
uint32_t t0 = millis();
while (!Serial && millis() - t0 < 2000) {}
/* Initialize DFPlayer */
dfSer.begin(9600);
delay(1000); // Wait for DFPlayer to boot
if (!df.begin(dfSer)) {
Serial.println("DFPlayer init fail → continuing without sound");
} else {
df.volume(25);
Serial.println("DFPlayer ready.");
}
/* Initialize ESP-NOW */
WiFi.mode(WIFI_STA);
delay(200); // Wait for Wi-Fi driver to start
ESP_ERROR_CHECK(esp_now_init());
esp_now_register_recv_cb(onRecv);
esp_now_peer_info_t pi{};
memcpy(pi.peer_addr, senderMac, 6);
pi.channel = 0;
ESP_ERROR_CHECK(esp_now_add_peer(&pi));
Serial.println("[Wi-Fi] Peer added");
}
void loop() {
// Play sound if triggered and at least 500 ms has passed since last play
if (playReq && millis() - lastPlay > 500) {
Serial.println("[PLAY] 0001.mp3");
df.play(1);
lastPlay = millis();
playReq = false;
}
}
Result
Wireless communication appears to be working well, but the value detected by the piezoelectric element may be too small to respond. I thought it was necessary to consider how much to set the value from now on, taking resistance and other factors into consideration.
impressions
- I thought wireless could be used for my FINAL project.
- I would need to find out how many piezo element values to use.
-
On May 23, after uploading code to the Xiao ESP32S3, the device's port was no longer recognized. This issue occurred because a program was written to GPIO pins originally assigned for USB-JTAG, without verifying the pin configuration beforehand. To prevent such problems, it is important to check the pin assignments before uploading code. The board can be recovered using bootloader mode.See below for details.
-
NOTE
- Seed studio bootloader mode
- Espressif - Note
- ESP32S3.USB-JTAG: GPIO19 and GPIO20 are used by USB-JTAG by default. If they are reconfigured to operate as normal GPIOs, USB-JTAG functionality will be disabled.
Reference Links
- L-Thica with piezoelectric element(in Japanese)