Browse Source

consolidate redundant sml energy fields

Thomas Buck 4 months ago
parent
commit
7211a8d1ca
2 changed files with 36 additions and 14 deletions
  1. 2
    3
      src/lora.cpp
  2. 34
    11
      src/smart_meter.cpp

+ 2
- 3
src/lora.cpp View File

133
 static bool lora_tx(enum lora_sml_type type, double value) {
133
 static bool lora_tx(enum lora_sml_type type, double value) {
134
     bool tx_legal = millis() > (last_tx + minimum_pause);
134
     bool tx_legal = millis() > (last_tx + minimum_pause);
135
     if (!tx_legal) {
135
     if (!tx_legal) {
136
-        //debug.printf("Legal limit, wait %i sec.\n", (int)((minimum_pause - (millis() - last_tx)) / 1000) + 1);
137
         return false;
136
         return false;
138
     }
137
     }
139
 
138
 
145
     uint8_t *data = (uint8_t *)&msg;
144
     uint8_t *data = (uint8_t *)&msg;
146
     const size_t len = sizeof(struct lora_sml_msg);
145
     const size_t len = sizeof(struct lora_sml_msg);
147
 
146
 
147
+    debug.printf("TX [%d] (%lu) ", data[0], len);
148
+
148
 #ifdef LORA_XOR_KEY
149
 #ifdef LORA_XOR_KEY
149
     for (size_t i = 0; i < len; i++) {
150
     for (size_t i = 0; i < len; i++) {
150
         data[i] ^= LORA_XOR_KEY[i];
151
         data[i] ^= LORA_XOR_KEY[i];
151
     }
152
     }
152
 #endif
153
 #endif
153
 
154
 
154
-    debug.printf("TX [%d] (%lu) ", data[0], len);
155
     radio.clearDio1Action();
155
     radio.clearDio1Action();
156
 
156
 
157
     heltec_led(LORA_LED_BRIGHTNESS);
157
     heltec_led(LORA_LED_BRIGHTNESS);
318
         return;
318
         return;
319
     }
319
     }
320
 
320
 
321
-    // If a packet was received, display it and the RSSI and SNR
322
     if (rx_flag) {
321
     if (rx_flag) {
323
         rx_flag = false;
322
         rx_flag = false;
324
 
323
 

+ 34
- 11
src/smart_meter.cpp View File

115
 
115
 
116
         if (!isnan(SumWh)) {
116
         if (!isnan(SumWh)) {
117
             debug.printf("Sum: %14.3lf Wh\n", SumWh);
117
             debug.printf("Sum: %14.3lf Wh\n", SumWh);
118
-#ifdef FEATURE_LORA
119
-            lora_sml_send(LORA_SML_SUM_WH, SumWh, counter);
120
-#endif // FEATURE_LORA
121
         }
118
         }
122
 
119
 
123
         if (!isnan(T1Wh)) {
120
         if (!isnan(T1Wh)) {
124
             debug.printf(" T1: %14.3lf Wh\n", T1Wh);
121
             debug.printf(" T1: %14.3lf Wh\n", T1Wh);
125
-#ifdef FEATURE_LORA
126
-            lora_sml_send(LORA_SML_T1_WH, T1Wh, counter);
127
-#endif // FEATURE_LORA
128
         }
122
         }
129
 
123
 
130
         if (!isnan(T2Wh)) {
124
         if (!isnan(T2Wh)) {
131
             debug.printf(" T2: %14.3lf Wh\n", T2Wh);
125
             debug.printf(" T2: %14.3lf Wh\n", T2Wh);
132
-#ifdef FEATURE_LORA
133
-            lora_sml_send(LORA_SML_T2_WH, T2Wh, counter);
134
-#endif // FEATURE_LORA
135
         }
126
         }
136
 
127
 
137
         if (!isnan(SumW)) {
128
         if (!isnan(SumW)) {
138
             debug.printf("Sum: %14.3lf W\n", SumW);
129
             debug.printf("Sum: %14.3lf W\n", SumW);
130
+
139
 #ifdef FEATURE_LORA
131
 #ifdef FEATURE_LORA
140
             lora_sml_send(LORA_SML_SUM_W, SumW, counter);
132
             lora_sml_send(LORA_SML_SUM_W, SumW, counter);
141
 #endif // FEATURE_LORA
133
 #endif // FEATURE_LORA
150
 
142
 
151
         if (!isnan(L2W)) {
143
         if (!isnan(L2W)) {
152
             debug.printf(" L2: %14.3lf W\n", L2W);
144
             debug.printf(" L2: %14.3lf W\n", L2W);
145
+
153
 #ifdef FEATURE_LORA
146
 #ifdef FEATURE_LORA
154
             lora_sml_send(LORA_SML_L2_W, L2W, counter);
147
             lora_sml_send(LORA_SML_L2_W, L2W, counter);
155
 #endif // FEATURE_LORA
148
 #endif // FEATURE_LORA
157
 
150
 
158
         if (!isnan(L3W)) {
151
         if (!isnan(L3W)) {
159
             debug.printf(" L3: %14.3lf W\n", L3W);
152
             debug.printf(" L3: %14.3lf W\n", L3W);
153
+
160
 #ifdef FEATURE_LORA
154
 #ifdef FEATURE_LORA
161
             lora_sml_send(LORA_SML_L3_W, L3W, counter);
155
             lora_sml_send(LORA_SML_L3_W, L3W, counter);
162
 #endif // FEATURE_LORA
156
 #endif // FEATURE_LORA
163
         }
157
         }
164
 
158
 
159
+        debug.println();
160
+
165
 #ifdef FEATURE_LORA
161
 #ifdef FEATURE_LORA
162
+        // the power readings (Watt) are just sent as is, if available.
163
+        // the energy readings are consolidated if possible, to avoid
164
+        // unneccessary data transmission
165
+        if ((!isnan(SumWh)) && (!isnan(T1Wh))
166
+                && (fabs(SumWh - T1Wh) < 0.1)
167
+                && ((isnan(T2Wh)) || (fabs(T2Wh) < 0.1))) {
168
+            // (SumWh == T1Wh) && ((T2Wh == 0) || (T2Wh == NAN))
169
+            // just send T1Wh
170
+            lora_sml_send(LORA_SML_T1_WH, T1Wh, counter);
171
+        } else if ((!isnan(SumWh)) && (!isnan(T2Wh))
172
+                && (fabs(SumWh - T2Wh) < 0.1)
173
+                && ((isnan(T1Wh)) || (fabs(T1Wh) < 0.1))) {
174
+            // (SumWh == T2Wh) && ((T1Wh == 0) || (T1Wh == NAN))
175
+            // just send T2Wh
176
+            lora_sml_send(LORA_SML_T2_WH, T2Wh, counter);
177
+        } else {
178
+            // just do normal sending if available
179
+            if (!isnan(SumWh)) {
180
+                lora_sml_send(LORA_SML_SUM_WH, SumWh, counter);
181
+            }
182
+            if (!isnan(T1Wh)) {
183
+                lora_sml_send(LORA_SML_T1_WH, T1Wh, counter);
184
+            }
185
+            if (!isnan(T2Wh)) {
186
+                lora_sml_send(LORA_SML_T2_WH, T2Wh, counter);
187
+            }
188
+        }
189
+
190
+        // update own battery state with each sml readout
166
         lora_sml_send(LORA_SML_BAT_V, lora_get_mangled_bat(), counter);
191
         lora_sml_send(LORA_SML_BAT_V, lora_get_mangled_bat(), counter);
167
 #endif // FEATURE_LORA
192
 #endif // FEATURE_LORA
168
-
169
-        debug.println();
170
     }
193
     }
171
 }
194
 }
172
 
195
 

Loading…
Cancel
Save