Skip to content

Instantly share code, notes, and snippets.

@dgomes
Created January 23, 2017 22:48
Show Gist options
  • Select an option

  • Save dgomes/26d2cef10be75c4a625c4aaca49e87b8 to your computer and use it in GitHub Desktop.

Select an option

Save dgomes/26d2cef10be75c4a625c4aaca49e87b8 to your computer and use it in GitHub Desktop.
Arduino sketch with 2 DS18S20 sensors and a RF433 OOK transmitter to monitor the temperature of water tanks
#include <DallasTemperature.h>
#include <OneWire.h>
#include <RCSwitch.h>
OneWire ds(12);
DallasTemperature sensors(&ds);
DeviceAddress prh, aqs;
typedef uint8_t ScratchPad[9];
RCSwitch mySwitch = RCSwitch();
static float t_aqs=0.0, t_prh=0.0, p_aqs=0.0, p_prh=0.0;
static long sleep = 5000;
void setup() {
Serial.begin(9600);
// Transmitter is connected to Arduino Pin #13
mySwitch.enableTransmit(13);
mySwitch.setRepeatTransmit(10);
sensors.begin();
sensors.setResolution(12); //12bits temperatures
if (!sensors.getAddress(prh, 0)) Serial.println("Unable to find address for PRH");
if (!sensors.getAddress(aqs, 1)) Serial.println("Unable to find address for AQS");
}
//Botched from dallas-temperature library to retrieve the 12 bits RAW
int16_t getTemp(uint8_t *dev) {
ScratchPad scratchPad;
if (sensors.isConnected(dev, scratchPad)) {
int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];
return rawTemperature;
}
}
byte CRC8(const byte *data, byte len) {
byte crc = 0x00;
while (len--) {
byte extract = *data++;
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
void loop() {
sensors.requestTemperatures(); // Send the command to get temperatures
t_prh = sensors.getTempC(prh);
t_aqs = sensors.getTempC(aqs);
uint32_t packet = 0;
if(abs(t_prh-p_prh) >= 0.1 || abs(t_aqs-p_aqs) >= 0.1) { //If temperature diff > 0.1º report in 10sec
sleep = 10000;
} else {
sleep+=60000;
if (sleep > 300000)
sleep = 60000;
}
if (abs(t_prh-p_prh) >= abs(t_aqs-p_aqs)) {
packet = ((uint32_t) 0b0001 << 12) | getTemp(prh); // ID1 = PRH
p_prh = t_prh;
} else {
packet = ((uint32_t) 0b0010 << 12) | getTemp(aqs); // ID2 = AQS
p_aqs = t_aqs;
}
if(abs(t_prh-p_prh) != 0 || abs(t_aqs-p_aqs) != 0) {
packet |= (uint32_t) CRC8((uint8_t *) &packet, 2) << 16;
//Serial.println(packet,HEX);
mySwitch.send(packet,24);
}
// Serial.println(String("sleep = ")+String(sleep));
delay(sleep);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment