FUN WITH LINUX

HackADay: Controlling computers and stuff with the mind

16 September 2016

I am very slothful. I let computers do my work. That’s why I became a sysadmin. In this article I am going to describe how I lifted up my lazyness to the next level by triggering a command with my mind to install a new virtual machine with: MariaDB, Nginx and Wordpress.

Intro

When I first read about the human-to-human-brain-interface I was fascinated. It’s like a science-fiction-x-men-story: controlling another human with the mind. But this is a little bit creepy too. Controlling a computer with the mind instead sounds for me more comfortable. In that way I could do all my work just by thinking of it while I am lying in my bed. That’s awesome.

Hardware

Mindwave Equipment

Headset

I did a little research and it seems the best option for an affordable EEG-headset would be the OpenBCI-Project. For my proof of concept it is still too expensive, that’s why I took the Neurosky Mindwave Headset. It’s has just a few electrodes but it is cheap and might be good enough for my experiment. I ordered a Mindwave-Headset and not a Mindwave-Mobile-headset. The Mindwave-headset ships with a USB-dongle.

Neurosky-Devices have a nice feature built-in: eSense. eSense calculates the “attention”(are you focused?) and “meditation”(do you have many thoughts?) of a person. I will use this feature for triggering a command-execution. I could also use the EEG-Data(16bin wave-format), but for this proof-of-concept I am fine with the eSense-meters.

Microcontroller

I have an old Raspberry Pi (Model B) and I installed Debian Jessie on it. A LED on a Gpio-Pin will indicate if I am concentrated or not.

Software

Reading out EEG-Data

I wrote a ruby gem for interfacing the Mindwave Headset. It simply reads out all the data via serial line and parses it. There are several callback methods to overwrite. In the following example I will override a method to read out the “attention”-value:

#!/usr/bin/env ruby

require 'mindwave'

class EEG < Mindwave::Headset
    # override Attention-Callback-Method
    def attentionCall(attention)
            str = eSenseStr(attention)
            puts "this is an attention #{attention} #{str}\n"
    end
end

# create a new instance
mw = EEG.new
# mw.log.level = Logger::DEBUG

# if we hit ctrl+c then just stop the run()-method
Signal.trap("INT") do
    mw.stop
end

# Create a new Thread
thread = Thread.new { mw.run }
# ..and run it
thread.join

mw.close

Manipulating the Raspberry-GPIO-Pins

I found a nice ruby gem for manipulating the GPIO-Pins of my raspberry at rubygems.org. This code will switch the GPIO_Pin 22 on every key-stroke:

#!/usr/bin/env ruby

# gem install rpi_gpio
require 'rpi_gpio'
# gem install io-console
require 'io/console'

RPi::GPIO.set_numbering :bcm
RPi::GPIO.clean_up 22
RPi::GPIO.setup 22, :as => :output

GPIO_NUM=22

RPi::GPIO.set_high GPIO_NUM

sohigh = true

input = 'c'

Signal.trap("INT") do
	puts "SiGINT Cleaning up.."
	RPi::GPIO.clean_up GPIO_NUM
	exit 1
end


while input != "q"
input = STDIN.getch

	if sohigh
		RPi::GPIO.set_low GPIO_NUM
	else
		RPi::GPIO.set_high GPIO_NUM
	end

	sohigh = !sohigh
end
	
puts "Cleaning up.."
RPi::GPIO.clean_up GPIO_NUM

Controlling a LED

So let’s put all parts together. Here is some code which toggles a LED if the attention value is bigger than 79:

#!/usr/bin/env ruby

require 'mindwave'

require 'rpi_gpio'

RPi::GPIO.set_numbering :bcm
RPi::GPIO.clean_up 22
RPi::GPIO.setup 22, :as => :output

RPi::GPIO.set_low 22

class EEG < Mindwave::Headset
	@ishigh = false
	attr_accessor :update


	def meditationCall(meditation)
		str = eSenseStr(meditation)
        	puts "Meditation #{meditation} #{str}\n"
	end

	# override Attention-Callback-Method
	def attentionCall(attention)
        	str = eSenseStr(attention)
        	puts "-> Attention #{attention} #{str}\n"

		if attention > 79 and @update == true
			puts "FIRE IT UP"
			if @ishigh
				RPi::GPIO.set_low 22
				@ishigh = false
			else
				RPi::GPIO.set_high 22
				
				@ishigh = true
				stop
			end
			
			@update = false
			
		end
	end
end

# create a new instance
mw = EEG.new
mw.update = true

# mw.log.level = Logger::INFO

# if we hit ctrl+c then just stop the run()-method
Signal.trap("INT") do
	mw.stop
end

# Create a new Thread
thread = Thread.new { mw.run }
# ..and run it

puts "Starting..."
thread.join

puts "Cleaning up.."
mw.close
RPi::GPIO.clean_up 22

Automization

Automization is a very important part of our work at Toscom. I believe that sysadmins should automize as much as possible. Not only to be more efficient but also for quality management. So we do not configure servers manually but program the configuration and reuse this code as often as possible. This makes the next step very easy. I just install the configurations-management-system Ansible on the raspberry pi, download some (wordpress related) ansible-roles from ansible-galaxyand call ansible-playbook within my script. Since Version 2 Ansible also includes modules for controlling Amazon AWS-Services. So I will create my virtual machine in the Amazon-Cloud. Here is my playbook.yml:

- hosts: localhost
  connection: local
  gather_facts: False

  tasks:
    - name: Provision Wordpress Instance
      ec2:
          aws_access_key: "MY-ACCESS-KEY"
          aws_secret_key: "MY-SUPER-SECRET-ACCESS-KEY"
          key_name: pi_key
          instance_type: t2.micro
          image: ami-ed82e39e
          wait: true
          wait_timeout: 500
          assign_public_ip: yes
          vpc_subnet_id: subnet-de3b5da8
          region: eu-west-1 
          group_id: sg-75d3c412
      register: ec2
    - name: Add new instance to host group
      add_host: hostname= groupname=launched ansible_user=ubuntu
      with_items: ''
    - name: Wait for SSH to come up
      wait_for: host= port=22 delay=60 timeout=320 state=started
      with_items: ''

- name: install wordpress
  hosts: launched
  become: True
  gather_facts: True
  vars:
    wp_mysql_host: 'localhost'
    session_post_max_size: '128M'
    session_upload_max_filesize: '128M'
    session_max_input_time: '70'
    session_max_execution_time: '90'
  roles:
    - valentinzberea.wordpress

I use the following roles (from ansible-galaxy):

  • mats116.nginx
  • valentinzberea.wordpress
  • mats116.mariadb-server
  • valentinzberea.hhvm

Proof of Concept

In the next video clip, I recorded a proof of concept. First I got a connection with my headset (the light turns blue), then it took me a while to be focused. As soon as I have a focus level of 80, my program will turn on a LED and call ansible-playbook. In the clip we will see that in my Amazon AWS-Console a new virtual machine will start and install Wordpress, MariaDB and Nginx. At the end of the clip, I will copy the IP-address of the new host and connect to the Wordpress-Page on it.

I did not use the Wave-Stream of the EEG-Data to detect the thoughts but only read out the attention-level of my mind. I also exited the script as soon as it fired up a command, otherwise I would have multiple command-executions on “long” thoughts aso.

Problems

  1. I cheated. I did not use the wave-data at all. I just used the attention-level I got from the Mindwave-Headset.
  2. This is difficult to use. How does one deliberately hold a focus with his mind? Some practice is needed…
  3. What if someone focuses longer? A timer is needed then, otherwise it toggles many times in a row

Conclusion

I triggered a command with my mind which created a virtual machine (Amazon EC2-Instance), installed MariaDB, Nginx and Wordpress. That is one of my daily tasks and I was able to make it just by concentrating. If I combine this project with my power-outlets, I could even make tea with my mind. It’s not perfect. This technique is not very precise (neither is this headset). Even though I am very happy with the result. Maybe I will go deeper and compare signatures of EEG data to detect different states of mind.

[ Linux  Programming  HackADay  Raspberry  Ruby  Hardware  Toscom  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti