Reply
Posts: 27
Registered: ‎11-07-2015

Big red button on Thunderboard Sense and Samsung Artik 5

[ Edited ]

As a weekend project, I decided to select an implementation of a big red button based on Thunderboard Sense and Samsung Artik 5. Why a big red button? Because everybody likes red buttons. About it even make films (The Box, 2009).

 

Sorry for any possible mistakes - I am not a native English speaker/writer. By the way this article there is in russian language.

 

What is needed for the project

 

  1. Big red button - for this all was conceived. The button should to be normally open
  2. Thunderboard Sense Kit
  3. Samsung Artik 5
  4. Seeed Studio Arduino Base Shield
  5. Seeed Studio Grove Buzzer

 

 

Setup Thunderboard Sense

 

Fortunately, we do not need to change anything in the code of the Thunderboard Sense, otherwise it would become a headache - Bluetooth SDK to this device requires an IAR ARM 7.80 compiler, that for many would be a big problem.

 

As an actuator will act SW1 button - on this button will be tied up all logic of own project.

 

 

Default firmware do not need any changes because it have following logic:

  1. Board sleep in inactive state and can't accept incoming connections. For change it mode to connectable mode need to push SW1.
  2. After push on SW1 green led start blink there is a possibility for connect to it.
  3. When disconnect is happened Thunderboard Sense again going to sleep.

Thunderboard Sense provides various BLE services and characteristics (full list available in Android App repository). For us interested only one characteristic - CHARACTERISTIC_PUSH_BUTTONS, which have UUID fcb89c40-c601-59f3-7dc3-5ece444a401b and constist from one value uint8_t, with following states:

  1. Zero (=0) if no buttons was pressed;
  2. One (=1) if SW1 was pressed;
  3. Two (=2) if SW2 was pressed;
  4. Three (=3) if was pressed both SW1 and SW2;

It is noteworthy that this characteristic has only read property which leads us to the solution of a periodic value reading.

 

Setup Artik 5

If it a first-time work with Artik 5 then you need to basic setup according to Quick Start Guide. For start developing you should upgrade to latest firmware and setup internet connection.

 

Pre-installed packet manager for OS Fedora on Artik is dnf so all following instructions will be for it.

 

First install all software needed for Bluetooth:

 

dnf install bluez bluez-libs bluez-libs-devel

Besides install Git:

dnf install git

Futher install Node.JS and npm:

dnf install nodejs npm

After this we need to install main module for work with BLE - noble - this module allows to serve BLE connections in case when controller have to do a central role.(for inverse situation there is the bleno module):

npm install noble

Now we ready for coding!

 

Pairing

 

For pairing devices we need to use the interactive util bluetoothctl. First start it:

 

bluetoothctl

After starting we need to switch on a pairing possibility:

 

pairable on

Then we should activate Thunderboard sense by pressing on SW1 and start scanning:

 

scan on

Target device we can determine by friendly name or by address.

 

 

When the needed device is finding we should stop the scanning:

scan off

The most important for us - remember the address of own Thunderboard Sense and execute following commands:

pair 00:0B:57:36:71:82
trust 00:0B:57:36:71:82
connect 00:0B:57:36:71:82

At this stage we get message "Connection successful" and now we can request information about connected device:

info 00:0B:57:36:71:82

Output will be similar to my:

 

 

Now input:

exit

and ... we ready to write code on Node.JS!

 

Work with Bluetooth on Artik 5

 

noble allows develop code for central device. So we have to implement following logic for work with Thunderboard Sense:

  1. Scanning for finding connectable Thunderboard Sense.
  2. Connect to device with a known address.
  3. Gettings lists of services and characteristics.
  4. Read the buttons characteristic's value.

If you need only working code - go to https://bitbucket.org/sithings/app_thunderboardtoartik

 

For start scanning we must make sure that Bluetooth controller is power up and only after that we can start scanning:

/* Event of the state change of the BLE controller on board */
noble.on('stateChange', function(state) {
	if (state === "poweredOn") {
		/*
		 * If BLE normally started start scanning with duplicates with any UUIDs services
		 */
		console.log("\x1b[36m", "\nBLE is poweredOn. Start scanning", "\x1b[0m");
		noble.startScanning([], false);
	} else {
		console.log("\nStopScanning. Status is %s", state);
		noble.stopScanning();
	}
});

After start scanning we must push SW1 on the Thunderboard Sense for wake up it. Once the device will be available script will be attempt connect to it:

noble.on("discover", function(peripheral) {
	
        ...

	/* If founded device is own big red button */
	if(config.bigRedButton.deviceAddress === peripheral.address) {
		peripheral.connect( function(error) {
                
                ...

		});
	}
});

After successful connect we need to get all services and characteristics:

peripheral.connect( function(error) {
	peripheral.discoverAllServicesAndCharacteristics(function(error, services, characteristics) {
		...
	});

In my code I define the desired characteristic by enumeration method:

 

for(i = 0; i < characteristics.length; i++) {
	/* If we find characteristic with button state when color it */
	if (characteristics[i].uuid === config.bigRedButton.characteristicUUID) {
		buttonStateChar = characteristics[i];
	}
	...

Desired characteristic which contaits value with button state have only one property - read. For acquiring button states we need implement peridical reading of desired value. Afrter define the desired characteristic we need to set a polling interval.

 

readingInterval = setInterval(readButtonCallback, config.bigRedButton.pollingInterval);

Listing of the callback:

 

/* Button callback started by setInterval */
function readButtonCallback() {
	buttonStateChar.read(function(error, data) {
		buf = data[0];
		console.log("\nData: %d", buf);
		if (buf === 1) {
			console.log("SW1 was pressed");
			/* Enable buzzer */
			config.buzzer.set();
		}
		else if (buf === 2) {
			console.log("SW2 was pressed");
		}
		else if (buf === 0) {
			console.log("No button pressed");
			/* Disable buzzer */
			config.buzzer.clear();
		} 
	});
}

 

Make buzz

 

Unfortunately, I could not get to work with GPIO over artik-sdk module and I decided work with GPIO over sysfs. Grove Buzzer in my solution connected over Seeed Studio Base shield to pin gpio121 (2 pin of Arduino expansion).

 

After starting the script a desired pin must to init as "out":

 

exec("echo 121 > /sys/class/gpio/export");
exec('echo "out" > /sys/class/gpio/gpio121/direction');

To buzzer squeaked the pin must be pulled up:

 

exec('echo "1" > /sys/class/gpio/gpio121/value');

To silence buzzer the pin must be pulled down:

 

exec('echo "0" > /sys/class/gpio/gpio121/value');

Then exiting the script the buzzer's pins must be released:

 

exec("121 > /sys/class/gpio/unexport");

All of this was implemented in config.js of the project. Link to repository.

 

Connect big red button to Thunderboard Sense.

 

I have this kind of the button:

 

 

Interesting fact - the button is more expensive than Thunderboard Sense Kit.

 

Connect the button by soldering as described on a following scheme:

 

 

In my case, it looks like this:

 

 

Testing

 

Video with test:

 

 

But it isn't serous because buzzer too weak. I change the buzzer to relay with signal lamp:

 

 

Now the big red button is really cool!

 

Results

 

It works. Repository with project - https://bitbucket.org/sithings/app_thunderboardtoartik

 

Links

 

Best regards,
Danil Borchevkin

Posts: 119
Registered: ‎05-30-2012

Re: Big red button on Thunderboard Sense and Samsung Artik 5

Very nice red button! Smiley Very Happy

Posts: 27
Registered: ‎11-07-2015

Re: Big red button on Thunderboard Sense and Samsung Artik 5

Thanks!

Best regards,
Danil Borchevkin

Posts: 19
Registered: ‎07-20-2015

Re: Big red button on Thunderboard Sense and Samsung Artik 5

Great tutorial Danil,

I managed to get it to work on a Raspberry Pi.

How would I change it to read some of the other sensors? Temperature? Humidity? etc. Any help would be useful.

 

Yours Simon M. 

Highlighted
Posts: 27
Registered: ‎11-07-2015

Re: Big red button on Thunderboard Sense and Samsung Artik 5

Thanks!

 

Retrvieving information from sensors can be done by reading characterictic (if it have only read property) or by enable notify property by writing ti it descriptor.

 

Full list of characteristics as point in the article - Android App repository

 

For read another sensors on Thunderboard Sense you need:

  1. read needed characteristics if it have read property as descriped in this article. You only need change UUID for reading and parse values.
  2. enable notify of desired characteristics which lead to generating events when characteristic was changed

If you need some examples and quick start for noble please follow https://github.com/sandeepmistry/noble

Best regards,
Danil Borchevkin