Add example sketches

* Add binary sensor sketch.
* Add light sketch.
* Add sensor sketch.
* Add switch sketch.
* Change example yaml to default value for optimistic setting.
This commit is contained in:
MartinHjelmare 2016-04-13 17:59:13 +02:00
parent 9b69b78bcf
commit 9e4a635494
5 changed files with 222 additions and 6 deletions

View file

@ -2,7 +2,7 @@
layout: page
title: "MySensors Sensor"
description: "Instructions how to integrate MySensors sensors into Home Assistant."
date: 2016-02-28 01:20 +0100
date: 2016-04-13 14:20 +0100
sidebar: true
comments: false
sharing: true
@ -58,5 +58,53 @@ By using V_UNIT_PREFIX, it's possible to set a custom unit for any sensor. The s
For more information, visit the [serial api] of MySensors.
### {% linkable_title Example sketch %}
```c++
/**
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* http://www.mysensors.org/build/light
*/
#include <SPI.h>
#include <MySensor.h>
#include <BH1750.h>
#include <Wire.h>
#define SN "LightLuxSensor"
#define SV "1.0"
#define CHILD_ID 1
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
BH1750 lightSensor;
MySensor gw;
MyMessage msg(CHILD_ID, V_LEVEL);
MyMessage msgPrefix(CHILD_ID, V_UNIT_PREFIX); // Custom unit message.
uint16_t lastlux = 0;
void setup()
{
gw.begin();
gw.sendSketchInfo(SN, SV);
gw.present(CHILD_ID, S_LIGHT_LEVEL);
lightSensor.begin();
gw.send(msg.set(lastlux));
gw.send(msgPrefix.set("lux")); // Set custom unit.
}
void loop()
{
uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
if (lux != lastlux) {
gw.send(msg.set(lux));
lastlux = lux;
}
gw.sleep(SLEEP_TIME);
}
```
[main component]: /components/mysensors/
[serial api]: https://www.mysensors.org/download/serial_api_15