forked from jp112sdl/HB-UNI-Sen-IAQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HB-UNI-Sen-IAQ.ino
178 lines (152 loc) · 5.53 KB
/
HB-UNI-Sen-IAQ.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// 2019-02-28 jp112sdl (Creative Commons)
//- -----------------------------------------------------------------------------------------------------------------------
// #define NDEBUG // disable all serial debug messages
// #define USE_CC1101_ALT_FREQ_86835 //use alternative frequency to compensate not correct working cc1101 modules
#define SENSOR_ONLY
#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <AskSinPP.h>
#include <LowPower.h>
#include <Register.h>
#include <MultiChannelDevice.h>
#include "sensors/sens_bme680.h"
#define LED_PIN 4
#define CONFIG_BUTTON_PIN 8
#define PEERS_PER_CHANNEL 6
// all library classes are placed in the namespace 'as'
using namespace as;
// define all device properties
const struct DeviceInfo PROGMEM devinfo = {
{0xf1, 0xd1, 0x00}, // Device ID
"JPIAQ00000", // Device Serial
{0xf1, 0xd1}, // Device Model Indoor
0x10, // Firmware Version
as::DeviceType::THSensor, // Device Type
{0x01, 0x00} // Info Bytes
};
/**
Configure the used hardware
*/
typedef AvrSPI<10, 11, 12, 13> SPIType;
typedef Radio<SPIType, 2> RadioType;
typedef StatusLed<LED_PIN> LedType;
typedef AskSin<LedType, BatterySensor, RadioType> BaseHal;
class Hal : public BaseHal {
public:
void init (const HMID& id) {
BaseHal::init(id);
#ifdef USE_CC1101_ALT_FREQ_86835
// 2165E8 == 868.35 MHz
radio.initReg(CC1101_FREQ2, 0x21);
radio.initReg(CC1101_FREQ1, 0x65);
radio.initReg(CC1101_FREQ0, 0xE8);
#endif
// measure battery every 1h
battery.init(seconds2ticks(60UL * 60), sysclock);
battery.low(22);
battery.critical(19);
}
bool runready () {
return sysclock.runready() || BaseHal::runready();
}
} hal;
DEFREGISTER(Reg0, MASTERID_REGS, 0x20, 0x21, 0x22, 0x23)
class SensorList0 : public RegList0<Reg0> {
public:
SensorList0(uint16_t addr) : RegList0<Reg0>(addr) {}
bool updIntervall (uint16_t value) const {
return this->writeRegister(0x20, (value >> 8) & 0xff) && this->writeRegister(0x21, value & 0xff);
}
uint16_t updIntervall () const {
return (this->readRegister(0x20, 0) << 8) + this->readRegister(0x21, 0);
}
bool height (uint16_t value) const {
return this->writeRegister(0x22, (value >> 8) & 0xff) && this->writeRegister(0x23, value & 0xff);
}
uint16_t height () const {
return (this->readRegister(0x22, 0) << 8) + this->readRegister(0x23, 0);
}
void defaults () {
clear();
updIntervall(10);
height(34);
}
};
class WeatherEventMsg : public Message {
public:
void init(uint8_t msgcnt, int16_t temp, uint16_t airPressure, uint8_t humidity, uint8_t iaq_percent, uint8_t iaq_state, bool batlow, uint8_t volt) {
uint8_t t1 = (temp >> 8) & 0x7f;
uint8_t t2 = temp & 0xff;
if ( batlow == true ) {
t1 |= 0x80; // set bat low bit
}
Message::init(0x11, msgcnt, 0x70, BIDI | WKMEUP, t1, t2);
pload[0] = (airPressure >> 8) & 0xff;
pload[1] = airPressure & 0xff;
pload[2] = humidity & 0xff;
pload[3] = iaq_percent & 0xff;
pload[4] = iaq_state & 0xff;
pload[5] = volt & 0xff;
}
};
class WeatherChannel : public Channel<Hal, List1, EmptyList, List4, PEERS_PER_CHANNEL, SensorList0>, public Alarm {
WeatherEventMsg msg;
Sens_Bme680<> bme680;
uint16_t millis;
public:
WeatherChannel () : Channel(), Alarm(10), millis(0) {}
virtual ~WeatherChannel () {}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
uint8_t msgcnt = device().nextcount();
// reactivate for next measure
tick = delay();
clock.add(*this);
bme680.measure(this->device().getList0().height());
msg.init( msgcnt,bme680.temperature(),bme680.pressureNN(),bme680.humidity(),bme680.iaq_percent(), bme680.iaq_state(), device().battery().low(), device().battery().current());
if (msgcnt % 20 == 1) device().sendPeerEvent(msg, *this); else device().broadcastEvent(msg, *this);
}
uint32_t delay () {
return seconds2ticks(max(this->device().getList0().updIntervall(),10));
}
void setup(Device<Hal, SensorList0>* dev, uint8_t number, uint16_t addr) {
Channel::setup(dev, number, addr);
bme680.init();
sysclock.add(*this);
}
uint8_t status () const {
return 0;
}
uint8_t flags () const {
return 0;
}
};
class IAQDevice : public MultiChannelDevice<Hal, WeatherChannel, 1, SensorList0> {
public:
typedef MultiChannelDevice<Hal, WeatherChannel, 1, SensorList0> TSDevice;
IAQDevice(const DeviceInfo& info, uint16_t addr) : TSDevice(info, addr) {}
virtual ~IAQDevice () {}
virtual void configChanged () {
TSDevice::configChanged();
//DPRINTLN("* Config Changed : List0");
//DPRINT(F("* SENDEINTERVALL : ")); DDECLN(this->getList0().updIntervall());
//DPRINT(F("* ALTITUDE : ")); DDECLN(this->getList0().height());
}
};
IAQDevice sdev(devinfo, 0x20);
ConfigButton<IAQDevice> cfgBtn(sdev);
void setup () {
DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
sdev.init(hal);
buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
sdev.initDone();
}
void loop() {
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if ( worked == false && poll == false ) {
hal.activity.savePower<Sleep<>>(hal);
}
}