Skip to content

Instantly share code, notes, and snippets.

@dcalacci
Created October 21, 2015 02:17
Show Gist options
  • Save dcalacci/87738d99353a70fa92c3 to your computer and use it in GitHub Desktop.
Save dcalacci/87738d99353a70fa92c3 to your computer and use it in GitHub Desktop.
temperature control power
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 9
#define TEMPERATURE_PRECISION 9
// 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);
// Constants for PowerSwitch tail II
const int powerPin = A0;
boolean powerOn = false;
void setup(void)
{
// This lines just make it so you can plug a DS18B20 directly into
// digitial pins 8-10.
digitalWrite( 8 , LOW );
pinMode( 8 , OUTPUT );
digitalWrite( 10 , LOW );
pinMode( 10 , OUTPUT );
// start serial port
Serial.begin(9600);
Serial.println(F("Starting Up..."));
pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH);
}
void loop(void)
{
#ifdef BUSFAIL
Serial.print(" Test:");
if (sensors.reset()) {
Serial.print("good");
} else {
if (sensors.busFail()) {
Serial.print("fail");
} else {
Serial.print("empty");
}
}
#endif
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
// For testing purposes, reset the bus every loop so we can see if any devices appear or fall off
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
Serial.print(" Parasite:");
if (sensors.isParasitePowerMode()) Serial.print("ON ");
else Serial.print("OFF ");
Serial.print("Count:");
Serial.print(numberOfDevices, DEC);
// report parasite power requirements
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print(" #");
Serial.print(i,DEC);
Serial.print("=");
float tempC = sensors.getTempC(tempDeviceAddress);
Serial.print(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
if (DallasTemperature::toFahrenheit(tempC) > 76.00) {
if (powerOn) {
digitalWrite(powerPin, HIGH);
Serial.println("\nPOWER OFF");
powerOn = false;
}
}
else {
if (!powerOn) {
digitalWrite(powerPin, LOW);
Serial.println("\nPOWER ON");
powerOn = true;
}
}
}
//else ghost device! Check your power requirements and cabling
}
Serial.println("");
delay(1000);
}
void togglePower() {
if (powerOn) {
Serial.println("Power off, turning on...");
digitalWrite(powerPin, LOW);
} else {
Serial.println("Power on, turning off...");
digitalWrite(powerPin, HIGH);
}
}
// 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