How do I use Python to control the IO over REST on the BB-400

FAQs

The BB-400 provides the capability of writing software in many different programming languages. This FAQ explains how the Python programming language can be used to obtain and set the IO line status over REST on the BB-400.

files/pages/support/faqs/bb-400-faqs/logos/python-logo.png

Requirements

Install Visual Studio Code, available here: https://code.visualstudio.com/.
Download and install Python on your PC:  https://www.python.org/downloads/.
Attach your BB-400 to the same network as the development PC using a wired or wireless connection.

If your version of Visual Studio Code does not have Python installed, then please refer to the following link: How do I set up visual studio code with Python.

Obtain the IP address of the BB-400 and the port number of the REST server

The REST server port number can be obtained from the Brainboxes Web admin IO page.
Here you can see that the REST server is running on port 9000, which is the default port number for REST:

files/pages/support/faqs/bb-400-faqs/How-do-I-use-Python-to-control-the-IO-over-REST-on-my-BB400-WebAdminIO-Port-Number.png

Sample Python Program for BB-400

In this example program we are going to obtain and set the status of the output lines using the REST server on the BB-400. Copy the following code into your sample program on VS Code and make sure you update the IP address and port number to match your BB-400.

import requests, json

def getRequest(urlString):
    '''To get the IO line state of the BB-400 using the GET request.

    Arguments:
        urlString {string} -- address of the IO line

    Returns:
        int -- state of the IO line
    '''
    # send a GET request to the URL, and store the response
    response = requests.get(urlString)
    # check if the GET request succeeded
    # if yes - return the response as python objects converted from JSON
    # if no - return the error response code and text
    if response.status_code == requests.codes['ok']:
        return json.loads(response.text)
    else:
        return "Error occurred: " + str(response.status_code) + " " + str(response.text)

def postRequest(urlString, data):
    '''To set the IO line state of the BB-400 using the POST request.

    Arguments:
        urlString {string} -- address of the IO line
        data {int} --  state the IO line needs to be set to  either 0 or 1

    Returns:
        string -- Success message
    '''
    # the REST server takes only only valid JSON as input
    json_data = json.dumps(int(data))
    # send a POST request to the URL along with the JSON data, and store the response
    response = requests.post(urlString, json=json_data)
    # check if the POST request succeeded
    # if yes - return Success message
    # if no - return the error response code and text
    if response.status_code == requests.codes['ok']:
        return "Success"
    else:
        return "Error occurred: "+ str(response.status_code) + " " + str(response.text)

# Entry point of the program
if __name__ == "__main__":
    '''Here we are going to get and set the IO lines state
    '''
    # this is the port number the REST server is running on the BB-400
    rest_server_port_number = 9000

    # appending the IP address of the BB-400 and the REST server port number
    bb400_ip_address = "http://192.168.0.85:" + str(rest_server_port_number)

    # io_lines dictionary gives a label for the IO lines and stores its corresponding IP address.
    io_lines = {
        "light": bb400_ip_address + "/io/outputs/0",
        "modem": bb400_ip_address + "/io/outputs/1",
        "motor": bb400_ip_address + "/io/outputs/2",
        "pump": bb400_ip_address + "/io/outputs/3",
        "sensor": bb400_ip_address + "/io/outputs/4",
        "fan": bb400_ip_address + "/io/outputs/5",
        "printer": bb400_ip_address + "/io/outputs/6",
        "buzzer": bb400_ip_address + "/io/outputs/7",
        "all_outputs": bb400_ip_address + "/io/outputs"
    }

    # state dictionary contains string values for 0 and 1
    state = {
        0: "off",
        1: "on"
    }

    # perform a GET request to get the state of all the output lines
    response = getRequest(io_lines["all_outputs"])

    # print the output of the GET request
    print("Light: " + str(state[response[0]]))
    print("Modem: " + str(state[response[1]]))
    print("Motor: " + str(state[response[2]]))
    print("Pump: " + str(state[response[3]]))
    print("Sensor: " + str(state[response[4]]))
    print("Fan: " + str(state[response[5]]))
    print("Printer: " + str(state[response[6]]))
    print("Buzzer: " + str(state[response[7]]))

    # perform a POST Request to set the IO line state
    # here we are inverting the IO line states of the outputs from 0 to 1 or 1 to 0
    print(postRequest(io_lines["light"], not response[0]))
    print(postRequest(io_lines["modem"], not response[1]))
    print(postRequest(io_lines["motor"], not response[2]))
    print(postRequest(io_lines["pump"], not response[3]))
    print(postRequest(io_lines["sensor"], not response[4]))
    print(postRequest(io_lines["fan"], not response[5]))
    print(postRequest(io_lines["printer"], not response[6]))
    print(postRequest(io_lines["buzzer"], not response[7]))

    # perform a GET request to get the new state and print them
    response = getRequest(io_lines["all_outputs"])

     # print the output of the GET request
    print("Light: " + str(state[response[0]]))
    print("Modem: " + str(state[response[1]]))
    print("Motor: " + str(state[response[2]]))
    print("Pump: " + str(state[response[3]]))
    print("Sensor: " + str(state[response[4]]))
    print("Fan: " + str(state[response[5]]))
    print("Printer: " + str(state[response[6]]))
    print("Buzzer: " + str(state[response[7]]))


This example is based on Python version 3.6 .

If you need to install missing header files, use the following command in the terminal window:

pip3 install requests

To go to the terminal window View > Terminal. The terminal window opens at the bottom of the editor:

image.png

To run the program right-click on the editor and select ‘Run Python File in Terminal’:

image.png

A terminal opens up at the bottom of the editor. The program is executed and the output is displayed in the terminal.

In the output you can see that initially we are getting the current status of the output lines of the BB-400 using the GET command. The output lines are then toggled using the POST command, and finally the new status of the output lines are obtained:

image.png

You can also check the IO lines status on the Web admin page of the BB-400:

files/pages/support/faqs/bb-400-faqs/How-do-I-use-Python-to-control-the-IO-over-REST-on-my-BB400-IO-status.png


FAQs