ERROR:websocket:error from callback



  • i cant read the event messages from the evok client. All i get is always that error message

    ERROR:websocket:error from callback <function on_message at 0xb67882b0>: string indices must be integers

    import websocket
    import json
    import logging
    #import time
    
    logging.basicConfig()
    # Replace this with your Neuron IP Address
    url = "ws://127.0.0.1:8080/ws"
    
    def on_message(ws, message):
            obj = json.loads(message)
            for single_obj in obj:
                    dev = single_obj['dev']
                    circuit = single_obj['circuit']
                    value = single_obj['value']
                    print "Value: " + str(value) + " Device: " + str(dev) + " Circuit: " + str(circuit)
            print message
    def on_error(ws, error):
            print error
    
    def on_close(ws):
            print "Connection closed"
    
    def on_open(ws):
    # Turn on filtering
            #ws.send('{"cmd":"filter", "devices":["ai","input",output]}')
            #ws.send(json.dumps({"cmd":"filter", "devices":["ai", "output"]}))
    # Turn on DO 1.01
            ws.send(json.dumps({"cmd":"set", "dev":"relay", "circuit": "1", "value":0}))
            ws.send(json.dumps({"cmd":"set", "dev":"relay", "circuit": "2", "value":0}))
    # Query for complete status
            ws.send(json.dumps({"cmd":"all"}))
    
    #receiving messages
    ws = websocket.WebSocketApp(url, on_open = on_open, on_message = on_message, on_error = on_error, on_close = on_close)
    ws.run_forever()
    
    

    The "cmd" "all" works fine at the first start

    Value: 0 Device: input Circuit: 11
    Value: 0 Device: input Circuit: 10
    Value: 0 Device: input Circuit: 12
    Value: 0 Device: input Circuit: 1
    Value: 0 Device: input Circuit: 3
    Value: 0 Device: input Circuit: 2
    Value: 0 Device: input Circuit: 5
    Value: 0 Device: input Circuit: 4
    Value: 0 Device: input Circuit: 7
    Value: 0 Device: input Circuit: 6
    Value: 0 Device: input Circuit: 9
    Value: 0 Device: input Circuit: 8
    Value: 0 Device: relay Circuit: 1
    Value: 0 Device: relay Circuit: 3
    Value: 0 Device: relay Circuit: 2
    Value: 1 Device: relay Circuit: 5
    Value: 0 Device: relay Circuit: 4
    Value: 1 Device: relay Circuit: 7
    Value: 1 Device: relay Circuit: 6
    Value: 0 Device: relay Circuit: 8
    Value: 0.000695620415538 Device: ai Circuit: 1
    Value: 0.000956478071364 Device: ai Circuit: 2

    Can someone help me please ?


  • administrators

    Does the web interface function correctly? It is based on the same websocket listening process and so it should exhibit the same behavior.

    I've tested your code (copy-paste into a python file called wstest.py) on the latest UniPi opensource distribution (essentially just debian with kernel drivers, which only improve UART functionality as far as EVOK is concerned) and it functions as expected, with the following output:

    Value: 0 Device: input Circuit: 1_01
    Value: 0 Device: input Circuit: 1_02
    Value: 0 Device: input Circuit: 1_03
    Value: 0 Device: input Circuit: 1_04
    Value: 0 Device: relay Circuit: 1_01
    Value: 1 Device: relay Circuit: 1_02
    Value: 1 Device: relay Circuit: 1_03
    Value: 0 Device: relay Circuit: 1_04
    Value: 0.00772160933422 Device: ai Circuit: 1_01
    Value: 2.29423873804 Device: ao Circuit: 1_01
    [{"counter_mode": "Enabled", "counter_modes": ["Enabled", "Disabled"], "glob_dev_id": 1, "dev": "input", "ds_modes": ["Simple", "Inverted", "Toggle"], "modes": ["Simple", "DirectSwitch"], "ds_mode": "Simple", "debounce": 50, "counter": 0, "value": 0, "mode": "DirectSwitch", "circuit": "1_01"}, {"counter_modes": ["Enabled", "Disabled"], "glob_dev_id": 1, "modes": ["Simple", "DirectSwitch"], "value": 0, "circuit": "1_02", "debounce": 50, "counter": 0, "counter_mode": "Enabled", "dev": "input", "mode": "Simple"}, {"counter_modes": ["Enabled", "Disabled"], "glob_dev_id": 1, "modes": ["Simple", "DirectSwitch"], "value": 0, "circuit": "1_03", "debounce": 50, "counter": 0, "counter_mode": "Enabled", "dev": "input", "mode": "Simple"}, {"counter_modes": ["Enabled", "Disabled"], "glob_dev_id": 1, "modes": ["Simple", "DirectSwitch"], "value": 0, "circuit": "1_04", "debounce": 50, "counter": 0, "counter_mode": "Enabled", "dev": "input", "mode": "Simple"}, {"glob_dev_id": 1, "modes": ["Simple", "PWM"], "value": 0, "circuit": "1_01", "pending": false, "relay_type": "digital", "dev": "relay", "mode": "Simple"}, {"glob_dev_id": 1, "modes": ["Simple", "PWM"], "value": 1, "circuit": "1_02", "pending": false, "relay_type": "digital", "dev": "relay", "mode": "Simple"}, {"glob_dev_id": 1, "modes": ["Simple", "PWM"], "value": 1, "circuit": "1_03", "pending": false, "relay_type": "digital", "dev": "relay", "mode": "Simple"}, {"glob_dev_id": 1, "modes": ["Simple", "PWM"], "value": 0, "circuit": "1_04", "pending": false, "relay_type": "digital", "dev": "relay", "mode": "Simple"}, {"glob_dev_id": 1, "unit": "V", "value": 0.007721609334224814, "circuit": "1_01", "range_modes": ["10.0"], "modes": ["Voltage", "Current"], "range": "10.0", "dev": "ai", "mode": "Voltage"}, {"modes": ["Voltage", "Current", "Resistance"], "value": 2.2942387380392786, "glob_dev_id": 1, "dev": "ao", "circuit": "1_01", "unit": "V", "mode": "Voltage"}]
    Value: 0 Device: wd Circuit: 1_01
    [{"circuit": "1_01", "value": 0, "glob_dev_id": 1, "dev": "wd", "timeout": 6000, "was_wd_reset": 0, "nv_save": 0}]
    Value: 0 Device: wd Circuit: 1_01
    [{"circuit": "1_01", "value": 0, "glob_dev_id": 1, "dev": "wd", "timeout": 6000, "was_wd_reset": 0, "nv_save": 0}]
    Value: 0 Device: wd Circuit: 1_01
    [{"circuit": "1_01", "value": 0, "glob_dev_id": 1, "dev": "wd", "timeout": 6000, "was_wd_reset": 0, "nv_save": 0}]
    Value: 0.00511440622282 Device: ai Circuit: 1_01
    Value: 0 Device: wd Circuit: 1_01
    [{"glob_dev_id": 1, "unit": "V", "value": 0.005114406222816542, "circuit": "1_01", "range_modes": ["10.0"], "modes": ["Voltage", "Current"], "range": "10.0", "dev": "ai", "mode": "Voltage"}, {"circuit": "1_01", "value": 0, "glob_dev_id": 1, "dev": "wd", "timeout": 6000, "was_wd_reset": 0, "nv_save": 0}]
    Value: 0.00772160933422 Device: ai Circuit: 1_01
    [{"glob_dev_id": 1, "unit": "V", "value": 0.007721609334224814, "circuit": "1_01", "range_modes": ["10.0"], "modes": ["Voltage", "Current"], "range": "10.0", "dev": "ai", "mode": "Voltage"}]
    Value: 0.00511440622282 Device: ai Circuit: 1_01
    Value: 0 Device: wd Circuit: 1_01
    [{"glob_dev_id": 1, "unit": "V", "value": 0.005114406222816542, "circuit": "1_01", "range_modes": ["10.0"], "modes": ["Voltage", "Current"], "range": "10.0", "dev": "ai", "mode": "Voltage"}, {"circuit": "1_01", "value": 0, "glob_dev_id": 1, "dev": "wd", "timeout": 6000, "was_wd_reset": 0, "nv_save": 0}]
    

    It would be helpful if you could post the output of "print obj" and "print message", inserted after the first line of the on_message function, as well as which distribution of EVOK you are using and which version of python.

    e: Added the output of the "all" which I've omitted at first.