Created
February 10, 2018 01:14
-
-
Save paruljain/103644612a9d4af8fd28b08dc5cbf73c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include the libraries we need | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <Ticker.h> | |
Ticker temperatureTicker; | |
// Data wire is plugged into port 2 on the Arduino | |
#define ONE_WIRE_BUS D2 | |
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) | |
OneWire oneWire(ONE_WIRE_BUS); | |
// Pass our oneWire reference to Dallas Temperature. | |
DallasTemperature sensors(&oneWire); | |
// arrays to hold device address | |
DeviceAddress insideThermometer; | |
//parameters | |
#define SAMPLES 30 | |
//arrays to hold temperature time series | |
float data[2][SAMPLES]; | |
int dataSetBeingWritten = 0; | |
int dataWriteIndex = 0; | |
void getTemp() | |
{ | |
sensors.requestTemperatures(); | |
data[dataSetBeingWritten][dataWriteIndex] = sensors.getTempF(insideThermometer); | |
dataWriteIndex++; | |
if (dataWriteIndex == SAMPLES) { | |
dataWriteIndex = 0; dataSetBeingWritten = 1 - dataSetBeingWritten; | |
float sum_x = 0; | |
float sum_x2 = 0; | |
float sum_y = 0; | |
float sum_xy = 0; | |
for (int i=0; i<SAMPLES; i++) { | |
sum_x += i; | |
sum_x2 += i * i; | |
sum_y += data[1-dataSetBeingWritten][i]; | |
sum_xy += i * data[1-dataSetBeingWritten][i]; | |
} | |
float mean_x = sum_x/SAMPLES; | |
float mean_x2 = sum_x2/SAMPLES; | |
float mean_y = sum_y/SAMPLES; | |
float mean_xy = sum_xy/SAMPLES; | |
float m = (mean_x * mean_y - mean_xy) / (mean_x * mean_x - mean_x2); | |
Serial.print("Slope = "); | |
Serial.print(m, 3); | |
Serial.print(" C = "); | |
Serial.print(data[1-dataSetBeingWritten][0], 3); | |
Serial.print(" CurrentTemp = "); | |
Serial.println(data[1-dataSetBeingWritten][SAMPLES - 1], 3); | |
} | |
} | |
void setup(void) | |
{ | |
// start serial port | |
Serial.begin(9600); | |
// locate devices on the bus | |
Serial.print("Locating devices..."); | |
sensors.begin(); | |
Serial.print("Found "); | |
Serial.print(sensors.getDeviceCount(), DEC); | |
Serial.println(" devices."); | |
// report parasite power requirements | |
Serial.print("Parasite power is: "); | |
if (sensors.isParasitePowerMode()) Serial.println("ON"); | |
else Serial.println("OFF"); | |
// Assign address manually. The addresses below will beed to be changed | |
// to valid device addresses on your bus. Device address can be retrieved | |
// by using either oneWire.search(deviceAddress) or individually via | |
// sensors.getAddress(deviceAddress, index) | |
// Note that you will need to use your specific address here | |
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 }; | |
// Method 1: | |
// Search for devices on the bus and assign based on an index. Ideally, | |
// you would do this to initially discover addresses on the bus and then | |
// use those addresses and manually assign them (see above) once you know | |
// the devices on your bus (and assuming they don't change). | |
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); | |
// method 2: search() | |
// search() looks for the next device. Returns 1 if a new address has been | |
// returned. A zero might mean that the bus is shorted, there are no devices, | |
// or you have already retrieved all of them. It might be a good idea to | |
// check the CRC to make sure you didn't get garbage. The order is | |
// deterministic. You will always get the same devices in the same order | |
// | |
// Must be called before search() | |
//oneWire.reset_search(); | |
// assigns the first address found to insideThermometer | |
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer"); | |
// show the addresses we found on the bus | |
Serial.print("Device 0 Address: "); | |
printAddress(insideThermometer); | |
Serial.println(); | |
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions) | |
sensors.setResolution(insideThermometer, 12); | |
Serial.print("Device 0 Resolution: "); | |
Serial.print(sensors.getResolution(insideThermometer), DEC); | |
Serial.println(); | |
temperatureTicker.attach(1, getTemp); | |
} | |
void loop(void) | |
{ | |
} | |
// function to print a device address | |
void printAddress(DeviceAddress deviceAddress) | |
{ | |
for (uint8_t i = 0; i < 8; i++) | |
{ | |
if (deviceAddress[i] < 16) Serial.print("0"); | |
Serial.print(deviceAddress[i], HEX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment