Sensors & WebThings

Philippe Coval <purl.org/rzr> #SFK20

sfk2020-1.png

$ whois Phil Coval

  • Software Engineer from Rennes, France
  • OpenSource Contributor, Mozilla Rep (IoT)
  • Involved in industry's OSS:
    • Tizen (Intel), Yocto, IoTivity (Samsung)
  • Contact: https://purl.org/rzr
    • Presentations, Videos, Social…
    • Currently available for cooperation
    • Link me from Fediverse or @RzrFreeFr

Agenda

  • My journey of playing with sensors
    • Types, Tips and hints
    • Examples use cases
  • How can it be improved for developers
  • Using sensors for IoT

Sensors vs Actuators

  • Sensor measures a physical variation
    • and produce an output value
    • eg: What is the temperature ?
  • Actuator produces a physical action
    • "consumes" an input value (order)
    • eg: Turn the light on

Sensor examples

  • Binary sensors:
    • door sensor: open/closed
  • Analog sensors:
    • humidity detector : 1.42v ~= 42%
  • Numerical sensors:
    • tank level: [0,1,2]: none, some, full
  • Sensor fusion:
    • air quality: healthy to hazardous
  • Devices or "services" sensors:

System's I/O

  • GPIO : Binary
  • ADC : Analog
  • Digital:
    • I2C: Inter-Integrated Circuit
    • SPI: Serial Peripheral Interface
    • UART: Universal async receiver-transmitter
  • Others: PWM, CAN, USB…

Prototyping

  • "Surface mount component" SMC
    • Hard to soldier on PCB
  • Sensors modules are useful for prototyping
    • Easy to wire
      • using dupont wires (bread board)
  • Daughters boards
    • Arduino's shields
    • Raspberry-Pi's Hats

Binary sensor

  • General purpose input/output (GPIO)
  • CPU's I/O usable from board's pins
  • can be configured dynamically as
    • as input : (sensor)
    • as output : (actuator)
  • Voltages: Low (0V) or High (Vcc)
    • 5V (TTL) : eg: Arduino
    • 3.3V (LVTTL) : eg: RaspberryPi
    • 1.8V : eg: Edison

Analog Sensors

  • Numeric value (vs binary value on GPIO)
  • Need Analog to Digital Converter (ADC)
    • Most Micro-controllers ship ADC units
    • (Prefer Arduino to bare Raspberry Pi)
  • Translate a physical phenomena to voltage
  • Need to be converted software side
    • range / quantification
  • Eg: gaz (MQ), proximity (HC-SR04)

Analog Sensor (yl-69)

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Digital sensors: I2C

  • Inter-Integrated Circuit (I2C) 1982
  • Serial communication bus
    • Each device has a "slave" address
    • 7bits -> 128 items
  • 4 Wires:
    • Serial Data Line (SDA)
    • Serial Clock Line (SCL)
    • VCC: +5V or +3.3V
    • GND

I2C Setup on Pi

sudo raspi-config # Enable I2C
ls -l /dev/i2c* || sudo reboot
sudo apt-get install i2c-tools
/usr/sbin/i2cdetect -y 1
#|      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
#| 00:          -- -- -- -- -- -- -- -- -- -- -- -- --
#| 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#| 20: -- -- -- 23 -- -- -- -- -- 29 -- -- -- -- -- --
#| 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#| 40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#| 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#| 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#| 70: -- -- -- -- -- -- -- 77

Ambient light sensor

BH1750FVI Drivers

Example (Node.js)

let BH1750 = require('@abandonware/bh1750');
const options = {
    device: '/dev/i2cยญ1', // RPi I2C bus
    address: 0x23, // I2C address
    command: 0x10, // 1 lx resolution
    length: 2
};
let sensor = new BH1750(options);
sensor.readLight((value) => {
   console.log(value); // 42
});

bh1750 abandonware

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Environment

  • Temperature
  • Pressure (Barometric)
    • LPS25HB from ST (in RPi sensehat)
  • AirQuality, Gaz, UV
    • SDS011 (UART), MQ-X (Analog) …

JS driver for BMPx8x

var bmp085 = require('@abandonware/bmp085-sensor');
var sensor = bmp085({address: 0x77, mode: 3});
sensor.calibrate(function (err, data) {
  // ...
  sensor.read(function (err, data) {
  console.log(data);
  // { pressure: 29.9, temp: 42.0 }
  });
});

bmp085 abandonware

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Motion sensors

  • Accelerometer, Gyroscope, (+Magnetic)
    • Fusion to avoid drift
    • Accurate Orientation, Compass
  • Several devices:

FXOS8700Q (Mbed)

#include "mbed.h"
#include "FXOS8700Q.h"

I2C i2c(PTE25, PTE24); // K64F's pins for SDA SDC
FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
motion_data_units_t v;
acc.enable();
acc.getAxis(v);
printf("X=%f Y=%f Z=%f\n", v.x, v.y, v.z);

LSM9DS1 IMU

  • The IMU (inertial measurement unit) sensor
    • combination of three sensors
    • each with an x, y and z axis.
    • 9 dof (degrees of freedom) sensor.
  • Shipped in RPi Sensehat
  • Supported by python module:
    • set_imu_config(compass, gyro, accel)
    • get_compass/orientation/accelerometer

Sensehat webthings

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Drivers mess

  • Many drivers from community
    • use custom API to do same thing
      • highlevel: start, read, fail, stop
  • For many languages and platforms:
    • Linux: Node.js, python
    • RTOS: C/C++ (Arduino, Mbed, NuttX)
  • + Mobile phones API for sensors
    • GPS, Orientation…
  • Even in web browsers !

Sensor API for Web

  • (Mobiles') Sensors exposed to browsers
  • W3C Specification:
    • to drive consistency across sensor APIs
    • set of interfaces
    • enable new use cases,
    • speed-up and ease specification
    • implementation of new sensors

The Web and beyond

  • Web is platform agnostic
    • Programmable: server or client side
  • Use JavaScript interpreted language
    • in browser, headless (Node)
    • also supported in Micro Controllers (MCU)

generic-sensor-lite

  • Lightweight JS implementation of W3C spec
  • https://github.com/rzr/generic-sensors-lite/
  • provide high level API
  • support many Sensors
    • devices and/or simulators
  • target constrained devices (MCU)
  • Several JavaScript runtimes are supported
    • Develop on Linux using Node.js
    • Deploy to MCU running IoT.js

Generic Sensor flow

  • Create new Sensor objects
    • with options (frequency)
  • Add callbacks functions
    • onerror, onreading…
  • start: loop on reading
    • object's values are updated on each read
    • onreading() callback called
    • wait next read (frequency opt)
  • stop

Simulator example

// file: example.js
var GenericSensors = require('generic-sensors-lite');
var sensor = new GenericSensors.Geolocation({frequency: 0.42});
sensor.onreading = function() {
 console.log("[" + this.latitude + "," + this.longitude + "]");
 this.stop();
};
sensor.start()
# Run JS application with Node.js
node example.js
[ 4.20180308, 2.20200923]

Creating new driver

var sensor = new ColorSensor({controller: "tcs34725"}) // I2C
sensor.onreading = function () {
  console.log('{"color": "' + this.color + '"}') // #badc0d
}
sensor.start()

Color sensor demo

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Sensors devices

Simulators

  • Accelerometer
  • Battery
  • Geolocation
  • Orientation
  • Proximity
  • more?

IoT.js : JS runtime

  • Use JerryScript interpreter (ES 5.1)
  • Low footprint: Flash=180+KB RAM=26KB
  • Built in Modules:
    • I/O: GPIO, ADC, I2C, SPI, UART, PWM
    • Sys: Net, FS/ROM, Crypto…
  • External JS modules from community
  • Support: Linux, Tizen, NuttX, TizenRT

Sensor script on MCU

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

NuttX RTOS

  • RTOS Committed to comply standards
    • POSIX, ANSI C (like Unix, GNU/Linux)
    • File based IO (/dev), BSD sockets (uIP)
  • Released by Gregory Nutt in 2007
  • Incubated by Apache Foundation
  • Base of derived projects:
    • TizenRT, PX4, Sony Spresense
  • IoT.js supports NuttX (watch NuttX2020Con)

Deploy JS app to MCU

  • Import IoT.js as NuttX application
  • Deploy application to ROMFS
    • example.js : main (loop) + js modules
  • Add startup script (NSH)
    • start interpreter:
    • iotjs "/rom /… /example.js"
  • Build firmware for supported MCU
    • I use ST Nucleo F746ZG
  • Watch https://nuttx.events 2020

Going online?

  • Ethics: Please consider privacy and security
  • Several protocols:
    • MQTT, CoAP, HTTP, WebSockets
  • Web Of Things (aka WoT)
    • W3C's Working group
    • Link devices (sensors, actuators)
  • FLOSS: WebThings smart-home platform

WebThings

WebThings

  • Smart-home platform born in Mozilla ET lab
    • with privacy by design
    • inspired by W3C WebOfThings (WoT)
  • Framework to build webthings (REST API)
    • Mozilla IoT schema (in JSON)
    • JS (Node or IoT.js), Python, Rust, C++…
  • Mozilla WebThings gateway

WebThings REST API

$ make -C generic-sensors-lite/example/webthing/ start
make runtime=iotjs start
make -C example/webthing runtime=iotjs start
curl -s  http://localhost:8888/properties
#| { (...) "illuminance":123., "celsius":42., "color":"#c0a175" (...) }

Webthings addon:

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Summary

  • Sensors drivers
    • Can use uniform API: W3C Generic Sensors
  • generic-sensor-lite implements spec in JS
    • work on MCU (with IoT.js)
    • or full OS (with Node.js)
  • WebThings SmartHome with Privacy by Design
    • Use webthing API
    • to connect devices to gateway
    • or its sensors w/ generic-sensors addon

Feedback welcome

Extra demos

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Video Playback

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Extra: Wearable

๐Ÿ“บ

๐Ÿ“บ

More

Playlist

๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ ๐Ÿ“บ

Created by Philippe Coval