Skip to content

Built a real-time embedded system on the BeagleY-AI to monitor and display local weather conditions. Developed the system in C on a Debian Linux VM, integrating hardware components such as a temperature sensor, NeoPixel LED strip, LCD screen, and rotary encoder. Implemented multithreaded I/O, GPIO control, and shared memory communication.

Notifications You must be signed in to change notification settings

danielvnguyen/BeagleYAI-Weather-Monitoring-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BeagleY-AI Weather Monitoring System

Steps to build project

  1. run sudo apt install sqlite3 libsqlite3-dev (optional, but recommended for much faster builds)
  2. run following on host for necessary libraries:

sudo apt-get install libcurl4-openssl-dev sudo apt-get install libcjson-dev # cJSON

sudo apt-get update sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

sudo dpkg --add-architecture arm64 sudo apt-get update sudo apt-get install libcurl4-openssl-dev:arm64

sudo dpkg --add-architecture arm64 sudo apt-get update sudo apt-get install libcjson-dev:arm64

  1. Run these commands to build for targeted compiler from main proj folder:

rm -rf build mkdir build && cd build cmake -DTARGET_ARCH=arm64 .. make

rm -rf build mkdir build && cd build cmake -DTARGET_ARCH=x86_64 .. make

Notes on testing

GoogleTest Primer: http://google.github.io/googletest/quickstart-cmake.html

Tests use Googletest framework which runs using C++, but can also be used for testing C code. One caveat of using GTest is that test source files must be .cpp files in order to build the tests.

To add tests, simply create a new source file in the tests/ directory. Headers should be wrapped in "extern" (see ExampleTest.cpp).

Assorted tips

  • If you find your VM is crashing a lot while building, it might be because the build process is trying to use more cores than your VM can handle. Try limiting concurrency:

    • In VS Code:
      • Go to User Settings (Cmd + Shift + p, search 'user settings')
      • Add: "cmake.buildArgs": ["-j2"] # 2 means use two cores

Steps to Add LCD to an Existing Project

  1. Load the SPI Overlay on the board (just do once) a. Edit config file: (byai)$ sudo nano /boot/firmware/extlinux/extlinux.conf b. Edit the last section to make it say:
     label microSD (default)
         kernel /Image
         append console=ttyS2,115200n8 root=/dev/mmcblk1p3 ro rootfstype=ext4 resume=/dev/mmcblk1p2 rootwait net.ifnames=0 quiet
         fdtdir /
         fdt /ti/k3-am67a-beagley-ai.dtb
         fdtoverlays /overlays/k3-am67a-beagley-ai-spidev0.dtbo
         initrd /initrd.img
    
    (If you are also enabling PWM, make the fdtoverlays line be a space-separated list of .dtbo files.) c. Reboot. d. You should now have two files: /dev/spidev0.0 and /dev/spidev0.1.
  2. Copy lcd/ and lgpio/ folders into project.
  3. In root CMakeLists.txt, add:
add_subdirectory(lgpio)
add_subdirectory(lcd)
  1. Into your app/CMakeLists.txt, add:
target_link_libraries(hello_world LINK_PRIVATE lcd)
target_link_libraries(hello_world LINK_PRIVATE lgpio)

(Changing hello_world to your project's name) 5. Create a module to interact with the screen (base on draw_stuff.h/.c, but name it better!) 6. Add calls to init and cleanup to your main(). 7. Trigger a CMake rebuild so it will compile your new files.

Running on Target

NEW FOR LCD PROJECT

  • Install the lgpio library on the target (byai)$ sudo apt install liblgpio-dev
  • At each boot you'll need to either: a. Change the SPI to be usable by anyone: sudo chmod a+rw /dev/spidev0.* b. Run the program with root access.

Structure

  • hal/: Contains all low-level hardware abstraction layer (HAL) modules
  • app/: Contains all application-specific code. Broken into modules and a main file
  • build/: Generated by CMake; stores all temporary build files (may be deleted to clean)
  • lcd/: Library for LCD use from https://www.waveshare.com/
  • lgpio/: Library used by LCD code, from https://github.com/joan2937/lg/archive/master.zip (No need to install the library on the host)
  .
  ├── app
  │   ├── include
  │   │   └── badmath.h
  │   ├── src
  │   │   ├── badmath.c
  │   │   └── main.c
  │   └── CMakeLists.txt           # Sub CMake file, just for app/
  ├── hal
  │   ├── include
  │   │   └── hal
  │   │       └── button.h
  │   ├── src
  │   │   └── button.c
  │   └── CMakeLists.txt           # Sub CMake file, just for hal/
  ├── lcd                          # Library for LCD access
  ├── lgpio                        # Library to support LCD
  ├── persistence                  # Persistence Layer (.db) files for SQLite
  ├── tests                        # Tests for all modules
  │   ├── <ExampleTest1.cpp>       # Tests should be .cpp files
  │   ├── <ExampleTest2.cpp>
  │   └── CMakeLists.txt
  ├── CMakeLists.txt               # Main CMake file for the project
  └── README.md

Note: This application is just to help you get started! It also has a bug in its computation (just for fun!)

Usage

  • Install CMake: sudo apt update and sudo apt install cmake
  • When you first open the project, click the "Build" button in the status bar for CMake to generate the build\ folder and recreate the makefiles.
    • When you edit and save a CMakeLists.txt file, VS Code will automatically update this folder.
  • When you add a new file (.h or .c) to the project, you'll need to rerun CMake's build (Either click "Build" or resave /CMakeLists.txt to trigger VS Code re-running CMake)
  • Cross-compile using VS Code's CMake addon:
    • The "kit" defines which compilers and tools will be run.
    • Change the kit via the menu: Help > Show All Commands, type "CMake: Select a kit".
      • Kit "GCC 10.2.1 arm-linux-gnueabi" builds for target.
      • Kit "Unspecified" builds for host (using default gcc).
    • Most CMake options for the project can be found in VS Code's CMake view (very left-hand side).
  • Build the project using Ctrl+Shift+B, or by the menu: Terminal > Run Build Task...
    • If you try to build but get an error about "build is not a directory", the re-run CMake's build as mentioned above.

Address Sanitizer

  • The address sanitizer built into gcc/clang is very good at catching memory access errors.
  • Enable it by uncomment the fsanitize=address lines in the root CMakeFile.txt.
  • For this to run on the BeagleBone, you must run: sudo apt install libasan6
    • Without this installed, you'll get an error: "error while loading shared libraries: libasan.so.6: cannot open shared object file: No such file or directory"

Suggested addons

  • "CMake Tools" automatically suggested when you open a CMakeLists.txt file
  • "Output Colourizer" by IBM --> Adds colour to the OUTPUT panel in VS Code; useful for seeing CMake messages

Other Suggestions

  • If you are trying to build with 3rd party libraries, you may want to consider the build setup suggested at the following link. Specificall, see the part on extracting the BB image to a folder, and then using chroot to run commands like apt on that image, which allows you to get libraries for the target on the build system. https://takeofftechnical.com/x-compile-cpp-bbb/

Manually Running CMake

To manually run CMake from the command line use:

  # Regenerate build/ folder and makefiles:
  rm -rf build/         # Wipes temporary build folder
  cmake -S . -B build   # Generate makefiles in build\

  # Build (compile & link) the project
  cmake --build build

Finer Points

  • When using the header files in HAL, you'll need to: #include "hal/myfile.h This extra "hal/..." helps distinguish the low-level access from the higher-level code.
  • One only need to run the CMake build the first time the project loads, and each time the .h and .c file names change, or new ones are added, or ones are removed. This regenerates the build/Makefile. Otherwise, just run a normal build (ctrl+shift+B)
  • If desired, one could provide an alternative implementation for the HAL modules that provides a software simulation of the hardware! This could be a useful idea if you have some complex hardware, or limited access to some hardware.

About

Built a real-time embedded system on the BeagleY-AI to monitor and display local weather conditions. Developed the system in C on a Debian Linux VM, integrating hardware components such as a temperature sensor, NeoPixel LED strip, LCD screen, and rotary encoder. Implemented multithreaded I/O, GPIO control, and shared memory communication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published