Skip to content

Example device smart switch

Greg Bowler edited this page Jan 13, 2017 · 1 revision

Let's build a smart switch: a mains switch that can be turned on and off via Neuron.

Step 1: Parts list.

The following devices will be used in the production of the smart switch:

Step 2: Prepare parts.

Cortex will run on the Raspberry Pi device. The only feature of the smart switch will be toggling a GPIO pin. We can use any pin for this purpose, but let's use the first non-assigned pin: pin 17 (see pinout board here).

Attach pin 17 to the NC contact of the relay board and pin 2 to common for power.

Next, attach the mains contacts of the relay to the neutral wire of the mains extension plug.

Be safe here - use correct terminal enclosures as you're dealing with live high voltage electricity!

Step 3: Write the device code for the simple switch.

Every device that can be used in Cortex-OS has its own directory within src/. The SimpleSwitch device has been included in the Cortex-OS repository already at src/SimpleSwitch.

Inside the device directory, create Device.php. Let's break down the code to operate this switch:

namespace Cortex\Switch;

use com\netbixx\WiringPiPHP\WiringPiPHP as WiringPi;
use ArtificialMinds\Neuron\NeuronTransmission;

class Device extends BaseDevice {

At the top of the file, we define the namespaces we will be using in the code. The first line defines the current namespace (which must match the device directory we're in).

Next, we alias other namespaced classes we will be using to perform the logic for this device. This allows any code to be loaded and utilised for each device, and as the code will only be loaded when it is used, having many devices present in the OS as standard is still very efficient.

The next line is our own class declaration. We are defining SimpleSwitch and stating that it is an extension of the simplest device there is: BaseDevice. The Base Device has one and only one feature: to register as a node on the Neuron network.

All devices should extend BaseDevice to add their own functionality, and functionality can be shared across other devices using this concept. Read more about how classes can extend eachother in the official PHP documentation for Object Oriented coding.

public function onPower() {
	$wiringPi = new WiringPi("gpio");
	$wiringPi->pinMode(self::GPIO_PIN, WiringPi::OUT);
	$wiringPi->digitalWrite(self::GPIO_PIN, WiringPi::OFF);
}

// TODO: Docs.

public function setup() {
	parent::setup();

	$wiringPi->addEventListener(self::GPIO_PIN,
		WiringPi::CHANGE, [$this, "update"]);
}

// TODO: Docs.

public function onNetworkUp() {
// Toggle the switch to indicate the network has just connected.
	for($i = 0; $i < 2; $i++) {
		$wiringPi->digitalWrite(self::GPIO_PIN, WiringPi::ON);
		usleep(500000);
		$wiringPi->digitalWrite(self::GPIO_PIN, WiringPi::OFF);
		usleep(500000);
	}
}

// TODO: Docs.

public function onNetworkDown() {

}

// TODO: Docs.

public function update(NeuronTransmission $data) {
	if($data->digital) {
		$wiringPi->digitalWrite(self::GPIO_PIN, WiringPi::ON);
	}
	else {
		$wiringPi->digitalWrite(self::GPIO_PIN, WiringPi::OFF);
	}
}

// TODO: Docs.