Skip to content

Commit d39398a

Browse files
authored
Merge branch 'master' into adjust_frame_notifications
2 parents 72b3b94 + 159f762 commit d39398a

File tree

8 files changed

+141
-111
lines changed

8 files changed

+141
-111
lines changed

proto

Submodule proto updated from 94bd0aa to 270cbdb

src/mesh/generated/admin.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern const pb_msgdesc_t AdminMessage_msg;
6767
#define AdminMessage_fields &AdminMessage_msg
6868

6969
/* Maximum encoded size of messages (where known) */
70-
#define AdminMessage_size 338
70+
#define AdminMessage_size 351
7171

7272
#ifdef __cplusplus
7373
} /* extern "C" */

src/mesh/generated/deviceonly.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extern const pb_msgdesc_t DeviceState_msg;
8282
#define DeviceState_fields &DeviceState_msg
8383

8484
/* Maximum encoded size of messages (where known) */
85-
#define DeviceState_size 6156
85+
#define DeviceState_size 6169
8686

8787
#ifdef __cplusplus
8888
} /* extern "C" */

src/mesh/generated/portnums.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ typedef enum _PortNum {
2020
PortNum_ADMIN_APP = 6,
2121
PortNum_REPLY_APP = 32,
2222
PortNum_IP_TUNNEL_APP = 33,
23-
PortNum_ENVIRONMENTAL_MEASUREMENT_APP = 34,
2423
PortNum_SERIAL_APP = 64,
2524
PortNum_STORE_FORWARD_APP = 65,
2625
PortNum_RANGE_TEST_APP = 66,
26+
PortNum_ENVIRONMENTAL_MEASUREMENT_APP = 67,
2727
PortNum_PRIVATE_APP = 256,
2828
PortNum_ATAK_FORWARDER = 257,
2929
PortNum_MAX = 511

src/mesh/generated/radioconfig.pb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ PB_BIND(RadioConfig_UserPreferences, RadioConfig_UserPreferences, 2)
1717

1818

1919

20+

src/mesh/generated/radioconfig.pb.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ typedef enum _LocationSharing {
5656
LocationSharing_LocDisabled = 2
5757
} LocationSharing;
5858

59+
typedef enum _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType {
60+
RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11 = 0
61+
} RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType;
62+
5963
/* Struct definitions */
6064
typedef struct _RadioConfig_UserPreferences {
6165
uint32_t position_broadcast_secs;
@@ -106,6 +110,9 @@ typedef struct _RadioConfig_UserPreferences {
106110
uint32_t environmental_measurement_plugin_read_error_count_threshold;
107111
uint32_t environmental_measurement_plugin_update_interval;
108112
uint32_t environmental_measurement_plugin_recovery_interval;
113+
bool environmental_measurement_plugin_display_farenheit;
114+
RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType environmental_measurement_plugin_sensor_type;
115+
uint32_t environmental_measurement_plugin_sensor_pin;
109116
} RadioConfig_UserPreferences;
110117

111118
typedef struct _RadioConfig {
@@ -131,16 +138,20 @@ typedef struct _RadioConfig {
131138
#define _LocationSharing_MAX LocationSharing_LocDisabled
132139
#define _LocationSharing_ARRAYSIZE ((LocationSharing)(LocationSharing_LocDisabled+1))
133140

141+
#define _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_MIN RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11
142+
#define _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_MAX RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11
143+
#define _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_ARRAYSIZE ((RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType)(RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11+1))
144+
134145

135146
#ifdef __cplusplus
136147
extern "C" {
137148
#endif
138149

139150
/* Initializer values for message structs */
140151
#define RadioConfig_init_default {false, RadioConfig_UserPreferences_init_default}
141-
#define RadioConfig_UserPreferences_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", 0, _RegionCode_MIN, _ChargeCurrent_MIN, _LocationSharing_MIN, _GpsOperation_MIN, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
152+
#define RadioConfig_UserPreferences_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", 0, _RegionCode_MIN, _ChargeCurrent_MIN, _LocationSharing_MIN, _GpsOperation_MIN, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_MIN, 0}
142153
#define RadioConfig_init_zero {false, RadioConfig_UserPreferences_init_zero}
143-
#define RadioConfig_UserPreferences_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", 0, _RegionCode_MIN, _ChargeCurrent_MIN, _LocationSharing_MIN, _GpsOperation_MIN, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
154+
#define RadioConfig_UserPreferences_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", 0, _RegionCode_MIN, _ChargeCurrent_MIN, _LocationSharing_MIN, _GpsOperation_MIN, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_MIN, 0}
144155

145156
/* Field tags (for use in manual encoding/decoding) */
146157
#define RadioConfig_UserPreferences_position_broadcast_secs_tag 1
@@ -190,6 +201,9 @@ extern "C" {
190201
#define RadioConfig_UserPreferences_environmental_measurement_plugin_read_error_count_threshold_tag 142
191202
#define RadioConfig_UserPreferences_environmental_measurement_plugin_update_interval_tag 143
192203
#define RadioConfig_UserPreferences_environmental_measurement_plugin_recovery_interval_tag 144
204+
#define RadioConfig_UserPreferences_environmental_measurement_plugin_display_farenheit_tag 145
205+
#define RadioConfig_UserPreferences_environmental_measurement_plugin_sensor_type_tag 146
206+
#define RadioConfig_UserPreferences_environmental_measurement_plugin_sensor_pin_tag 147
193207
#define RadioConfig_preferences_tag 1
194208

195209
/* Struct field encoding specification for nanopb */
@@ -246,7 +260,10 @@ X(a, STATIC, SINGULAR, BOOL, environmental_measurement_plugin_measurement_
246260
X(a, STATIC, SINGULAR, BOOL, environmental_measurement_plugin_screen_enabled, 141) \
247261
X(a, STATIC, SINGULAR, UINT32, environmental_measurement_plugin_read_error_count_threshold, 142) \
248262
X(a, STATIC, SINGULAR, UINT32, environmental_measurement_plugin_update_interval, 143) \
249-
X(a, STATIC, SINGULAR, UINT32, environmental_measurement_plugin_recovery_interval, 144)
263+
X(a, STATIC, SINGULAR, UINT32, environmental_measurement_plugin_recovery_interval, 144) \
264+
X(a, STATIC, SINGULAR, BOOL, environmental_measurement_plugin_display_farenheit, 145) \
265+
X(a, STATIC, SINGULAR, UENUM, environmental_measurement_plugin_sensor_type, 146) \
266+
X(a, STATIC, SINGULAR, UINT32, environmental_measurement_plugin_sensor_pin, 147)
250267
#define RadioConfig_UserPreferences_CALLBACK NULL
251268
#define RadioConfig_UserPreferences_DEFAULT NULL
252269

@@ -258,8 +275,8 @@ extern const pb_msgdesc_t RadioConfig_UserPreferences_msg;
258275
#define RadioConfig_UserPreferences_fields &RadioConfig_UserPreferences_msg
259276

260277
/* Maximum encoded size of messages (where known) */
261-
#define RadioConfig_size 335
262-
#define RadioConfig_UserPreferences_size 332
278+
#define RadioConfig_size 348
279+
#define RadioConfig_UserPreferences_size 345
263280

264281
#ifdef __cplusplus
265282
} /* extern "C" */

src/plugins/esp32/EnvironmentalMeasurementPlugin.cpp

Lines changed: 97 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@
1010
#include <OLEDDisplay.h>
1111
#include <OLEDDisplayUi.h>
1212

13-
EnvironmentalMeasurementPlugin *environmentalMeasurementPlugin;
14-
EnvironmentalMeasurementPluginRadio *environmentalMeasurementPluginRadio;
15-
16-
EnvironmentalMeasurementPlugin::EnvironmentalMeasurementPlugin() : concurrency::OSThread("EnvironmentalMeasurementPlugin") {}
17-
18-
uint32_t sensor_read_error_count = 0;
19-
20-
#define DHT_11_GPIO_PIN 13
2113
#define DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS 1000 // Some sensors (the DHT11) have a minimum required duration between read attempts
2214
#define FAILED_STATE_SENSOR_READ_MULTIPLIER 10
2315
#define DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN true
2416

25-
DHT dht(DHT_11_GPIO_PIN,DHT11);
26-
2717

2818
#ifdef HAS_EINK
2919
// The screen is bigger so use bigger fonts
@@ -49,11 +39,15 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
4939
Uncomment the preferences below if you want to use the plugin
5040
without having to configure it from the PythonAPI or WebUI.
5141
*/
42+
5243
/*radioConfig.preferences.environmental_measurement_plugin_measurement_enabled = 1;
5344
radioConfig.preferences.environmental_measurement_plugin_screen_enabled = 1;
5445
radioConfig.preferences.environmental_measurement_plugin_read_error_count_threshold = 5;
5546
radioConfig.preferences.environmental_measurement_plugin_update_interval = 30;
56-
radioConfig.preferences.environmental_measurement_plugin_recovery_interval = 600;*/
47+
radioConfig.preferences.environmental_measurement_plugin_recovery_interval = 60;
48+
radioConfig.preferences.environmental_measurement_plugin_display_farenheit = true;
49+
radioConfig.preferences.environmental_measurement_plugin_sensor_pin = 13;
50+
radioConfig.preferences.environmental_measurement_plugin_sensor_type = RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType::RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11;*/
5751

5852
if (! (radioConfig.preferences.environmental_measurement_plugin_measurement_enabled || radioConfig.preferences.environmental_measurement_plugin_screen_enabled)){
5953
// If this plugin is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it
@@ -62,20 +56,32 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
6256

6357
if (firstTime) {
6458
// This is the first time the OSThread library has called this function, so do some setup
65-
DEBUG_MSG("EnvironmentalMeasurement: Initializing\n");
66-
environmentalMeasurementPluginRadio = new EnvironmentalMeasurementPluginRadio();
59+
6760
firstTime = 0;
68-
// begin reading measurements from the sensor
69-
// DHT have a max read-rate of 1HZ, so we should wait at least 1 second
70-
// after initializing the sensor before we try to read from it.
71-
// returning the interval here means that the next time OSThread
72-
// calls our plugin, we'll run the other branch of this if statement
73-
// and actually do a "sendOurEnvironmentalMeasurement()"
61+
7462
if (radioConfig.preferences.environmental_measurement_plugin_measurement_enabled)
7563
{
64+
DEBUG_MSG("EnvironmentalMeasurement: Initializing\n");
7665
// it's possible to have this plugin enabled, only for displaying values on the screen.
7766
// therefore, we should only enable the sensor loop if measurement is also enabled
78-
dht.begin();
67+
switch(radioConfig.preferences.environmental_measurement_plugin_sensor_type) {
68+
case RadioConfig_UserPreferences_EnvironmentalMeasurementSensorType_DHT11:
69+
dht = new DHT(radioConfig.preferences.environmental_measurement_plugin_sensor_pin,DHT11);
70+
this->dht->begin();
71+
this->dht->read();
72+
DEBUG_MSG("EnvironmentalMeasurement: Opened DHT11 on pin: %d\n",radioConfig.preferences.environmental_measurement_plugin_sensor_pin);
73+
break;
74+
default:
75+
DEBUG_MSG("EnvironmentalMeasurement: Invalid sensor type selected; Disabling plugin");
76+
return (INT32_MAX);
77+
break;
78+
}
79+
// begin reading measurements from the sensor
80+
// DHT have a max read-rate of 1HZ, so we should wait at least 1 second
81+
// after initializing the sensor before we try to read from it.
82+
// returning the interval here means that the next time OSThread
83+
// calls our plugin, we'll run the other branch of this if statement
84+
// and actually do a "sendOurEnvironmentalMeasurement()"
7985
return(DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
8086
}
8187
return (INT32_MAX);
@@ -96,6 +102,7 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
96102
"EnvironmentalMeasurement: TEMPORARILY DISABLED; The environmental_measurement_plugin_read_error_count_threshold has been exceed: %d. Will retry reads in %d seconds\n",
97103
radioConfig.preferences.environmental_measurement_plugin_read_error_count_threshold,
98104
radioConfig.preferences.environmental_measurement_plugin_recovery_interval);
105+
sensor_read_error_count = 0;
99106
return(radioConfig.preferences.environmental_measurement_plugin_recovery_interval*1000);
100107
}
101108
DEBUG_MSG(
@@ -110,7 +117,7 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
110117
sensor_read_error_count,
111118
radioConfig.preferences.environmental_measurement_plugin_read_error_count_threshold-sensor_read_error_count);
112119
}
113-
if (! environmentalMeasurementPluginRadio->sendOurEnvironmentalMeasurement() ){
120+
if (!sendOurEnvironmentalMeasurement() ){
114121
// if we failed to read the sensor, then try again
115122
// as soon as we can according to the maximum polling frequency
116123
return(DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS);
@@ -123,20 +130,10 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
123130
#endif
124131
}
125132

126-
bool EnvironmentalMeasurementPluginRadio::wantUIFrame() {
133+
bool EnvironmentalMeasurementPlugin::wantUIFrame() {
127134
return radioConfig.preferences.environmental_measurement_plugin_screen_enabled;
128135
}
129136

130-
void EnvironmentalMeasurementPluginRadio::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
131-
{
132-
display->setTextAlignment(TEXT_ALIGN_LEFT);
133-
display->setFont(FONT_MEDIUM);
134-
display->drawString(x, y, "Environment");
135-
display->setFont(FONT_SMALL);
136-
display->drawString(x, y += fontHeight(FONT_MEDIUM), lastSender+": T:"+ String(lastMeasurement.temperature,2) + " H:" + String(lastMeasurement.relative_humidity,2));
137-
138-
}
139-
140137
String GetSenderName(const MeshPacket &mp) {
141138
String sender;
142139

@@ -149,55 +146,99 @@ String GetSenderName(const MeshPacket &mp) {
149146
return sender;
150147
}
151148

152-
bool EnvironmentalMeasurementPluginRadio::handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement *pptr)
149+
uint32_t GetTimeSinceMeshPacket(const MeshPacket *mp) {
150+
uint32_t now = getTime();
151+
152+
uint32_t last_seen = mp->rx_time;
153+
int delta = (int)(now - last_seen);
154+
if (delta < 0) // our clock must be slightly off still - not set from GPS yet
155+
delta = 0;
156+
157+
return delta;
158+
159+
}
160+
161+
162+
float EnvironmentalMeasurementPlugin::CelsiusToFarenheit(float c) {
163+
return (c*9)/5 + 32;
164+
}
165+
166+
167+
void EnvironmentalMeasurementPlugin::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
153168
{
154-
const EnvironmentalMeasurement &p = *pptr;
169+
display->setTextAlignment(TEXT_ALIGN_LEFT);
170+
display->setFont(FONT_MEDIUM);
171+
display->drawString(x, y, "Environment");
172+
if (lastMeasurementPacket == nullptr) {
173+
display->setFont(FONT_SMALL);
174+
display->drawString(x, y += fontHeight(FONT_MEDIUM), "No measurement");
175+
//DEBUG_MSG("EnvironmentalMeasurement: No previous measurement; not drawing frame\n");
176+
return;
177+
}
178+
179+
EnvironmentalMeasurement lastMeasurement;
155180

181+
182+
uint32_t agoSecs = GetTimeSinceMeshPacket(lastMeasurementPacket);
183+
String lastSender = GetSenderName(*lastMeasurementPacket);
184+
185+
auto &p = lastMeasurementPacket->decoded;
186+
if (!pb_decode_from_bytes(p.payload.bytes,
187+
p.payload.size,
188+
EnvironmentalMeasurement_fields,
189+
&lastMeasurement)) {
190+
display->setFont(FONT_SMALL);
191+
display->drawString(x, y += fontHeight(FONT_MEDIUM), "Measurement Error");
192+
DEBUG_MSG("EnvironmentalMeasurement: unable to decode last packet");
193+
return;
194+
}
195+
196+
display->setFont(FONT_SMALL);
197+
String last_temp = String(lastMeasurement.temperature,0) +"°C";
198+
if (radioConfig.preferences.environmental_measurement_plugin_display_farenheit){
199+
last_temp = String(CelsiusToFarenheit(lastMeasurement.temperature),0) +"°F";;
200+
}
201+
202+
display->drawString(x, y += fontHeight(FONT_MEDIUM), lastSender+": "+last_temp +"/"+ String(lastMeasurement.relative_humidity,0) + "%("+String(agoSecs)+"s)");
203+
204+
}
205+
206+
bool EnvironmentalMeasurementPlugin::handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement *p)
207+
{
156208
if (!(radioConfig.preferences.environmental_measurement_plugin_measurement_enabled || radioConfig.preferences.environmental_measurement_plugin_screen_enabled)){
157209
// If this plugin is not enabled in any capacity, don't handle the packet, and allow other plugins to consume
158210
return false;
159211
}
160-
bool wasBroadcast = mp.to == NODENUM_BROADCAST;
161212

162213
String sender = GetSenderName(mp);
163-
164-
// Show new nodes on LCD screen
165-
if (DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN && wasBroadcast) {
166-
String lcd = String("Env Measured: ") +sender + "\n" +
167-
"T: " + p.temperature + "\n" +
168-
"H: " + p.relative_humidity + "\n";
169-
screen->print(lcd.c_str());
170-
}
171-
DEBUG_MSG("-----------------------------------------\n");
172214

173215
DEBUG_MSG("EnvironmentalMeasurement: Received data from %s\n", sender);
174-
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", p.relative_humidity);
175-
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", p.temperature);
216+
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", p->relative_humidity);
217+
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", p->temperature);
218+
219+
lastMeasurementPacket = packetPool.allocCopy(mp);
176220

177-
lastMeasurement = p;
178-
lastSender = sender;
179221
return false; // Let others look at this message also if they want
180222
}
181223

182-
bool EnvironmentalMeasurementPluginRadio::sendOurEnvironmentalMeasurement(NodeNum dest, bool wantReplies)
224+
bool EnvironmentalMeasurementPlugin::sendOurEnvironmentalMeasurement(NodeNum dest, bool wantReplies)
183225
{
184226
EnvironmentalMeasurement m;
185227

186228
m.barometric_pressure = 0; // TODO: Add support for barometric sensors
187-
m.relative_humidity = dht.readHumidity();
188-
m.temperature = dht.readTemperature();;
189-
190229
DEBUG_MSG("-----------------------------------------\n");
191230

192231
DEBUG_MSG("EnvironmentalMeasurement: Read data\n");
193-
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", m.relative_humidity);
194-
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", m.temperature);
195-
196-
if (isnan(m.relative_humidity) || isnan(m.temperature) ){
232+
if (!this->dht->read(true)){
197233
sensor_read_error_count++;
198234
DEBUG_MSG("EnvironmentalMeasurement: FAILED TO READ DATA\n");
199235
return false;
200236
}
237+
m.relative_humidity = this->dht->readHumidity();
238+
m.temperature = this->dht->readTemperature();
239+
240+
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", m.relative_humidity);
241+
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", m.temperature);
201242

202243
sensor_read_error_count = 0;
203244

0 commit comments

Comments
 (0)