Individual assignment:
Design, build, and connect wired or wireless node(s) with network or bus addresses
I choose to use my microcontroller board, which I designed in Week 9's Output_Devices, and connect two ESP32 through Bluetooth connection for this week's assignment.
Bluetooth is a short-range wireless technology standard that is used for exchanging data between fixed and mobile devices over short distances and building personal area networks. In the most widely used mode, transmission power is limited to 2.5 milliwatts, giving it a very short range of up to 10 metres.
Devices connected in a Bluetooth network communicate with each other using ultra-high frequency (UHF) radio waves. These are electromagnetic waves with frequencies around 2.4 gigahertz (2.4 billion waves per second).
Bluetooth communication can be used for a variety of purposes, such as transferring files between devices, connecting to wireless headphones or speakers, or controlling smart home devices. It is a convenient and easy way to connect and communicate between devices without the hassle of cables and wires.
Bluetooth data communication
Bluetooth employs Radio Frequency (RF) for communication. It makes use of frequency modulation to generate radio waves in the ISM band. The usage of Bluetooth has widely increased for its special features. Bluetooth offers a uniform structure for a wide range of devices to connect and communicate with each other.
To establish communication between two different boards, I connected two different boards together. And if you want to learn more about bluetooth connectivity click here. I used the esp32 dev (board I made) and esp32 dev (commercial) as mentioned in the assignment. To learn more about how I made the board, click here. The esp32 is also embedded with bluetooth, so it is suitable to use.
Embedded networking and communications refers to the integration of networking and communication technologies into embedded systems. Embedded systems are computer systems that are built into devices and equipment to perform specific functions. They are found in a wide range of devices, such as cars, medical equipment, home appliances, and industrial control systems.
Below is how I made it with codes:
As previously mentioned, there are two boards that need to communicate with each other. To establish communication, I designated one board as the master and the other as the slave. To enable this communication, the master device needs to scan the slave device's MAC address and address it to establish a connection. Initially, I uploaded the code responsible for scanning the MAC address of the slave device, and subsequently, the following codes facilitated their communication.
void setup(void) {
Serial.begin(115200); // starting serial terminal
Serial.println("-----------------");
uint8_t macBT[6]; // array variable holding the mac address to store the
esp_read_mac(macBT, ESP_MAC_BT);
}
void loop() {
uint8_t macBT[6];
esp_read_mac(macBT, ESP_MAC_BT);
Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\r\n", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
delay(10000);
}
I have chosen the shown address, copied it, and will utilize it as I mentioned before.
I then added the mac address of the esp32 (slave) inside the highlighted variable "address[6]"
master
#include "BluetoothSerial.h"
#define USE_NAME // Comment this to use MAC address instead of a slaveName
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
#ifdef USE_NAME
String slaveName = "myslave"; // Change this to reflect the real name of your slave BT device
#else
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing 7C:9E:BD:49:4E:2A
uint8_t address[6] = {0x7C, 0x9E, 0xBD, 0x49, 0x4E, 0x2A}; // Change this to reflect real MAC address of your slave BT device
#endif
String myName = "MasterBT";
void setup() {
bool connected;
Serial.begin(115200);
SerialBT.begin(myName, true);
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
#ifndef USE_NAME
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
#ifdef USE_NAME
connected = SerialBT.connect(slaveName);
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
#else
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
#endif
if(connected) {
Serial.println("Connected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Successfully!");
}
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
SerialBT.connect();
if(connected) {
Serial.println("Reconnected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
}
}
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
Slave
#include "BluetoothSerial.h"
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.
String device_name = "myslave";
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin(device_name); //Bluetooth device name
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}