Skip to content

Week 11. Group / Networking and Communications

This is group assignment page for West harima student :

Student

group assignment

  • send a message between two projects

hero video

ESP32C3s communicate with each other using ESP-NOW. A piezo element is attached to the back of the blue case. When the piezo element on the transmitter side reacts, the LED on the receiver lights up.

ESP-NOW

What is ESP-NOW

  • quote from ESPRESSIF

    ESP-NOW is a wireless communication protocol based on the data-link layer, which reduces the five layers of the OSI model to only one. This way, the data need not be transmitted through the network layer, the transport layer, the session layer, the presentation layer, and the application layer. Also, there is no need for packet headers or unpackers on each layer, which leads to a quick response reducing the delay caused by packet loss in congested networks.

board

I am using XIAO ESP32C3 for both sender and receiver. One of them was borrowed from my instructor.
I originally planned to use the original board I made, but it seems my XIAO ESP32S3 has broken…
I think the reason it broke is because I wanted to reuse the parts when I rebuilt the board, so I removed it many times.

Pinout(ESP32C3)

alt text alt text

input sensor

What is piezoelectric element?

An electronic component that generates voltage when pressure is applied (piezoelectric effect) or deforms when voltage is applied (reverse piezoelectric effect).

alt text alt text

I tried a simple design and stuck a piezoelectric element on a case made by 3D printing. - wiring

alt text

MAC address

How to identify the communication target?

  • Each network device has “MAC address” (Media Access Control address) to identify it.
  • MAC address consists of 12 letters (0-9 and A-F).
  • For confirming MAC address of the device used for this assignment, I used below

program from Oiia-Kurogara site(in Japanese)

#include "esp_mac.h"  // required - exposes esp_mac_type_t values

void setup() {

  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }

  Serial.println("Interface\t\t\t\t\t\tMAC address (6 bytes, 4 universally administered, default)");

  Serial.print("Wi-Fi Station (using 'esp_efuse_mac_get_default')\t");
  Serial.println(getDefaultMacAddress());

  Serial.print("WiFi Station (using 'esp_read_mac')\t\t\t");
  Serial.println(getInterfaceMacAddress(ESP_MAC_WIFI_STA));

  Serial.print("WiFi Soft-AP (using 'esp_read_mac')\t\t\t");
  Serial.println(getInterfaceMacAddress(ESP_MAC_WIFI_SOFTAP));

  Serial.print("Bluetooth (using 'esp_read_mac')\t\t\t");
  Serial.println(getInterfaceMacAddress(ESP_MAC_BT));

  Serial.print("Ethernet (using 'esp_read_mac')\t\t\t\t");
  Serial.println(getInterfaceMacAddress(ESP_MAC_ETH));
}

void loop() { /* Nothing in loop */ }

String getDefaultMacAddress() {

  String mac = "";

  unsigned char mac_base[6] = {0};

  if (esp_efuse_mac_get_default(mac_base) == ESP_OK) {
    char buffer[18];  // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator
    sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]);
    mac = buffer;
  }

  return mac;
}

String getInterfaceMacAddress(esp_mac_type_t interface) {

  String mac = "";

  unsigned char mac_base[6] = {0};

  if (esp_read_mac(mac_base, interface) == ESP_OK) {
    char buffer[18];  // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator
    sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]);
    mac = buffer;
  }

  return mac;
}

I confirm sender MAC address.
Once upload above program, I got below in serial monitor. This MAC address should be used in Sender’s program. alt text

Antenna

alt text

This was my first time using the antenna. It seems to make communication smoother.

It was difficult to install it, though:)

Programming

I generated the programming code using ChatGPT(GPT-4).
I gave the following instructions, plus instructions to have the serial monitor display the value that the piezo element is sensing.

Use ESPNOW to light the LED.
When the piezo element sensor responds, the LED should light up via wireless communication.

Sender:ESP32C3(mac address:68:67:25:EC:27:B4)
Recipient: ESP32C3 (mac address: 58:CF:79:EC:63:34)

input device: piezoelectric device (connect to A0 of the sender)
LED: Connects to A0 of the receiver

Below codes were generated by ChatGPT(GPT-4).

Sender’s code (XIAO ESP32C3)

#include <WiFi.h>
#include <esp_now.h>

#define PIEZO_PIN A0  // Pin connected to the piezo sensor
#define THRESHOLD 1500 // Threshold value for the sensor will depends on conditions

// Structure for the data to be sent
typedef struct struct_message {
  bool led_on;  // Flag to control whether the LED should turn on or off
} struct_message;

// MAC address of the receiver
uint8_t peerAddress[] = { 0x58, 0xCF, 0x79, 0xEC, 0x63, 0x34 };  // Receiver's ESP32 MAC address

struct_message myData;  // Data to be sent

void setup() {
  Serial.begin(115200);
  pinMode(PIEZO_PIN, INPUT);  // Set the pin connected to the piezo sensor as an input

  // Set Wi-Fi mode to Station (Client)
  WiFi.mode(WIFI_STA);

  // Initialize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW Initialization failed");
    return;
  }

  // Set the receiver's MAC address
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, peerAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;

  // Add the receiver as a peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Peer addition failed");
    return;
  }
}

void loop() {
  int piezoValue = analogRead(PIEZO_PIN);  // Read the value from the piezo sensor

  // Display the piezo sensor value on the serial monitor
  Serial.print("Piezo value: ");
  Serial.println(piezoValue);

  // If the piezo sensor value exceeds the threshold, send the signal to turn on the LED
  if (piezoValue > THRESHOLD) {
    myData.led_on = true;
  } else {
    myData.led_on = false;
  }

  // Send the data to the receiver
  esp_now_send(peerAddress, (uint8_t *)&myData, sizeof(myData));

  delay(100); // Send data every 100ms
}

Receiver’s code (XIAO ESp32C3)

#include <WiFi.h>
#include <esp_now.h>

#define LED_PIN A0  // Pin connected to the LED

// Structure for the received data
typedef struct struct_message {
  bool led_on;  // Flag to control whether the LED should turn on or off
} struct_message;

struct_message receivedData;  // Received data

// Callback function to match the esp_now_recv_cb_t type
void onDataReceive(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
  memcpy(&receivedData, incomingData, sizeof(receivedData));

  // Control the LED (commented out to avoid duplicate actions)
  // if (receivedData.led_on) {
  //   digitalWrite(LED_PIN, HIGH);  // Turn on the LED
  // } else {
  //   digitalWrite(LED_PIN, LOW);   // Turn off the LED
  // }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);  // Set the LED pin as an output

  // Set Wi-Fi mode to Station (Client)
  WiFi.mode(WIFI_STA);

  // Initialize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW initialization failed");
    return;
  }

  // Register the callback function for receiving data
  esp_now_register_recv_cb(onDataReceive);
}

void loop() {
  // Control the LED based on the received data
  if (receivedData.led_on) {
    digitalWrite(LED_PIN, HIGH);  // Turn on the LED
    delay(1000);  // Keep the LED on for 1 second
  } else {
    digitalWrite(LED_PIN, LOW);   // Turn off the LED
  }
}

result

success!

In this case the final threshold was 1500. This value may change from time to time.

NOTE :
1Mohm is used for the resistance of the piezo element.