gateway serial and dimmer example
-
after hours (days???) of fighting against the machine (rage?), a waf dangerously low, i got a solution... it's not the most acceptable for me, BUT IT WORKS so be it! i just change the sensor ID from 1 to 100, i'm sure that no message could be send or transfert or whatever exepted the one i chose, and trust me it's good to feel the power of the light!! i hope that some one could explain how to understand every details of Mymessage.h , anyway now this is the code...```
/**- The MySensors Arduino library handles the wireless radio link and protocol
- between your home built sensors/actuators and HA controller of choice.
- The sensors forms a self healing radio network with optional repeaters. Each
- repeater and gateway builds a routing tables in EEPROM which keeps track of the
- network topology allowing messages to be routed to nodes.
- Created by Henrik Ekblad henrik.ekblad@mysensors.org
- Copyright (C) 2013-2015 Sensnology AB
- Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
- Documentation: http://www.mysensors.org
- Support Forum: http://forum.mysensors.org
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- version 2 as published by the Free Software Foundation.
- DESCRIPTION
- The ArduinoGateway prints data received from sensors on the serial link.
- The gateway accepts input on seral which will be sent out on radio network.
- The GW code is designed for Arduino Nano 328p / 16MHz
- Wire connections (OPTIONAL):
-
- Inclusion button should be connected between digital pin 3 and GND
-
- RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
- LEDs (OPTIONAL):
-
- To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
-
- RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
-
- TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
-
- ERR (red) - fast blink on error during transmission error or recieve crc error
*/
// Enable debug prints to serial monitor
#define MY_DEBUG// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level.
#define MY_RF24_PA_LEVEL RF24_PA_MAX// Enable serial gateway
#define MY_GATEWAY_SERIAL// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
//#define MY_INCLUSION_MODE_BUTTON_PIN 3// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300// Inverses the behavior of leds
//#define MY_WITH_LEDS_BLINKING_INVERSE// Flash leds on rx/tx/err
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
//#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED#include <MySensors.h>
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)static int16_t currentLevel = 0; // Current dim level...
MyMessage dimmerMsg(100, V_DIMMER);
MyMessage lightMsg(100, V_LIGHT);void setup()
{
// Setup locally attached sensors
request( 100, V_DIMMER );
}void presentation()
{
// Present locally attached sensors
// Register the LED Dimmable Light with the gateway
present( 100, S_DIMMER );
}void loop()
{
// Send locally attached sensor data here
}
void receive(const MyMessage &message)
{
if (message.sensor == 100 ){if (message.type == V_LIGHT || message.type == V_DIMMER) {
// Retrieve the power or dim level from the incoming request message int requestedLevel = atoi( message.data ); // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on] requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 ); // Clip incoming level to valid range of 0 to 100 requestedLevel = requestedLevel > 100 ? 100 : requestedLevel; requestedLevel = requestedLevel < 0 ? 0 : requestedLevel; Serial.print( "Changing level to " ); Serial.print( requestedLevel ); Serial.print( ", from " ); Serial.println( currentLevel ); fadeToLevel( requestedLevel ); // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value... send(lightMsg.set(currentLevel > 0)); // hek comment: Is this really nessesary? send( dimmerMsg.set(currentLevel) );}
}}
/***
- This method provides a graceful fade up/down effect
*/
void fadeToLevel( int toLevel )
{
int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
while ( currentLevel != toLevel ) {
currentLevel += delta;
analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
delay( FADE_DELAY );
}
}