Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Detect Interrupt / Timer

Detect Interrupt / Timer

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 2 Posters 923 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave2526
    wrote on last edited by
    #1

    Hello,
    maybe a noob Question but i can´t find an answer:
    I want to combine the Motion and the DS18B20 program. So The Temperature is sending every xx minutes, and the Motion Sensor is sending imedently. So I have to detect if the sleep is ending by the Time or The Interrupt. Is there a simple way to detect? The Code is dirty combined, i Know. I will make it better if its possible. Thanks!

    /**
     * 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.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    #define MY_RADIO_RFM69
    //#define MY_IS_RFM69HW
    
    #define MY_NODE_ID      200
    // HW hinten dran = #define MY_IS_RFM69HW wird benötigt
    // BWM WZ     101
    // Test       200
    
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    
    #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define INTERRUPT BUTTON_PIN-2
    
    int BATTERY_SENSE_PIN = A0;
    unsigned long SLEEP_TIME = 600000;
    //12Std = 43200000
    
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    bool metric = true;
    // Initialize temperature message
    
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(0,V_TEMP);
    MyMessage msg1(3,V_TRIPPED);
    
    void before()
    {
      // Startup up the OneWire library
      sensors.begin();
    }
    
    void setup()  
    {  
      // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-down
      digitalWrite(BUTTON_PIN,LOW);
    
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
      analogReference(INTERNAL);
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
    
      analogReference(INTERNAL);
    
    }
    
    void presentation() {
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      sendSketchInfo("BWM Temp", "1.0");
      present(3, S_DOOR);  
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
    }
    }
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
    
      // BWM
      int value = digitalRead(BUTTON_PIN);
      send(msg1.set(value==HIGH));
    
    
      
      // Temp
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
      // query conversion time and sleep until conversion completed
      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
    
        // Only send data if temperature has changed and no error
        #if COMPARE_TEMP == 1
        if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
    
          // Send in the new temperature
          send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
          Serial.print(temperature);
        }
      }
    
          // get the battery Voltage
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
    #ifdef MY_DEBUG
        Serial.println(sensorValue);
        Serial.println(digitalRead(BUTTON_PIN));
    #endif
    
        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
        // 3.44/1023 = Volts per bit = 0.003363075
    
        int batteryPcnt = sensorValue *0.9375 -779.375;
        // Raum       Gain             Offset
        // Terrasse   0.9375          -779.375
    
    #ifdef MY_DEBUG
        float batteryV  = sensorValue * 0.003363075;
        Serial.print("Battery Voltage: ");
        Serial.print(batteryV);
        Serial.println(" V");
    
        Serial.print("Battery percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
    #endif
    
        sendBatteryLevel(batteryPcnt);
    
       sleep(INTERRUPT,CHANGE,SLEEP_TIME);
    }
    
    mfalkviddM 1 Reply Last reply
    0
    • D Dave2526

      Hello,
      maybe a noob Question but i can´t find an answer:
      I want to combine the Motion and the DS18B20 program. So The Temperature is sending every xx minutes, and the Motion Sensor is sending imedently. So I have to detect if the sleep is ending by the Time or The Interrupt. Is there a simple way to detect? The Code is dirty combined, i Know. I will make it better if its possible. Thanks!

      /**
       * 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.
       *
       *******************************
       *
       * DESCRIPTION
       *
       * Simple binary switch example 
       * Connect button or door/window reed switch between 
       * digitial I/O pin 3 (BUTTON_PIN below) and GND.
       * http://www.mysensors.org/build/binary
       */
      
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      //#define MY_RADIO_NRF24
      #define MY_RADIO_RFM69
      //#define MY_IS_RFM69HW
      
      #define MY_NODE_ID      200
      // HW hinten dran = #define MY_IS_RFM69HW wird benötigt
      // BWM WZ     101
      // Test       200
      
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <DallasTemperature.h>
      #include <OneWire.h>
      
      #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
      
      #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected 
      #define MAX_ATTACHED_DS18B20 16
      
      #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
      #define INTERRUPT BUTTON_PIN-2
      
      int BATTERY_SENSE_PIN = A0;
      unsigned long SLEEP_TIME = 600000;
      //12Std = 43200000
      
      OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
      DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
      float lastTemperature[MAX_ATTACHED_DS18B20];
      int numSensors=0;
      bool receivedConfig = false;
      bool metric = true;
      // Initialize temperature message
      
      int oldValue=-1;
      
      // Change to V_LIGHT if you use S_LIGHT in presentation below
      MyMessage msg(0,V_TEMP);
      MyMessage msg1(3,V_TRIPPED);
      
      void before()
      {
        // Startup up the OneWire library
        sensors.begin();
      }
      
      void setup()  
      {  
        // Setup the button
        pinMode(BUTTON_PIN,INPUT);
        // Activate internal pull-down
        digitalWrite(BUTTON_PIN,LOW);
      
        // requestTemperatures() will not block current thread
        sensors.setWaitForConversion(false);
        analogReference(INTERNAL);
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
      
        analogReference(INTERNAL);
      
      }
      
      void presentation() {
        // Register binary input sensor to gw (they will be created as child devices)
        // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
        // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
        sendSketchInfo("BWM Temp", "1.0");
        present(3, S_DOOR);  
      
        // Fetch the number of attached temperature sensors  
        numSensors = sensors.getDeviceCount();
      
        // Present all sensors to controller
        for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
           present(i, S_TEMP);
      }
      }
      
      //  Check if digital input has changed and send in new value
      void loop() 
      {
      
        // BWM
        int value = digitalRead(BUTTON_PIN);
        send(msg1.set(value==HIGH));
      
      
        
        // Temp
        // Fetch temperatures from Dallas sensors
        sensors.requestTemperatures();
      
        // query conversion time and sleep until conversion completed
        int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
        // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
        sleep(conversionTime);
      
        // Read temperatures and send them to controller 
        for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
      
          // Fetch and round temperature to one decimal
          float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
      
          // Only send data if temperature has changed and no error
          #if COMPARE_TEMP == 1
          if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
          #else
          if (temperature != -127.00 && temperature != 85.00) {
          #endif
      
            // Send in the new temperature
            send(msg.setSensor(i).set(temperature,1));
            // Save new temperatures for next compare
            lastTemperature[i]=temperature;
            Serial.print(temperature);
          }
        }
      
            // get the battery Voltage
          int sensorValue = analogRead(BATTERY_SENSE_PIN);
      #ifdef MY_DEBUG
          Serial.println(sensorValue);
          Serial.println(digitalRead(BUTTON_PIN));
      #endif
      
          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
          // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
          // 3.44/1023 = Volts per bit = 0.003363075
      
          int batteryPcnt = sensorValue *0.9375 -779.375;
          // Raum       Gain             Offset
          // Terrasse   0.9375          -779.375
      
      #ifdef MY_DEBUG
          float batteryV  = sensorValue * 0.003363075;
          Serial.print("Battery Voltage: ");
          Serial.print(batteryV);
          Serial.println(" V");
      
          Serial.print("Battery percent: ");
          Serial.print(batteryPcnt);
          Serial.println(" %");
      #endif
      
          sendBatteryLevel(batteryPcnt);
      
         sleep(INTERRUPT,CHANGE,SLEEP_TIME);
      }
      
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      Welcome to the forum @Dave2526

      Yes. The return value of the sleep call will let you know the reason for waking up. Documentation: https://www.mysensors.org/download/sensor_api_20#sleeping

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Dave2526
        wrote on last edited by
        #3

        Thats what I think, a really simple thing, Thank You, it works!

        resp_sleep=sleep(INTERRUPT,CHANGE,SLEEP_TIME);

        and then hust a simple if resp_sleep....

        Thanks!

        1 Reply Last reply
        0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        22

        Online

        12.1k

        Users

        11.2k

        Topics

        113.4k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular