How to combine 2 sketches



  • Hello all ,

    I want to make a device to check if a door is open/closed and also have a temperature senoron the same arduino.

    But how do I combine the 2 sketches..... my programming skills are basically non existant.

    I did try some things which looked logical to me , but when i virify in codebender it gives me an error code.

    This is the original temperature sketch:

    // Example sketch showing how to send in OneWire temperature readings
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    void setup()  
    { 
      // Startup OneWire 
      sensors.begin();
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.0");
    
      // 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++) {   
         gw.present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures(); 
    
      // 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>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        if (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msg.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
      gw.sleep(SLEEP_TIME);
    }``````
    

    And thisis original one for the door sensor:

    // Simple binary switch example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 3 (BUTTON_PIN below) and GND.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
     // 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 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.
      gw.present(CHILD_ID, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    } 
    

    The easy bit ( I think)

    inserted the libary and made the door switch/sensor on port 4

    // Example sketch showing how to send in OneWire temperature readings
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 3
    #define BUTTON_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16```
    

    But I have really no clue what to do next .... can someone help me ?

    Thanks,
    Cor


  • Hero Member

    I'm using a DHT22 Temperature in Combination with a reed switch to check if a window is open.

    You just have to switch the DHT-Part with the DS18b20-Part

    #include <MySensor.h>
    #include <SPI.h>
    #include <DHT.h>
    
    #define CHILD_ID_SWITCH 3
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_VOLTAGE 2
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define HUMIDITY_SENSOR_DIGITAL_PIN 8
    #define BATTERY_SENSE_PIN A0
    #define NODE_ID 103
    
    unsigned long SLEEP_TIME = 90000;  // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    
    DHT dht;
    float lastTemp;
    float lastHum;
    float lastVolt;
    boolean metric = true;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID_SWITCH,V_TRIPPED);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
    
    void setup()  
    {  
      gw.begin(NULL, NODE_ID, false);
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
      
      analogReference(INTERNAL);
      
      gw.sendSketchInfo("HumTempReed", "1.0");
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
        
      // 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.
      gw.present(CHILD_ID_SWITCH, S_DOOR);
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
    
      metric = gw.getConfig().isMetric;  
    }
    
    void measureBattery() {
     // R1 = 1MOhm, R2 = 100 kOhm
     int sensorValue = analogRead(BATTERY_SENSE_PIN); 
     float batteryV = sensorValue * 12.1 / 1023;
     if(lastVolt != batteryV) {
       lastVolt = batteryV;
       Serial.print("Voltage: ");
       Serial.println(batteryV);
       gw.send(msgVolt.set(batteryV, 1));
     }
    }
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
      gw.send(msg.set(value==HIGH ? 1 : 0));
      
      // READ DHT22
      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);
      }
      
      measureBattery();
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    } 
    


  • Thanks for the quick reply . Unfortunately this is too complcated for me ...... sorry , I as you sais , chaning everything for the DTH to the DAllas sensor. This is what i came up with , but it doesn't work. about 8 erros according codebender 😞

    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    #define CHILD_ID_SWITCH 3
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_VOLTAGE 2
    #define BUTTON_PIN  4  // Arduino Digital I/O pin for button/reed switch
    #define HUMIDITY_SENSOR_DIGITAL_PIN 8
    #define BATTERY_SENSE_PIN A0
    #define NODE_ID 103
    
    
    MySensor gw;
    
    float lastHum;
    float lastVolt;
    boolean metric = true;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID_SWITCH,V_TRIPPED);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
    
    void setup()  
    {  
      // Startup OneWire 
      sensors.begin();
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.0");
    
      // 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++) {   
         gw.present(i, S_TEMP);
      
      analogReference(INTERNAL);
      
      gw.sendSketchInfo("HumTempReed", "1.0");
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
        
      // 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.
      gw.present(CHILD_ID_SWITCH, S_DOOR);
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
    
      metric = gw.getConfig().isMetric;  
    }
    
    void measureBattery() {
     // R1 = 1MOhm, R2 = 100 kOhm
     int sensorValue = analogRead(BATTERY_SENSE_PIN); 
     float batteryV = sensorValue * 12.1 / 1023;
     if(lastVolt != batteryV) {
       lastVolt = batteryV;
       Serial.print("Voltage: ");
       Serial.println(batteryV);
       gw.send(msgVolt.set(batteryV, 1));
     }
    }
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
      gw.send(msg.set(value==HIGH ? 1 : 0));
      
      // Startup OneWire 
      sensors.begin();
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.0");
    
      // 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++) {   
         gw.present(i, S_TEMP);
      }
      
      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);
      }
      
      measureBattery();
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    } ```
    
    thanks, 
    Cor

  • Hero Member

    Try the following example.

    I can't test it but it compiles.

    The sensor checks the temperature every 30 seconds and sends the temperature to the gateway if a change occured. A message is sent every time the button/switch/reed is used.

    https://codebender.cc/sketch:99911



  • Hmm, it doesn't work.

    Is this part correct?

    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    #define CHILD_ID_SWITCH 17
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch```
    

    looks they are both on pin3 , can I change reed switch to 4

    #define BUTTON_PIN  4  // Arduino Digital I/O pin for button/reed switch
    

    Thanks for your help,
    Cor



  • I tried both , but I get an errorin codebender when i want to upload it:


    Oops! Looks like there was a serious issue with your project.

    /mnt/tmp/compiler.hyQVDL/files/DallasAndButton copy.o: In function loop': DallasAndButton copy.cpp:(.text.loop+0x14c): undefined reference to MySensor::sleep(int, int, unsigned long)'


    Thanks,
    Cor


  • Hero Member

    Oh yes, that does not work.
    Change the Button Pin to 2, otherwise the Interrupt / sleep won't work. Disconnect Interrupt from Radio if it is attached there. It is not needed.



  • Done , but codebender still gives an error when I try to upload:
    This error:

    /mnt/tmp/compiler.5IZJzn/files/DallasAndButton copy copy.o: In function loop': DallasAndButton copy copy.cpp:(.text.loop+0x14c): undefined reference to MySensor::sleep(int, int, unsigned long)'

    I removed the pin 2 from the radio and used that now for the reed switch. and changed the code like this:

    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    #define CHILD_ID_SWITCH 17
    #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
    

    How to tackle the problem with the error whil uploading?

    Thanks,
    Cor



  • @Corvl Try copy and pasting the code from @TimO into the Arduino IDE and skip codebender altogether. Like Tim0 I am not able to test the code right now, but it complies correctly. And since you are getting a compile error, that leads me to think its codebender.



  • Yes , indeed . it is working like this ... Uploaded via arduino programm. included on the vera 3 , and now I have a temp sensor and reed sensor on 1 arduino.

    Many thanks,

    Cor

    For future reference:
    (Where Temp sensor is on pin 3 and reed switch on pin 2. number 2 pin for radio not neccesary)

    // Example sketch showing how to send in OneWire temperature readings
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    #define CHILD_ID_SWITCH 17
    #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
    
    
    
    
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    
    int oldValue=-1;
    
    // Initialize temperature message
    MyMessage msgTemp(0,V_TEMP);
    MyMessage msgButton(CHILD_ID_SWITCH,V_TRIPPED);
    
    void setup()  
    { 
      // Startup OneWire 
      sensors.begin();
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.0");
    
      // 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++) {   
         gw.present(i, S_TEMP);
      }
      
      
      // INIT SWITCH
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      gw.present(CHILD_ID_SWITCH, S_DOOR);
    }
    
    
    void loop()     
    {     
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures(); 
    
      // 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>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        if (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msgTemp.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
      
      
      // READ SWITCH STATUS
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
      if (value != oldValue) {
         // Send in the new value
         gw.send(msgButton.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    }```


  • Hmm..... got an issue.

    The sensor doesn't alway work .... the switch works about 8 of 10 times,

    When I looked at the serial monitor I also see some fails, and that is when I connect pin2 with ground ( reed switch)..

    Anyone has an idea?

    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.0
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.1
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:1
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.2
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:0
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.3
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:1
    send: 2-2-255-255 s=255,c=3,t=7,pt=0,l=0,st=fail:
    

    thanks,
    Cor



  • @Corvl said:

    Hmm..... got an issue.

    The sensor doesn't alway work .... the switch works about 8 of 10 times,

    When I looked at the serial monitor I also see some fails, and that is when I connect pin2 with ground ( reed switch)..

    Anyone has an idea?

    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.0
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.1
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:1
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.2
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:0
    send: 2-2-0-0 s=0,c=1,t=0,pt=7,l=5,st=fail:25.3
    send: 2-2-0-0 s=17,c=1,t=16,pt=2,l=2,st=fail:1
    send: 2-2-255-255 s=255,c=3,t=7,pt=0,l=0,st=fail:
    

    thanks,
    Cor

    try to check your radio and put 47uF capacitor between your radio vcc and ground, and try to check your bootloader also because I experienced with radio problem when I change my bootloader and crystal



  • capacitor I tried as well ( didn't have one at first) , now theres is one , but it makes no difference.

    What do you mean by checking the bootloader?

    thanks,
    Cor


  • Admin



  • @ Hek; Thanks.

    Bugger though.
    Although I have installed the arduino where I am planning to use it , and it is working flawlesly for the last couple of hours ....

    Wait and see.

    PS, I have tried 2 different radio's who look a bit different in appearance , theyboth have/had the same issue.

    Thinking about it , and having that monitor seeing what is being send , it looks like , the info of the reed sensoe ( pin2) is only send when it changes status .

    When it isn't send , nothing happends.
    Is it possible to have the arduino check what the vera has recieved , if the status is not the same , try again a couple of seconds later?

    Thanks,
    Cor


  • Hero Member

    @Corvl said:

    Thinking about it , and having that monitor seeing what is being send , it looks like , the info of the reed sensoe ( pin2) is only send when it changes status .

    When it isn't send , nothing happends.

    🙂 Haha, I have the same problem. I'm currently sending the current value, when ever the arduino awakes, not only when it changes. Not really nice though.



  • Will it be difficult to change the sketch , that the value is resend every X seconds?

    Will there be disadvantages on this?
    Will my Vera3 have a much higher load due to this?

    Thanks,
    Cor


  • Hero Member

    No, it is not difficult. Just remove the if statement, where you check if the old value differs from the new one.

    The value is send every 30 seconds in your sketch, that is no big deal. Though the battery is drained more.



  • no battery ,it has a 5v powersource.

    Just to make sure I don't make a mistake, this is the code I completely have remove?

    // Send in the new value
      if (value != oldValue) {
         // Send in the new value
         gw.send(msgButton.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    

    including the

      }
    

    there on the end


  • Hero Member

    Remove everything but the line with gw.send(), that line actually sends the current value.



  • Just to be sure, I only delete this:

    // Send in the new value
      if (value != oldValue) {
    

    I wish I had learned for a real job 😓 😄

    Thanks,
    Cor


  • Hero Member

    Don't forget to delete the "}" bracket! 🙂



  • Too many of these brackets 😳

    This is the original:

    
      // READ SWITCH STATUS
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
      if (value != oldValue) {
         // Send in the new value
         gw.send(msgButton.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    }
    

    It needs to be changed in:

    
      // READ SWITCH STATUS
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
         gw.send(msgButton.set(value==HIGH ? 1 : 0));
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    }
    

    Or does that gw.sleep(button_pin etc alsobe removed.

    Thanks,
    Cor


  • Hero Member

    Looks good. Does it compile? The gw.sleep() is still needed.



  • Just uploaded it and when the sensor doesn't report immediately to vera3 , some time later ( 30 seconds?) it is done.

    A pity it isn't done immediately always , but good enough for this project,

    Many thanks for you help,
    Cor

    For my reference, here the final sketch:

    // Example sketch showing how to send in OneWire temperature readings
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    #define CHILD_ID_SWITCH 17
    #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
    
    
    
    
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    
    int oldValue=-1;
    
    // Initialize temperature message
    MyMessage msgTemp(0,V_TEMP);
    MyMessage msgButton(CHILD_ID_SWITCH,V_TRIPPED);
    
    void setup()  
    { 
      // Startup OneWire 
      sensors.begin();
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.0");
    
      // 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++) {   
         gw.present(i, S_TEMP);
      }
      
      
      // INIT SWITCH
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      gw.present(CHILD_ID_SWITCH, S_DOOR);
    }
    
    
    void loop()     
    {     
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures(); 
    
      // 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>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        if (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msgTemp.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
      
      
        // READ SWITCH STATUS
      // Get the update value
      int value = digitalRead(BUTTON_PIN);
     
      // Send in the new value
         gw.send(msgButton.set(value==HIGH ? 1 : 0));
      
      gw.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    }
    

  • Hero Member

    I can't figure out why it does not report the status immeadiatly. It sholud report after start up.



  • Oh sorry Timo , I said it wrong.

    What I was trying to say was , that it is a pity , that there are so many fails sending the status.
    But now trying to send it every 30 seconds, by removing that little piece of coding is very acceptable.

    Again , many thanks for your time helping me,

    Cor


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 1
  • 3
  • 1
  • 6

55
Online

11.4k
Users

11.1k
Topics

112.6k
Posts