Skip to content

Commit dd33485

Browse files
brahmafearMartinHjelmare
authored andcommitted
Update required message types and add v2.0 ex. (home-assistant#1728)
* Update required message types and add v2.0 ex. Changed wording of section listing required message types to make more clear the necessity of sending V_STATUS messages along with V_PERCENTAGE or V_RGB messages. Also, added a MySensors v2.x example sketch which is substantially different from v.1.x. Example sketch taken from MySensors github repo with some comments removed. * Updated example sketch Simplified and style corrected. * V_LIGHT changed to V_STATUS ... Updated to use non-deprecated types. Also updated library link. * Updated wording of type description Updated description of which type messages to send in response to events. This is a hard requirement to fully explain, but hopefully the example sketches will provide guidance.
1 parent f45e982 commit dd33485

File tree

1 file changed

+135
-3
lines changed

1 file changed

+135
-3
lines changed

source/_components/light.mysensors.markdown

Lines changed: 135 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ S_TYPE | V_TYPE
3030
S_DIMMER | [V_DIMMER\* or V_PERCENTAGE\*], [V_LIGHT\* or V_STATUS\*]
3131
S_RGB_LIGHT | V_RGB*, [V_LIGHT\* or V_STATUS\*], [V_DIMMER or V_PERCENTAGE]
3232

33-
V_TYPES with a star (\*) denotes required V_TYPES. Use either V_LIGHT or V_STATUS and either V_DIMMER or V_PERCENTAGE for an applicable actuator.
33+
V_TYPES with a star (\*) denote V_TYPES that should be sent at sketch startup. For an S_DIMMER, send both a V_DIMMER/V_PERCENTAGE and a V_LIGHT/V_STATUS message. For an S_RGB_LIGHT, send both a V_RGB and a V_LIGHT/V_STATUS message with a V_DIMMER/V_PERCENTAGE message being optional. Sketch should acknowledge a command sent from controller with the same type. If command invokes a change to off state (including a V_PERCENTAGE or V_RGB message of zero), only a V_STATUS of zero message should be sent. See sketches below for examples.
3434

3535
For more information, visit the [serial api] of MySensors.
3636

37-
### {% linkable_title Example sketch %}
37+
### {% linkable_title MySensors 1.x example sketch %}
3838

3939
```cpp
4040
/*
@@ -108,6 +108,138 @@ void incomingMessage(const MyMessage &message) {
108108
}
109109
}
110110
```
111+
### {% linkable_title MySensors 2.x example sketch %}
111112
113+
```cpp
114+
/*
115+
* Example Dimmable Light
116+
* Code adapted from http://github.com/mysensors/MySensors/tree/master/examples/DimmableLight
117+
*
118+
* Documentation: http://www.mysensors.org
119+
* Support Forum: http://forum.mysensors.org
120+
*
121+
*/
122+
123+
// Enable debug prints
124+
#define MY_DEBUG
125+
126+
// Enable and select radio type attached
127+
#define MY_RADIO_NRF24
128+
//#define MY_RADIO_RFM69
129+
130+
#include <MySensors.h>
131+
132+
#define CHILD_ID_LIGHT 1
133+
134+
#define LIGHT_OFF 0
135+
#define LIGHT_ON 1
136+
137+
#define SN "Dimmable Light"
138+
#define SV "1.0"
139+
140+
int16_t last_state = LIGHT_ON;
141+
int16_t last_dim = 100;
142+
143+
MyMessage light_msg( CHILD_ID_LIGHT, V_STATUS );
144+
MyMessage dimmer_msg( CHILD_ID_LIGHT, V_PERCENTAGE );
145+
146+
void setup()
147+
{
148+
update_light();
149+
Serial.println( "Node ready to receive messages..." );
150+
}
151+
152+
void loop()
153+
{
154+
//In MySensors2.x, first message must come from within loop()
155+
static bool first_message_sent = false;
156+
if ( first_message_sent == false ) {
157+
Serial.println( "Sending initial state..." );
158+
send_dimmer_message();
159+
send_status_message();
160+
first_message_sent = true;
161+
}
162+
}
163+
164+
void presentation()
165+
{
166+
// Send the sketch version information to the gateway
167+
sendSketchInfo( SN, SV );
168+
present( CHILD_ID_LIGHT, S_DIMMER );
169+
}
170+
171+
void receive(const MyMessage &message)
172+
{
173+
//When receiving a V_STATUS command, switch the light between OFF
174+
//and the last received dimmer value
175+
if ( message.type == V_STATUS ) {
176+
Serial.println( "V_STATUS command received..." );
177+
178+
int lstate = message.getInt();
179+
if (( lstate < 0 ) || ( lstate > 1 )) {
180+
Serial.println( "V_STATUS data invalid (should be 0/1)" );
181+
return;
182+
}
183+
last_state = lstate;
184+
185+
//If last dimmer state is zero, set dimmer to 100
186+
if (( last_state == LIGHT_ON ) && ( last_dim == 0 )) {
187+
last_dim=100;
188+
}
189+
190+
//Update constroller status
191+
send_status_message();
192+
193+
} else if ( message.type == V_PERCENTAGE ) {
194+
Serial.println( "V_PERCENTAGE command received..." );
195+
int dim_value = constrain( message.getInt(), 0, 100 );
196+
if ( dim_value == 0 ) {
197+
last_state = LIGHT_OFF;
198+
199+
//Update constroller with dimmer value & status
200+
send_dimmer_message();
201+
send_status_message();
202+
} else {
203+
last_state = LIGHT_ON;
204+
last_dim = dim_value;
205+
206+
//Update constroller with dimmer value
207+
send_dimmer_message();
208+
}
209+
210+
} else {
211+
Serial.println( "Invalid command received..." );
212+
return;
213+
}
214+
215+
//Here you set the actual light state/level
216+
update_light();
217+
}
218+
219+
void update_light()
220+
{
221+
//For this example, just print the light status to console.
222+
if ( last_state == LIGHT_OFF ) {
223+
Serial.println( "Light state: OFF" );
224+
} else {
225+
Serial.print( "Light state: ON, Level: " );
226+
Serial.println( last_dim );
227+
}
228+
}
229+
230+
void send_dimmer_message()
231+
{
232+
send( dimmer_msg.set( last_dim ) );
233+
}
234+
235+
void send_status_message()
236+
{
237+
if ( last_state == LIGHT_OFF ) {
238+
send( light_msg.set( (int16_t)0) );
239+
} else {
240+
send( light_msg.set( (int16_t)1) );
241+
}
242+
}
243+
```
112244
[main component]: /components/mysensors/
113-
[serial api]: https://www.mysensors.org/download/serial_api_15
245+
[serial api]: http://www.mysensors.org/download

0 commit comments

Comments
 (0)