Skip to content

Update mysensors sensor #1976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions source/_components/sensor.mysensors.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ S_TYPE | V_TYPE
S_TEMP | V_TEMP
S_HUM | V_HUM
S_BARO | V_PRESSURE, V_FORECAST
S_WIND | V_WIND, V_GUST
S_WIND | V_WIND, V_GUST, V_DIRECTION
S_RAIN | V_RAIN, V_RAINRATE
S_UV | V_UV
S_WEIGHT | V_WEIGHT, V_IMPEDANCE
Expand Down Expand Up @@ -68,7 +68,7 @@ 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 %}
### {% linkable_title MySensors 1.5 example sketch %}

```cpp
/**
Expand Down Expand Up @@ -116,5 +116,69 @@ void loop()
}
```

### {% linkable_title MySensors 2.x example sketch %}

```cpp
/**
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* http://www.mysensors.org/build/light
*/

#define MY_DEBUG
#define MY_RADIO_NRF24

#include <BH1750.h>
#include <Wire.h>
#include <MySensors.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;
MyMessage msg(CHILD_ID, V_LEVEL);
MyMessage msgPrefix(CHILD_ID, V_UNIT_PREFIX); // Custom unit message.
uint16_t lastlux = 0;
bool initialValueSent = false;

void setup()
{
sendSketchInfo(SN, SV);
present(CHILD_ID, S_LIGHT_LEVEL);
lightSensor.begin();
}

void loop()
{
if (!initialValueSent) {
Serial.println("Sending initial value");
send(msgPrefix.set("custom_lux")); // Set custom unit.
send(msg.set(lastlux));
Serial.println("Requesting initial value from controller");
request(CHILD_ID, V_LEVEL);
wait(2000, C_SET, V_LEVEL);
}
uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
if (lux != lastlux) {
send(msg.set(lux));
lastlux = lux;
}

sleep(SLEEP_TIME);
}

void receive(const MyMessage &message) {
if (message.type == V_LEVEL) {
if (!initialValueSent) {
Serial.println("Receiving initial value from controller");
initialValueSent = true;
}
}
}
```

[main component]: /components/mysensors/
[serial api]: https://www.mysensors.org/download/serial_api_15