Week 14 Networking and Communications

Group members

Week 14 group assignment Networking and Communications

Weeks assignment

This week we learnt how to make microcontollers talk with each other wirelessly. We worked on making a network connections using wifi between three Seeed XIAO ESP32-C3 microcontrollers. We have followed the following steps:

First, we started by downloading the esp32 library. We followed the following tutorial

Board management
Installation of library

Then, we followed another tutorial to get the mac address of our microcontrollers. Here is the code that we used. The code did not print any thing in the serial board but we got the mac address from the output terminal

MAC address

Then, we identified the board tools-> esp32-> XIAO_ESP32C3.

Then, we divided the roles so one of the team run the sender code and two to run the receiver code. The code sends and receive random coordinates

Messages received correctly

The code

Sender code

The sender source code was copied from: https://randomnerdtutorials.com/esp-now-one-to-many-esp32-esp8266/#more-95718

The code was edited in the following way:

  • Reducing the number of connections from three to two
  • Writing the correct MAC addresses for the connections
  • Reads data from Serial, and sends it through ESP NOW
/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/

  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 <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0x34, 0x85, 0x18, 0x05, 0x0C, 0x7C};
uint8_t broadcastAddress2[] = {0x34, 0x85, 0x18, 0x03, 0x69, 0x24};

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct test;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_send_cb(OnDataSent);

  // register peer
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  // register first peer  
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // register second peer  
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}



void loop() {

  if (Serial.available()) {
    char byte = Serial.read();

    int y = atoi(&byte);

    test.y = y;

  }

  test.x = test.x +1;

  if (test.x > 50) { test.x = 0; test.y = test.y +1; }
  if (test.y > 50) test.y = 0;

  esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));

  if (result == ESP_OK) {
    char strBuf[50];
    sprintf(strBuf, "Sent with success %d, %d", test.x, test.y);
    Serial.println(strBuf);
  }
  else {
    Serial.println("Error sending the data");
  }

  delay(2000);
}

Receiver code

The source code for the receivers were copied from the same source: https://randomnerdtutorials.com/esp-now-one-to-many-esp32-esp8266/#more-95718

The receiver code was mostly unedited.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
  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 <esp_now.h>
#include <WiFi.h>

//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  int x;
  int y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}

void setup() {
  //Initialize Serial Monitor
  Serial.begin(115200);

  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}

void loop() {

}

It is interesting how different microcontrollers have different internal capabilities, that are not at all tied to peripherals that we attach to them. The fact that ESP-32c is capable of Wifi connection with just an antenna, and XIAO RP2040 seems counterintuitive, but that is what happens when electronics shrinks and things are packaged to meet different demands.