-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDallasTemperatureSimple.ino
150 lines (124 loc) · 5.16 KB
/
DallasTemperatureSimple.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
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 <[email protected]>
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
Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
http://www.mysensors.org/build/temp
Enhanced Version also sending the Dallas-ROM-ID, MySensors Version >=2.1.0
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
#define MAX_ATTACHED_DS18B20 16
uint8_t DS_First_Child_ID = 7; //First Child-ID to be used by Dallas Bus; set this to be higher than other Child-ID's who need EEPROM storage to avoid conflicts
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
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;
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int resolution = 12; // precision: 12 bits = 0.0625°C, 11 bits = 0.125°C, 10 bits = 0.25°C, 9 bits = 0.5°C
int conversionTime = 0;
// Initialize temperature message
MyMessage msgTemp(0, V_TEMP);
MyMessage msgId(0, V_ID);
char* charAddr = "Check for faults";
#define SEND_ID
void before()
{
// 12 bits = 750 ms, 11 bits = 375ms, 10 bits = 187.5ms, 9 bits = 93.75ms
conversionTime = 750 / (1 << (12 - resolution));
// Startup up the OneWire library
sensors.begin();
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Temperature Sensor", "1.2");
// 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++) {
sensors.getAddress(tempDeviceAddress, i);
charAddr = addrToChar(tempDeviceAddress);
present(i + DS_First_Child_ID, S_TEMP, charAddr);
#ifdef MY_DEBUG
Serial.println(charAddr);
#endif
}
}
void setup()
{
// requestTemperatures() will not block current thread
sensors.setWaitForConversion(false);
for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
sensors.getAddress(tempDeviceAddress, i);
#ifdef SEND_ID
// 8 will assure a length of 16 of the sent ROM-ID
send(msgId.setSensor(i + DS_First_Child_ID).set(tempDeviceAddress, 8));
#endif
sensors.setResolution(tempDeviceAddress, resolution);
metric = getControllerConfig().isMetric;
}
}
void loop()
{
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// 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>((metric ? 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(msgTemp.setSensor(i + DS_First_Child_ID).set(temperature, 1));
wait(20);
// Save new temperatures for next compare
lastTemperature[i] = temperature;
}
}
// sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
sleep(SLEEP_TIME);
}
char* addrToChar(uint8_t* data) {
String strAddr = String(data[0], HEX); //Chip Version; should be higher than 16
byte first ;
int j = 0;
for (uint8_t i = 1; i < 8; i++) {
if (data[i] < 16) strAddr = strAddr + 0;
strAddr = strAddr + String(data[i], HEX);
strAddr.toUpperCase();
}
for (int j = 0; j < 16; j++) {
charAddr[j] = strAddr[j];
}
return charAddr;
}