/* main.cpp - ATTNode / TinyTX Firmware Copyright (c) 2019-2020, Stefan Brand All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include // secconfig.h Configures RF Module, TTN Keys / RF Networks and used Sensor #include "secconfig.h" // Create needed Variables and Objects for RF Module #ifdef RF_LORA // Include LoRaWAN #include #include // For Storing Frame Counter #define DIO0 PIN_B0 #define NSS PIN_B1 RFM95 rfm(DIO0,NSS); LoRaWAN lora = LoRaWAN(rfm); uint16_t Frame_Counter_Tx = 0x0000; #endif #ifdef RF_RFM69 // Include RFM69 #include RFM69 radio; #endif // Create Sensor Class #if defined HAS_BME280 || defined HAS_BME280_BRIGHTNESS || defined HAS_BME280_ALARM #include BME280 sensor; #endif #if defined HAS_SHT21 || defined HAS_SHT21_BRIGHTNESS || defined HAS_SHT21_ALARM #include SHT21 sensor; #endif #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM volatile boolean alarm = false; // Get the Interrupt Mask for the selected Alarm PIN #if ALARM_PIN == PIN_A0 #define ALARM_INT PCINT0 #elif ALARM_PIN == PIN_A1 #define ALARM_INT PCINT1 #elif ALARM_PIN == PIN_A2 #define ALARM_INT PCINT2 #elif ALARM_PIN == PIN_A3 #define ALARM_INT PCINT3 #elif ALARM_PIN == PIN_A7 #define ALARM_INT PCINT7 #elif ALARM_PIN == PIN_B2 #define ALARM_INT PCINT9 #endif #endif #ifdef DS18B20_PIN #include #include OneWire oneWire(DS18B20_PIN); DallasTemperature sensors(&oneWire); #endif // Global Variable to Track Deep Sleep uint16_t sleep_interval; #ifdef LED_PIN void blink(uint8_t num) { pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, 0); for (uint8_t i=0; i Frame_Counter_Tx) { Frame_Counter_Tx = Frame_Counter_Sv; } else { break; } eeprom_addr += sizeof(Frame_Counter_Tx); EEPROM.get(eeprom_addr, Frame_Counter_Sv); } } #endif #ifdef RF_RFM69 // Setup RFM69 Module radio.initialize(RF69_433MHZ,RFM69_NODEID,RFM69_NETWORKID); #ifdef RFM69_ENCKEY radio.encrypt(RFM69_ENCKEY); #endif radio.setPowerLevel(RFM69_TXPOWER); radio.sleep(); #endif #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke GIMSK = (1< 9) data.bat += 1; // LED On before Sending #ifdef LED_ON_SEND digitalWrite(LED_PIN, 1); #endif #ifdef RF_LORA #ifdef HAS_NO_SENSOR // Send Packet in all 6 SFs for Beacon Mode unsigned char Frame_Port =0x07; for (int i = SF7BW125; i<=SF12BW125; i++) { lora_data tdata = data; // Some FrameCounter Trickery to not write the value to EEPROM 6 Times but still have a working WearLeveling lora.Send_Data((unsigned char *)&tdata, sizeof(tdata), i+(Frame_Counter_Tx*6), static_cast(i), Frame_Port); Frame_Port++; delay(25); } #else // Send LoRa Packet, Increment Frame Counter lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01); #endif // Save the next FrameCounter to EEPROM Frame_Counter_Tx++; EEPROM.put(calcEepromAddr(Frame_Counter_Tx), Frame_Counter_Tx); #endif #ifdef RF_RFM69 radio.send(RFM69_GATEWAY, &data, sizeof(data)); radio.sleep(); #endif // Led Off after Sending #ifdef LED_ON_SEND digitalWrite(LED_PIN, 0); #endif // Sleep until next Measurement sleep(SLEEP_TIME); }