Week 11, Embedded Networking and Communication
Objectives
- 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
Group Assignment
For the group assignment we explored I2C communication between 2 development boards. We were able to successfully send and recieve data and perform actions on the recieved data. It was illuminating to see how easy it was to deploy simple commuinication protocols.
Data pushed from app showing up on the serial monitor.
Link the group page here
Design, build and connect wired or wireless node(s)
For this exploration I chose to build a Bluetooth connection from my phone to my development board to control the LEDs on my rainbow thrower prototype. In week 10 (Output Devices) I had the development board cycling through different colors by pressing the button on the development board. To give my prototype more flexibility I wanted to cycle through the colors with a command from my phone. Especially since the button on my dev board is hard to reach with a finger.
The prototype from last week that I want to control with BLE.
BLE Scan
I started by testing some code to exercise the BLE radio on the XIAO ESP32C3 and do a scan of nearby BLE devices.
Setup
I built assembled 2 more of my purple development boards before starting the work this week. I used one of these new ones with a XIAO ESP32C3 plugged into it, but no additional sensors or actuators.
Code
I used the XIAO BLE test code that was provided on the Seeed Studio site for the XIAO. On their Getting Started page they outlined some sample programs. I used the BLE scan routine exactly as they posted it included the few line updates for updated onboard firmware.
Testing and Results
The code uproad and ran with no trouble. As I was on campus in our lab it foud 79 devices in a crowded wireless space.
Sample of found BLE devices from the serial monitor
BLE Write
I knew the BLE worked and that I could scan. Now I wanted to see if I could write and send messages from my phone and have them show up on the serial monitor on my XIAO.
Setup
This was the same setup as the previous test with just a XIAO and the dev board with no additional hardware.
Code
I used the XIAO ESP32C3 as Bluetooth server code as provided on the Seeed website.
Testing and Results
I uploaded the code to the XIAO and opened the serial monitor to listen to what messages may come through. Then I had to send a message with my phone. The Seeed website said to use the LightBlue App, but I had used the BLE Scanner app in the past with good success and decided to use it instead. I opened the app and was immediately able to see and pair with the device called "MyESP32" which is what the device was named in the code above.
The XIAO showing up in the device list as "MyESP32"
After I clicked connect, I selected the available service. Then I pressed the "W" button to write a message to the device.
The service and characteristic being advertised by the XIAO.
Then I wrote some strings and sent them to the device. I was able to see them come through instantly.
Sending text strings through the BLE Connect app.
Data pushed from app showing up on the serial monitor.
BLE Write and Light Neopixel
Now that I could read data coming through from the phone to the XIAO, I knew I could react to the data to make it do things on the board. So I decided to have the LED change color through a cycle of predefined colors when any string came across the BLE.
Setup
My development board already has a Neopixel wired to pin 10, so there was no wiring required for this exploration.
Code
I used the code from the last test and added pieces from my week 10 color cycling code. I added the Adafruit Neopixel library, the color definitions, and had the color index variable update every time a non-empty message was recieved over BLE.
Inside the if loop where the length of the incoming string is calculated I added code to set the color of the onboard neopixel to the next color in the list and light the LED.
BLE_Write_Neo.ino | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
Testing and Results
I uploaded the code to the XIAO and opened the serial monitor. Then I opened up the BLE Scanner app and connected to the XIAO. I then wrote random text strings, even just a single letter and sent them to the device. I could see the strings pop up on the serial monitor and the Neopixel dutifully changed colors.
The XIAO board with the serial monitor up in the background. Note that I have sent random text strings and they have showed up on the monitor and printed out the new color. In this image I have gotten as far in the list of color as cyan which is shown on the monitor and on the board.
Bluefruit Connect and UART
Being able to update the colors with a BLE message was a big win. However, it is still cluncky to have to input text to cycle the colors. So I decied to explore using the Adafruit Bluefruit Connect app and UART to see if I could use the buttons in the app to cycle the colors with a click instead of a text.
Playing with the app in the past I knew it had a color picker and buttons built in and I wanted to see if I could use either of them to control my LEDs.
The first step was to see if I could read messages from the app in the serial window.
Setup
I used the same hardware setup for all previous explorations as I just wanted to see the serial output from the XIAO. I obviously already had the app installed on my phone.
Code
I used the UART sample code that was listed under the BLE library inside of my Arduino installation. This has a very similar structure as the code above. To save space I will provide a link here instead of showing the full code.
Testing and Results
Once the code was uploaded I opened the serial monitor inside Arduinon and opened the Adafruit Bluefruit Connect app and connected to the device which was named "MyESP32". Then I clicked through the menus to get to the color picker.
The app flow to get to the color picker.
However, when I used the color picker to send data, the data came through as garbage with some question marks and weird stuff. I tried a few different colors and they were all garbage. If I sent the same color, the received data was the same version of garbage, but garbage none the less.
Garbage strings being received on the serial port.
From the info in the app it is supposed to send bytes of RGB data.
From the documentation in the app:
The Color Picker sends a color's RGB values to Bluefruit LE. This can be used to control the state of RGB LEDs such as Neopixels.
-
Touch the color wheel to choose desired color
-
Press Send to send the chosen color's red, green, and blue values to Bluefruit via UART in the following format: [ ‘!’ ] [ ‘C’ ] [ byte r ] [ byte g ] [ byte b ]
So, It is finding the "C" but the rest of the string is not useable. There must be some weirdness in the data transfer.
Then I backed out of the color picker in the app and clicked into the Control Pad function.
Control Pad part of the app.
When I pushed the buttons on the control pad the text strings were much more normal and predictable, and match the info from the app.
Control Pad data recieved after pressing (from the top of the screen) up, down, left and right buttons in the app.
From the documentation inside the app:
The Control Pad function provides a familiar momentary button interface for common control scenarios. Data is sent on the press and release of each button. Each packet consists of 4 bytes, each representing a char value. The first two chars identify the packet as a button message, the third specifies a button, and the fourth signifies either a press or release. Example:
Button 4 pressed: [ ‘!’ ] [ ‘B’ ] [ ‘4’ ] [ ‘1’ ] Button 4 released: [ ‘!’ ] [ ‘B’ ] [ ‘4’ ] [ ‘0’ ]
From the screenshot of the serial monitor, predictable button press and release events are being sent quickly and properly.
So, while the color picker is the ultimate goal, having a button push to cycle through the table of colors is a great step.
Bluefruit, UART, and Neopixels
Now that I could read the characters from the button pushes I added the code for the Neopixels to increment and decrement my color index variable and change the color of the LED if the up or down buttons in the app were pushed.
Setup
For this iteration I used the dev board that I have connected to my camera. This is the same development board that I had been using, but has a string of 3 Neopixels, 2 of which are high power. The high power LEDs were plugged into the PCB connected to 5V, GND, and D10.
Code
I merged the LED incrementing code I developed to change with a button push with the UART code from above and made a few key changes.
I created 2 new character variable to hold the 3rd and 4th characters of the string coming over the UART serial connection. I did this so I could then do some logic to see if the up (button #5) or down buttons (button #6) on the app were pushed. Then if the buttons were pushed the colorIndex variable was updated.
Since I now had 2 buttons, an up and a down, I now had to add a routine to decrement the color index variable and make sure that it looped back to position 9 if it got down past 0. I used ChatGPT with the prompt "can you give me similar code but to decreseas the colorIndex varaible by one and wrap back to 9?" It responded with the following lines of code:
The explanation given was: "When colorIndex is 0 and you subtract 1, you get -1, which would break the % operator (because it gives negative results with negative numbers in C++). So we add totalColors to ensure it's always positive before modulus."
With this I updated the key callback part of the code that controls the logic of the LEDs as follows:
The full code can be found in the file here.
Testing and Results
I uploaded the code and opened the app again. I connect to the board and navigated to the controller page and tested the up and down buttons. I also had the board hooked up to the computer to monitor the serial output from the board.
The board reacted perfectly to the button pushes and changed the color of the LEDs as expected.
Testing the Bluefruit app and cycling through the colors.
Bluefruit, UART, Neopixels, and the OLED
Finally, since I had the OLED already attached to the prototype, I decided to add the code to that to display the text of the color being shown.
Setup
Same setup as above with the OLED connected to 5V, GND, and the SCL and SDA lines.
Code
I took the code developed from the OLED from week 10 and merged it together with the code from above. There was no additional logic required, just adding the libraries and the commands to display the text when the color index changed.
The full code can be found here.
Testing and Results
I uploaded the code and tested the up and down buttons on the controller. As expected, the OLED now displayed the text of the current color.
Testing the Bluefruit app and cycling through the colors with the OLED also displaying the correct color.
Going Forward
I would like to retread the color picker part of the app and work to get custom RGB colors pushed to the board and plan to explore that during Interface and Application Programming week.