Skip to content

11. Networking and Communications.

week11image11

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.

You can find below that i have tried using Wireless communication network to transmit data to control my XIAO ESP32 C3 microcontroller board to actuate the mini speaker with DF Player mini.

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.

This assignment consists of designing nodes from week6 and building nodes which I fabricated during week8, using mini speaker with the DF player Mini and wireless communication with bluetooth.I will be using Arduino IDE to run the program and Serial Bluetooth Terminal to interface from my mobile phone.

Components Required for my assignment:

  1. 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 wearable applications.

week11image1 This is my reference source about XIAO ESP32-C3.

  1. DF player Mini as Audio player with MicroSD card and mini speaker of 8ohm.

DF Player mini is a small, compact audio player with in built amplifier designed for use with speakers. It helps to decode MP3 and WAV files from microSD card.It can be used in combination with boards that have RX/TX capabilities.

Here is the link for dfplayer that i have referred.

Pin Description of DF Player mini week10image11 source link from here

Mini speaker

week11image2

It is 8 ohm and 0.5W mini speaker.

  1. Jumper wires and bread board.
  2. Blynk IoT app from play Store.
  3. External Power supply via USB from laptop.

The detailed process of above workflow is below:

Workflow of the assignment.

  1. Wiring the components that are listed above.
  2. Setting the Blynk IoT mobile app to monitor the program.
  3. Programming the ESP32-C3
  4. Testing the system using Blynk IoT from phone.

The Detailed workflow:

1. Wiring the components.

1.1) XIAO ESP32-C3 to DF Player mini.

- D3 (XIAO ESP32-C3) to TX(DF Player mini).
- D2(XIAO ESP32 -C3) to RX(DF Player mini)
- 5V(XIAO ESP32-C3) to VCC(DF Player mini)
- GND(XIAO ESP32-C3) to GND(DF Player mini)

1.2) mini speaker to DF Player mini

- SPK1(DF Player mini) to positive terminal of Mini speaker.
- SPK2(DF Player mini) to negative terminal of the mini speaker.

week11image3

1.3) Insert microSD card into the DF Player mini. I named audio files as 0001, 0002 and 0003 in four digits and kept inside a folder named MP3.

week11image3

2.Setting the Blynk IoT mobile app to monitor the program.

  • Firstly i went to Play Store and searched for Blynk IoT
  • Installed it.

week11image5

  • Sign Up to create my Blynk Account with my email with password.

week11image6

  • Switched to the developer mode and set up template name as “Xiao Dorji”, hardware as ESP32, Connection as Wi-Fi.

week11image7

  • Added datastreams by selecting ‘New Datastream’ and ‘Virtual’

week11image8

  • Next clicked on “Devices” and clicked on “New Device” and select “From Template”.The template ID, Template Name and Auth Token was generated which can be pasted in Arduino IDE code.

week11image9

  • Now i had built the mobile dashboard by openning the Blynk App and choosing Developer mode and added widgets to my mobile UI.There i added button for play/stop, slider for volume adjustment, Next or previous button.

week11image10

  • Now i saved the widgets that i have set and ready to control the ESP32 C3 program.

3. Programming the ESP32-C3

I had opened the Arduino IDE and pasted the code.I have depended to the template Blynk code.Here is the initial code i referred.

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL3gVIgbDe5"
#define BLYNK_TEMPLATE_NAME         "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "Dyq_EP2dii5IGTCi9B38PNimMUxejAd_"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

For this programming, some libraries that is to be added are:

  1. DFRobotDFPlayerMini.h – to control the DFPlayer Mini module for MP3 playback.

  2. SoftwareSerial.h – to enable serial communication on custom pins (for DFPlayer).

  3. WiFi.h – Provides Wi-Fi connectivity for the ESP32.

  4. WiFiClient.h – Handles Wi-Fi client connections.

  5. BlynkSimpleEsp32.h – Integrates Blynk IoT platform with ESP32 for remote control.

So, my final code after connecting with Wi-Fi and settings from Blynk IoT platform from phone is below.

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6ZU0_EUrL"
#define BLYNK_TEMPLATE_NAME "Xiao Dorji"
#define BLYNK_AUTH_TOKEN "VjTTdRgTDbYXl_O9dueiiS__ibP7Zejt"
#define BLYNK_PRINT Serial
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Initialize software serial on pins D0 (RX) and D1 (TX)
SoftwareSerial mySerial(D3, D2); 

// Create DFPlayer object
DFRobotDFPlayerMini myDFPlayer;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Dorji";
char pass[] = "12345678";

BlynkTimer timer;



// Volume control from Blynk slider on V1
BLYNK_WRITE(V2) {
  int vol = constrain(param.asInt(), 0, 30);
  myDFPlayer.volume(vol);
  Serial.print(F("Volume set to: "));
  Serial.println(vol);
}
bool status = 1;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0) {   
  // Called when the datastream virtual pin V0 is updated by Blynk.Console, Blynk.App, or HTTP API. 
  String value = param.asStr();
  if (value == "play") {
    if (status){
      Serial.println("'play' button pressed");
      myDFPlayer.play();  //Play the first mp3
      delay(1000);
      status = !status; // Toggle pause state
    }else if(!status) {
      myDFPlayer.start();  //start the mp3 from the pause
      delay(1000);
    }

  } else if (value == "stop") {
    Serial.println("'stop' button pressed");
    myDFPlayer.pause();  //pause the mp3

  } else if (value == "prev") {
    Serial.println("'prev' button pressed");
    myDFPlayer.previous();  //Play previous mp3
    delay(1000);

  } else if (value == "next") {
    Serial.println("'next' button pressed");
    myDFPlayer.next();  //Play next mp3
    delay(1000);

  } else {
    Serial.print("V0 = '");
    Serial.print(value);
    Serial.println("'");    
  }
}

void setup(){
  // Debug console
  Serial.begin(115200);
  mySerial.begin(9600);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // Initialize DFPlayer
  if (!myDFPlayer.begin(mySerial)) {
    Serial.println(F("DFPlayer initialization failed!"));
    Serial.println(F("1. Check RX/TX connections (must be crossed)"));
    Serial.println(F("2. Insert SD card with MP3 files"));
    while(true);  // Halt if initialization fails
  }
}

void loop(){
  Blynk.run();
  timer.run();
}

I selected the board and port and compiled and uploaded. Then clicked on serial monitor.

week11image12

4. Testing the Program using Blynk IoT app.

Now, i had opened the Blynk IoT app in my mobile and test the program by pressing to stop/start button, next/previous icon and the volume adjustment icon.My monitor from the mobile app worked.

weekimage11

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 the Blynk IoT platform which is user friendly from phone.

Disclaimer : I have used the code from my friend Tshering Dorji.However, i tried to change pins and some minor changes as per my fabricated board.

Files.

Here is the code i used for this assignment in Arduino IDE