Reading inputs, MySensor Gateway

Moderator: leecollings

Post Reply
SmartMK11
Posts: 1
Joined: Monday 21 March 2022 7:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Reading inputs, MySensor Gateway

Post by SmartMK11 »

Hello,

Can you help me with reading inputs (like door, window open)?

I try to use mega2560 to read inputs (reed swiches) and set up relays from domoticz.


With this code, i see in domoticz input1 and input2 always on...
Do you know what can be wrong?
My sketch:

Code: Select all


// 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_LOW

// 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 == 12000000L
#define MY_BAUD_RATE 115200
#endif

// Flash leds on rx/tx/err
// #define MY_LEDS_BLINKING_FEATURE
// Set blinking period
// #define MY_DEFAULT_LED_BLINK_PERIOD 300

// Inverses the behavior of leds
// #define MY_WITH_LEDS_BLINKING_INVERSE

// 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 

// 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 <SPI.h>
#include <MySensors.h>  
#include <Bounce2.h>

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE


#define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2  5
#define RELAY_3  6
#define RELAY_4  7
#define RELAY_5  8
#define RELAY_6  9
#define RELAY_7  10
#define RELAY_8  11
#define RELAY_9  12
#define RELAY_10  13
#define RELAY_11  14
#define RELAY_12  15
#define NUMBER_OF_RELAYS  12 // Total number of attached relays                             dodać na próbę wejścia
#define RELAY_ON 1  // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay


#define CHILD_ID_1 20
#define CHILD_ID_2 21

#define INPUT_1  20
#define INPUT_2  21


Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce(); 

int oldValue1 = -1;
int oldValue2 = -1;



void before() { 
  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);   
    // Set relay to last known state (using eeprom storage) 
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  }
}


void setup()  {


  pinMode(INPUT_1,INPUT);
  debouncer1.attach(INPUT_1);
  debouncer1.interval(5);
  digitalWrite(INPUT_1,HIGH);
  

  pinMode(INPUT_2,INPUT);
  debouncer2.attach(INPUT_2);
  debouncer2.interval(5);
  digitalWrite(INPUT_2,HIGH);

}


void presentation()  
{   
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Relay", "1.0");
 present(CHILD_ID_1, S_DOOR);
 present(CHILD_ID_2, S_DOOR);

  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Register all sensors to gw (they will be created as child devices)
    present(sensor, S_LIGHT);



  }
}



MyMessage msg(1, V_LIGHT);
MyMessage msg2(2, V_LIGHT);
MyMessage msg3(3, V_LIGHT);
MyMessage msg4(4, V_LIGHT);
MyMessage msg5(5, V_LIGHT);
MyMessage msg6(6, V_LIGHT);
MyMessage msg7(7, V_LIGHT);
MyMessage msg8(8, V_LIGHT);
MyMessage msg9(9, V_LIGHT);
MyMessage msg10(10, V_LIGHT);
MyMessage msg11(11, V_LIGHT);
MyMessage msg12(12, V_LIGHT);
MyMessage msg13(CHILD_ID_1, V_TRIPPED);
MyMessage msg14(CHILD_ID_2, V_TRIPPED);



void loop() {



  debouncer1.update();
  int value1 = debouncer1.read();
 
  if (value1 != oldValue1) {
     send(msg13.set(value1 == HIGH ? 1 : 0));
     oldValue1 = value1;
  }

       debouncer2.update();
  int value2 = debouncer2.read();
 
  if (value2 != oldValue2) {
     send(msg14.set(value2 == HIGH ? 1 : 0));
     oldValue2 = value2;
  }
}
  


void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LIGHT) {
     // Change relay state
     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
     // Store state in eeprom
     saveState(message.sensor, message.getBool());
     // Write some debug info
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
   } 
   }
Post Reply

Who is online

Users browsing this forum: Sjonnie2017 and 1 guest