read temp - first draft
This commit is contained in:
parent
863e8aec99
commit
f6b06ca8db
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@
|
||||
.cproject
|
||||
.ino*
|
||||
Release/
|
||||
Prototype/
|
||||
|
||||
@ -1,21 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <TinySensor.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
18
libraries/Tiny/TempSensor.h
Normal file
18
libraries/Tiny/TempSensor.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <TinySensor.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include <ContactSensor.h>
|
||||
|
||||
#include <TempSensor.h>
|
||||
|
||||
64
libraries/Tiny/TinySensor.h
Normal file
64
libraries/Tiny/TinySensor.h
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
};
|
||||
62
temp_sensor/temp_sensor.ino
Normal file
62
temp_sensor/temp_sensor.ino
Normal file
@ -0,0 +1,62 @@
|
||||
#include "Arduino.h"
|
||||
#include <Tiny.h>
|
||||
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user