Friday, June 1, 2018

Using the Kano Motion Sensor from Powershell

Kano has created a nice motion sensor package for $20-30.

https://www.amazon.com/Kano-Motion-Sensor-Kit-movement/dp/B072JGWCM8/

They say you're required to use the Kano app, but I didn't want to. Luckily, once the drivers are installed the sensor simply acts as a serial device. I looked in device manager and found it on COM4 on my machine:


So I opened powershell and tried to connect to COM4:

PS C:\Users\user> $kano = new-object system.io.ports.serialport COM4,115200,none,8,one
PS C:\Users\user> $kano.open()
Exception calling "Open" with "0" argument(s): "Access to the port 'COM4' is denied."
At line:1 char:1
+ $kano.open()
+ ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : UnauthorizedAccessException

Oops! I still had the Kano app open - it was hogging the port! So I closed the app and tried again:

PS C:\Users\user> $kano.open()
PS C:\Users\user> while (1) { $kano.ReadLine() }
{"type": "event", "name": "proximity-data", "detail": {"proximity": 0}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 0}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 82}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 94}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 229}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 255}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 0}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 251}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 255}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 159}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 1}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 113}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 21}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 1}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 0}}
{"type": "event", "name": "proximity-data", "detail": {"proximity": 0}}

Success! From here I can parse the JSON output to get motion events and proximity. 

But don't forget to clean up:

PS C:\Users\mccartrb> $kano.close()