From cf0df8b102806c4087814b895601321a32026e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 11 Nov 2015 01:39:21 +0100 Subject: [PATCH] make sender more stable by delaying start, reseting address, and not waiting before re-requesting --- arduino_nano/main.c | 30 ++++++++++++++---------------- arduino_nano/main.h | 2 +- 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=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 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 };