request power meter request periode to 5 seconds and make it as setting

This commit is contained in:
King Kévin 2015-11-08 17:01:11 +01:00
parent 938cfd04fc
commit 5eb31d153e
3 changed files with 23 additions and 30 deletions

View File

@ -92,7 +92,6 @@ TODO
arduino_nano:
- use encryption for the RF communication (AES-128)
- user a slower timer value (5s) for querying the PZEM-004 data. currently it's set to 1s but the power meter needs already more time for the 4 requested values
- enable the watchdog (in case the interrupt handling is putting the device in an unexpected state)
rpi:

View File

@ -38,7 +38,8 @@
#include "nrf24.h" // nRF24L01 functions
/* variables */
volatile bool query_pzem004 = false; // flag to query the PZEM-004 power meter
bool query_pzem004 = false; // flag to query the PZEM-004 power meter
volatile uint8_t timer_seconds = 0; // how many seconds have passed
/* disable watchdog when booting */
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
@ -63,7 +64,7 @@ void io_init(void)
TCCR1B |= (0<<WGM13)|(1<<WGM12); // use timer 1 in CTC mode
TCCR1A |= (0<<WGM11)|(0<<WGM10); // use timer 1 in CTC mode
TCNT1 = 0; // reset timer 1 counter
OCR1A = (F_CPU/256UL)*1.0; // the time to wait before triggering. with a prescale of 256 the max is 1.05s. here I choose 1s.
OCR1A = F_CPU/256UL; // the timer to trigger every second with a prescale of 256 (the max is 1.05s)
TIMSK1 |= (1<<OCIE1A); // enable interrupt
sei(); // enable interrupts
@ -75,8 +76,7 @@ int main(void)
//printf(PSTR("welcome to the spar counter power meter monitor\n"));
/* fallback nRF24 node configuration */
struct configuration conf = {{1,'h','o','m','e'},{0,'h','o','m','e'},42};
struct configuration conf; // node configuration
// read configuration data from EEPROM + CRC-8-iButton value
uint8_t eeprom_conf[sizeof(struct configuration)+1];
uint8_t crc = 0; // CRC8 iButton/1Wire/Dallas/Maxim calculation
@ -84,12 +84,13 @@ int main(void)
eeprom_conf[i] = eeprom_read_byte((const uint8_t*)i); // read data
crc = _crc_ibutton_update(crc, eeprom_conf[i]); // calculate CRC
}
if (crc==0) { // CRC succeeded
memcpy(&conf,eeprom_conf,sizeof(struct configuration));
if (crc) { // CRC failed
return 1; // stop doing anything
}
nrf24_set_rx_addr(conf.rx_addr);
nrf24_set_tx_addr(conf.tx_addr);
nrf24_set_rf_channel(conf.channel);
memcpy(&conf,eeprom_conf,sizeof(struct configuration)); // get configuration
nrf24_set_rx_addr(conf.rx_addr); // set device receiving address
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) */
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)
@ -98,6 +99,7 @@ int main(void)
}
/* start timer to periodically query the power meter */
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
@ -106,9 +108,14 @@ int main(void)
float voltage = 0, current = 0, power = 0, energy = 0; // the read values
bool send_values = false; // set to true to send out the values
bool action = false; // to know if we performed any king of action during which some other activity could have happend
bool action = false; // to know if we performed any king of action during which some other activity could have happened
while (true) { // endless loop for micro-controller
action = false; // new cycle of actions
if (timer_seconds!=0 && timer_seconds==conf.request_period) {
action = true; // an action is performed
timer_seconds = 0; // restart timer
query_pzem004 = true; // remember to query power meter
}
if (query_pzem004) {
action = true; // an action is performed
query_pzem004 = false; // clear flag
@ -235,10 +242,8 @@ int main(void)
return 0;
}
/* timer 1 triggered
* start querying the power meter
*/
/* timer 1 triggered */
ISR(TIMER1_COMPA_vect)
{
query_pzem004 = true; // warm main programm to start querying
timer_seconds++; // count seconds
}

View File

@ -18,29 +18,18 @@
* they are then transmitted using an nRF2L01+ transceiver
*/
/* peripherals */
/* LED to indicate scale reading
* pin: PB5, LED L
*/
/* not used because PB5 is used for SCK
#define LED_IO PB5
#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED_PIN PINB
*/
/* spark counter configuration */
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
};
/* spark counter default configuration stored in EEPROM
* it's corresponds to the configuration structure
* there is an additionnal CRC8 iButton/1Wire/Dallas/Maxim checksum
* there is an additional CRC8 iButton/1Wire/Dallas/Maxim checksum
* the variable order is reversed (avr-gcc behaviour?)
*/
uint8_t EEMEM eeprom_crc = 0xeb; // CRC8-ibutton checksum
struct configuration EEMEM eeprom_default_conf = {{1,'h','o','m','e'},{0,'h','o','m','e'},42};
uint8_t EEMEM eeprom_crc = 0xf6; // CRC8-ibutton checksum
struct configuration EEMEM eeprom_default_conf = {{1,'h','o','m','e'},{0,'h','o','m','e'},42,5};