2 capteur meme information

Forum dédie aux capteurs et gateway mysensors.org
ledouble33
Messages : 286
Inscription : 07 févr. 2015, 07:45

2 capteur meme information

Message par ledouble33 »

bonjour, j ai 2 capteurs mysensor :
1 avec 2 pir,1 relais,1dth11

Code : Tout sélectionner

#include <SPI.h>
#include <MySensor.h>
#include <Wire.h>
#include <DHT.h>
#include <SimpleTimer.h>

#define CHILD_ID_HUM 1
#define CHILD_ID_TEMP 2
#define CHILD_ID_MOTION 3
#define CHILD_ID_MOTION2 5
#define CHILD_ID_RELAY 4

#define HUMIDITY_SENSOR_DIGITAL_PIN 4
#define MOTION_SENSOR_DIGITAL_PIN 3
#define MOTION2_SENSOR_DIGITAL_PIN 5
#define INTERRUPT MOTION_SENSOR_DIGITAL_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)

#define RELAY  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 0  // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay

unsigned long SLEEP_TIME = 600000; // Sleep time between reads (in milliseconds) - 10mins

MySensor gw;
DHT dht;
SimpleTimer timer;

float lastTemp;
float lastHum;
boolean lastTripped;
boolean lastTripped2;
boolean metric = true;

MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msg(CHILD_ID_MOTION, V_TRIPPED);
MyMessage msg2(CHILD_ID_MOTION2, V_TRIPPED);

void setup()  
{ 
  // Initialize library and add callback for incoming messages
  gw.begin(incomingMessage, AUTO, true);
  
  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 

  // Send the Sketch Version Information to the Gateway
  gw.sendSketchInfo("HumTempRelayMotion", "1.0");

  // REGISTER all sensors to gw (they will be created as child devices)
  gw.present(CHILD_ID_HUM, S_HUM);
  gw.present(CHILD_ID_TEMP, S_TEMP);
  gw.present(CHILD_ID_MOTION, S_MOTION);
  gw.present(CHILD_ID_MOTION2, S_MOTION);
  gw.present(CHILD_ID_RELAY, S_LIGHT);
  pinMode(RELAY, OUTPUT);
  digitalWrite(RELAY, gw.loadState(RELAY)?RELAY_OFF:RELAY_ON);
  
  //Serial.begin(9600);
  timer.setInterval(30000, getMeasure);
  metric = gw.getConfig().isMetric;
  
}

void loop()      
{  
  // Alway process incoming messages whenever possible
  gw.process();
  timer.run();
  
  boolean tripped = digitalRead(MOTION_SENSOR_DIGITAL_PIN) == HIGH;    
  if (tripped != lastTripped) {
    lastTripped = tripped;
    Serial.print("M: ");
    Serial.println(tripped);
    gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
  }
  boolean tripped2 = digitalRead(MOTION2_SENSOR_DIGITAL_PIN) == HIGH;   // 
  if (tripped2 != lastTripped2) {
    lastTripped2 = tripped2;
    Serial.print("M2: ");
    Serial.println(tripped2);
    gw.send(msg2.set(tripped2?"1":"0"));  // Send tripped value to gw   //
  }

}

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
     digitalWrite( RELAY, message.getBool()?RELAY_ON:RELAY_OFF);
     // Store state in eeprom
     gw.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());
   } 
}

void getMeasure() {
  delay(dht.getMinimumSamplingPeriod());

  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
      Serial.println("Failed reading temperature from DHT");
  } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    gw.send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
  }
  
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
      Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum) {
      lastHum = humidity;
      gw.send(msgHum.set(humidity, 1));
      Serial.print("H: ");
      Serial.println(humidity);
  }
}
et un autre avec 1 pir,1led,1relais et sonde 1-wire

Code : Tout sélectionner

/**
 * Door, movement and temperature switch.
 *
 * This sensor has three main functions:
 * - A PIR Sensor for sensing movement around my front door. Each detected movement is being reported to the MySensors gateway.
 *   if the NIGHTMODE switch is on, it will also turn on the relay when a movement is being detected. (See design decisions)
 * - A door sensor for monitoring when the door of my little workshop is open or closed. Will use this in the future to trigger
 *   an alarm if the door is opened when we do not want it to be opened. E.g. when I'm a sleep at night.
 * - Since we've got some extra pins that are not in use, we might as well monitor the workshop's temperature.
 *
 * Created: September 26th 2015
 * Version: 1.1
 * Author:  by Theo
 * Website: http://www.houtbewerken-voor-dummies.com/domotica-beweging-in-je-tuin-meten
 *
 * Changelog:
 * 02-10-2015 - V1.1 Added function to leave the relay on for a while after the last detected movement.
 * 26-09-2015 - V1.0 initial version
 *
 * Design decision(s):
 * - Use a Dalas DS18b20 as a temperature sensor. It's much faster than a DHT11 or DHT22. We don't need a super accurate sensor, we're not
 *   going to control anything based upon the temperature in the Workshop.
 * - Add a dummy Light node for letting the PIR sensor controlling a relay. The switch needs to be turned on and off by the domotica controller
 *   when it's dark or not. In an earlier version I controlled the relay from my Domotica Controller, but I had a delay of about 1 second before the
 *   relay was switched on. Which was unacceptable to me. It would have been nice to read the delay value from Domoticz, but Domoticz doesn't allow  user variables for MySensors nodes. Vera does, but I don't have Vera so I can't test and develop it.
 */

// Import the necessary libraries
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#include <DallasTemperature.h>
#include <OneWire.h>

// define constants. #define is more memory friendly than using static variables.
#define TEMP3_CHILD_ID 20
#define MOTION_CHILD_ID 18
#define SUNSET_CHILD_ID 2
#define DOOR_CHILD_ID 3
#define DOORSENSOR_PIN  4  // Arduino input pin for button/reed switch
#define MOTION6SENSOR_PIN 6 // Arduino input pin for PIR motion sensor
#define MOTIONLED_PIN 5    // Arduino output pun for visualization of detecting motion
#define RELAY_PIN 7    // Arduino output pun for visualization of detecting motion
#define ONE_WIRE_BUS 3     // Pin where dallas Ds18b20 sensor is connected
#define TEMPERATURE_READING_INTERVAL 600 // temperature reading interval in ms (min * 60000) in production we'll use 10 minutes
#define LIGHTOFFDELAY 20000 // The amount of milliseconds the relay will be switched off after the last movement has been deteced (We'll use 20 seconds). Vera users can use a Vera variable for this. But I can not test this with Domoticz.

// define sketch name and version
#define SN "1-wire_1door_1MotionDetection"
#define SV "1.1"

// Declare and initialize objects.
MySensor gw;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature dt_sensor(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
Bounce doorDebouncer = Bounce();   // declare a software debounce for debouncing the door reed relay switch 
Bounce motionDebouncer = Bounce(); // declare a software debounce for debouncing the PIR motion sensor

// declare and initialize global variables. Used to store old sensor and interval readings.
int oldDoorSensorValue = -1;    // remember the door sensor's current state for detecting changes
int oldMotionSensorValue = -1;  // remember the PIR motion sensor's current state for detecting changes
float lastTemperature = 0.0;    // remember the temperature for detecting changes
bool sunsetActive = false;      // state for indicating whether the relay has to be controllered by motion (domotica controller should set this to on after sunset and off after sunrise
unsigned long previousMillis = 0; // interval variable for temperature sensor reading
unsigned long scheduledRelayOffMillis = 0; // Scheduler for determining when the relay has to be switched off

// Declare messages that will be sent to the MySensor's gateway.
MyMessage doorMsg( DOOR_CHILD_ID, V_TRIPPED );
MyMessage tempMsg( TEMP3_CHILD_ID, V_TEMP );
MyMessage motionMsg( MOTION_CHILD_ID, V_TRIPPED );

// Initialization code for the sketch
void setup() {  
//  Serial.begin( 115200 ); // uncomment during debugging.

  // setup a connection to the MySensor's gateway and request an ID if we do not have one yet.
  gw.begin(incomingMessage);
  gw.sendSketchInfo(SN, SV);
  
  // Initialize Dalles temp sensor
  dt_sensor.begin();
  dt_sensor.setWaitForConversion( false );
  
  // initialize the door sensor so that the arduino can read it's value. And use internal 
  // pullup
  pinMode( DOORSENSOR_PIN, INPUT_PULLUP );
  // setup debouncer
  doorDebouncer.attach( DOORSENSOR_PIN );
  doorDebouncer.interval( 5 );
  
  // initialize PIR motion sensor
  pinMode( MOTIONSENSOR_PIN, INPUT_PULLUP );
  // setup debouncer
  motionDebouncer.attach( MOTIONSENSOR_PIN );
  motionDebouncer.interval( 5 );
  
  // initialize PIR visualization LED
  pinMode( MOTIONLED_PIN, OUTPUT );
  
  // initialize RELAY
  pinMode( RELAY_PIN, OUTPUT );
 //  Serial.println( "Testing relay" );
  // Testing relay just to let to knew everything is allright.. Used to test turnRelayOn and turnRelayOff method development. I liked it and kept it.
  for ( int i=0; i <3; i++ ) {
    turnOnRelay();
    gw.wait( 2000 );
// delay(2000);
    turnOffRelay();
    gw.wait( 2000 );
//delay(2000);
  }
   
  // Register binary input sensors to gw (they will be created as child devices)
  gw.present( DOOR_CHILD_ID, S_DOOR);
  gw.present( MOTION_CHILD_ID, S_MOTION );
  gw.present( TEMP3_CHILD_ID, S_TEMP  );
  gw.present( SUNSET_CHILD_ID, S_LIGHT );
  gw.sendBatteryLevel( 100, false ); // Let the Domotica controller no that we're a 100%
                                                               // powered sensor.
  gw.request( SUNSET_CHILD_ID, V_LIGHT ); // request current sunset state from
                                                             // controller
  
  // check temperature. Next reading will occur after first interval has been reached. 
  // so check the temperature as an initial reading
  checkTemperature();
}

/**
 * Turn off the relay to which a light will be connected
 */
void turnOnRelay() {
  digitalWrite( RELAY_PIN, 0 ); // Relay needs inverted input. HIGH meaning off and LOW meaning ON
}

/**
 * Turn on the relay to which a light will be connected
 */
void turnOffRelay() {
  digitalWrite( RELAY_PIN, 1 ); // Relay needs inverted input. HIGH meaning off and LOW meaning ON
}

/**
 * Check current temperature and send changes to the MySensors gateway
 */
void checkTemperature() {
//  Serial.println( "Checking temp" );
  // Fetch and round temperature to one decimal
  // Fetch temperatures from Dallas sensors
  dt_sensor.requestTemperatures();
  // query conversion time and sleep until conversion completed
  int16_t conversionTime = dt_sensor.millisToWaitForConversion( dt_sensor.getResolution() );
  // use gw.wait instead of Arduino's delay. That way the gw.process() is still working in the background
  // and we'll be able te receive messages from the Domotica controller.
  gw.wait(conversionTime);
// delay(conversionTime);
  float temperature = static_cast<float>(static_cast<int>((dt_sensor.getTempCByIndex(0)) * 10.)) / 10.;
  if ( temperature < 85.0 && temperature > -127.0 && temperature != lastTemperature ) {
    lastTemperature = temperature;
    gw.send( tempMsg.set( temperature, 1 ) );
// Serial.println( temperature );
  }
}

/**
 * Check the door sensor for state changes and send the state to the MySensor gateway of the state has been changed
 */
void checkDoorSensor() {
  doorDebouncer.update();
 
  // Get the update value
  int value = doorDebouncer.read();
 
  if (value != oldDoorSensorValue ) {
     // Notify the gateway of the new door status
     gw.send( doorMsg.set( value==HIGH ? 1 : 0 ) );
// Serial.println( (String)value + " door state " );
     oldDoorSensorValue = value;
  }
}

/**
 * Check the motion sensor for state changes and send the state to the MySensor gateway of the state has been changed
 */
void checkMotionSensor() {
  motionDebouncer.update();
 
  // Get the update value
  int value = motionDebouncer.read();
  if (value != oldMotionSensorValue ) {
     // Notify the gateway of the new door status
     gw.send( motionMsg.set( value==HIGH ? 1 : 0 ) );
// Serial.println( (String)value + " motion state" );
     oldMotionSensorValue = value;
     digitalWrite( MOTIONLED_PIN, value );
     if ( sunsetActive == true ) { // check whether the active motion mode is set on to turn on relay and schedule for next relay off time
       // check whether motion has been detected
       if ( value ==   1 ) {
         digitalWrite( RELAY_PIN, LOW ); // activate relay. But only when the OUTDOORLIGHT mode is active.
         // calculate next off interval if motion has been detected
         scheduledRelayOffMillis = millis() + LIGHTOFFDELAY;
       }
     }
  }
}

/**
 * Main loop.
 */
void loop() {
  // check if the temperature needs to be read.
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= TEMPERATURE_READING_INTERVAL ) {
    previousMillis = currentMillis;
    checkTemperature();
  }
  checkDoorSensor();
  checkMotionSensor();
  // check if the relay has to be turned off. Which will be LIGHTOFFDELAY milliseconds after last detected movement.
  currentMillis = millis();
  if ( digitalRead( RELAY_PIN ) == LOW && currentMillis >= scheduledRelayOffMillis ) {
    turnOffRelay();
  }
  gw.process(); // No delaying this needs to be a real-time sensor. At least for the door and motion sensor.
} 

/**
 * Call back handler, for handling messages send by the MySensors gateway
 */
void incomingMessage(const MyMessage &message) {
  if ( message.type == V_LIGHT ) {
    // check if the SUNSET mode's state changed
    if ( message.sensor == SUNSET_CHILD_ID ) {
      sunsetActive = message.getBool();
      // check if light needs to be turned on or off.
      bool relayOn = ( digitalRead( RELAY_PIN ) == LOW );
//       Serial.println( (String)relayOn + " relais status" );
      if ( sunsetActive == true && relayOn == false ) {
         turnOnRelay();
      }
      else if ( sunsetActive == false && relayOn == true ) {
         turnOffRelay();
      }
    }
  }
}
le probleme c est que je recois sur domoticz les memes info
les sondes 1-wire m indique biens la temperature mais l humidité aussi ????
les capteurs de mouvement du premier script s'affiche sur domoticz comme si c etais le deuxieme script

je suppose qu il y a une modif a faire ci dessous mais je comprend pas trop

// Declare messages that will be sent to the MySensor's gateway.
MyMessage doorMsg( DOOR_CHILD_ID, V_TRIPPED );
MyMessage tempMsg( TEMP3_CHILD_ID, V_TEMP );
MyMessage motionMsg( MOTION_CHILD_ID, V_TRIPPED );

// Register binary input sensors to gw (they will be created as child devices)
gw.present( DOOR_CHILD_ID, S_DOOR);
gw.present( MOTION6_CHILD_ID, S_MOTION );
gw.present( TEMP3_CHILD_ID, S_TEMP );
gw.present( SUNSET_CHILD_ID, S_LIGHT );

si quelqu un pourrai m expliqué ces quelques ligne ?
fets
Messages : 22
Inscription : 27 oct. 2015, 14:30

Re: 2 capteur meme information

Message par fets »

Il faudrait voir les logs de domoticz et ceux des sensors pour essayer de comprendre
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: 2 capteur meme information

Message par vil1driver »

également connaitres les version de domoticz et mysensors que tu utilises,

voir également le tableaux des nodes..
voir si domoticz te fournis bien des id différents.. au pire les fixer manuellement
peut être effectué un clear eprom sur les nodes..

et oui +1 pour les logs... toujours les logs..
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
ledouble33
Messages : 286
Inscription : 07 févr. 2015, 07:45

Re: 2 capteur meme information

Message par ledouble33 »

j utilise my sensor 1.5 et domoticz v2.3533
vil1driver a écrit : voir également le tableaux des nodes..
voir si domoticz te fournis bien des id différents.. au pire les fixer manuellement
peut être effectué un clear eprom sur les nodes..
tableaux des nodes =dans les dispositis?
comment tu fais pour les fixer manuellement et pour faire un clear eprom sur les nodes.. ?

pour les logs
c est du chinois
Capture.JPG
Capture.JPG (30.77 Kio) Consulté 12840 fois
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: 2 capteur meme information

Message par vil1driver »

Tableau des nodes c'est dans matériel mais je ne sais plus en quelle version de domoticz cela a été ajouté.

Pour fixer en manuel a la place de auto dans le gw.begin

Pour les logs c'est étrange...

Pour le clear eprom c'est un sketch special a charger et à faire tourner quelques secondes (fournis dans les exemples)
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
ledouble33
Messages : 286
Inscription : 07 févr. 2015, 07:45

Re: 2 capteur meme information

Message par ledouble33 »

je pense que ca fonctionne
j ai fais un clear eprom ,j ai plus l humidité avec mes sonde ds18b20
encore merci
fets
Messages : 22
Inscription : 27 oct. 2015, 14:30

Re: 2 capteur meme information

Message par fets »

Pour les logs arduino, il faut changer le baudrate (en bas à droite de la fenêtre log) et passer de 9600 à 115200.
ledouble33
Messages : 286
Inscription : 07 févr. 2015, 07:45

Re: 2 capteur meme information

Message par ledouble33 »

bonjour , j ai trouver une belle boite pour mon arduino nano
sonde 1-wire (dans la dent) et le pir
20160518_181702_resized.jpg
20160518_181702_resized.jpg (88.67 Kio) Consulté 12389 fois

vue de dos
Pièces jointes
20160518_181730_resized.jpg
20160518_181730_resized.jpg (103.01 Kio) Consulté 12389 fois
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: 2 capteur meme information

Message par vil1driver »

super fun, j’adore :mrgreen:
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
ledouble33
Messages : 286
Inscription : 07 févr. 2015, 07:45

Re: 2 capteur meme information

Message par ledouble33 »

les cadeaux mcdo finisse toujours a la poubelle, j ai fais un geste pour la planete ;)
Dernière modification par ledouble33 le 18 mai 2016, 19:43, modifié 1 fois.
Répondre