Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize the use of JSON_MSG_BUFFER #1114

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions main/ZgatewayRF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,7 @@ void setupRF() {

void RFtoMQTT() {
if (mySwitch.available()) {
# ifdef ZradioCC1101 //receiving with CC1101
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(8);
# else
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(7);
# endif
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
Log.trace(F("Rcv. RF" CR));
# ifdef ESP32
Expand Down
3 changes: 1 addition & 2 deletions main/ZgatewayRF2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ void RF2toMQTTdiscovery(JsonObject& data) {
void RF2toMQTT() {
if (rf2rd.hasNewData) {
Log.trace(F("Creating RF2 buffer" CR));
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RF2data = jsonBuffer.to<JsonObject>();

rf2rd.hasNewData = false;
Expand Down
15 changes: 5 additions & 10 deletions main/ZgatewayWeatherStation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ void PairedDeviceAdded(byte newID) {
Serial.print("ZgatewayWeatherStation: New device paired ");
Serial.println(newID, DEC);
# endif
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(2);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["sensor"] = newID;
RFdata["action"] = "paired";
Expand All @@ -55,8 +54,7 @@ void setupWeatherStation() {
void sendWindSpeedData(byte id, float wind_speed, byte battery_status) {
unsigned long MQTTvalue = 10000 + round(wind_speed);
if (!isAduplicateSignal(MQTTvalue)) { // conditions to avoid duplications of RF -->MQTT
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(3);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["sensor"] = id;
RFdata["wind_speed"] = wind_speed;
Expand All @@ -70,8 +68,7 @@ void sendWindSpeedData(byte id, float wind_speed, byte battery_status) {
void sendRainData(byte id, float rain_volume, byte battery_status) {
unsigned long MQTTvalue = 11000 + round(rain_volume * 10.0);
if (!isAduplicateSignal(MQTTvalue)) { // conditions to avoid duplications of RF -->MQTT
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(3);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["sensor"] = id;
RFdata["rain_volume"] = rain_volume;
Expand All @@ -85,8 +82,7 @@ void sendRainData(byte id, float rain_volume, byte battery_status) {
void sendWindData(byte id, int wind_direction, float wind_gust, byte battery_status) {
unsigned long MQTTvalue = 20000 + round(wind_gust * 10.0) + wind_direction;
if (!isAduplicateSignal(MQTTvalue)) { // conditions to avoid duplications of RF -->MQTT
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["sensor"] = id;
RFdata["wind_direction"] = wind_direction;
Expand All @@ -101,8 +97,7 @@ void sendWindData(byte id, int wind_direction, float wind_gust, byte battery_sta
void sendTemperatureData(byte id, float temperature, int humidity, byte battery_status) {
unsigned long MQTTvalue = 40000 + abs(round(temperature * 100.0)) + humidity;
if (!isAduplicateSignal(MQTTvalue)) { // conditions to avoid duplications of RF -->MQTT
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject RFdata = jsonBuffer.to<JsonObject>();
RFdata["sensor"] = id;
RFdata["tempc"] = temperature;
Expand Down
7 changes: 1 addition & 6 deletions main/ZsensorADC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ void MeasureADC() {
} else {
if (val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC) {
Log.trace(F("Creating ADC buffer" CR));
# if defined(ADC_DIVIDER)
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(2);
# else
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
# endif
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject ADCdata = jsonBuffer.to<JsonObject>();
ADCdata["adc"] = (int)val;
# if defined(ADC_DIVIDER)
Expand Down
3 changes: 1 addition & 2 deletions main/ZsensorGPIOInput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ void MeasureGPIOInput() {
if (reading != InputState) {
InputState = reading;
Log.trace(F("Creating GPIOInput buffer" CR));
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject GPIOdata = jsonBuffer.to<JsonObject>();
if (InputState == HIGH) {
GPIOdata["gpio"] = "HIGH";
Expand Down
3 changes: 1 addition & 2 deletions main/ZsensorHCSR501.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ void setupHCSR501() {

void MeasureHCSR501() {
if (millis() > TimeBeforeStartHCSR501) { //let time to init the PIR sensor
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
StaticJsonDocument<JSON_MSG_CALC_BUFFER> jsonBuffer;
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject HCSR501data = jsonBuffer.to<JsonObject>();
static int pirState = LOW;
int PresenceValue = digitalRead(HCSR501_GPIO);
Expand Down