# How to use sensors in mangoh-yellow

**URL:** <https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308>\
**Category:** mangOH Yellow\
**Created:** [January 1, 2020, 9:58am UTC](https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308 "2020-01-01T09:58:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeongBeom27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@SeongBeom27](https://mangoh.discourse.group/u/SeongBeom27)\
**Post date:** [January 1, 2020, 9:58am UTC](https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308/1 "2020-01-01T09:58:25Z")

</div>

How to use sensors in mango-yellow??

As for temperature, api is well supplied as in “le\_temp”.

Is there an api that makes it comfortable to use accelerometers, gyroscopes, pressure, humidity, air quality, temperature and photometric sensors?

---

<div class="post-metadata">

**Author:** ![jchitty](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jchitty/32/846_2.png) [@jchitty](https://mangoh.discourse.group/u/jchitty)\
**Post date:** [January 2, 2020, 9:23pm UTC](https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308/2 "2020-01-02T21:23:56Z")

</div>

You can fetch that information from the Data Hub using the Data Hub APIs. Some of the other APIs may be available in some cases, but in many cases they have not yet been implemented for Yellow.

To generate the Data Hub API documentation, you can

```auto
$ cd apps/DataHub
$ make docs

```

The documentation will appear under `docs/html`. You can view it by opening the it in a web browser.

```auto
$ firefox docs/html/index.html

```

Unfortunately, this is not quite as straightforward as using specialized APIs, but once you learn how to do it, it works the same for any type of sensor or actuator that is connected to the Data Hub.

For example, to get the mangOH Yellow’s light sensor to start performing readings, you need someone to set its `period` and `enable` resources. This can be done via Octave or using the `dhub` command-line tool. You can also use the Data Hub’s Admin API to do it from your own app.

```auto
admin_PushNumeric("/app/light/period", 0, 10.0 /* seconds */);
admin_PushBoolean("/app/light/enable", 0, true);

```

Then, to fetch the last light sensor reading, you can use the Data Hub’s Query API.

```auto
double timestamp;
double value;
le_result_t result;

result = query_GetNumeric("/app/light/value", &timestamp, &value);
if (result == LE_OK)
{
    // handle reading.
}
else
{
    // handle lack of reading.
}

```

Or, if you’d prefer to get a call-back every time a new sensor reading is taken, you can set that up using the Admin API.

```auto
static void ProcessLightLevelSample(double timestamp, double value, void* contextPtr)
{
    // Process the sensor reading.
}

COMPONENT_INIT
{
    ...
    admin_AddNumericPushHandler("/app/light/value", ProcessLightLevelSample, NULL);
    ...
}

```

---

<div class="post-metadata">

**Author:** ![SeongBeom27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@SeongBeom27](https://mangoh.discourse.group/u/SeongBeom27)\
**Post date:** [January 4, 2020, 5:47am UTC](https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308/3 "2020-01-04T05:47:59Z")

</div>

Thanks your answer!!

I try to study your answer…!  
but in many cases they have not yet been implemented for Yellow…??? i’m so sad…  
Where i can refer to API just a little?? you know?

---

<div class="post-metadata">

**Author:** ![jchitty](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jchitty/32/846_2.png) [@jchitty](https://mangoh.discourse.group/u/jchitty)\
**Post date:** [January 7, 2020, 1:28am UTC](https://mangoh.discourse.group/t/how-to-use-sensors-in-mangoh-yellow/3308/4 "2020-01-07T01:28:44Z")

</div>

Hi,

In general, you can check what services are being offered by running the `sdir list` command on the target device shell. Look in the `SERVICES` section of the output.

I see that the following sensor APIs are available:

- `<root>.ma_battery`
- `<root>.light`
- `imu.imu` (gyro and accelerometer)
- `imu.temperature` (temperature of the IMU)
- `environment.mangOH_bme680`

Unfortunately, because the apps that serve the `ma_battery` and `light` APIs are running unsandboxed, they just show up as “`<root>`” in the list of services. You should be able to bind to those using “`<root>.ma_battery`” and “`<root>.light`”, but you can also bind to those using “`battery.ma_battery`” and “`light.light`” (because the apps that serve those are called “battery” and “light”).

You can use `find` and `grep` to hunt for which applications’ .adef files export those APIs, if you want. For example,

```auto
$ find -name '*.adef' | xargs grep 'ma_battery'
./apps/BatteryService/battery.adef: battery.batteryComponentRed.ma_battery
./apps/BatteryService/battery.adef: battery.batteryComponentYellow.ma_battery
./samples/BatteryClient/batteryClient.adef: batteryClient.batteryClientComponent.ma_battery -> battery.ma_battery

```

Sadly, I can’t think of a more user-friendly way to find this information. (Seems like there’s an opportunity to add a handy Legato feature for this.) But, as I said, you should be able to bind to “`<root>.ma_battery`” and “`<root>.light`” (exactly as it appears in the `sdir list` output) to make it work. The only downside of binding to `<root>.ma_battery` instead of `battery.ma_battery` is that your binding would break if the battery app were changed to run in a sandbox. But, that’s easy to fix.

To find the `.api` files, you can use `find`.

```auto
$ find -name ma_battery.api

```

Once you have “required” the `.api` files in your `Component.cdef`, added your bindings to your `.adef` or `.sdef`, and run a build, you can find the generated `.h` files using `find`. E.g.,

```auto
$ find -name ma_battery_interface.h

```

I hope this helps.

–Jen
