aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-11 01:39:21 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-11 01:39:21 +0100
commitcf0df8b102806c4087814b895601321a32026e54 (patch)
treeef5315fd10b6d67268924f5f5159eca5c3016b15
parent3baadec22ceb664726d2b3c2cc756d96c808f42b (diff)
make sender more stable by delaying start, reseting address, and not waiting before re-requesting
-rw-r--r--arduino_nano/main.c30
-rw-r--r--arduino_nano/main.h2
2 files changed, 15 insertions, 17 deletions
diff --git a/arduino_nano/main.c b/arduino_nano/main.c
index 19a471c..350a9f0 100644
--- a/arduino_nano/main.c
+++ b/arduino_nano/main.c
@@ -91,6 +91,7 @@ void io_init(void)
int main(void)
{
+ _delay_ms(1000); // wait a bit until power meter started
io_init(); // initialize IOs
uint8_t nrf24_payload[1+4*4+1] = {0}; // measurement values to be sent: ID byte + encrypted 4x4 float values (voltage, current, power, energy) + 1 CRC8 Mixim/Dallas/iButton/1Wire of the plain text (to know if the key/IV is correct)
@@ -101,11 +102,8 @@ int main(void)
nrf24_set_tx_addr(conf.tx_addr); // set transmission destination address
nrf24_set_rf_channel(conf.channel); // set transceiver channel
- /* set the PZEM-004 IP address (it won't reply to other requests before) */
+ /* PZEM-004 IP address (it won't reply to other requests before it is set) */
uint8_t cmd[7] = {0xB4,0xC0,0xA8,0x01,0x01,0x00,0x1E}; // set the address 192.168.1.1 (keep this address for the other commands)
- for (uint8_t i=0; i<sizeof(cmd); i++) {
- usart_putchar_nonblocking(cmd[i],NULL);
- }
/* start encryption */
aes128_ctx_t ctx; // the context where the round keys are stored
@@ -131,7 +129,6 @@ int main(void)
timer_seconds = 0; // restart seconds counter
TCCR1B |= (1<<CS12)|(0<<CS11)|(0<<CS10); // set prescale to 256 (starting the timer)
query_pzem004 = false; // immediately start querying the power meter
- bool queried_pzem004 = true; // all values have been queried from the power meter
/* PZEM-004 power meter values */
float voltage = 0, current = 0, power = 0, energy = 0; // the read values
@@ -153,16 +150,13 @@ int main(void)
}
if (query_pzem004) {
action = true; // an action is performed
- query_pzem004 = false; // clear flag
- if (queried_pzem004) { // wait until previous queries finished
- queried_pzem004 = false;
- // query all values (voltage, current, power, energy)
- // start with voltage, the other will be queried after the power meter answered
- cmd[0] = 0xb0; // query voltage
- cmd[6] = 0x1a; // update checksum
- for (uint8_t i=0; i<sizeof(cmd); i++) {
- usart_putchar_nonblocking(cmd[i],NULL);
- }
+ query_pzem004 = false; // clear flag (do not wait until previous is finished)
+ // query all values (voltage, current, power, energy)
+ // start with setting the address, the values will be queried after the power meter answered
+ cmd[0] = 0xb4; // set address
+ cmd[6] = 0x1e; // update checksum
+ for (uint8_t i=0; i<sizeof(cmd); i++) {
+ usart_putchar_nonblocking(cmd[i],NULL);
}
}
if (usart_incoming>=7) { // wait for an answer to be available
@@ -210,10 +204,14 @@ int main(void)
break;
case 0xa3: // energy
energy = (((uint32_t)answer[1])<<16)+((uint32_t)(answer[2])<<8)+answer[3];
- queried_pzem004 = true; // all have been queried
send_values = true; // this should be the last of the 4 values we requested. now send them
break;
case 0xa4: // address
+ cmd[0] = 0xb0; // query voltage
+ cmd[6] = 0x1a; // update checksum
+ for (uint8_t i=0; i<sizeof(cmd); i++) {
+ usart_putchar_nonblocking(cmd[i],NULL);
+ }
break;
}
}
diff --git a/arduino_nano/main.h b/arduino_nano/main.h
index 1c832f9..abd2985 100644
--- a/arduino_nano/main.h
+++ b/arduino_nano/main.h
@@ -24,7 +24,7 @@ struct configuration {
uint8_t rx_addr[5]; // nRF24 receiving address
uint8_t tx_addr[5]; // nRF24 transmit address
uint8_t channel; // nRF24 channel
- uint8_t request_period; // how many seconds between power meter requests
+ uint8_t request_period; // how many seconds between power meter requests (should be > 2 to have enough time to query all values)
uint8_t key[16]; // the AES 128 bits key
uint8_t iv[16]; // the initialisation vector for CBC encryption
};