make sender more stable by delaying start, reseting address, and not waiting before re-requesting

This commit is contained in:
King Kévin 2015-11-11 01:39:21 +01:00
parent 3baadec22c
commit cf0df8b102
2 changed files with 15 additions and 17 deletions

View File

@ -91,6 +91,7 @@ void io_init(void)
int main(void) int main(void)
{ {
_delay_ms(1000); // wait a bit until power meter started
io_init(); // initialize IOs 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) 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_tx_addr(conf.tx_addr); // set transmission destination address
nrf24_set_rf_channel(conf.channel); // set transceiver channel 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) 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 */ /* start encryption */
aes128_ctx_t ctx; // the context where the round keys are stored 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 timer_seconds = 0; // restart seconds counter
TCCR1B |= (1<<CS12)|(0<<CS11)|(0<<CS10); // set prescale to 256 (starting the timer) TCCR1B |= (1<<CS12)|(0<<CS11)|(0<<CS10); // set prescale to 256 (starting the timer)
query_pzem004 = false; // immediately start querying the power meter 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 */ /* PZEM-004 power meter values */
float voltage = 0, current = 0, power = 0, energy = 0; // the read values float voltage = 0, current = 0, power = 0, energy = 0; // the read values
@ -153,16 +150,13 @@ int main(void)
} }
if (query_pzem004) { if (query_pzem004) {
action = true; // an action is performed action = true; // an action is performed
query_pzem004 = false; // clear flag query_pzem004 = false; // clear flag (do not wait until previous is finished)
if (queried_pzem004) { // wait until previous queries finished // query all values (voltage, current, power, energy)
queried_pzem004 = false; // start with setting the address, the values will be queried after the power meter answered
// query all values (voltage, current, power, energy) cmd[0] = 0xb4; // set address
// start with voltage, the other will be queried after the power meter answered cmd[6] = 0x1e; // update checksum
cmd[0] = 0xb0; // query voltage for (uint8_t i=0; i<sizeof(cmd); i++) {
cmd[6] = 0x1a; // update checksum usart_putchar_nonblocking(cmd[i],NULL);
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 if (usart_incoming>=7) { // wait for an answer to be available
@ -210,10 +204,14 @@ int main(void)
break; break;
case 0xa3: // energy case 0xa3: // energy
energy = (((uint32_t)answer[1])<<16)+((uint32_t)(answer[2])<<8)+answer[3]; 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 send_values = true; // this should be the last of the 4 values we requested. now send them
break; break;
case 0xa4: // address 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; break;
} }
} }

View File

@ -24,7 +24,7 @@ struct configuration {
uint8_t rx_addr[5]; // nRF24 receiving address uint8_t rx_addr[5]; // nRF24 receiving address
uint8_t tx_addr[5]; // nRF24 transmit address uint8_t tx_addr[5]; // nRF24 transmit address
uint8_t channel; // nRF24 channel 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 key[16]; // the AES 128 bits key
uint8_t iv[16]; // the initialisation vector for CBC encryption uint8_t iv[16]; // the initialisation vector for CBC encryption
}; };