rc-gateway/temp_sensor/src/temp_sensor.cpp
2021-12-24 15:09:37 +01:00

77 lines
1.2 KiB
C++

#include <Arduino.h>
#include <Tiny.h>
#include <TinyPower.h>
#include <SoftwareSerial_Tiny.h>
#define DEBUG 0
#define SEND_INTERVAL (int)(5*60/8)
// Pins
#define SENDER PIN_B4
#define TEMP_PIN A1
#if DEBUG
#define RxD PIN_B3
#define TxD PIN_B4
SoftwareSerial AttinySerial(RxD, TxD);
#endif
int readTemp();
TempSensor sensor = TempSensor(TEMP_SENSOR, SENDER);
volatile int counter = 0;
volatile bool shouldSend = true;
void setup() {
#if DEBUG
AttinySerial.begin(9600);
AttinySerial.println("starting...");
#endif
sensor.setup();
analogReference(INTERNAL);
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
}
void loop() {
if (shouldSend) {
shouldSend = false;
sensor.sendTemp(readTemp());
}
TinyPower::sleep();
}
int readTemp() {
int reading = analogRead(TEMP_PIN);
#if DEBUG
AttinySerial.println(reading);
#endif
float voltage = reading * (1100 / 1024.0);
#if DEBUG
AttinySerial.print(voltage);
AttinySerial.println(" mili volts");
#endif
float temperatureC = (voltage - 500) / 10;
#if DEBUG
AttinySerial.print(temperatureC);
AttinySerial.println(" degrees C");
#endif
return roundf(temperatureC * 10);
}
ISR(PCINT0_vect) {
}
ISR(WDT_vect) {
counter++;
if (counter % SEND_INTERVAL == 0) {
shouldSend = true;
counter = 0;
}
}