Door en temperature sensor

I like that the temperature sensor for arduino reports with 1 decimal.

The next project will be a door sensor and temperature sensor in 1.

I uploaded both sketches on an arduino and they work fine! , but how do I combine the 2 sketches , I allready tried several options which looked logical for me , but unfortunately my programming skills are non existant. , there were everytime lots of errors on the sketch.

Anyone knows how to combine these 2 sketches?

Thanks,
Cor

The door swtich/sensor:

[code]// 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;
}
} [/code]

The temperature sensor:

[code]// 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);
}[/code]

Got it working , someone on mysensor forum adapted the sketch.


Uploaded via arduino programm (doesn’t work with codebender) . included on the vera 3 , and now I have a temp sensor and reed sensor on 1 arduino.

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

[code]// 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);
}[/code]