SHARE
TWEET

Untitled

a guest Dec 7th, 2017 186 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Enable debug prints to serial monitor
  2. #define MY_DEBUG
  3.  
  4.  
  5. // Enable and select radio type attached
  6. //#define MY_RADIO_NRF24
  7. //#define MY_RADIO_RFM69
  8.  
  9. // Set LOW transmit power level as default, if you have an amplified NRF-module and
  10. // power your radio separately with a good regulator you can turn up PA level.
  11. //#define MY_RF24_PA_LEVEL RF24_PA_LOW
  12.  
  13. // Enable serial gateway
  14. #define MY_GATEWAY_SERIAL
  15.  
  16. // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
  17. #if F_CPU == 8000000L
  18. #define MY_BAUD_RATE 38400
  19. #endif
  20.  
  21. // Flash leds on rx/tx/err
  22. // #define MY_LEDS_BLINKING_FEATURE
  23. // Set blinking period
  24. // #define MY_DEFAULT_LED_BLINK_PERIOD 300
  25.  
  26. // Inverses the behavior of leds
  27. // #define MY_WITH_LEDS_BLINKING_INVERSE
  28.  
  29. // Enable inclusion mode
  30. #define MY_INCLUSION_MODE_FEATURE
  31. // Enable Inclusion mode button on gateway
  32. #define MY_INCLUSION_BUTTON_FEATURE
  33.  
  34. // Inverses behavior of inclusion button (if using external pullup)
  35. //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
  36.  
  37. // Set inclusion mode duration (in seconds)
  38. #define MY_INCLUSION_MODE_DURATION 60
  39. // Digital pin used for inclusion mode button
  40. #define MY_INCLUSION_MODE_BUTTON_PIN  3
  41.  
  42. // Uncomment to override default HW configurations
  43. //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
  44. //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
  45. //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
  46.  
  47. #include <SPI.h>
  48. #include <MySensors.h>  
  49. #include <Bounce2.h>
  50.  
  51. // Enable repeater functionality for this node
  52. #define MY_REPEATER_FEATURE
  53.  
  54.  
  55. #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
  56. #define RELAY_2  5
  57. #define NUMBER_OF_RELAYS 2 // Total number of attached relays
  58. #define RELAY_ON 1  // GPIO value to write to turn on attached relay
  59. #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
  60.  
  61. #define BUTTON_PIN A1
  62. #define BUTTON2_PIN A2
  63.  
  64.  
  65. void before() {
  66.   for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
  67.     // Then set relay pins in output mode
  68.     pinMode(pin, OUTPUT);  
  69.     // Set relay to last known state (using eeprom storage)
  70.     digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  71.   }
  72. }
  73. Bounce debouncer = Bounce();
  74. Bounce debouncer2 = Bounce();
  75.  
  76. void setup() {
  77.   // Setup locally attached sensors
  78.   delay(10000);
  79.    // Setup the button.
  80.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  81.   pinMode(BUTTON2_PIN, INPUT_PULLUP);
  82.   // After setting up the button, setup debouncer.
  83.   debouncer.attach(BUTTON_PIN);
  84.   debouncer.interval(5);
  85.   debouncer2.attach(BUTTON2_PIN);
  86.   debouncer2.interval(5);
  87.  
  88.   //presentation();
  89. }
  90. void presentation()  
  91. {  
  92.   // Send the sketch version information to the gateway and Controller
  93.   sendSketchInfo("Relay", "1.0");
  94.  
  95.   for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
  96.     // Register all sensors to gw (they will be created as child devices)
  97.     present(sensor, S_LIGHT);
  98.   }
  99. }
  100.  
  101. MyMessage msg(1, V_LIGHT);
  102. MyMessage msg2(2, V_LIGHT);
  103.  
  104. void loop() {
  105.   // Send locally attached sensor data here
  106.   if (debouncer.update()) {
  107.     // Get the update value.
  108.     int value = debouncer.read();
  109.     // Send in the new value.
  110.     if(value == LOW){
  111.          saveState(1, !loadState(1));
  112.          digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
  113.          send(msg.set(loadState(1)));
  114.          }
  115.   }
  116.   if (debouncer2.update()) {
  117.       int value2 = debouncer2.read();
  118.     if(value2 == LOW){
  119.          saveState(2, !loadState(2));
  120.          digitalWrite(RELAY_2, loadState(2)?RELAY_ON:RELAY_OFF);
  121.          send(msg2.set(loadState(2)));
  122.          }
  123.   }
  124. }
  125.  
  126.  
  127. void receive(const MyMessage &message) {
  128.   // We only expect one type of message from controller. But we better check anyway.
  129.   if (message.type==V_LIGHT) {
  130.      // Change relay state
  131.      digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
  132.      // Store state in eeprom
  133.      saveState(message.sensor, message.getBool());
  134.      // Write some debug info
  135.      Serial.print("Incoming change for sensor:");
  136.      Serial.print(message.sensor);
  137.      Serial.print(", New status: ");
  138.      Serial.println(message.getBool());
  139.    }
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top