Skip to content

11. Networking and Communications

Group Assignment

For this group assignment, I and Mkhitar decided to set up communication between our boards using the I2C protocol.I used my Xiao RP2040 board, and Mkhitar used a board based on the ATTINY3216. The goal was to send a message from one board to the other.

First, we connected the boards using three wires: GND (ground), SCL, and SDA. Since we hadn’t planned to use I2C when designing the boards, there were no pull-up resistors on them. So, we added the resistors on a breadboard, pulling the SDA and SCL lines up to 3.3V.

In our setup, the ATTINY3216 board acts as the sender — it transmits the message. My Xiao RP2040 board receives the data. On the ATTINY side, the code reads data from Serial and sends it over the I2C bus.

When Mkhitar’s board sent the signal 1, my board received it and activated a DC motor. When the signal 0 was received, the motor stopped. To make the motor's operation more visible, we attached a small sticker with a cat image to the motor shaft, so we could easily see it spinning.

As a result, we successfully established communication between two different microcontrollers and controlled the motor using I2C.

My part of the code

#include <Wire.h>
#define DC_PIN 2

void setup() {
Serial.begin(9600); 
Wire.begin(0x0A);
Wire.onReceive (I2C_ReceiveHandler);
pinMode(DC_PIN, OUTPUT);
}

byte Տվյալ;

void I2C_ReceiveHandler(int numBytes)
{
  while(Wire.available()) { 
    Տվյալ = Wire.read();
    char data = (char) Տվյալ;
    Serial.println(data); 
    if (data=='1') {
      analogWrite(DC_PIN, 200);
      Serial.println("motorOn");
    }
    else if(data=='0'){
    digitalWrite(DC_PIN, LOW);
    Serial.println("motorOff");
    }
  }
}

void loop() {

}

Individual Assignment

For my individual assignment, I decided to try out a new microcontroller, the ESP32-C3, which I recently received. I chose it because it supports Wi-Fi, opening up possibilities for wireless device control. As my first test, I connected an LED strip and successfully controlled its power on and off using a mobile app over Wi-Fi.

Blynk IoT

For this, I decided to use Blynk IoT — a platform for remote control and monitoring of devices over the internet. It allows easy connection of microcontrollers, such as the ESP32, to a mobile application without the need for complex coding. With Blynk, I can create custom control interfaces (such as buttons, sliders, and charts) on my smartphone and control devices via Wi-Fi.

First, it is necessary to register on the Blynk website, download the mobile app to your phone, and most importantly, install the Blynk library in Arduino IDE to work with the code.

To do this, I opened the Arduino IDE, went to the Library Manager, typed Blynk by Volodymyr Shymanskyy in the search bar, and installed it.

To create a template in Blynk, you can use either the browser version or the mobile app. I tried both options.

Blynk Web

To create a template in the web version, go to the Developer Zone, select My Templates, and click + New Template. In the window that opens, first give the template a name, then choose Hardware and Connection Type. After that, click Done.

After creating a new device on the home page, I find Template ID, Name, and AuthToken, which need to be copied and pasted at the beginning of my code to control the LED.

After the template is created, go to Data Streams, click Edit, then + New Data Stream, and select Virtual Pin. A window will open to configure the virtual pin, where you need to give the pin a name, choose the pin number, and set the Data Type.

Then, set the minimum and maximum values. Since I only need to turn the LED strip on and off, I set 0 and 1. The Default Value is set to 0 so that the LED is initially off. After completing these steps, click Create.

Next, to add a device for further work, I went to the Devices section in the main menu, selected + New Device. A window will open where I choose Manual Entry, and then in this window, I select the previously created template.

After that, go back to the Developer Zone in the main menu, select My Templates, find the template we need, and click on it.

To make changes, click on Web Dashboard, then Edit.

Now, I can add a switch. I find the desired switch, hold it, and drag it to the workspace. Then, I go to Switch Settings to link its actions to the virtual pin. I select the previously created DataStream P1(V1), where P1 is the pin name, and V1 is the virtual pin number. After that, I click Save and Save & Apply.

Blynk App

I open the Blynk app on my smartphone. Since I already have an account, I simply log in by entering my credentials.

  1. Create a new device. After logging in, the main screen of the app displays a "Create New Device" button. By pressing this button, I start the process of creating a new device.

  2. Name the template. On the next screen, I am prompted to enter a name for my new template. I name it, for example, "ESP32c3" so I can easily identify which device I’m working with, and then press "Continue" to move to the next step.

  3. Choose the tile design. Now, the app asks me to select a tile design for the device. I choose "Button", as I want to create a button for control, but at this stage, I could also select other components, such as sliders or graphs, depending on what I need to control the device.

  4. Move to the workspace. After choosing the tile design, I enter the workspace where I can configure and add additional components. The workspace shows various control elements, such as buttons, sliders, gauges, graphs, and more. I can place, move, and adjust these elements according to my needs.

  5. Add components. To add a new component, I simply tap the plus sign on the green background that appears on the screen and drag the selected component to the desired location in the workspace. This allows me to quickly and easily set up the interface for controlling my device.

Since I first created the template on the website, I decided to try uploading it.

  • First, I press the +Add Device button to add a new device.

  • Then, I select the Manual from Template option because I want to upload the template I created earlier.

  • After that, I am prompted to select the template, and I choose the one I created before.

  • After selecting the template, I press on it again to confirm my choice.

  • Finally, I press the Create button to create the device with this template.

  • After pressing Create, I am redirected to the template page.

  • On this page, I click on the green icon with a key symbol, which has the Switch to Developer Zone function.

  • As I mentioned earlier, in the workspace, I add a switch to control the device.

  • At the bottom of the workspace, there is a settings icon — I click on it to configure the template.

  • The Settings window opens, where I can adjust the main parameters of the template.

  • Then there is the The Design window, where I can change the main color and add icons to the template.

  • The Datastreams window is used to add a virtual pin to the button (the name may be different because I created a new virtual pin).

  • The Code window allows me to copy the Template ID, Name, and AuthToken to add them to the code.

  • When I press the button in edit mode, the button settings window opens.

  • In this window, I can change the button's mode: the first option is Push, where the button is active only while it's pressed; the second option is Switch, which works like a regular toggle switch. The other two options are available in the paid version. A bit above, I can select a Virtual Pin by clicking on this field.

  • If no VDatastreams has been created yet, I can either select or create a new one right in this window by pressing + Create Virtual Pin Datastream.

  • Here, everything is similar to the web version: I enter the pin name, data type, minimum and maximum values, and the default value. After setting everything up, I press Save and go back to the button settings window.

  • This is how the button settings window looks after all the changes.

  • By clicking on Design, I can customize the appearance of the button:

  • I can add a name for the button, meaning I can set the text that will be displayed on the button in its ON and OFF states.

  • Change the text size on the button.

  • Change the button's color.

  • After making all the changes, I press the arrow at the top of the screen to exit the edit mode and switch to working mode.

Programming part

To write the code, I used the Blynk example library. This library contains many ready-made examples covering different functions and features of the Blynk platform. They help to better understand the code structure, how to connect devices, configure virtual pins, and interact with the app. Thanks to these examples, the process of writing your own code becomes much simpler.

Code for Blynk

#define BLYNK_TEMPLATE_NAME "ESP32c3"    // Sets the template name for the Blynk project to identify the device in the Blynk app.
#define BLYNK_AUTH_TOKEN "ngAjSe5CRlp_grjzXjpsi3K2hjnofWXf"    // Sets the authorization token to connect the device to Blynk.
#define BLYNK_TEMPLATE_ID "TMPL6eNc4GzN2"    // Sets the unique template ID for your Blynk project (optional, can be left empty).

#define BLYNK_PRINT Serial    // Specifies that all debug messages will be output through the serial port.

#include <WiFi.h>    // Includes the library for Wi-Fi functionality on ESP32.
#include <WiFiClient.h>    // Includes the library for Wi-Fi client functionality.
#include <BlynkSimpleEsp32.h>    // Includes the Blynk library for simple Blynk connection using ESP32.

char ssid[] = "staff";    // Sets the Wi-Fi network name to connect to.
char pass[] = "SRGB2020";    // Sets the Wi-Fi network password to connect to.
int pinValue = 0;    // Variable to store the value from the virtual pin.

WidgetLED led1(V0);    // Creates an LED widget in Blynk on virtual pin V0, which will control the LED in the Blynk app.
const int ledPin = 8;    // Sets the physical pin 8 to which the external LED is connected.

BLYNK_WRITE(V0)    // Function to handle writes from virtual pin V0. It triggers when the user changes the value in the Blynk app.
{
  pinValue = param.asInt();    // Assigns the value from the parameter sent from virtual pin V0 to the pinValue variable.
}

void setup()    // The function that runs once when the device starts.
{
  Serial.begin(115200);    // Initializes the serial port for debugging messages at the specified baud rate.
  pinMode(8, OUTPUT);    // Sets pin 8 as an output for the LED.
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);    // Initializes the Blynk connection using the authorization token, Wi-Fi SSID, and password.
}

void loop()    // The main function that runs in an infinite loop.
{
  Blynk.run();    // Keeps the Blynk library running in the background, handling incoming requests and sending data.

  if (pinValue == 1) {    // If pinValue is 1 (signal from the Blynk app), turn on the LED.
    digitalWrite(ledPin, HIGH);    // Turns on the LED at pin 8.
  }
  else {    // If pinValue is not 1, turn off the LED.
    digitalWrite(ledPin, LOW);    // Turns off the LED at pin 8.
  }
}

1. Blynk Settings

#define BLYNK_TEMPLATE_NAME "ESP32c3"
#define BLYNK_AUTH_TOKEN "ngAjSe5CRlp_grjzXjpsi3K2hjnofWXf"
#define BLYNK_TEMPLATE_ID "TMPL6eNc4GzN2"
#define BLYNK_PRINT Serial

Here I enter the data from my Blynk project: template name, ID, and token. The line BLYNK_PRINT Serial allows me to see debug messages in the Serial Monitor.

2. Libraries

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

I include libraries that help my ESP32 work with Wi-Fi and the Blynk app.

3. Wi-Fi Data and Variables

char ssid[] = "staff";
char pass[] = "SRGB2020";
int pinValue = 0;

WidgetLED led1(V0);
const int ledPin = 8;

Here I write the name of my Wi-Fi network (ssid) and password (pass). I create a variable pinValue, which will store the command from the app. I also create a virtual LED in Blynk on pin V0 and set pin 8 on my board to control the real LED or motor.

4. Getting Commands from the App

BLYNK_WRITE(V0)
{
  pinValue = param.asInt();
}

This part runs when I press the button in the Blynk app. The value (0 or 1) is saved to the pinValue variable.

5. Setup on Start

void setup()
{
  Serial.begin(115200);
  pinMode(8, OUTPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

In the setup() function, I start the Serial Monitor, set pin 8 as output, and connect to Wi-Fi and Blynk.

6. Main Loop

void loop()
{
  Blynk.run();

  if (pinValue == 1) {
    digitalWrite (ledPin, HIGH);
  }
  else {
    digitalWrite (ledPin, LOW);
  }
}

In loop(), I run Blynk to keep the connection working. Then I check the pinValue:

  • If it is 1, I turn pin 8 on, and the motor or LED starts working.
  • If it is 0, I turn pin 8 off, and the device stops.

And here is the result — how the program works.

Conclusion

This week was very intense and productive. I learned how to work with the Blynk platform, which greatly simplified the implementation of my project. The platform turned out to be very user-friendly for beginners, as it is easy to learn and does not require deep programming knowledge. Thanks to this, I was able to focus on the most important aspects of my project rather than spending time on learning complex software solutions.

After comparing the web version with the mobile app, I concluded that working through the mobile app is much more convenient and intuitive. The mobile app has a more user-friendly interface and provides quick access to all the features. This is especially important for remote control, when I need to quickly and easily manage the system.

I also noticed that the mobile version provides much more comfort when managing, especially in real-world conditions. With it, I can easily turn devices on and off, monitor the system's status, and receive notifications.

Considering all these factors, I am confident that I will continue using Blynk as the main app for remotely controlling my hydroponic system. The platform fully meets my needs and allows me to easily manage the system from any device.

Files of this Week

Blynk code