diff --git a/.gitignore b/.gitignore index 7917494..07d8a24 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .cproject .ino* Release/ +Prototype/ diff --git a/libraries/Tiny/ContactSensor.h b/libraries/Tiny/ContactSensor.h index 68d1315..e16e350 100644 --- a/libraries/Tiny/ContactSensor.h +++ b/libraries/Tiny/ContactSensor.h @@ -1,21 +1,13 @@ #pragma once +#include #include -class ContactSensor { - short id; - short senderPin; - RCSwitch mySwitch = RCSwitch(); +class ContactSensor: public TinySensor { public: - ContactSensor(short id, short senderPin) { - this->id = id; - this->senderPin = senderPin; - } - - void setup() { - mySwitch.enableTransmit(senderPin); - mySwitch.setProtocol(2); + ContactSensor(short id, short senderPin) : + TinySensor(id, senderPin) { } void sendStateAndVoltage(bool state) { @@ -23,33 +15,6 @@ public: value |= readVcc() << 6; value |= !state << 5; value |= id; - mySwitch.send(value, 32); - } - - long readVcc() { - // Read 1.1V reference against AVcc - // set the reference to Vcc and the measurement to the internal 1.1V reference -#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); -#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) - ADMUX = _BV(MUX5) | _BV(MUX0); -#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) - ADMUX = _BV(MUX3) | _BV(MUX2); -#else - ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); -#endif - - delay(2); // Wait for Vref to settle - ADCSRA |= _BV(ADSC); // Start conversion - while (bit_is_set(ADCSRA, ADSC)) - ; // measuring - - uint8_t low = ADCL; // must read ADCL first - it then locks ADCH - uint8_t high = ADCH; // unlocks both - - long result = (high << 8) | low; - - result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 - return result; // Vcc in millivolts + sendInfo(value); } }; diff --git a/libraries/Tiny/TempSensor.h b/libraries/Tiny/TempSensor.h new file mode 100644 index 0000000..6cd78f7 --- /dev/null +++ b/libraries/Tiny/TempSensor.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +class TempSensor: public TinySensor { + +public: + TempSensor(short id, short senderPin) : + TinySensor(id, senderPin) { + } + + unsigned long sendTempAndVoltage(int temp) { + unsigned long value = ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(6); + sendInfo(value); + return value; + } +}; diff --git a/libraries/Tiny/Tiny.h b/libraries/Tiny/Tiny.h index 76e0528..77aeef2 100644 --- a/libraries/Tiny/Tiny.h +++ b/libraries/Tiny/Tiny.h @@ -1,4 +1,4 @@ #pragma once #include - +#include diff --git a/libraries/Tiny/TinySensor.h b/libraries/Tiny/TinySensor.h new file mode 100644 index 0000000..5ee503d --- /dev/null +++ b/libraries/Tiny/TinySensor.h @@ -0,0 +1,64 @@ +#pragma once + +#include + +#define ID(value) (value & 0x1F) +#define STATE(value) ((value & 0x1) << 5) +#define VCC(value) ((value & 0x1FFF) << 6) +#define TEMP(value) (((unsigned long)value & 0x1FF) << 19) +#define TYPE(value) (((unsigned long)value & 0xF) << 28) +//#define ID(value, id) ((value & 0xFFFFFFE0) | id) +//#define STATE(value, state) ((value & 0xFFFFFFDF) | state) +//#define VCC(value, vcc) ((value & 0xFFF0003F) | vcc) +//#define TEMP(value, temp) ((value & 0xF00FFFFF) | temp) +//#define TYPE(value, type) ((value & 0x0FFFFFFF) | type) + + +class TinySensor { +protected: + short id; + short senderPin; + RCSwitch mySwitch = RCSwitch(); + + long readVcc() { + // Read 1.1V reference against AVcc + // set the reference to Vcc and the measurement to the internal 1.1V reference +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = _BV(MUX5) | _BV(MUX0); +#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = _BV(MUX3) | _BV(MUX2); +#else + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#endif + + delay(2); // Wait for Vref to settle + ADCSRA |= _BV(ADSC); // Start conversion + while (bit_is_set(ADCSRA, ADSC)) + ; // measuring + + uint8_t low = ADCL; // must read ADCL first - it then locks ADCH + uint8_t high = ADCH; // unlocks both + + long result = (high << 8) | low; + + result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 + return result; // Vcc in millivolts + } + + void sendInfo(unsigned long value) { + mySwitch.send(value, 32); + } + +public: + TinySensor(short id, short senderPin) { + this->id = id; + this->senderPin = senderPin; + } + + void setup() { + mySwitch.enableTransmit(senderPin); + mySwitch.setProtocol(2); + } +}; diff --git a/temp_sensor/temp_sensor.ino b/temp_sensor/temp_sensor.ino new file mode 100644 index 0000000..77a4659 --- /dev/null +++ b/temp_sensor/temp_sensor.ino @@ -0,0 +1,62 @@ +#include "Arduino.h" +#include + +// Pins +#define SENDER 2 +#define TEMP_PIN 3 + +#define SENSOR_ID 4 + +TempSensor sensor = TempSensor(SENSOR_ID, SENDER); + +int counter = 0; + +void setup() { + + Serial.begin(9600); + + sensor.setup(); + +// TinyPower::setup(); +// TinyPower::enableWdt(WDTO_8S); +} + +void loop() { +// TinyPower::sleep(); + unsigned long value = sensor.sendTempAndVoltage(readTemp()); + Serial.println(value); + delay(5000); +} + +int readTemp() { + int reading = analogRead(TEMP_PIN); + + // converting that reading to voltage, for 3.3v arduino use 3.3 + float voltage = reading * 5.0; + voltage /= 1024.0; + + // print out the voltage + Serial.print(voltage); + Serial.println(" volts"); + + // now print out the temperature + float temperatureC = (voltage - 0.5) * 100; //converting from 10 mv per degree wit 500 mV offset + //to degrees ((voltage - 500mV) times 100) + Serial.print(temperatureC); + Serial.println(" degrees C"); + return roundf(temperatureC*10); +} + +ISR(PCINT0_vect) { + sensor.sendTempAndVoltage(readTemp()); + delay(5000); + sensor.sendTempAndVoltage(readTemp()); +} + +ISR(WDT_vect) { + counter++; + if (counter % 220 == 0) { + counter = 0; + sensor.sendTempAndVoltage(readTemp()); + } +}