SHARE
TWEET

Untitled

a guest Dec 24th, 2015 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * The MySensors Arduino library handles the wireless radio link and protocol
  3.  * between your home built sensors/actuators and HA controller of choice.
  4.  * The sensors forms a self healing radio network with optional repeaters. Each
  5.  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6.  * network topology allowing messages to be routed to nodes.
  7.  *
  8.  * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9.  * Copyright (C) 2013-2015 Sensnology AB
  10.  * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
  11.  *
  12.  * Documentation: http://www.mysensors.org
  13.  * Support Forum: http://forum.mysensors.org
  14.  *
  15.  * This program is free software; you can redistribute it and/or
  16.  * modify it under the terms of the GNU General Public License
  17.  * version 2 as published by the Free Software Foundation.
  18.  *
  19.  *******************************
  20.  *
  21.  * REVISION HISTORY
  22.  * Version 1.0 - Henrik Ekblad
  23.  *
  24.  * DESCRIPTION
  25.  * Example sketch for a "light switch" where you can control light or something
  26.  * else from both HA controller and a local physical button
  27.  * (connected between digital pin 3 and GND).
  28.  * This node also works as a repeader for other nodes
  29.  * http://www.mysensors.org/build/relay
  30.  */
  31.  
  32. // Enable debug prints to serial monitor
  33. #define MY_DEBUG
  34.  
  35. // Enable and select radio type attached
  36. //#define MY_RADIO_NRF24 // Disable to switch into USB GW without radio
  37. //#define MY_RADIO_RFM69
  38.  
  39. // Enabled repeater feature for this node
  40. #define MY_REPEATER_FEATURE
  41.  
  42. // Enable serial gateway
  43. #define MY_GATEWAY_SERIAL
  44.  
  45. #include <SPI.h>
  46. #include <MySensor.h>
  47. #include <Bounce2.h>
  48.  
  49. #define RELAY_PIN  13  // Arduino Digital I/O pin number for relay  // for optical feedback
  50. #define BUTTON_PIN  3  // Arduino Digital I/O pin number for button
  51. #define CHILD_ID 1   // Id of the sensor child
  52. #define RELAY_ON 1
  53. #define RELAY_OFF 0
  54.  
  55. Bounce debouncer = Bounce();
  56. int oldValue=0;
  57. bool state;
  58.  
  59. MyMessage msg(CHILD_ID,V_LIGHT);
  60.  
  61. void setup()  
  62. {  
  63.   // Setup the button
  64.   pinMode(BUTTON_PIN,INPUT);
  65.   // Activate internal pull-up
  66.   digitalWrite(BUTTON_PIN,HIGH);
  67.  
  68.   // After setting up the button, setup debouncer
  69.   debouncer.attach(BUTTON_PIN);
  70.   debouncer.interval(5);
  71.  
  72.   // Make sure relays are off when starting up
  73.   digitalWrite(RELAY_PIN, RELAY_OFF);
  74.   // Then set relay pins in output mode
  75.   pinMode(RELAY_PIN, OUTPUT);  
  76.      
  77.   // Set relay to last known state (using eeprom storage)
  78.   state = loadState(CHILD_ID);
  79.   digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
  80. }
  81.  
  82. void presentation()  {
  83.   // Send the sketch version information to the gateway and Controller
  84.   sendSketchInfo("Relay & Button", "1.1");
  85.  
  86.   // Register all sensors to gw (they will be created as child devices)
  87.   present(CHILD_ID, S_LIGHT);
  88. }
  89.  
  90. /*
  91. *  Example on how to asynchronously check for new messages from gw
  92. */
  93. void loop()
  94. {
  95.   debouncer.update();
  96.   // Get the update value
  97.   int value = debouncer.read();
  98.   if (value != oldValue && value==0) {
  99.       send(msg.set(state?false:true), true); // Send new state and request ack back
  100.   }
  101.   oldValue = value;
  102. }
  103.  
  104. void receive(const MyMessage &message) {
  105.   // We only expect one type of message from controller. But we better check anyway.
  106.   if (message.isAck()) {
  107.      Serial.println("This is an ack from gateway");
  108.   }
  109.  
  110.   if (message.type == V_LIGHT) {
  111.      // Change relay state
  112.      state = message.getBool();
  113.      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
  114.      // Store state in eeprom
  115.      saveState(CHILD_ID, state);
  116.    
  117.      // Write some debug info
  118.      Serial.print("Incoming change for sensor:");
  119.      Serial.print(message.sensor);
  120.      Serial.print(", New status: ");
  121.      Serial.println(message.getBool());
  122.    }
  123. }
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