Skip to content

Commit e5925e5

Browse files
author
Normunds Gavars
committed
Telemetry: add support of AHT10/AHT20 temp/humidity sensor to Promicro
1 parent 1680eb2 commit e5925e5

File tree

3 files changed

+70
-50
lines changed

3 files changed

+70
-50
lines changed

variants/promicro/platformio.ini

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ build_src_filter = ${nrf52840_base.build_src_filter}
1919
+<../variants/promicro>
2020
lib_deps= ${nrf52840_base.lib_deps}
2121
adafruit/Adafruit SSD1306 @ ^2.5.13
22+
robtillaart/INA3221 @ ^0.4.1
23+
robtillaart/INA219 @ ^0.4.1
24+
adafruit/Adafruit AHTX0@^2.0.5
2225

2326
[env:Faketec_Repeater]
2427
extends = Faketec
@@ -36,8 +39,6 @@ build_flags =
3639
; -D MESH_DEBUG=1
3740
lib_deps = ${Faketec.lib_deps}
3841
adafruit/RTClib @ ^2.1.3
39-
robtillaart/INA3221 @ ^0.4.1
40-
robtillaart/INA219 @ ^0.4.1
4142

4243
[env:Faketec_room_server]
4344
extends = Faketec
@@ -54,8 +55,6 @@ build_flags = ${Faketec.build_flags}
5455
; -D MESH_DEBUG=1
5556
lib_deps = ${Faketec.lib_deps}
5657
adafruit/RTClib @ ^2.1.3
57-
robtillaart/INA3221 @ ^0.4.1
58-
robtillaart/INA219 @ ^0.4.1
5958

6059
[env:Faketec_terminal_chat]
6160
extends = Faketec
@@ -69,8 +68,6 @@ build_src_filter = ${Faketec.build_src_filter}
6968
lib_deps = ${Faketec.lib_deps}
7069
densaugeo/base64 @ ~1.4.0
7170
adafruit/RTClib @ ^2.1.3
72-
robtillaart/INA3221 @ ^0.4.1
73-
robtillaart/INA219 @ ^0.4.1
7471

7572
[env:Faketec_companion_radio_usb]
7673
extends = Faketec
@@ -85,8 +82,6 @@ build_src_filter = ${Faketec.build_src_filter}
8582
lib_deps = ${Faketec.lib_deps}
8683
adafruit/RTClib @ ^2.1.3
8784
densaugeo/base64 @ ~1.4.0
88-
robtillaart/INA3221 @ ^0.4.1
89-
robtillaart/INA219 @ ^0.4.1
9085

9186
[env:Faketec_companion_radio_ble]
9287
extends = Faketec
@@ -107,8 +102,6 @@ build_src_filter = ${Faketec.build_src_filter}
107102
lib_deps = ${Faketec.lib_deps}
108103
adafruit/RTClib @ ^2.1.3
109104
densaugeo/base64 @ ~1.4.0
110-
robtillaart/INA3221 @ ^0.4.1
111-
robtillaart/INA219 @ ^0.4.1
112105

113106
[ProMicroLLCC68]
114107
extends = nrf52840_base
@@ -125,6 +118,10 @@ build_src_filter =
125118
${nrf52840_base.build_src_filter}
126119
+<helpers/nrf52/PromicroBoard.cpp>
127120
+<../variants/promicro>
121+
lib_deps= ${nrf52840_base.lib_deps}
122+
robtillaart/INA3221 @ ^0.4.1
123+
robtillaart/INA219 @ ^0.4.1
124+
adafruit/Adafruit AHTX0@^2.0.5
128125

129126
[env:ProMicroLLCC68_Repeater]
130127
extends = ProMicroLLCC68
@@ -138,8 +135,6 @@ build_flags = ${ProMicroLLCC68.build_flags}
138135
; -D MESH_DEBUG=1
139136
lib_deps = ${ProMicroLLCC68.lib_deps}
140137
adafruit/RTClib @ ^2.1.3
141-
robtillaart/INA3221 @ ^0.4.1
142-
robtillaart/INA219 @ ^0.4.1
143138

144139
[env:ProMicroLLCC68_room_server]
145140
extends = ProMicroLLCC68
@@ -153,8 +148,6 @@ build_flags = ${ProMicroLLCC68.build_flags}
153148
; -D MESH_DEBUG=1
154149
lib_deps = ${ProMicroLLCC68.lib_deps}
155150
adafruit/RTClib @ ^2.1.3
156-
robtillaart/INA3221 @ ^0.4.1
157-
robtillaart/INA219 @ ^0.4.1
158151

159152
[env:ProMicroLLCC68_terminal_chat]
160153
extends = ProMicroLLCC68
@@ -168,8 +161,6 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
168161
lib_deps = ${ProMicroLLCC68.lib_deps}
169162
densaugeo/base64 @ ~1.4.0
170163
adafruit/RTClib @ ^2.1.3
171-
robtillaart/INA3221 @ ^0.4.1
172-
robtillaart/INA219 @ ^0.4.1
173164

174165
[env:ProMicroLLCC68_companion_radio_usb]
175166
extends = ProMicroLLCC68
@@ -183,8 +174,6 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
183174
lib_deps = ${ProMicroLLCC68.lib_deps}
184175
adafruit/RTClib @ ^2.1.3
185176
densaugeo/base64 @ ~1.4.0
186-
robtillaart/INA3221 @ ^0.4.1
187-
robtillaart/INA219 @ ^0.4.1
188177

189178
[env:ProMicroLLCC68_companion_radio_ble]
190179
extends = ProMicroLLCC68
@@ -204,5 +193,3 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
204193
lib_deps = ${ProMicroLLCC68.lib_deps}
205194
adafruit/RTClib @ ^2.1.3
206195
densaugeo/base64 @ ~1.4.0
207-
robtillaart/INA3221 @ ^0.4.1
208-
robtillaart/INA219 @ ^0.4.1

variants/promicro/target.cpp

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,48 +77,41 @@ mesh::LocalIdentity radio_new_identity() {
7777

7878
static INA3221 INA_3221(TELEM_INA3221_ADDRESS, &Wire);
7979
static INA219 INA_219(TELEM_INA219_ADDRESS, &Wire);
80+
static Adafruit_AHTX0 AHTX;
8081

8182
bool PromicroSensorManager::begin() {
82-
if (INA_3221.begin()) {
83-
MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", INA_3221.getAddress());
84-
MESH_DEBUG_PRINTLN("%04X %04X %04X", INA_3221.getDieID(), INA_3221.getManufacturerID(), INA_3221.getConfiguration());
83+
initINA3221();
84+
initINA219();
85+
initAHTX();
8586

86-
for(int i = 0; i < 3; i++) {
87-
INA_3221.setShuntR(i, TELEM_INA3221_SHUNT_VALUE);
88-
}
89-
INA3221initialized = true;
90-
} else {
91-
INA3221initialized = false;
92-
MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS);
93-
}
94-
if (INA_219.begin()) {
95-
MESH_DEBUG_PRINTLN("Found INA219 at address: %02X", INA_219.getAddress());
96-
INA219_CHANNEL = INA3221initialized ? TELEM_CHANNEL_SELF + 4 : TELEM_CHANNEL_SELF + 1;
97-
INA_219.setMaxCurrentShunt(TELEM_INA219_MAX_CURRENT, TELEM_INA219_SHUNT_VALUE);
98-
INA219initialized = true;
99-
} else {
100-
INA219initialized = false;
101-
MESH_DEBUG_PRINTLN("INA219 was not found at I2C address %02X", TELEM_INA219_ADDRESS);
102-
}
10387
return true;
10488
}
10589

10690
bool PromicroSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
91+
int nextAvalableChannel = TELEM_CHANNEL_SELF + 1;
10792
if (requester_permissions & TELEM_PERM_ENVIRONMENT) {
10893
if (INA3221initialized) {
10994
for(int i = 0; i < 3; i++) {
11095
// add only enabled INA3221 channels to telemetry
11196
if (INA3221_CHANNEL_ENABLED[i]) {
112-
telemetry.addVoltage(INA3221_CHANNELS[i], INA_3221.getBusVoltage(i));
113-
telemetry.addCurrent(INA3221_CHANNELS[i], INA_3221.getCurrent(i));
114-
telemetry.addPower(INA3221_CHANNELS[i], INA_3221.getPower(i));
97+
telemetry.addVoltage(nextAvalableChannel, INA_3221.getBusVoltage(i));
98+
telemetry.addCurrent(nextAvalableChannel, INA_3221.getCurrent(i));
99+
telemetry.addPower(nextAvalableChannel, INA_3221.getPower(i));
100+
nextAvalableChannel++;
115101
}
116102
}
117103
}
118-
if(INA219initialized) {
119-
telemetry.addVoltage(INA219_CHANNEL, INA_219.getBusVoltage());
120-
telemetry.addCurrent(INA219_CHANNEL, INA_219.getCurrent());
121-
telemetry.addPower(INA219_CHANNEL, INA_219.getPower());
104+
if (INA219initialized) {
105+
telemetry.addVoltage(nextAvalableChannel, INA_219.getBusVoltage());
106+
telemetry.addCurrent(nextAvalableChannel, INA_219.getCurrent());
107+
telemetry.addPower(nextAvalableChannel, INA_219.getPower());
108+
nextAvalableChannel++;
109+
}
110+
if (AHTXinitialized) {
111+
sensors_event_t humidity, temp;
112+
AHTX.getEvent(&humidity, &temp);
113+
telemetry.addTemperature(TELEM_CHANNEL_SELF, temp.temperature);
114+
telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, humidity.relative_humidity);
122115
}
123116
}
124117

@@ -162,4 +155,40 @@ bool PromicroSensorManager::setSettingValue(const char* name, const char* value)
162155
}
163156
}
164157
return false;
165-
}
158+
}
159+
160+
void PromicroSensorManager::initINA3221() {
161+
if (INA_3221.begin()) {
162+
MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", INA_3221.getAddress());
163+
MESH_DEBUG_PRINTLN("%04X %04X %04X", INA_3221.getDieID(), INA_3221.getManufacturerID(), INA_3221.getConfiguration());
164+
165+
for(int i = 0; i < 3; i++) {
166+
INA_3221.setShuntR(i, TELEM_INA3221_SHUNT_VALUE);
167+
}
168+
INA3221initialized = true;
169+
} else {
170+
INA3221initialized = false;
171+
MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS);
172+
}
173+
}
174+
175+
void PromicroSensorManager::initINA219() {
176+
if (INA_219.begin()) {
177+
MESH_DEBUG_PRINTLN("Found INA219 at address: %02X", INA_219.getAddress());
178+
INA_219.setMaxCurrentShunt(TELEM_INA219_MAX_CURRENT, TELEM_INA219_SHUNT_VALUE);
179+
INA219initialized = true;
180+
} else {
181+
INA219initialized = false;
182+
MESH_DEBUG_PRINTLN("INA219 was not found at I2C address %02X", TELEM_INA219_ADDRESS);
183+
}
184+
}
185+
186+
void PromicroSensorManager::initAHTX() {
187+
if (AHTX.begin(&Wire, 0, TELEM_AHTX_ADDRESS)) {
188+
MESH_DEBUG_PRINTLN("Found AHT10/AHT20 at address: %02X", TELEM_AHTX_ADDRESS);
189+
AHTXinitialized = true;
190+
} else {
191+
AHTXinitialized = false;
192+
MESH_DEBUG_PRINTLN("AHT10/AHT20 was not found at I2C address %02X", TELEM_AHTX_ADDRESS);
193+
}
194+
}

variants/promicro/target.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <helpers/SensorManager.h>
1111
#include <INA3221.h>
1212
#include <INA219.h>
13+
#include <Adafruit_AHTX0.h>
1314

1415
#define NUM_SENSOR_SETTINGS 3
1516

@@ -26,6 +27,7 @@ mesh::LocalIdentity radio_new_identity();
2627

2728
#define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current sensor I2C address
2829
#define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address
30+
#define TELEM_AHTX_ADDRESS 0x38 // AHT10, AHT20 temperature and humidity sensor I2C address
2931

3032
#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts
3133
#define TELEM_INA3221_SETTING_CH1 "INA3221-1"
@@ -38,13 +40,15 @@ mesh::LocalIdentity radio_new_identity();
3840
class PromicroSensorManager: public SensorManager {
3941
bool INA3221initialized = false;
4042
bool INA219initialized = false;
43+
bool AHTXinitialized = false;
4144

4245
// INA3221 channels in telemetry
43-
int INA3221_CHANNELS[NUM_SENSOR_SETTINGS] = {TELEM_CHANNEL_SELF + 1, TELEM_CHANNEL_SELF + 2, TELEM_CHANNEL_SELF+ 3};
4446
const char * INA3221_CHANNEL_NAMES[NUM_SENSOR_SETTINGS] = { TELEM_INA3221_SETTING_CH1, TELEM_INA3221_SETTING_CH2, TELEM_INA3221_SETTING_CH3};
4547
bool INA3221_CHANNEL_ENABLED[NUM_SENSOR_SETTINGS] = {true, true, true};
4648

47-
int INA219_CHANNEL;
49+
void initINA3221();
50+
void initINA219();
51+
void initAHTX();
4852
public:
4953
PromicroSensorManager(){};
5054
bool begin() override;

0 commit comments

Comments
 (0)