Created
September 6, 2012 21:58
-
-
Save brooksware2000/3660676 to your computer and use it in GitHub Desktop.
Sends sensor data and relay status from the thermostat/temperature node over the serial port.
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
/* | |
Demonstration sketch for the Hobbybotics Wireless Thermostat/Temperature Node. | |
Sends sensor data and relay status over the serial port. | |
*/ | |
#include <MCP23008.h> // MCP23008 I/O Expander (I2C) | |
#include <LCD.h> // I2C LCD | |
#include <Wire.h> // I2C functions | |
#include <OneWire.h> // One-wire devices | |
#include <DallasTemperature.h> // DS18B20 | |
#include <MAX6675.h> // MAX6675 Thermocouple | |
#include <SHT15.h> // SHT15 humidity/temperature sensor | |
#include <TEMT6000.h> // TEMT6000 light sensor | |
#include <SPI.h> // DS3234 RTC | |
#include <DS3234.h> // DS3234 RTC | |
/*----------------------------------------------------------------------------------------------- | |
* Pin definitions | |
* ----------------------------------------------------------------------------------------------*/ | |
// Dallas DS18B20 temperature sensors | |
#define DS18B20_BUS A0 | |
// TEMT6000 light sensor pin | |
#define TEMT6000_PIN A1 | |
/*----------------------------------------------------------------------------------------------- | |
* Create object references | |
* ----------------------------------------------------------------------------------------------*/ | |
// Default I2C address for LCD is 0 | |
LCD lcd; | |
// Setup One-wire instance to communicate with DS18B20 sensors | |
OneWire oneWire(DS18B20_BUS); | |
// Pass oneWire reference to DallasTemperature library | |
DallasTemperature _DS18B20(&oneWire); | |
// Create an array for DS18B20 sensor addresses | |
DeviceAddress DS18B20[3]; | |
// Setup MCP23008 I/O Expander instance to communicate with the relays--address 2 | |
MCP23008 relays; | |
// Setup MAX6675 instance to communicate with Type-K Thermocouple | |
MAX6675 _MAX6675; | |
// Setup light sensor object | |
TEMT6000 _TEMT6000; | |
// Setup SHT15 humidity/temperature sensor object | |
SHT15 _SHT15; | |
// Setup instance of DS3234 _DS3234 | |
DS3234 _DS3234; | |
/*----------------------------------------------------------------------------------------------- | |
* Serial packet data structure | |
* ----------------------------------------------------------------------------------------------*/ | |
struct{ | |
int light; // TEMT6000 measured light intensity | |
int humidity; // SHT15 humidity 0..100% | |
int temperature[5]; // Celcius: SHT15, DS18B20[1..4], MAX6675 | |
int relays[7]; // Relay status1..7 | |
int date_time[5]; // DS3234 day/month/year/hour/min/sec | |
} txdata; | |
/*----------------------------------------------------------------------------------------------- | |
* LCD custom characters | |
* ----------------------------------------------------------------------------------------------*/ | |
// make a degree symbol | |
const char degree = 223; | |
/*----------------------------------------------------------------------------------------------- | |
* Function: init_relays | |
* Description: Initialize relays. Relays are connected to MCP23008 I/O Expander (address 2) | |
* Ins: none | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void init_relays(){ | |
relays.begin(2); | |
for(int x = 0; x <= 7; x++){ | |
relays.pinMode(x, OUTPUT); | |
} | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: get_relay_state | |
* Description: Get relay state(ON = 1/OFF = 0) | |
* Ins: Integer value for relay (0-7) | |
* Outs: relay state (1 = ON, 0 = OFF) | |
* ----------------------------------------------------------------------------------------------*/ | |
int get_relay_state(int relaynum){ | |
return relays.digitalRead(relaynum); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: init_DS18B20 | |
* Description: Initialize DS18B20 Temperature sensor(s) | |
* Ins: Integer value for sensor precision. Can be 9, 10, 11 or 12 bits | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void init_DS18B20(int precision){ | |
_DS18B20.begin(); | |
int available = _DS18B20.getDeviceCount(); | |
for(int x = 0; x <= available; x++){ | |
if(_DS18B20.getAddress(DS18B20[x], x)){ | |
_DS18B20.setResolution(DS18B20[x], precision); | |
} | |
} | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: DS18B20_CELSIUS | |
* Description: Get celsius reading from DS18B20 Temperature sensor | |
* Ins: Integer value for sensor address | |
* Outs: Returns celsius reading | |
* ----------------------------------------------------------------------------------------------*/ | |
int DS18B20_CELSIUS(int address){ | |
if (_DS18B20.getAddress(DS18B20[address], address)){ | |
_DS18B20.requestTemperatures(); | |
return _DS18B20.getTempC(DS18B20[address]); | |
} | |
else | |
return 0; | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: DS18B20_FAHRENHEIT | |
* Description: Get fahrenheit reading from DS18B20 Temperature sensor | |
* Ins: Integer value for sensor address | |
* Outs: Returns fahrenheit reading | |
* ----------------------------------------------------------------------------------------------*/ | |
//int DS18B20_FAHRENHEIT(int address){ | |
// if (_DS18B20.getAddress(DS18B20[address], address)){ | |
// _DS18B20.requestTemperatures(); | |
// return DallasTemperature::toFahrenheit(_DS18B20.getTempC(DS18B20[address])); | |
// } | |
// else | |
// return 0; | |
//} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: init_DS3234 | |
* Description: initialize the _DS3234 and check if date/time has already been set | |
* Ins: none | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void init_DS3234(){ | |
SPI.begin(); | |
_DS3234.begin(); | |
if (! _DS3234.isrunning()) // set _DS3234 to date & time this sketch was compiled | |
_DS3234.adjust(DateTime(__DATE__, __TIME__)); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: init_DS3234 | |
* Description: initialize the _DS3234 and check if date/time has already been set | |
* Ins: none | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void get_datetime(){ | |
DateTime now = _DS3234.now(); | |
txdata.date_time[0] = now.hour(); | |
txdata.date_time[1] = now.minute(); | |
txdata.date_time[2] = now.second(); | |
txdata.date_time[3] = now.day(); | |
txdata.date_time[4] = now.month(); | |
txdata.date_time[5] = now.year(); | |
txdata.date_time[6] = now.dayOfWeek(); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: set_cursor | |
* Description: Set LCD cursor to column and row | |
* Ins: Integer value for column and row | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void set_cursor(int col, int row){ | |
lcd.setCursor(col, row); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: print_string | |
* Description: Set LCD cursor to column and row and print string | |
* Ins: Integer value for column and row. Pointer to string to print | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void print_string(char *str, int col, int row){ | |
lcd.setCursor(col, row); | |
lcd.print(str); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: print_custom_char | |
* Description: Set LCD cursor to column and row and print custom LCD character | |
* Ins: Integer value for column, row and address. | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void print_custom_char(int address, int col, int row){ | |
lcd.setCursor(col, row); | |
lcd.write(address); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: print_int | |
* Description: Set LCD cursor to column and row and print a Integer | |
* Ins: Integer value for column, row and number | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void print_int(int num, int col, int row){ | |
lcd.setCursor(col, row); | |
lcd.print(num); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Function: splash_screen | |
* Description: | |
* Ins: none | |
* Outs: none | |
* ----------------------------------------------------------------------------------------------*/ | |
void splash_screen(){ | |
lcd.clear(); | |
print_string("Hobbybotics", 0, 0); | |
print_string("Serial Sensor Test 1", 0, 1); | |
} | |
/*----------------------------------------------------------------------------------------------- | |
* Main routines | |
* ----------------------------------------------------------------------------------------------*/ | |
void setup(){ | |
Serial.begin(9600); | |
// Init LCD | |
lcd.begin(20, 4); | |
// Init light sensor | |
_TEMT6000.init(TEMT6000_PIN); | |
// Init MAX6675 | |
_MAX6675.init(4, 5, 6); | |
// Init SHT15 sensor. SDA = D16 (A2), SCL = D17 (A3) | |
_SHT15.init(16, 17); | |
// Init relays (I2C) | |
init_relays(); | |
// Init DS18B20 sensors with precision set to 9 | |
init_DS18B20(9); | |
// Init DS3234 | |
init_DS3234(); | |
splash_screen(); | |
delay(3000); | |
} | |
void loop(){ | |
// TEMT6000 : Light 0..100% | |
Serial.print("TEMT6000: "); | |
Serial.print(txdata.light = _TEMT6000.get_lux_int()); | |
Serial.print("%"); | |
// SHT15 : Humidity 0..100%, Temperature Celsius | |
Serial.print(" SHT15: "); | |
Serial.print(txdata.humidity = _SHT15.measure(HUMI)); | |
Serial.print("% "); | |
Serial.print(txdata.temperature[0] = _SHT15.measure(TEMPC)); | |
Serial.write(degree); | |
Serial.print("C"); | |
// DS18B20 : Temperature Celsius | |
Serial.print(" DS18B20: "); | |
Serial.print(txdata.temperature[1] = DS18B20_CELSIUS(0)); | |
Serial.write(degree); | |
Serial.print("C "); | |
Serial.print(txdata.temperature[2] = DS18B20_CELSIUS(1)); | |
Serial.write(degree); | |
Serial.print("C "); | |
Serial.print(txdata.temperature[3] = DS18B20_CELSIUS(2)); | |
Serial.write(degree); | |
Serial.print("C "); | |
// Serial.print(txdata.temperature[4] = DS18B20_CELSIUS(3)); | |
// Serial.write(degree); | |
// Serial.print("C"); | |
// MAX6675 : Temperature Celsius | |
Serial.print(" MAX6675: "); | |
Serial.print(txdata.temperature[5] = _MAX6675.measure(_MAX6675.TEMPC)); | |
Serial.write(degree); | |
Serial.print("C"); | |
// Relays : 0..7 | |
Serial.print(" Relays: "); | |
Serial.print(txdata.relays[0] = get_relay_state(0)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[1] = get_relay_state(1)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[2] = get_relay_state(2)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[3] = get_relay_state(3)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[4] = get_relay_state(4)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[5] = get_relay_state(5)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[6] = get_relay_state(6)); | |
Serial.print(" "); | |
Serial.print(txdata.relays[7] = get_relay_state(7)); | |
// DS3234 : hh:mm:ss dd:mm:yyyy d | |
DateTime now = _DS3234.now(); | |
Serial.print(" Time: "); | |
// Hour | |
txdata.date_time[0] = now.hour(); | |
// Pad if necessary for double-digit | |
if (txdata.date_time[0] < 10) | |
Serial.print("0"); | |
Serial.print(txdata.date_time[0]); | |
Serial.print(":"); | |
// Minutes | |
txdata.date_time[1] = now.minute(); | |
// Pad if necessary for double-digit | |
if (txdata.date_time[1] < 10) | |
Serial.print("0"); | |
Serial.print(txdata.date_time[1]); | |
Serial.print(":"); | |
// Seconds | |
txdata.date_time[2] = now.second(); | |
// Pad if necessary for double-digit | |
if (txdata.date_time[2] < 10) | |
Serial.print("0"); | |
Serial.print(txdata.date_time[2]); | |
Serial.print(" Date: "); | |
// Day of month | |
txdata.date_time[3] = now.day(); | |
// Pad if necessary for double-digit | |
if (txdata.date_time[3] < 10) | |
Serial.print("0"); | |
Serial.print(txdata.date_time[3]); | |
Serial.print("/"); | |
// Month | |
txdata.date_time[4] = now.month(); | |
// Pad if necessary for double-digit | |
if (txdata.date_time[4] < 10) | |
Serial.print("0"); | |
Serial.print(txdata.date_time[4]); | |
Serial.print("/"); | |
// Year | |
Serial.print(txdata.date_time[5] = now.year()); | |
Serial.print(" Day: "); | |
// Day of week | |
Serial.print(txdata.date_time[6] = now.dayOfWeek()); | |
Serial.println(); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment