How do I use Python to control the IO over Unix Domain Socket on the BB-400?

FAQs

This FAQ will explain how Python can be used to retrieve/modify the BB-400 IO status over Unix Domain Socket.

Requirements

Attach your BB-400 to the same network as the development PC using a wired or wireless connection.

Sample Python Program for BB-400

In this example program we are going to obtain and set the status of the output lines using Unix Domain Server on the BB-400.

The Unix Domain Socket file used to control the IO is located at: « /var/run/bb-io.sock ».
The script sends GET and POST requests using the request_unixsocket module.
Firstly, log into your BB-400 using PuTTY or a similar terminal emulator.

This example is based on Python version 3.72.

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

  • sudo pip3 import requests_unixsocket

Create a new python file and copy the following code into it:

import json
import requests_unixsocket

socket_name = "http+unix://%2Fvar%2Frun%2Fbb-io.sock"
session = requests_unixsocket.Session()

def getRequest(led):
    response = requests_unixsocket.get(socket_name+led)
    if response:
        return json.loads(response.text)
    else:
        return "Error occurred: " + str(response.status_code) + " " + str(response.text)

def postRequest(led, data):
    postresponse = requests_unixsocket.post(socket_name+led, data=str(data))
    if postresponse:
        return postresponse
    else:
        return "Error occurred: " + str(postresponse.status_code) + " " + str(postresponse.text)

if __name__ == "__main__":

    io_lines = {
        "DIO0": "/io/outputs/0",
        "DIO1":  "/io/outputs/1",
        "DIO2": "/io/outputs/2",
        "DIO3": "/io/outputs/3",
        "DIO4": "/io/outputs/4",
        "DIO5": "/io/outputs/5",
        "DIO6":  "/io/outputs/6",
        "DIO7": "/io/outputs/7",
        "all_outputs":  "/io/outputs"
    }

    # GET status of IO lines
    print("Current IO status is: ")
    response = getRequest(io_lines["all_outputs"])
    print(response)

    getresponseDIO0 = getRequest(io_lines["DIO0"])
    getresponseDIO1 = getRequest(io_lines["DIO1"])
    getresponseDIO2 = getRequest(io_lines["DIO2"])
    getresponseDIO3 = getRequest(io_lines["DIO3"])
    getresponseDIO4 = getRequest(io_lines["DIO4"])
    getresponseDIO5 = getRequest(io_lines["DIO5"])
    getresponseDIO6 = getRequest(io_lines["DIO6"])
    getresponseDIO7 = getRequest(io_lines["DIO7"])

    print("nSending POST request via Unix Domain Socket to change IO status to 0 or 1")
    # #POST Request to set the IO line states to 0 and 1
    postresponseDIO0 = postRequest(io_lines["DIO0"], 0)
    postresponseDIO1 = postRequest(io_lines["DIO1"], 1)
    postresponseDIO2 = postRequest(io_lines["DIO2"], 0)
    postresponseDIO3 = postRequest(io_lines["DIO3"], 1)
    postresponseDIO4 = postRequest(io_lines["DIO4"], 1)
    postresponseDIO5 = postRequest(io_lines["DIO5"], 0)
    postresponseDIO6 = postRequest(io_lines["DIO6"], 0)
    postresponseDIO7 = postRequest(io_lines["DIO7"], 1)

    print("nNew IO status is as follows: ")
    # Print new IO status
    response = getRequest(io_lines["all_outputs"])
    print(response)   

To run the script on Linux:

  • sudo python3 NAMEOFPROGRAM.py

The output looks as follows:

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

FAQs