Skip to content

Commit 74ac6b9

Browse files
Update mysensors sensor (home-assistant#1976)
* Add V_DIRECTION. * Add example sketch for mysensors 2.x.
1 parent d7d4ed8 commit 74ac6b9

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

source/_components/sensor.mysensors.markdown

+66-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ S_TYPE | V_TYPE
2424
S_TEMP | V_TEMP
2525
S_HUM | V_HUM
2626
S_BARO | V_PRESSURE, V_FORECAST
27-
S_WIND | V_WIND, V_GUST
27+
S_WIND | V_WIND, V_GUST, V_DIRECTION
2828
S_RAIN | V_RAIN, V_RAINRATE
2929
S_UV | V_UV
3030
S_WEIGHT | V_WEIGHT, V_IMPEDANCE
@@ -68,7 +68,7 @@ By using V_UNIT_PREFIX, it's possible to set a custom unit for any sensor. The s
6868

6969
For more information, visit the [serial api] of MySensors.
7070

71-
### {% linkable_title Example sketch %}
71+
### {% linkable_title MySensors 1.5 example sketch %}
7272

7373
```cpp
7474
/**
@@ -116,5 +116,69 @@ void loop()
116116
}
117117
```
118118
119+
### {% linkable_title MySensors 2.x example sketch %}
120+
121+
```cpp
122+
/**
123+
* Documentation: http://www.mysensors.org
124+
* Support Forum: http://forum.mysensors.org
125+
*
126+
* http://www.mysensors.org/build/light
127+
*/
128+
129+
#define MY_DEBUG
130+
#define MY_RADIO_NRF24
131+
132+
#include <BH1750.h>
133+
#include <Wire.h>
134+
#include <MySensors.h>
135+
136+
#define SN "LightLuxSensor"
137+
#define SV "1.0"
138+
#define CHILD_ID 1
139+
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
140+
141+
BH1750 lightSensor;
142+
MyMessage msg(CHILD_ID, V_LEVEL);
143+
MyMessage msgPrefix(CHILD_ID, V_UNIT_PREFIX); // Custom unit message.
144+
uint16_t lastlux = 0;
145+
bool initialValueSent = false;
146+
147+
void setup()
148+
{
149+
sendSketchInfo(SN, SV);
150+
present(CHILD_ID, S_LIGHT_LEVEL);
151+
lightSensor.begin();
152+
}
153+
154+
void loop()
155+
{
156+
if (!initialValueSent) {
157+
Serial.println("Sending initial value");
158+
send(msgPrefix.set("custom_lux")); // Set custom unit.
159+
send(msg.set(lastlux));
160+
Serial.println("Requesting initial value from controller");
161+
request(CHILD_ID, V_LEVEL);
162+
wait(2000, C_SET, V_LEVEL);
163+
}
164+
uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
165+
if (lux != lastlux) {
166+
send(msg.set(lux));
167+
lastlux = lux;
168+
}
169+
170+
sleep(SLEEP_TIME);
171+
}
172+
173+
void receive(const MyMessage &message) {
174+
if (message.type == V_LEVEL) {
175+
if (!initialValueSent) {
176+
Serial.println("Receiving initial value from controller");
177+
initialValueSent = true;
178+
}
179+
}
180+
}
181+
```
182+
119183
[main component]: /components/mysensors/
120184
[serial api]: https://www.mysensors.org/download/serial_api_15

0 commit comments

Comments
 (0)