request all measruements from all meter
This commit is contained in:
parent
071cda7035
commit
520e5fac16
77
main.c
77
main.c
|
@ -258,13 +258,8 @@ void main(void)
|
|||
|
||||
// setup SDM120 electricity meter
|
||||
printf("setup SDM120 electricity meter: ");
|
||||
sensor_sdm120_setup(); // setup SDM120 electricity meter
|
||||
sensor_sdm120_setup(9600); // setup SDM120 electricity meter (get baud rate by scrolling through the menu on the device)
|
||||
printf("OK\n");
|
||||
sensor_sdm120_measurement_request(2,SENSOR_SDM120_VOLTAGE);
|
||||
//sensor_sdm120_configuration_request(1,SENSOR_SDM120_METER_ID);
|
||||
//sensor_sdm120_configuration_request(1,SENSOR_SDM120_BAUD_RATE);
|
||||
//sensor_sdm120_configuration_set(1,SENSOR_SDM120_METER_ID,2);
|
||||
//sensor_sdm120_configuration_set(2,SENSOR_SDM120_BAUD_RATE,2);
|
||||
|
||||
//setup ESP8266 WiFi SoC
|
||||
printf("setup ESP8266 WiFi SoC: ");
|
||||
|
@ -307,9 +302,18 @@ void main(void)
|
|||
char c = ' '; // to store received character
|
||||
bool char_flag = false; // a new character has been received
|
||||
|
||||
struct sensor_pzem_measurement_t pzem_measurements[2][4]; // PZEM-004T measurements
|
||||
uint8_t pzem_measurement_i = 0; // PZEM-004T measurement index
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_measurement_i, SENSOR_PZEM_VOLTAGE); // start measurement (based on base address plus meter number, and voltage is the first measurement)
|
||||
// start getting all PZEM-004T measurements from all meters
|
||||
struct sensor_pzem_measurement_t pzem_measurements[2][SENSOR_PZEM_MAX]; // PZEM-004T measurements (2 meters, all measurements)
|
||||
uint8_t pzem_id = 0; // PZEM-004T meter index (add to prefix)
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_id, 0); // request first measurement
|
||||
(void)pzem_measurements[pzem_id][0];
|
||||
|
||||
// start getting all SDM120 measurements from all meters
|
||||
float sdm120_measurements[2][SENSOR_SDM120_MEASUREMENT_MAX]; // SDM120 measurements (2 meters, all measurements)
|
||||
uint8_t sdm120_meter = 0; // SDM120 meter index (add to 1 to get ID)
|
||||
uint8_t sdm120_measurement = 0; // SDM120 measurement index
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request first measurement
|
||||
(void)sdm120_measurements[sdm120_meter][sdm120_measurement];
|
||||
|
||||
while (true) { // infinite loop
|
||||
iwdg_reset(); // kick the dog
|
||||
|
@ -346,46 +350,69 @@ void main(void)
|
|||
}
|
||||
while (sensor_pzem_measurement_received) { // measurement from electricity meter received
|
||||
struct sensor_pzem_measurement_t measurement = sensor_pzem_measurement_decode(); // decode measurement
|
||||
pzem_measurements[pzem_measurement_i][measurement.type] = measurement; // save measurement (the type matches the index)
|
||||
(void)pzem_measurements[pzem_measurement_i][measurement.type];
|
||||
if (measurement.type>=SENSOR_PZEM_MAX) {
|
||||
fprintf(stderr,"unknown measurement type: %u\n", measurement.type);
|
||||
while (true); // unhandled error
|
||||
}
|
||||
pzem_measurements[pzem_id][measurement.type] = measurement; // save measurement (the type matches the index)
|
||||
if (measurement.valid) { // only show valid measurement
|
||||
switch (measurement.type) {
|
||||
case SENSOR_PZEM_VOLTAGE:
|
||||
printf("PZEM-004T meter %u voltage: %.01f V\n", pzem_measurement_i, measurement.value.voltage); // display measurement
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_measurement_i, SENSOR_PZEM_CURRENT); // request next measurement
|
||||
printf("PZEM-004T meter %u voltage: %.01f V\n", pzem_id, measurement.value.voltage); // display measurement
|
||||
break;
|
||||
case SENSOR_PZEM_CURRENT:
|
||||
printf("PZEM-004T meter %u current: %.02f A\n", pzem_measurement_i, measurement.value.current);
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_measurement_i, SENSOR_PZEM_POWER); // request next measurement
|
||||
printf("PZEM-004T meter %u current: %.02f A\n", pzem_id, measurement.value.current);
|
||||
break;
|
||||
case SENSOR_PZEM_POWER:
|
||||
printf("PZEM-004T meter %u power: %.00f W\n", pzem_measurement_i, measurement.value.power);
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_measurement_i, SENSOR_PZEM_ENERGY); // request next measurement
|
||||
printf("PZEM-004T meter %u power: %.00f W\n", pzem_id, measurement.value.power);
|
||||
break;
|
||||
case SENSOR_PZEM_ENERGY:
|
||||
printf("PZEM-004T meter %u energy: %lu Wh\n", pzem_measurement_i, measurement.value.energy);
|
||||
pzem_measurement_i++; // got to next meter (sending to none existing meter will just end in void)
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_measurement_i, SENSOR_PZEM_VOLTAGE); // request next measurement
|
||||
printf("PZEM-004T meter %u energy: %lu Wh\n", pzem_id, measurement.value.energy);
|
||||
break;
|
||||
/* not used for this application
|
||||
case SENSOR_PZEM_ADDRESS:
|
||||
printf("PZEM-004T meter %u address set\n", pzem_measurement_i);
|
||||
printf("PZEM-004T meter %u address set\n", pzem_id);
|
||||
break;
|
||||
case SENSOR_PZEM_ALARM:
|
||||
printf("PZEM-004T meter %u alarm threshold set\n", pzem_measurement_i);
|
||||
printf("PZEM-004T meter %u alarm threshold set\n", pzem_id);
|
||||
break;
|
||||
*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (measurement.type<SENSOR_PZEM_MAX-1) { // not all measurement types requested
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_id, measurement.type+1); // request next measurement
|
||||
} else { // all measurement types requested
|
||||
pzem_id++; // got to next meter
|
||||
if (pzem_id<LENGTH(pzem_measurements)) { // ensure next meter exists
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_id, 0); // request measurement for next meter
|
||||
}
|
||||
}
|
||||
} else { // measurement not valid
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_id, measurement.type+1); // request same measurement
|
||||
}
|
||||
}
|
||||
while (sensor_sdm120_measurement_received) { // measurement from electricity meter received
|
||||
float measurement = sensor_sdm120_measurement_decode(); // decode measurement
|
||||
if (isnan(measurement)) {
|
||||
printf("error in response\n");
|
||||
printf("error in SDM120 response\n");
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request same measurement
|
||||
} else if (isinf(measurement)) {
|
||||
printf("error message received\n");
|
||||
printf("error SDM120 message received\n");
|
||||
while (true); // unhandled error
|
||||
} else {
|
||||
printf("measurement: %.01f\n",measurement);
|
||||
sdm120_measurements[sdm120_meter][sdm120_measurement] = measurement; // save measurement
|
||||
printf("SDM120 meter %u measurement %u: %.01f\n", sdm120_meter, sdm120_measurement, measurement);
|
||||
if (sdm120_measurement<SENSOR_SDM120_MEASUREMENT_MAX-1) { // not all measurement type required
|
||||
sdm120_measurement++; // go to next measurement
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request next measurement
|
||||
} else { // all measurement types requested
|
||||
sdm120_meter++; // got to next meter (sending to none existing meter will just end in void)
|
||||
sdm120_measurement = 0; // start requesting all measurement
|
||||
if (sdm120_meter<LENGTH(sdm120_measurements)) { // ensure next meter exists
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request measurement for next meter
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (button_flag) { // user pressed button
|
||||
|
|
Loading…
Reference in New Issue