14. Networking and communications¶
Tasks¶
Group assignment: - Send a message between two projects - Document your work to the group work page and reflect on your individual page what you learned
Individual assignment: - design, build and connect wired or wireless node(s) with network or bus addresses and a local input and/or output devices
Individual:¶
For this week, I used the same board design as I did for input week. The only difference was that I placed an attena on the XIAO ESP-32-C3 microcontroller.
To integrate Bluetooth functionality, I wanted to repurpose my original MPU6050 accelerometer code and have it wirelessly send sensor data to my phone using Bluetooth Low Energy. To do this, I copied my entire original code from input week and pasted it into ChatGPT, asking it to change the code to include BLE support while keeping the MPU6050.
#include <Wire.h> // Standard I2C library
#include "I2Cdev.h" // Required by MPU6050 library
#include "MPU6050.h" // MPU6050 library by Electronic Cats/jrowberg
/* MPU6050 default I2C address is 0x68 */
MPU6050 mpu;
//MPU6050 mpu(0x69); // Use if AD0 pin is HIGH
//MPU6050 mpu(0x68, &Wire1); // Use if using a non-default Wire interface
// Define the LED pin (as specified, D2 for XIAO ESP32-C3)
const int ledPin = D2; // Or just 2 if D2 isn't defined in your board package
// Define the threshold for X-axis acceleration
// --- YOU WILL LIKELY NEED TO TUNE THIS VALUE ---
// Observe the 'ax' values printed to the Serial Monitor when the sensor is still
// and when you move it in the way you want to detect. Pick a value slightly
// above the resting noise level but below the movement level you want to trigger the LED.
// Raw values range roughly from -17000 to +17000 for +/-2g sensitivity (default).
const int accelThreshold = 15000; // Example threshold, adjust as needed! //orginally 4000 set to 3000
// Variables to store sensor data
int16_t ax, ay, az;
int16_t gx, gy, gz;
void setup() {
// Initialize Serial communication for debugging
Serial.begin(115200); // Use a common baud rate like 115200
while (!Serial); // Wait for Serial port to connect (optional, good practice)
Serial.println("Initializing I2C devices...");
// Initialize I2C communication
// For XIAO ESP32-C3, default SDA is D4, SCL is D5.
// Wire.begin() typically uses the default pins.
// If your MPU6050 is connected to different pins, specify them:
// Wire.begin(SDA_PIN, SCL_PIN); e.g. Wire.begin(D4, D5);
Wire.begin();
// Initialize MPU6050
Serial.println("Initializing MPU6050...");
mpu.initialize();
// Verify MPU6050 connection
Serial.println("Testing MPU6050 connection...");
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed! Check wiring.");
// Halt execution if the sensor isn't found
while (1);
}
// (Optional but recommended) Load calibration offsets if you have them
// Use a separate sketch (like MPU6050_Calibration) to find these values
// Serial.println("Applying MPU offsets (if any)...");
// mpu.setXAccelOffset(YOUR_OFFSET_X);
// mpu.setYAccelOffset(YOUR_OFFSET_Y);
// mpu.setZAccelOffset(YOUR_OFFSET_Z);
// mpu.setXGyroOffset(YOUR_GYRO_OFFSET_X);
// mpu.setYGyroOffset(YOUR_GYRO_OFFSET_Y);
// mpu.setZGyroOffset(YOUR_GYRO_OFFSET_Z);
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Start with the LED off
Serial.println("Setup complete. Reading sensors...");
Serial.println("Move the sensor along the X-axis to trigger the LED.");
Serial.println("Raw Accelerometer readings (ax, ay, az):");
}
void loop() {
// Read raw accelerometer and gyroscope data
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print the raw accelerometer values for debugging/tuning
Serial.print("ax: "); Serial.print(ax);
Serial.print("\tay: "); Serial.print(ay);
Serial.print("\taz: "); Serial.println(az);
// --- LED Control Logic ---
// Check if the absolute value of X-axis acceleration exceeds the threshold
if (abs(ax) > accelThreshold) {
// If acceleration is high enough, turn the LED ON
digitalWrite(ledPin, HIGH);
// Optional: Print a message when triggered
// Serial.println("X-Axis Threshold Exceeded - LED ON");
} else {
// Otherwise, turn the LED OFF
digitalWrite(ledPin, LOW);
}
// Add a small delay to prevent overwhelming the Serial portr
// and allow time for sensor readings to stabilize slightly.
// Adjust as needed, but avoid long delays for responsiveness.
delay(50);
}
I pasted my original code from input week into chatgpt, and I asked it to use this code and change it so it allows me connect to a bluetooth app. It then gave me this code:
Chatgpt process¶
-
I shared my existing Arduino code, explaining I wanted to add Bluetooth to transmit the accelerometer data.
-
ChatGPT suggested using the BLEDevice, BLEUtils, BLEServer, and BLE2902 libraries for setting up the Bluetooth Low Energy server on the XIAO ESP32-C3.
-
It created the code to initialize a BLE server. This set up a service and characteristic UUID, and send acceleration data as a string of notifications when a device was connected.
-
It kept my original LED control based on the X-axis acceleration threshold and added code to for the BLE connections and data transmission.
Here is the entire chatgpt conversation.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 mpu;
// LED on GPIO 3 (D1 on XIAO ESP32-C3)
const int ledPin = 3;
const int accelThreshold = 15000;
// Sensor data
int16_t ax, ay, az, gx, gy, gz;
// BLE UUIDs
#define SERVICE_UUID "12345678-1234-1234-1234-1234567890ab"
#define CHARACTERISTIC_UUID "abcd1234-5678-90ab-cdef-1234567890ab"
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
// BLE server callback
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Wire.begin(6, 7); // XIAO ESP32-C3 SDA=SCL pins
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 connected.");
} else {
Serial.println("MPU6050 connection failed!");
while (1);
}
// BLE Setup
BLEDevice::init("ESP32-C3-Motion");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->start();
Serial.println("BLE advertising started...");
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Debug output
Serial.print("ax: "); Serial.print(ax);
Serial.print(" ay: "); Serial.print(ay);
Serial.print(" az: "); Serial.println(az);
// LED trigger
if (abs(ax) > accelThreshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Send data over BLE if connected
if (deviceConnected) {
char data[50];
snprintf(data, sizeof(data), "AX:%d AY:%d AZ:%d", ax, ay, az);
pCharacteristic->setValue(data);
pCharacteristic->notify();
}
delay(200);
}
I then uploaded this code through ardunino on my microcontroller. I then set up the curcuit on a breadboard. Once it was set up, I downloaded the app BluetoothLE on my phone. Once it was downloaded, I waited for XIAO ESP32-C3 to pop up on Scan Devices. I then pressed connect and waited until the data popped up.
Once the I knew that everything worked I used my PCB board from input week and connect it to my iphone.
Group Reflection¶
Here is the link to our group assignment.
For our networking and communications group assignment, we connected two different microcontrollers—XIAO RP2040 and XIAO ESP32-C3—using a direct GPIO connection. We linked a touch sensor on the RP2040 to a servo motor on the ESP32-C3 so that when the sensor was touched, the servo would move. Each team member contributed to different aspects of the project: Angel led the connection and testing, Elle handled the wiring and code setup, and I designed and milled the final PCB. Thistaught us how to send digital signals between boards, the importance of sharing a common ground, and how to coordinate separate inputs and outputs across different devices. Moving from a breadboard to a custom PCB helped us understand of how to design more permanent and organized circuits.
Individual Reflection¶
For my individual assignment, I used my previous MPU6050 board and modified the code to add Bluetooth Low Energy using the XIAO ESP32-C3. After setting up the BLE structure with the code, I connected it to my phone using the LightBlue app to view acceleration data. The project helped me understand how BLE works, how to structure sensor data for wireless transmission, and how to troubleshoot Bluetooth connections. Seeing my board wirelessly send data to my phone made the concept of networking much more understanding.
Here are files for this week.