Automated power measurements in functional testing with a Raspberry Pi & Otii

Ensuring the proper functionality of embedded devices is crucial, especially when these devices are integral to applications ranging from home automation to industrial control systems. A key aspect of the development process is testing, which ensures that these devices operate as intended. Given the iterative nature of product development—requiring repetitive functional testing—automation is not just beneficial but essential.

But… What if I told you that it is possible to automate power measurements tests to verify that the device is operating as intended using a Raspberry Pi? Yes, one of the most cost-effective and versatile tools available in the market.

How does it work?

To get started, the following hardware and software resources are required.

Hardware

  • Otii hardware: Otii Arc Pro or Otii Ace Pro
  • Raspberry Pi 4B rev 1.2 8 GB or Raspberry Pi 5 8GB
  • USB SSD disk
  • External DC power supply
  • Device under test (DUT)
  • Jumper wires

Software

  • Otii Automation Toolbox License
  • Raspberry Pi OS (64-bit)

Before start, keep in mind:

  • Otii server will run on a Raspberry Pi 4/5 with less than 8 GB RAM, but it is not recommended.
  • Running the system on an SD card will degrade performance, and the memory card will likely quickly wear out. Use the USB SSD disk instead, as recommended.
  • Otii 3 Server is a 64-bit application. Therefore, the 64-bit version of the Raspberry Pi OS is a must.

Setting up Raspberry Pi

Installing Raspberry Pi OS on the SSD Disk

To set up your Raspberry Pi with an SSD, start by plugging the SSD disk into your computer via USB. Download, install, and run Raspberry Pi Imager. Within the Imager, select your device as Raspberry Pi 4 or 5, choose Raspberry Pi OS (64-bit) as the operating system, and assign the storage to the SSD disk. Once everything is selected, click “NEXT” and in the next window, click “EDIT SETTINGS” under “Use OS customization.

In the GENERAL tab, configure the hostname, username, password, SSID, network password, Wireless LAN country, time zone, and keyboard layout. In the SERVICE tab, enable SSH and select “Use password authentication.” Save your changes, and when prompted, click “YES” to apply the custom settings.

A warning will appear to confirm that you want to proceed, as it will overwrite existing data on the disk. Confirm by clicking “YES” and entering your computer password. The system will then download the OS and write it to the SSD disk, a process that will take a few minutes. Once completed, you will see a message indicating that the disk can be removed.

Finally, connect the SSD to the Raspberry Pi, turn it on, wait a few seconds, and then access it via SSH.

Wiring up

To wire up the system, first ensure you have all the necessary hardware components on hand. Begin by connecting the SSD disk to the bottom USB 3 port of the Raspberry Pi, without powering it up. Next, using the USB cable provided in the Otii hardware box, connect the USB output to the Otii hardware box and the USB Type A output to the top USB 3 port of the Raspberry Pi.

Power the Otii hardware with an external DC power supply, ensuring it meets the required specifications; once powered, the LED on the front of the Otii hardware should light up. Then, supply power to the Raspberry Pi via its USB-C power supply port, ensuring the power parameters are optimal: 5V and at least 3A (5A for Raspberry Pi 5).

Once the system is connected, it should look like this:

Finally, connect the Device Under Test (DUT) to the system, configuring it to work with UART configuration to allow debugging of incoming messages and enabling automation of processes based on these messages.

Access Raspberry Pi using SSH

To access your Raspberry Pi via SSH after powering it up, first ensure you have an SSH client like Putty or Termius installed. If not, download, install, and open your preferred SSH client. In the connection configuration, set the hostname to the one assigned in Raspberry Pi Imager (e.g., raspberry.local) or use the IP address of the Raspberry Pi. To find the IP address, you can use network scanning tools like Fing. Set the port to 22 and the connection type to SSH.

After configuring these settings, follow the specific steps for your SSH client to establish the connection. Once connected, enter the previously assigned username and password (e.g., pi.qoitech). You should now be interacting directly with the Raspberry Pi.

Installing Otii Server

To set up automated testing with the Otii Product Suite, start by enabling the Otii server, which supports scripting in popular languages like Python and Java. First, visit the Qoitech User Management portal and log into your account. If you don’t have an account, create one by clicking “Create an account” and verify it through the associated email.

Once logged in, navigate to the Download page within the portal. Accept the EULA terms by checking the box, then select the Raspberry Pi version of the Otii server to download.

Next, transfer the downloaded .deb file to your Raspberry Pi. You can do this in one of two ways:

  1. Direct Download: If you have access to the Raspberry Pi interface, use the browser on the Raspberry Pi to navigate the User Management portal and download the file directly.

  2. Using SCP: Alternatively, use the scp command to copy the file from your computer to the Raspberry Pi. Run the following command in your terminal, replacing the placeholders with your specific details:

scp <Path to File> <username>@<IP Address of Raspberry Pi>:<Destination Path>

For example:

scp Downloads/otii_server_3.4.3_arm64.deb pi.qoitech@192.168.1.64:/home/pi.qoitech/Downloads

Once the file is transferred, access the Raspberry Pi remotely via SSH, navigate to the directory where the .deb package was copied or downloaded:

cd Downloads

Then, install the Otii server with the following command:

sudo dpkg -i otii_server_3.x.x_arm64.deb

After installation, you can start scalable, low-power functional testing for IoT and embedded systems using the Otii Product Suite.

Running Otii Server

To set up and interact with the Otii server using SSH, you need to establish two SSH connections. One connection will be used to run the Otii server, while the other will be for user management and scripting. Note that the Otii server can also be activated directly from scripting if needed.

  1. First SSH Connection: Run the Otii server by entering the following command. Keep this connection running in the background:
otii_server
  1. Second SSH Connection: In the second connection, enable the user management capability within the command line interface (CLI) by setting up a Python virtual environment and installing the necessary package:
python3 -m venv path/to/venv
source path/to/venv/bin/activate
pip install otii_tcp_client

Once set up, you can manage and script the Otii server using Python or other scripting languages. For detailed instructions, refer to the Automation Toolbox documentation.

Running Python script

To run a Python example on your Raspberry Pi that demonstrates how to log in, list licenses, reserve and return a license, and log out using the Otii TCP Client, follow these steps:

  1. Create a Python File: On your Raspberry Pi, use the nano command to create a new Python file named user_management.py:
nano user_management.py
  1. Paste the Python Code: Copy and paste the Python code below into the user_management.py file. This script connects to the Otii server, logs in, lists available licenses, reserves a license, performs actions, and then returns the license before logging out.
import json
import os
import time
from otii_tcp_client import otii_connection, otii as otii_application

# The default hostname and port of the Otii 3 application
HOSTNAME = '127.0.0.1'
PORT = 1905

CREDENTIALS = './credentials.json'

ABBREVATIONS = {
    "Automation": "AT",
    "Battery": "BT",
    "BatteryValidation": "BVT",
}

def list_and_reserve_licenses():
    '''
    This example shows you how to login,
    list licenses, reserve and return a license,
    and how to logout.
    '''
    if not os.path.isfile(CREDENTIALS):
        raise Exception('You need to create a credentials.json file')

    # Connect to the Otii 3 application
    connection = otii_connection.OtiiConnection(HOSTNAME, PORT)
    connect_response = connection.connect_to_server(try_for_seconds=10)
    if connect_response['type'] == 'error':
        raise Exception(f'Exit! Error code: {connect_response["errorcode"]}, '
                        f'Description: {connect_response["payload"]["message"]}')
    otii = otii_application.Otii(connection)

    # Read the credentials
    with open(CREDENTIALS, encoding='utf-8') as file:
        credentials = json.load(file)

    # Login to the Otii license server
    otii.login(credentials['username'], credentials['password'])

    # List all available licenses
    otii_licenses = otii.get_licenses()
    print('  Id Type         Available Reserved to     Hostname')
    for otii_license in otii_licenses:
        available = "Yes" if otii_license["available"] else "No "
        print(f'{otii_license["id"]:4d} {otii_license["type"]:12} {available}       '
              f'{otii_license["reserved_to"]:15} {otii_license["hostname"]}')
        for addon in otii_license['addons']:
            print(f'     - {addon["id"]}')

    # Reserve a license that includes access to Automation Toolbox
    otii.reserve_license(credentials['license_id'])

    # Do your stuff
    time.sleep(3)

    # Return the license and logout
    otii.return_license(credentials['license_id'])
    otii.logout()

if __name__ == '__main__':
    list_and_reserve_licenses()
  1. Create a Credentials File: In the same directory as your script, create a JSON file named credentials.json. This file should contain your Qoitech User Management account username, password, and the Automation Toolbox license ID. The content of the file should look like this:
{
    "username": "YOUR USERNAME",
    "password": "YOUR PASSWORD",
    "license_id": "YOUR LICENSE ID"
}

Replace “YOUR USERNAME”, “YOUR PASSWORD”, and “YOUR LICENSE ID” with your actual credentials and license ID.

  1. Run the Python Script: Execute the user_management.py script by running the following command in your terminal:
python user_management.py
  1. Review the Output: Upon running the script, the system will display logs listing available licenses and indicating whether the license has been reserved and returned successfully.
Id Type         Available Reserved to     Hostname
4389 Pro          Yes                       None
4723 All          Yes       {your_username}  raspberrypi

Ensure that the Otii 3 Desktop App is closed, no session is logged in, and no license is reserved to run the script successfully.

  1. Explore Further: After completing this example, you can explore more functionalities of the Otii TCP Client by creating custom Python scripts to automate tests during your hardware development cycles. Refer to Qoitech’s GitHub for ready-to-use examples and further details on using TCP clients for Python or Java.

Conclusion

Functional testing is not just a box to be checked—it’s a critical step in ensuring that hardware products perform as intended, focusing on the user interface, APIs, databases, security, and other functionalities. For embedded and IoT devices, this means ensuring sensors read data accurately, actuators respond correctly, and communication protocols work as expected. As well as, the actual power consumption measurements to understand how changes affect device efficiency.

If you want to learn more about energy optimization and automation, check out Qoitech docs or the Qoitech YouTube channel.