advice to improve my door and temeperature sketches



  • Its been a while since I've been on the forum as my temp and door contact sensors have been working well , recently I purchased some easy PCB's to upgrade the hardware of my sensors. Now I'm wondering if I could upgrade the Software as well any advice on how i could improve my sketches would be really appreciated .

    **Door sketch **

    // ------------------ MYSENSORS ------------------
    // 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_NODE_ID 4
    
    #include <SPI.h>
    #include <MySensors.h>
    
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    
    #define SLEEP_TIME 0 // sleep forever
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    // ------------------ BATTERY ------------------
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    int oldBatteryPcnt = 0;  // remember last measured percentage
    #define V_MIN 2.7  // 0% Battery voltage
    #define V_MAX 3.3  // 100% Battery voltage
    
    byte oldSwitch_State = HIGH; 
    
    
    void setup()  
    {  
      // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      
      
      
    }
    
    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.
      present(CHILD_ID, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      // Setup 1.1 V internal reference for analoge GPIO
      analogReference(INTERNAL);
      delay(500);
      // read analog pin
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
      // calculate battery voltage
      float vBat  = static_cast<float>((sensorValue * (V_MAX/1023)) + 0.10);
      // calculate % left of defined interval
      int batteryPcnt = static_cast<int>(((vBat-V_MIN)/(V_MAX-V_MIN))*100.);  
      // debugging
      #ifdef MY_DEBUG
        Serial.print("Sensor value: ");
        Serial.println(sensorValue);
        Serial.print("Battery Voltage: ");
        Serial.print(vBat);
        Serial.println(" V");
        Serial.print("Battery percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
      #endif
      // If battery % has change send to controller
      if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
        
      }
    {
     
     {
      
       byte switchState = digitalRead (BUTTON_PIN);
      
        
      
      if (switchState != oldSwitch_State) {
         // Send in the new value
         send(msg.set(switchState==LOW ? 1 : 0));
         oldSwitch_State = switchState;
    
         sleep(5);
      }
       
    
     sleep(digitalPinToInterrupt(BUTTON_PIN), CHANGE, SLEEP_TIME);
    } 
    }
    }
    
    

    Temp Sketch

    // ------------------ MYSENSORS ------------------
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #define MY_NODE_ID 3
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    unsigned long SLEEP_TIME =  5 * 60000; // Sleep time between reads (in milliseconds), 60000 ms = 1 minute
    
    // ------------------ TEMPERATURE ------------------
    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS 5 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 1
    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
    MyMessage msg(0,V_TEMP);
    
    // ------------------ BATTERY ------------------
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    int oldBatteryPcnt = 0;  // remember last measured percentage
    #define V_MIN 2.7  // 0% Battery voltage
    #define V_MAX 3.3  // 100% Battery voltage
    
    void before()
    {
      sensors.begin();
    }
    
    void setup()
    {
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Setup 1.1 V internal reference for analoge GPIO
      analogReference(INTERNAL);
    }
    
    void presentation()
    {
       // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temperature Sensor", "1.1");
      // 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);
      }
    }
    
    void loop()
    {
      // read analog pin
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
      // calculate battery voltage
      float vBat  = static_cast<float>(sensorValue * (V_MAX/1023));
      // calculate % left of defined interval
      int batteryPcnt = static_cast<int>(((vBat-V_MIN)/(V_MAX-V_MIN))*100.);  
      // debugging
      #ifdef MY_DEBUG
        Serial.print("Sensor value: ");
        Serial.println(sensorValue);
        Serial.print("Battery Voltage: ");
        Serial.print(vBat);
        Serial.println(" V");
        Serial.print("Battery percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
      #endif
      // If battery % has change send to controller
      if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
      }
    
      // 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;
        }
      }
      sleep(SLEEP_TIME);
    }
    

    thank you in advance .


 

380
Online

8.0k
Users

8.8k
Topics

94.3k
Posts