/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /* Copyright (c) 2015 King Kévin */ /* the spark counter transmits electricity measurements over radio * the electricity measurements (voltage, current, power, energy) are query everry second from a peacefair PZEM-004 power meter * they are then transmitted using an nRF2L01+ transceiver */ #include // Standard Integer Types #include // Standard IO facilities #include // General utilities #include // Boolean #include // Strings #include // AVR device-specific IO definitions #include // Convenience functions for busy-wait delay loops #include // Interrupts #include // Watchdog timer handling #include // Program Space Utilities #include // Power Management and Sleep Modes #include // CRC Computations #include "main.h" // main definitions #include "usart.h" // basic USART functions #include "nrf24.h" // nRF24L01 functions // AES encryption functions #include "aes_types.h" #include "aes128_enc.h" #include "aes_keyschedule.h" /* variables */ bool query_pzem004 = false; // flag to query the PZEM-004 power meter volatile uint8_t timer_seconds = 0; // how many seconds have passed volatile bool watchdog = true; // set when watchdog interrupt occurred /* disable watchdog when booting */ void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); void wdt_init(void) { MCUSR = 0; wdt_disable(); } /* enable watchdog with interrupt and system reset (modified wdt_enable) */ #define wdt_set(value) \ __asm__ __volatile__ ( \ "in __tmp_reg__,__SREG__" "\n\t" \ "cli" "\n\t" \ "wdr" "\n\t" \ "sts %0,%1" "\n\t" \ "out __SREG__,__tmp_reg__" "\n\t" \ "sts %0,%2" "\n\t" \ : /* no outputs */ \ : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE) | _BV(WDIE)), \ "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDE) | _BV(WDIE) | (value & 0x07)) ) \ : "r0" \ ) /* initialize GPIO */ void io_init(void) { /* use UART as terminal */ usart_init(); stdout = &usart_output; stdin = &usart_input; /* use nRF24L01 */ nrf24_init(); /* set up timer 1 to regularly wake up and request data from the power meter */ TCCR1B |= (0<=7) { // wait for an answer to be available action = true; // an action is performed //printf(PSTR("got value\n")); // store answer uint8_t answer[7]; for (uint8_t i=0; i