This code provides an example of how to stream OpenBCI Ganglion data through the lab streaming layer using the NodeJS SDK.
Follow the steps in this README to start streaming. The code is ready to run as-is, but can be modified and extended to customize how you are sending your data. This is designed to be used with the OpenBCI Ganglion (for Ganglion support, see the Ganglion Node SDK).
-
pip install pyzmq
-
pip install pylsl
First, install Python dependencies:
python setup.py install
Next, install NodeJS dependencies:
npm install
Note: depending on your computer settings, you may need to run as administrator or with sudo
.
npm start
For running just the node, for example if you were running the python in a separate ide and debugging, it's useful.
npm run start-node
Note: depending on your computer settings, you may need to run as administrator or with sudo
.
If you would like to use lab streaming layer in a custom OpenBCI NodeJS application, you must include an instance of the OpenBCI NodeJS library and an instance of a Python interface. A basic example is shown below:
index.js
const Ganglion = require('@openbci/ganglion').Ganglion;
var portPub = 'tcp://127.0.0.1:3004';
var zmq = require('zmq-prebuilt');
var socket = zmq.socket('pair');
let ganglion = new Ganglion();
socket.bind(portPub)
ganglion.once('ganglionFound', (peripheral) => {
ganglion.searchStop();
ganglion.on('sample', (sample) => {
socket.send(JSON.stringify({message: sample}))
});
ganglion.once('ready', () => {
ganglion.streamStart();
});
ganglion.connect(peripheral);
});
// ZMQ
/* Insert additional exit handlers and cleanup below*/
handoff.py
import json
import zmq
from pylsl import StreamInfo, StreamOutlet
# Create ZMQ socket
context = zmq.Context()
_socket = context.socket(zmq.PAIR)
_socket.connect("tcp://localhost:3004")
# Create a new labstreaminglayer outlet
numChans = 4;
sampleRate = 256;
info = StreamInfo('OpenBCI_EEG', 'EEG', numChans, sampleRate, 'float32', 'openbci_12345')
outlet = StreamOutlet(info)
# Stream incoming data to LSL
while True:
msg = _socket.recv()
try:
dicty = json.loads(msg)
message = dicty.get('message')
data = message.get('channelDataCounts')
timeStamp = message.get('timeStamp')
outlet.push_sample(data,timeStamp)
except BaseException as e:
print(e)
Please PR if you have code to contribute!