-
-
Notifications
You must be signed in to change notification settings - Fork 14
Sensor
In IoT and embedded world, sensors and actuators are key components to interact with the external physical world, which is the base of many use cases or Concepts.
While there is only two types of operations:
- READ: Measuring some physical energy
- WRITE: Delivering some energy to some device that can alter an established situation
Some devices are combining both, that's the reason sometimes actuators are sometimes mixed with sensors.
To avoid any ambiguity I'll use those 2 terms:
- Sensor for reading input data (sensing)
- Actuator for writing outout data (acting)
In WoT context, those two elements are defined as properties which could be readable or writable, other semantics are also used: BooleanProperty, OnOffProperty, BinarySensor, SwitchOnOff, don't be confused.
Note that when real time monitoring is needed (for Sensor) you may consider to use webthing-node which is fully supporting websockets (this is demonstrated by "Clap Sensor" part of "Smart Orchid" demo).
To illustrate Concept, watch Sensor part of the "Smart Orchid" demo video:
The simplest use case is just basic interaction from the RaspberryPi's button to the artik ARTIK05x's LED.
To recap here are all elements we will explain in following chapters:
- ARTIK05s on board LED is simplest Actuator running on IoT.js and TizenRT
- "Button sensor" on RaspberryPi is simplest sensor running on IoT.js on RaspberryPi, (connected to earlier Actuator via Gateway)
- "Clap sensor" using webthing-node (on RaspberryPI)
- "Ambient Light" and "Temperature" using generic-sensor-lite connected to RaspberryPi
- "Moisture" Level Sensor on Extra Arduino MCU board
- Actuator RGB Lamp on ESP8266 MCU
General Purpose Input/Output interface is widely used digital communication (on/off):
NodeJS is suporting this with different external implementations, the most generic one is using "sysfs" (Linux kernel interface and thus provide genericity among different hardware (ARTIK, RPi etc):
While IoT.js uses a built in module, which can be optionnaly enabled at built time:
Unfortunately API are not aligned, so I made an abstraction class to provide portability:
-
Node.js:
-
IoT.js:
Note Raspberry Pi users might be used to "WiringPi" library, it can be installed also as Node.js module:
To avoid rewriting GPIO calls, a wrapper class can be also used for IoT.js:
There are two onboard buttons that can be used on ARTIK 05x board, check following IoT.js code (made for TizenRT but portable to other OS):
If you can turn on a LED then code is same to control a relay that can power an other device. All you need is to use or build an electronic circuit or use standalone module, like "Flex RaspberryPi's hat":
"Smart Orchid Demo" was using Gateway's GPIO Adapter, and relay is turning an USB fan (powered by 5V source).
Watch previously explanations for ARTIK10 (to prototype an IoTivity thing but it's same problem):
- https://www.slideshare.net/SamsungOSG/iotivity-tutorial-prototyping-iot-devices-on-gnulinux/26
- https://www.slideshare.net/SamsungOSG/iotivity-tutorial-prototyping-iot-devices-on-gnulinux/27
- https://www.slideshare.net/SamsungOSG/iotivity-tutorial-prototyping-iot-devices-on-gnulinux/28
Webthing-node is now supporting GPIO, ARTIK530 is used as reference platform because it feature 2 onboard buttons.
Or we can validate GPIO inputs using an OpenSource hardware "buttons" board "PlayPHat":
project="webthing-iotjs"
url="https://github.com/rzr/${project}"
branch='sandbox/rzr/review/master'
git clone --recursive --depth 1 -b "$branch" "$url" ; cd "$project"
make -C example/platform play-phat
Expected log:
NODE_PATH=.:../.. node index play-phat
log: board: play-phat: Loading
Usage:
/home/pi/.nvm/versions/node/v8.11.4/bin/node /home/pi/webthing-iotjs/example/platform/index [board] [port]
Try:
curl -H "Accept: application/json" http://localhost:8888
log: board: play-phat: Started
log: GPIO: Left: open:
log: GPIO: Right: open:
log: GPIO: Up: open:
log: GPIO: Down: open:
log: GPIO: A: open:
log: GPIO: B: open:
log: GPIO: Start: open:
log: GPIO: Select: open:
log: GPIO: Start: change: false
log: GPIO: Start: change: true
There are two onboard buttons that can be used on ARTIK 05x board, check following IoT.js code (made for TizenRT but portable to other OS):
If using Node.js and GPIO's module this example can be used as base on GPIO11 (here used for "Flex RaspberryPi hat"):
"Smart Orchid Demo" showed a "Clap sensor", which is basically just a GPIO input of KY-037's digital pin:
I used it on a Raspberry Pi using node, but it should work on any other device.
For the record I used this PiHat, and modified it to plug additional sensor in (clap):
- https://www.tindie.com/products/anavi/anavi-flex-raspberry-pi-hat-for-iot/
- https://github.com/AnaviTechnology/anavi-flex/issues/1#issuecomment-274616910
If curious check Mozilla blog post about an alternative way to build a "Clap sensing webthing" :
Note: It's possible to use I2C sensors on IoT.js but since some sensors changes are not yet upstreamed this chapter starts to explain the currently supported use.
In "Smart Orchid demo" "I2C sensors were connected to RaspberryPi's bus of Gateway (using Node,js runtime), then the "Generic sensors adapter" was installed as Gateway's addon (released since mozilla-iot-gateway-0.4).
The adapter is built on a high level wrapper abstract drivers, o "generic-sensor-lite" module can be also used outside Gateway, check more detail on project page:
Check supported sensors For now I recommend to use either BH1650 or BMP082 modules, community implemented drivers, which are supported by NPM community, since my fixes were upstreamed.
Now you can try to create webthings, I suggest to start making them for supported Node.js and then port to IoT.js (in development).
- https://github.com/miroRucka/bh1750/pull/15# (Under review)
- https://github.com/miroRucka/bh1750/pull/14# (Merged)
- https://github.com/miroRucka/bh1750/pull/13# (Merged)
- https://github.com/miroRucka/bh1750/pull/10# (Released)
For the record following repos were used:
Check Concept page for overview, Gateway to get started, IotJs page to install runtime to build webthing as explained in Home page.
For further experiments check Social and Sensor, or Extra parts like WebApp (for Tizen or PWA) or MCU info about running on other microcontrollers not supported by TizenRT.
While Home focus mostly on using iotjs to build webthings (on GNU/Linux or TizenRT for ARTIK05X devices).
This document is still in draft state, but reviews are always welcome, if you try to replicate it and stuck on missing instructions I would appreciate that you file issues or even better make pull request (just edit in github) that insert "TODO marks" in following chapters, like:
- TODO: please explain more this chapter and then remove this TODO line
Community contributions are welcome at:
Support is also possible, ask in:
- https://github.com/rzr/webthing-iotjs
- irc://irc.mozilla.org/#iot
WARNING: Developement branches could break over time.
Instead of maintaining "quick and dirty" demo code, I decided to split demo in smaller independents parts (which can reused) and I am upstreaming the most I can.
Then support can be done on mainline branches (or released versions).
Note that, Upstreaming can be a slow process, so snapshots links will remain until 100% of code is upstreamed.
Licence:
Reference documentation is at:
-
Concept:
- Demo Concept and Architecture
-
Gateway:
- Getting started with Mozilla IoT gateway
-
IotJs:
- Install IoT.js needed to run webthings
-
Home:
- Welcome page to build WebThings using IotJs
-
Social:
- Notification service using Mastodon FLOSS
-
TizenRT:
- webthing-iotjs on ARTIK05x
-
Sensor: and Actuator
- Physical interactions
-
Extra hints:
- Docker: About running in container
- MCU: About microcontrollers (not supported by TizenRT)
- WebApp: Alternate browser (Tizen and PWA)
- GnuLinux: Article about Edison and other
- Raspbian: Article about RaspberryPi
- Arduino: Alt For atmel or Esprissif boards
- DigitalTwins : WiP experiments
- TODO: Work in progress