Skip to main content

Sensors Simple API

What are Sensors?#

Sensors are small electronic devices used measure an enviornmental quantity (for eg: temperature, pressure..)

List of features implemented in Shunya stack#

With Shunya stack you can Measure these enviornmental Changes

  1. Voltage
  2. Light Intensity
  3. Distance
  4. Heart Rate
  5. Liquid Flow
  6. Motion Thershold
  7. Proximity Threshold
  8. Touch Threshold
  9. Light Thershold
  10. Tilt Thereshold
  11. Generic Digital pin

List of Sensors supported by Shunya stack#

ClassSensorImageSensor ID
PCF8591Oops!, No Image to display.6
ADS1115Oops!, No Image to display.18
Light Intensity
BH1750Oops!, No Image to display.7
HCSR04Oops!, No Image to display.8
Heart Rate
SEN11574Oops!, No Image to display.9
Liquid Flow
YFS201Oops!, No Image to display.10
Motion Thershold
PIROops!, No Image to display.11
Proximity Threshold
IR Proximity sensorOops!, No Image to display.12
Touch Threshold
Touch SensorOops!, No Image to display.13
Light Thershold
Light Blocking SensorOops!, No Image to display.14
Tilt Thereshold
KY_017Oops!, No Image to display.15
KY_025Oops!, No Image to display.16
BALL_SWITCHOops!, No Image to display.17
Generic Digital pin
Any GPIO sensor----

Using Sensors with Shunya stack#

Requirements to use Sensors with Shunya stack#

  1. Shunya OS installed (supported Arm devices)
  2. Respective Sensors connected to the supported Arm device

Steps to use Sensors with Shunya stack#

  1. Connect Sensors to your board.
  2. Set Sensor settings in Shunya.
  3. Read from sensor to get the desired value.

Run the steps given below inside Shunya OS installed (supported Arm devices)

Step 1: Connect Sensors to your board#

  1. A quick search on Google will lead to images of the connection daigrams for each sensor.
  2. Note down which Sensor pin is connected to pin on the Device.

Step 2: Set Sensor settings in Shunya.#

Set Sensor settings, by editing the config file /etc/shunya/interfaces/config.yaml inside Shunya OS.

A simple configuration should Tells Shunya OS your hardware connections.

Sensor ID's and Connection ID's#

You tell all your Hardware connections to Shunya OS via Sensor ID's and Connection ID's.


There is no need to tell Shunya OS the Power connections i.e Vcc and GND. Shunya OS assumes that those are connected to the respective Pins for sensor to work.

Each Sensor is given a special ID which the Shunya OS library recognizes. And each Hardware pin on the Sensor is given a Connection ID.

So when we write pin 1: 1.1 Shunya OS understands it as <Sensor 1>.<Sensor pin 1> connected to pin 1 of Raspberry Pi(or your chosen device).

For Example: Let's say we have connected 2 devices to the Raspberry Pi (BME280 and PCF8591) to pin 3 (SDA) & pin 5 (SCL) respectively.

The Config file will look like this.

pin 3: [1.1, 6.1]
pin 4: null
pin 5: [1.2, 6.2]

The configuration file allows you to quickly change the Sensors whithout changing the code.

Step 3: Read from sensor to get the desired value.#

  1. Start with an ready to use template for building with shunya stack

    git clone
    cd examples/template
  2. Open the main.cpp in an text editor and modify as per your use case.

    Example main.cpp file:

    /* --- Standard Includes --- */
    #include <iostream>
    #include <cstdlib>
    #include <stdint.h>
    #include <time.h>
    #include <unistd.h>
    /* --- RapidJSON Includes --- */
    /* MANDATORY: Allows to parse Shunya AI binaries output */
    #include "rapidjson/document.h"
    #include "rapidjson/writer.h"
    #include "rapidjson/istreamwrapper.h"
    #include "rapidjson/stringbuffer.h"
    #include "rapidjson/ostreamwrapper.h"
    #include "rapidjson/filereadstream.h"
    #include "subprocess.hpp" /* MANDATORY: Allows to run Shunya AI binaries */
    #include "exutils.h" /* MANDATORY: Allows to parse Shunya AI binaries output */
    /* --- Shunya Interfaces Includes --- */
    #include <si/shunyaInterfaces.h> /* MANDATORY: Contains all IoT Functions */
    using namespace std;
    using namespace rapidjson;
    int main(void)
    /* MANDATORY: Initializes the Shunya components */
    /* Write your code here */
  3. Write code to read the value from the sensor.

    For reading data from Voltage sensor, write code

    int16_t voltage = getAdc(ADC_CHANNEL); /* ADC_CHANNEL is channel number i.e 0, 1, 2 ... */
    /* Replace ADC_CHANNEL with your connected channel */
    if (voltage < 0) {
    printf("Error unable to read Voltage. Error number %d= ", errNum);
  4. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
  5. Run the code

    sudo ./project-name

Facing errors with the component?#