relais avec bouton (modification)

Forum dédie aux capteurs et gateway mysensors.org
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

relais avec bouton (modification)

Message par vil1driver »

Salut,

me suis mis à créer mes propres prises commandées à base de mysensors,
avec un petit bouton sur la prise pour faire de la commande manuelle..

un sketch existe pour cela cool, on va gagner du temp :D

mais voila je n'aime pas du tout son fonctionnement car pour moi l'idée d'avoir un bouton c'est surtout pour avoir une commande de secours,
du moins ne pas avoir besoin d'un logiciel de contrôle domotique pour fonctionner..
mais la personne qui à écrit ce sketch ne le voyait pas ainsi, son sketch fonctionne comme cela:
l'appui sur le bouton envoi l'info à domoticz,
domoticz en retour, signal à notre prise qu'il a bien reçu l'info,
la prise change d'état.

c'est sympa mais si le signal passe mal ou pas du tout (domoticz HS) c'est la prise de tête.. la prise ne changera pas d'état :evil:

alors voici la version modifiée (le bouton agit directement sur le relais, domoticz est prévenu ou pas :D )

Code : Tout sélectionner

/**
 * 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.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0 - Henrik Ekblad
 * Version 1.1 - vil1driver
 * 
 * DESCRIPTION
 * Example sketch for a "light switch" where you can control light or something 
 * else from both HA controller and a local physical button 
 * (connected between digital pin 3 and GND).
 * This node also works as a repeader for other nodes
 * http://www.mysensors.org/build/relay
 */ 

#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>

#define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN  3  // Arduino Digital I/O pin number for button 
#define CHILD_ID 1   // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce(); 
int oldValue=0;
bool state;
MySensor gw;
MyMessage msg(CHILD_ID,V_LIGHT);

void setup()  
{  
  gw.begin(incomingMessage, AUTO, true);

  // Send the sketch version information to the gateway and Controller
  gw.sendSketchInfo("Relay & Button", "1.1");

 // Setup the button
  pinMode(BUTTON_PIN,INPUT);
  // Activate internal pull-up
  digitalWrite(BUTTON_PIN,HIGH);
  
  // After setting up the button, setup debouncer
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);

  // Register all sensors to gw (they will be created as child devices)
  gw.present(CHILD_ID, S_LIGHT);

  // Make sure relays are off when starting up
  digitalWrite(RELAY_PIN, RELAY_OFF);
  // Then set relay pins in output mode
  pinMode(RELAY_PIN, OUTPUT);   
      
  // Set relay to last known state (using eeprom storage) 
  state = gw.loadState(CHILD_ID);
  digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
}


/*
*  Example on how to asynchronously check for new messages from gw
*/
void loop() 
{
  gw.process();
  debouncer.update();
  // Get the update value
  int value = debouncer.read();
  if (value != oldValue && value==0) {
    // inverse state
    state = state?false:true;
    // Change relay state
    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    gw.saveState(CHILD_ID, state);
    // Send new state
    gw.send(msg.set(state), false); 
    }
  oldValue = value;
} 
 
void incomingMessage(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
     state = message.getBool();
     digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
     // Store state in eeprom
     gw.saveState(CHILD_ID, state);
    
     // Write some debug info
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
   } 
}

petite précision, il faut un bouton à impulsion, sans maintient.

ps: il a tout de même été intéressant d'apprendre que l'accusé de réception contient le message complet...
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
Grincheux

Re: relais avec bouton (modification)

Message par Grincheux »

Cela à le mérite de m’éclaircir les idées.
Je me bats sur le même problème mais dans mon "if" du loop avec value qui ne repasse à 1 qu'après 2 tours , j'ai jamais compris.
Du coup j'ai décidé de passer temporairement par le modèle avec gw.send
le state = state?false:true; , est peut être la solution, je testerais.
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: relais avec bouton (modification)

Message par vil1driver »

A mon avis ton problème est ailleurs, une mauvaise déclaration des boutons/debouncer..?

Code : Tout sélectionner

state = state?false:true;
Est juste une autre façon de faire

Code : Tout sélectionner

if (state == 1){
state = 0;
}else{
state = 1;
}
Tout comme ceci

Code : Tout sélectionner

digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
Est juste une autre façon de faire

Code : Tout sélectionner

if(state == 1){
digitalWrite(RELAY_PIN, RELAY_ON);
}else{
digitalWrite(RELAY_PIN, RELAY_OFF);
} 
On gagne quelques précieux octets..
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
Répondre