Skip to content

13. Networking and Communications - Collin K and Evan P

We combined our two networking projects, a bluetooth Phone to Xiao RP2040 and a Xiao RP2040 to Xiao RP2040 communication using I2C.

To do this we integrated our code together and replaced the seeed for the servo with my seeed for bluetooth on a breadboard.

Integrated Codes

Bluetooth Seeed

This code received values from the other seeed and displayed them in letter terms on my phone using bluetooth

/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <Wire.h>

BLEDis bledis;
BLEHidAdafruit blehid;

bool hasKeyPressed = false;

void setup() 
{
Serial.begin(115200);
Wire.begin(8);
while ( !Serial ) delay(10);   // for nrf52840 with native usb



Bluefruit.begin();
Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values

// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather 52");
bledis.begin();

/* Start BLE HID
* Note: Apple requires BLE device must have min connection interval >= 20m
* ( The smaller the connection interval the faster we could send data).
* However for HID and MIDI device, Apple could accept min connection interval 
* up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max
* connection interval to 11.25  ms and 15 ms respectively for best performance.
*/
blehid.begin();

// Set callback for set LED from central


/* Set connection interval (min, max) to your perferred value.
* Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms
* min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms 
*/
/* Bluefruit.Periph.setConnInterval(9, 12); */

// Set up and start advertising
startAdv();
}

void startAdv(void)
{  
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);

// Include BLE HID service
Bluefruit.Advertising.addService(blehid);

// There is enough room for the dev name in the advertising packet
Bluefruit.Advertising.addName();

/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
* 
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html   
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds
}

void loop() {
// Only send KeyRelease if previously pressed to avoid sending
// multiple keyRelease reports (that consume memory and bandwidth)
if ( hasKeyPressed )
{
    hasKeyPressed = false;
    blehid.keyRelease();

    // Delay a bit after a report
    delay(5);
}

if (Serial.available())
{

    int ch = Wire.read();
    char ch1 =  'M';
    char ch2 =  'A';
    char ch3 =  'X';
    char ch4 =  'M';
    char ch5 =  'I';
    char ch6 =  'N';

    // echo
    Serial.println(ch);

    if (Wire.read() >= 243) {
        blehid.keyPress(ch1);
        delay(5);
        blehid.keyRelease();
        delay(5);
        blehid.keyPress(ch2);
        delay(5);
        blehid.keyRelease();
        delay(5);
        blehid.keyPress(ch3);
        delay(5);
        hasKeyPressed = true;
    }else {
        blehid.keyPress(ch);
        hasKeyPressed = true;
    }

    // Delay a bit after a report
    delay(1000);
}
}

/**
* Callback invoked when received Set LED from central.
* Must be set previously with setKeyboardLedCallback()
*
* The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
*    Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
*/

Potentiometer Seeed

This code takes the values of the potentiometer, Maps it to a integer value, and sends it through I2C to the bluetooth seeed.

include

int val = 1;

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

void loop() { int sensorValue = analogRead(A0); float voltage = sensorValue * (5.0 / 1023.0); Serial.println(voltage); if (voltage == 5){ Wire.beginTransmission(8); Wire.write(val); Serial.println(“sent”); Serial.println(val); Wire.endTransmission(); delay(1000); } }

Final Working Code Video

Though we haven’t been able to perfect the value to letters, we have proven our understanding of networking and how to communicate 2 projects together accomplishing the goal of this weeks group assignment.


Last update: May 13, 2024