Files
MySensors/NodeJsController/DallasTemperatureSensor/DallasTemperatureSensor.ino
ToSa27 5968ddf219 Send reboot msg after message retrieval to deal with sensors going to sleep
minimal adjustment to the dallas temperature sensor as an example
added scripts to drop the database and to set a reboot manually for
debugging purposes
2014-07-26 11:28:57 +02:00

70 lines
2.0 KiB
C++

// 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 WAIT_TIME = 30000; // Wait for feedback from controller after sending and before going to sleep (in milliseconds)
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;
}
}
delay(WAIT_TIME);
gw.process();
gw.sleep(SLEEP_TIME);
}