MassimilianoMichele Valentini
Published © CC BY

How to Control Lights Through Gesture

A new application for The Tactigon, this time combined to Raspberry Pi to achieve a futuristic lights control!

BeginnerFull instructions provided2 hours1,288

Things used in this project

Story

Read more

Schematics

Connections

Code

Code snippet #8

bluetooth.service - Bluetooth service
    Loaded: loaded (/lib/systemd/ system/bluetooth.service; disabled)
   Active: active (running) since Mon 2018-02-12 09:18:15
UTC; 15s ago
     Docs: man:bluetoothd(8) Main PID: 1022 (bluetoothd)
Status: “Running”
   CGroup: /system.slice/ bluetooth.service
??1022 /usr/local/
libexec/bluetooth/bluetoothd --experimental

Code snippet #11

Python
#WAIT FOR NOTIFICATIONS
try:
while True: if
p.waitForNotifications(1.0):
continue
except:
print(Terminated)

Code snippet #12

C/C++
if (!lastSupX && accData.x > threshold) {
lastSupX = true; lastInfX = false; lastInfY = false; lastSupY = false; buffData[0] = 0x00; buffData[1] = 0xBB; flash();
bleChar.
update(buffData);
justSent = true;
}

Light Control Through Gesture

Arduino
The code for The Tactigon
#include <tactigon_led.h>
#include <tactigon_IMU.h>
#include <tactigon_BLE.h>


T_Led rLed, bLed, gLed;
T_ACC accMeter;
T_AccData accData;

T_QUAT qMeter;
T_QData qData;

T_BLE bleManager;
UUID uuid;
T_BLE_Characteristic bleChar;
boolean lastInfZ = false;
boolean lastSupZ = false;
boolean lastInfY = false;
boolean lastSupY = false;
boolean lastInfX = false;
boolean lastSupX = false;
boolean justSent = true;
float threshold = 9.6;
int ticks;
//Used for LEDs cycle
int  ticksLed, stp;

void setup() {

  ticks = 0;
  rLed.init(T_Led::RED);
  gLed.init(T_Led::GREEN);
  bLed.init(T_Led::BLUE);

  rLed.off();
  gLed.off();
  bLed.off();
  bleManager.InitRole(TACTIGON_BLE_PERIPHERAL);
  //bleManager.setName("TTGPS");
  //add acc characteristic
  uuid.set("7ac71000-503d-4920-b000-acc000000001");
  bleChar = bleManager.addNewChar(uuid, 2);
}

void flash() {
  rLed.on();
  gLed.on();
  bLed.on();
}

void loop() {
  unsigned char buffData[2] = {0x00, 0x00};
  if (GetCurrentMilli() >= (ticksLed + (1000 / 50))) {
    //get acc data
    accData = accMeter.getAxis();
    if (!justSent) {
      if (accData.z > threshold && !lastSupZ) {
        lastSupZ = true;
        lastInfZ = false;
        lastInfX = false;
        lastSupX = false;
        lastInfY = false;
        lastSupY = false;
        buffData[0] = 0x00;
        buffData[1] = 0xFF;
        flash();
        //bleChar.update(buffData);
        justSent = true;
      }
      if (accData.z < -threshold && !lastInfZ) {
        lastInfZ = true;
        lastSupZ = false;
        lastInfX = false;
        lastSupX = false;
        lastInfY = false;
        lastSupY = false;
        buffData[0] = 0xFF;
        buffData[1] = 0xFF;
        flash();
        //bleChar.update(buffData);
        justSent = true;
      }
      if (accData.y > threshold && !lastSupY) {
        lastSupY = true;
        lastInfY = false;
        lastInfZ = false;
        lastSupZ = false;
        lastInfX = false;
        lastSupX = false;
        lastInfY = false;

        buffData[0] = 0x5A;
        buffData[1] = 0x5A;
        flash();
        bleChar.update(buffData);
        justSent = true;
      }
      if (accData.y < -threshold && !lastInfY) {
        lastInfY = true;
        lastSupY = false;
        lastInfZ = false;
        lastSupZ = false;
        lastInfX = false;
        lastSupX = false;
        lastSupY = false;

        buffData[0] = 0x00;
        buffData[1] = 0x88;
        flash();
        bleChar.update(buffData);
        justSent = true;
      }
      if (accData.x > threshold && !lastSupX) {
        lastSupX = true;
        lastInfX = false;
        lastInfZ = false;
        lastSupZ = false;
        lastInfX = false;
        lastInfY = false;
        lastSupY = false;
        buffData[0] = 0x00;
        buffData[1] = 0xBB;
        flash();
        bleChar.update(buffData);
        justSent = true;
      }
      if (accData.x < -threshold && !lastInfX) {
        lastInfX = true;
        lastSupX = false;
        lastInfZ = false;
        lastSupZ = false;
        lastSupX = false;
        lastInfY = false;
        lastSupY = false;

        buffData[0] = 0x15;
        buffData[1] = 0x15;
        flash();
        bleChar.update(buffData);
        justSent = true;
      }
    }
  }
  if (GetCurrentMilli() >= (ticksLed + (1000 / 2))) {

    justSent = false;
    ticksLed = GetCurrentMilli();
    if (stp == 0) {
      rLed.on();
      gLed.off();
      bLed.off();
    }
    else if (stp == 1) {
      rLed.off();
      gLed.off();
      bLed.on();
    }
    else if (stp == 2) {
      rLed.off();
      gLed.on();
      bLed.off();
    }
    stp = (stp + 1) % 3;
  }
}

BLERASPBERRYTACTIGONRELAY.py

Python
The code to run in the Raspberry Pi 3
import RPi.GPIO as GPIO

import struct
import binascii
import bluepy
from bluepy import btle

#GPIO SETTINGS AND CONFIGURATIONS
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
channelRelay = [7, 11, 13, 15]
GPIO.setup(channelRelay, GPIO.OUT, initial=GPIO.LOW)
configA=[channelRelay[1],channelRelay[3]]
configB=[channelRelay[0],channelRelay[2]]

#DELEGATE CLASS FOR NOTIFICATION
class MyDelegate(btle.DefaultDelegate):
    def __init__(self):
        btle.DefaultDelegate.__init__(self)
        
    #MESSAGE PARSING AND GPIO CONTROL
	def handleNotification(self, cHandle, data):
        print("handleNotification()")
        val=binascii.b2a_hex(data)
        val=binascii.unhexlify(val)
        val = struct.unpack('>H', val)[0]
        if(val==0x5A5A):
            GPIO.output(configB, GPIO.HIGH)
            GPIO.output(configA, GPIO.LOW)
            print("Configuration B ON")
        elif(val==0x0088):
            GPIO.output(configA, GPIO.HIGH)
            GPIO.output(configB, GPIO.LOW)
            print("Configuration A ON")
        elif(val==0x00BB):
            GPIO.output(channelRelay, GPIO.HIGH)
            print("All ON")
        elif(val==0x1515):
            GPIO.output(channelRelay, GPIO.LOW)
            print("All OFF")
        
#BLE DEVICE SETTINGS
tactigon_uuid = btle.UUID("7ac71000-503d-4920-b000-acc000000001")
service_uuid = btle.UUID("bea5760d-503d-4920-b000-101e7306b000")  
p = btle.Peripheral("BE:A5:7F:2F:76:68")
p.setDelegate(MyDelegate())

#CHARACTERISTICS LIST
tactigonService = p.getServiceByUUID(service_uuid)
#for ch in tactigonService.getCharacteristics():
        #print str(ch)

#OBTAIN DATA FROM CHARACTERISTIC
tactigonSensorValue = tactigonService.getCharacteristics(tactigon_uuid)[0]
val = tactigonSensorValue.read()
#print "Characteristic value: ", binascii.b2a_hex(val)

#SET NOTIFICATION 
notifyTactigon = tactigonService.getDescriptors(forUUID=0x2902)[0]
notifyTactigon.write(struct.pack('<bb', 0x01, 0x00))

#WAIT FOR NOTIFICATIONS
try:
    while True:
        if p.waitForNotifications(1.0):
            continue
except:
    print("Terminated")

Credits

Massimiliano

Massimiliano

29 projects • 112 followers
I'm an engineer and I like robotics, embedded systems and linux. I spent a lot of time in automation and firmware development.
Michele Valentini

Michele Valentini

20 projects • 67 followers
From Pepper growing to CNC milling, i don't like to waste time sleeping

Comments