add temperature sensor

This commit is contained in:
Nicu Hodos 2017-04-04 19:36:05 +02:00
parent f6b06ca8db
commit 366363ae0c
2 changed files with 33 additions and 42 deletions

View File

@ -10,9 +10,7 @@ public:
TinySensor(id, senderPin) {
}
unsigned long sendTempAndVoltage(int temp) {
unsigned long value = ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(6);
sendInfo(value);
return value;
void sendTempAndVoltage(int temp) {
sendInfo(ID(id) | VCC(readVcc()) | TEMP(temp) | TYPE(6));
}
};

View File

@ -1,62 +1,55 @@
#include "Arduino.h"
#include <Tiny.h>
#include <SoftwareSerial_Tiny.h>
#define DEBUG 0
// Pins
#define SENDER 2
#define TEMP_PIN 3
#define SENDER 1
#define TEMP_PIN A1
#define SENSOR_ID 4
#if DEBUG
#define RxD 3
#define TxD 4
SoftwareSerial AttinySerial(RxD, TxD);
#endif
TempSensor sensor = TempSensor(SENSOR_ID, SENDER);
int counter = 0;
void setup() {
Serial.begin(9600);
#if DEBUG
AttinySerial.begin(9600);
#endif
sensor.setup();
// TinyPower::setup();
// TinyPower::enableWdt(WDTO_8S);
analogReference(INTERNAL);
}
void loop() {
// TinyPower::sleep();
unsigned long value = sensor.sendTempAndVoltage(readTemp());
Serial.println(value);
delay(5000);
sensor.sendTempAndVoltage(readTemp());
delay(60000L);
}
int readTemp() {
int reading = analogRead(TEMP_PIN);
#if DEBUG
AttinySerial.println(reading);
#endif
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
float voltage = reading * (1100 / 1024.0);
#if DEBUG
AttinySerial.print(voltage);
AttinySerial.println(" mili volts");
#endif
// 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());
}
float temperatureC = (voltage - 500) / 10;
#if DEBUG
AttinySerial.print(temperatureC);
AttinySerial.println(" degrees C");
#endif
return roundf(temperatureC * 10);
}