Capturing Mindwave Mobile (Neurosky) EEG recordings in JSON format using R

By Elliot Noma

In the previous post I described the BinaryPacket method for retrieving data from the headset. This post describes the R code used to retrieve the data in JSON format. According to the company documentation, this format will be more stable as further changes are made to the hardware and supporting software.

The data received falls into three categories

1. raw eeg data received at a rate of 512 Hz

2. summaries of eeg wave patterns provided by the interface at 1Hz intervals

3. blink event indicators

These three types of messages are interleaved in the stream of JSON messages.

1.  {\”rawEeg\”:19}

2.  {\”eSense\”:{\”attention\”:61,\”meditation\”:56},\”eegPower\”: \”delta\”:75352,\”theta\”:47262,\”lowAlpha\”:3073,\”highAlpha\”:4947,\”lowBeta\”:2618,\”highBeta\”:9062,\”lowGamma\”:4357,\”highGamma\”:2634},\”poorSignalLevel\”:0}

3.  {\”blinkStrength\”:38}

To initiate the connection between the headset and R

1. start the Thinkgear Connector executable – the program is probably already executing in the background
2. turn on the bluetooth device
3. run the code below
4. pair the headset with the bluetooth receiver using code 0000
5. once the socket responds, write the line to set the output to JSON format
6. read the socket.

The function to initiate the connection follows:
connectToMindwave <- function()
sha1 <- digest(“mindwave”, “sha1”)

con <- socketConnection(host = “localhost”, port = 13854, blocking = FALSE, open=”w+”)
writeLines(paste(‘{“appName”: “Brainwave”, “appKey”: “‘,sha1,'”}’, sep=”), con)

# wait until the socket responds

eeg0 <- NULL
startTime <- Sys.time()
while( length(eeg0) == 0) {
eeg0 <- readLines(con)

# write message to socket to request the feed in Json format

writeLines(‘{“enableRawOutput”: true, “format”: “Json”}’, con)


The output can be received and stored using the call to the function and the following code:
con <- connectToMindwave()

# set a timer and read the socket until the timer runs out

eeg <- NULL
startTime <- Sys.time()
while(difftime(Sys.time(), startTime, unit=”mins”) < 1) {
eeg <- c(eeg, readLines(con))

To extract the levels from the JSON format in each line, one can use the fromJSON() function within the rjson package.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: