Skip to content

13. Networking and Communications: Landon Broadwell & Alana Duffy

The goal of this week’s group documentation was to connect two projects through communication or networking between them. Our group members were Landon Broadwell and Alana Duffy. To connect the two projects as is required, we looked into a simple I2C communication. To connect Landon’s ATtiny1614 and its BNO-055 sensor with Alana’s Xiao RP2040-nRF52840, we hooked up the SDA and SCL lines of each sensor and to the BNO-055 sensor and asked GPT 4.0 to modify Alana’s code so that it could read and take a value from Landon’s.

Prompt:

This above code uses Bluetooth to communicate with a phone. Normally, the code will create a window on a computer to type in the message, but I want to transmit a sensor reading from another device over I2C, read it, and then send it over bluetooth.

(Code)

ChatGPT responded by providing the following output for the code:

#include <bluefruit.h>
#include <Wire.h>

int data;
BLEDis bledis;
BLEHidAdafruit blehid;

bool hasKeyPressed = false;
bool newSensorData = false;

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

    Serial.println("Bluefruit52 HID Keyboard Example");
    Serial.println("--------------------------------\n");

    Serial.println();
    Serial.println("Go to your phone's Bluetooth settings to pair your device");
    Serial.println("then open an application that accepts keyboard input");

    Serial.println();
    Serial.println("Enter the character(s) to send:");
    Serial.println();

    Bluefruit.begin();
    Bluefruit.setTxPower(4);  

    bledis.setManufacturer("Adafruit Industries");
    bledis.setModel("Bluefruit Feather 52");
    bledis.begin();

    Wire.begin(8);
    Wire.onReceive(receiveEvent);

    blehid.begin();

    // Set callback for set LED from central
    blehid.setKeyboardLedCallback(set_keyboard_led);

    // 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();

    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);
    }

    // Check if new sensor data is available
    if (newSensorData)
    {
        newSensorData = false;

        // Convert the sensor data to a character
        Serial.println(data);
        int ch = (int) data;

        // Send the sensor data over Bluetooth
        blehid.keyPress(data);
        hasKeyPressed = true;

        // Echo the character to the Serial monitor
        Serial.write(ch);

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

void set_keyboard_led(uint16_t conn_handle, uint8_t led_bitmap)
{
    (void) conn_handle;

    // light up Red Led if any bits is set
    if ( led_bitmap )
    {
        ledOn( LED_RED );
    }
    else
    {
        ledOff( LED_RED );
    }
}

void receiveEvent(int howMany)
{
    while (Wire.available())
    {
        data = Wire.read();
        newSensorData = true;
    }
}

The changes it made included using the Wire library so assign a slave address to the RP2040, adding a function named recieveEvent for recieving the data, and a conditional to void(loop) to pass the data through the Bluetooth connection. Landon used Adrian Torres’ documentation to modify his code to be the master device and send one part of the orientation data from his sensor over to the RP2040.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

// Check I2C device address and correct line below (by default address is 0x29 or 0x28)
//                                   id, address
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire);

const int address = 0x08;

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

  if (!bno.begin()) {
    Serial.print("No BNO055 detected ... Check your wiring or I2C ADDR!");
    while (1);
  }

  delay(1000);
}

void loop() {
  delay(100);
  sensors_event_t orientationData;
  bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
  int data = (int)orientationData.orientation.x; // Convert to int for simplicity

  Wire.beginTransmission(address);
  Wire.write(data);
  Wire.endTransmission();

  Serial.print("Sent data: ");
  Serial.println(data);
}

After both codes were properly modified, we connected the I2C lines and uploaded the code. It didn’t work initially, which worried me since my attempt at networking in a similar way had failed before as well. We asked David Vaughn for his assistance, and he reccomended connecting the GND lines to each other for both devices. After doing this, it immediately started working, although it was only producing gibberish.

While we didn’t capture the gibberish from this attempt, below shows some of the gibberish we got on a later attempt.

Gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggecb`^ZWUTSPNMMLJJJJIIKQ]#*-/03641—+’$ c^]_ceMOR]emw~99.{99uld^ZSH>96654210/16:ERSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSXYZZZZZZZZZZZZ[\abccccbcccgffgijjjjjjjjjjjjjjjjjjiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiv||||||||||||||||{vvvurrrrrrrrrrrrrrrrrrrrrrrrrrrqqrrrrxwvvw~{~9±..~{{{{||}~~~~}}}}}}}}}}}}}}}}}}}}}}}||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

The random characters seemed to be getting messed up over the Bluetooth connection, as we could see by Serial printing that the sensor values were coming through correctly. These characters would change whenever the sensor was rotated and would remain constant when the sensor was still, so we know that this gibberish comes directly from the Bluetooth connection. Since this I2C + Bluetooth connection was what we needed to accomplish, we were done for the week.


Last update: May 30, 2024