BresserWeatherSensorLW
Bresser 868 MHz Weather Sensor Radio Receiver based on ESP32/RP2040 and SX1262/SX1276/LR1121 - sends data to a LoRaWAN Network
Loading...
Searching...
No Matches
AppLayer.h
Go to the documentation of this file.
1
2// AppLayer.h
3//
4// LoRaWAN node application layer
5//
6// created: 04/2024
7//
8//
9// MIT License
10//
11// Copyright (c) 2025 Matthias Prinke
12//
13// Permission is hereby granted, free of charge, to any person obtaining a copy
14// of this software and associated documentation files (the "Software"), to deal
15// in the Software without restriction, including without limitation the rights
16// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17// copies of the Software, and to permit persons to whom the Software is
18// furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included in all
21// copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29// SOFTWARE.
30//
31//
32// History:
33//
34// 20240402 Created
35// 20240414 Added separation between LoRaWAN and application layer
36// 20240417 Added sensor configuration functions
37// 20240419 Modified downlink decoding
38// 20240424 Fixed BLE address initialization from Preferences, added begin()
39// 20240426 Moved bleAddrInit() out of begin()
40// 20240504 Added BresserWeatherSensorLWCmd.h
41// 20240515 Added getOneWireTemperature()
42// 20240520 Moved 1-Wire sensor code to PayloadOneWire.h/.cpp
43// 20240524 Added appPayloadCfgDef, setAppPayloadCfg() & getAppPayloadCfg()
44// Moved code to PayloadBresser, PayloadAnalog & PayloadDigital
45// 20240530 Removed BleSensors as base class & from initializers
46// 20240531 Moved BLE specific code to PayloadBLE.h
47// 20240603 Added appStatus[]
48// 20240607 Added getAppStatusUplinkInterval()
49// 20240716 Added CMD_SCAN_SENSORS
50// 20240722 Renamed STATUS_INTERVAL to APP_STATUS_INTERVAL
51// 20250728 Replaced rtc/clocksync by sysCtx
52//
53// ToDo:
54// -
55//
57
61
62#if !defined(_APPLAYER_H)
63#define _APPLAYER_H
64
65#include "WeatherSensorCfg.h"
66#include <WeatherSensor.h>
67#include <Preferences.h>
68#include "../BresserWeatherSensorLWCfg.h"
69#include "../BresserWeatherSensorLWCmd.h"
70#include "PayloadBresser.h"
71#include "PayloadOneWire.h"
72#include "PayloadAnalog.h"
73#include "PayloadDigital.h"
74#include "PayloadBLE.h"
75#include "SystemContext.h"
76#include <LoraMessage.h>
77
79const uint8_t appPayloadCfgDef[APP_PAYLOAD_CFG_SIZE] = {
80 APP_PAYLOAD_CFG_TYPE00,
81 APP_PAYLOAD_CFG_TYPE01,
82 APP_PAYLOAD_CFG_TYPE02,
83 APP_PAYLOAD_CFG_TYPE03,
84 APP_PAYLOAD_CFG_TYPE04,
85 APP_PAYLOAD_CFG_TYPE05,
86 APP_PAYLOAD_CFG_TYPE06,
87 APP_PAYLOAD_CFG_TYPE07,
88 APP_PAYLOAD_CFG_TYPE08,
89 APP_PAYLOAD_CFG_TYPE09,
90 APP_PAYLOAD_CFG_TYPE10,
91 APP_PAYLOAD_CFG_TYPE11,
92 APP_PAYLOAD_CFG_TYPE12,
93 APP_PAYLOAD_CFG_TYPE13,
94 APP_PAYLOAD_CFG_TYPE14,
95 APP_PAYLOAD_CFG_TYPE15,
96 APP_PAYLOAD_CFG_ONEWIRE1, // onewire[15:8]
97 APP_PAYLOAD_CFG_ONEWIRE0, // onewire[7:0]
98 APP_PAYLOAD_CFG_ANALOG1, // analog[15:0]
99 APP_PAYLOAD_CFG_ANALOG0, // analog[7:0]
100 APP_PAYLOAD_CFG_DIGITAL3, // digital[31:24]
101 APP_PAYLOAD_CFG_DIGITAL2, // digital[23:16]
102 APP_PAYLOAD_CFG_DIGITAL1, // digital[15:8]
103 APP_PAYLOAD_CFG_DIGITAL0 // digital[7:0]
104};
105
112#ifdef ONEWIRE_EN
113 ,
115#endif
116#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
117 ,
119#endif
120{
121private:
122 SystemContext *_sysCtx;
123
125 Preferences appPrefs;
126
128 uint8_t appPayloadCfg[APP_PAYLOAD_CFG_SIZE];
129
131 uint8_t appStatus[APP_STATUS_SIZE];
132
133public:
140#ifdef ONEWIRE_EN
141 ,
143#endif
144#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
145 ,
146 PayloadBLE()
147#endif
148 {
149 _sysCtx = sysCtx;
150 for (int i = 0; i < APP_STATUS_SIZE; i++)
151 {
152 appStatus[i] = 0;
153 }
154 };
155
160 void begin(void)
161 {
163
164 // Sensor scan requested,
165 // no other payload encoders will be used
166 if (ws_scantime) {
167 return;
168 }
169
172#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
174#endif
175
176 if (!getAppPayloadCfg(appPayloadCfg, APP_PAYLOAD_CFG_SIZE))
177 {
178 memcpy(appPayloadCfg, appPayloadCfgDef, APP_PAYLOAD_CFG_SIZE);
179 }
180 };
181
191 uint8_t decodeDownlink(uint8_t port, uint8_t *payload, size_t size);
192
199 void genPayload(uint8_t port, LoraEncoder &encoder);
200
213 void getPayloadStage1(uint8_t &port, LoraEncoder &encoder);
214
226 void getPayloadStage2(uint8_t &port, LoraEncoder &encoder);
227
238 void getConfigPayload(uint8_t cmd, uint8_t &port, LoraEncoder &encoder);
239
246 {
247 appPrefs.begin("BWS-LW-APP", false);
248 uint8_t status_interval = appPrefs.getUChar("app_stat_int", APP_STATUS_INTERVAL);
249 appPrefs.end();
250 return status_interval;
251 };
252
259 void setAppPayloadCfg(uint8_t *bytes, uint8_t size);
260
269 bool getAppPayloadCfg(uint8_t *bytes, uint8_t size);
270};
271#endif // _APPLAYER_H
const uint8_t appPayloadCfgDef[APP_PAYLOAD_CFG_SIZE]
Default AppLayer payload configuration.
Definition AppLayer.h:79
LoRaWAN node application layer - analog channels.
LoRaWAN node application layer - BLE sensors.
LoRaWAN node application layer - Bresser sensors.
LoRaWAN node application layer - digital channels.
LoRaWAN node application layer - 1-Wire sensors.
System context for BresserWeatherSensorLW.
uint8_t decodeDownlink(uint8_t port, uint8_t *payload, size_t size)
Decode app layer specific downlink messages.
Definition AppLayer.cpp:142
void setAppPayloadCfg(uint8_t *bytes, uint8_t size)
Definition AppLayer.cpp:472
void getPayloadStage1(uint8_t &port, LoraEncoder &encoder)
Prepare / get payload at startup.
Definition AppLayer.cpp:86
void genPayload(uint8_t port, LoraEncoder &encoder)
Generate payload (by emulation).
Definition AppLayer.cpp:77
void getPayloadStage2(uint8_t &port, LoraEncoder &encoder)
Get payload before uplink.
Definition AppLayer.cpp:135
void begin(void)
AppLayer initialization.
Definition AppLayer.h:160
AppLayer(SystemContext *sysCtx)
Constructor.
Definition AppLayer.h:139
uint8_t getAppStatusUplinkInterval(void)
Get sensor status message uplink interval.
Definition AppLayer.h:245
void getConfigPayload(uint8_t cmd, uint8_t &port, LoraEncoder &encoder)
Get configuration data for uplink.
Definition AppLayer.cpp:351
bool getAppPayloadCfg(uint8_t *bytes, uint8_t size)
Definition AppLayer.cpp:459
PayloadAnalog()
Constructor.
Definition PayloadAnalog.h:65
void begin(void)
Analog channel startup code.
Definition PayloadAnalog.cpp:47
void begin(void)
BLE startup code.
Definition PayloadBLE.h:100
PayloadBLE()
Constructor.
Definition PayloadBLE.h:95
PayloadBresser(SystemContext *sysCtx)
Constructor.
Definition PayloadBresser.h:167
uint8_t ws_scantime
Weather Sensor Scan Request.
Definition PayloadBresser.h:91
void begin(void)
Bresser sensors startup code.
Definition PayloadBresser.cpp:64
void begin(void)
Digital channel startup code.
Definition PayloadDigital.cpp:46
PayloadDigital()
Constructor.
Definition PayloadDigital.h:74
PayloadOneWire()
Constructor.
Definition PayloadOneWire.h:69
System context for BresserWeatherSensorLW.
Definition SystemContext.h:95