diff --git a/src/tests/BoardConfig.cpp b/src/tests/BoardConfig.cpp new file mode 100644 index 0000000..2c137d8 --- /dev/null +++ b/src/tests/BoardConfig.cpp @@ -0,0 +1,18 @@ +#include "BoardConfig.h" + +BoardConfig::BoardConfig( const Pin LDOEnable, + const Pin NRFCe, + const Pin NRFCs, + const Pin I2CSDA, + const Pin I2CSCL, + const Pin LDRVSensEnable, + const Pin BATVSensEnable, + const Pin MOSI, + const Pin MISO, + const Pin SCK, + const Pin BATAnalogVSens, + const Pin LDRAnalogVSens +):LDOEnable(LDOEnable), NRFCe(NRFCe), NRFCs(NRFCs), I2CSDA(I2CSDA), I2CSCL(I2CSCL), LDRVSensEnable(LDRVSensEnable), BATVSensEnable(BATVSensEnable), MOSI(MOSI), MISO(MISO), SCK(SCK), BATAnalogVSens(BATAnalogVSens), LDRAnalogVSens(LDRAnalogVSens) +{ + +} diff --git a/src/tests/BoardConfig.h b/src/tests/BoardConfig.h new file mode 100644 index 0000000..3cebdd0 --- /dev/null +++ b/src/tests/BoardConfig.h @@ -0,0 +1,46 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created the : 30/05/2021 + * This class encapsulates the various configuration values for the board like the Pin Mapping for instance. + */ +#ifndef BOARDCONFIG_H +#define BOARDCONFIG_H + +#include "definition.h" + +class BoardConfig +{ + public: + BoardConfig( + const Pin LDOEnable = D2_LDO_EN, + const Pin NRFCe = D3_NRF_CE, + const Pin NRFCs = D10_NRF_CS, + const Pin I2CSDA = A4_SDA, + const Pin I2CSCL = A5_SCL, + const Pin LDRVSensEnable = D4_LDR_V_SENS_EN, + const Pin BATVSensEnable = D5_BAT_V_SENS_EN, + const Pin MOSI = D11_MOSI, + const Pin MISO = D12_MISO, + const Pin SCK = D13_SCK, + const Pin BATAnalogVSens = A0_BAT_V_SENS, + const Pin LDRAnalogVSens = A1_LDR_V_SENS + ); + + const Pin LDOEnable; + const Pin NRFCe; + const Pin NRFCs; + const Pin I2CSDA; + const Pin I2CSCL; + const Pin LDRVSensEnable; + const Pin BATVSensEnable; + const Pin MOSI; + const Pin MISO; + const Pin SCK; + const Pin BATAnalogVSens; + const Pin LDRAnalogVSens; + + protected: + private: +}; + +#endif //BOARDCONFIG_H diff --git a/src/tests/WSPeripherals.cpp b/src/tests/WSPeripherals.cpp new file mode 100644 index 0000000..079321d --- /dev/null +++ b/src/tests/WSPeripherals.cpp @@ -0,0 +1,107 @@ +#include "WSPeripherals.h" + +WSPeripherals::WSPeripherals(const BoardConfig &boardConfig): _boardConfig(boardConfig), _HTU21(HTU21D_RES_RH12_TEMP14), _NRF(boardConfig.NRFCe, boardConfig.NRFCs) +{} + +uint8_t WSPeripherals::init() +{ + uint8_t toReturn(0); + //We initialize used pins : + pinMode(_boardConfig.LDOEnable, OUTPUT); + _3V3PowerRail(OFF); + pinMode(_boardConfig.LDRVSensEnable, OUTPUT); + digitalWrite(_boardConfig.LDRVSensEnable, HIGH); //High means that it is disabled and low is active /!\ + pinMode(_boardConfig.BATVSensEnable, OUTPUT); + digitalWrite(_boardConfig.BATVSensEnable, LOW); + + //Unused pins are set as inputs with internal pullup inable to reduce power consumption during sleep + pinMode(0,INPUT_PULLUP); + //pinMode(1,INPUT_PULLUP); TX pin for serial + pinMode(6,INPUT_PULLUP); + pinMode(7,INPUT_PULLUP); + pinMode(8,INPUT_PULLUP); + pinMode(9,INPUT_PULLUP); + pinMode(10,INPUT_PULLUP); + pinMode(11,INPUT_PULLUP); + pinMode(12,INPUT_PULLUP); + pinMode(A2,INPUT_PULLUP); + pinMode(A3,INPUT_PULLUP); + pinMode(A6,INPUT_PULLUP); + pinMode(A7,INPUT_PULLUP); + + //We check that every external devices are responding + _3V3PowerRail(ON); + toReturn |= _BMP280.begin(0x76); + toReturn |= _HTU21.begin() << 1; + toReturn |= _NRF.begin() << 2; + _3V3PowerRail(OFF); + return toReturn; +} + +uint8_t WSPeripherals::initExternalPeripherals() +{ + uint8_t toReturn(0); + _BMP280.setSampling( Adafruit_BMP280::MODE_FORCED, + Adafruit_BMP280::SAMPLING_X16, + Adafruit_BMP280::SAMPLING_X16, + Adafruit_BMP280::FILTER_X16, + Adafruit_BMP280::STANDBY_MS_4000); + + toReturn |= _BMP280.begin(0x76); + toReturn |= _HTU21.begin() << 1; + toReturn |= _NRF.begin() << 2; + return toReturn; +} + +float WSPeripherals::batteryVoltage() +{ + //We close the voltage divider bridge and we do the measurement + digitalWrite(_boardConfig.BATVSensEnable, HIGH); + int rawBatteryValue = analogRead(_boardConfig.BATAnalogVSens); + digitalWrite(_boardConfig.BATVSensEnable, LOW); + + return float(rawBatteryValue) * ADC_QUANTUM * VOLTAGE_DIV_COEFF; +} + +int WSPeripherals::sunlightMeasurement() +{ + //We enable the voltage divider bridge and we do the measurement + digitalWrite(_boardConfig.LDRVSensEnable, LOW); + int rawSunlightMeasurement = analogRead(_boardConfig.LDRAnalogVSens); + digitalWrite(_boardConfig.LDRVSensEnable, HIGH); + + return rawSunlightMeasurement; +} + +float WSPeripherals::temperatureFromBMP280() +{ + return _BMP280.readTemperature(); +} + +float WSPeripherals::ATMPressure() +{ + return _BMP280.readPressure(); +} + +float WSPeripherals::temperatureFromHTU21() +{ + return _HTU21.readTemperature(); +} + +float WSPeripherals::humidity() +{ + return _HTU21.readHumidity(); +} + +float WSPeripherals::compensatedHumidity() +{ + return _HTU21.readCompensatedHumidity(); +} + +void WSPeripherals::_3V3PowerRail(State state) +{ + digitalWrite(_boardConfig.LDOEnable, state); +} + +const RF24 &WSPeripherals::getRadio(){return _NRF;} +void WSPeripherals::applyRadioConfig(uint8_t channel = RADIO_CHANNEL, uint8_t paLevel = RADIO_PA_LEVEL, rf24_datarate_e datarate = RADIO_DATARATE); diff --git a/src/tests/WSPeripherals.h b/src/tests/WSPeripherals.h new file mode 100644 index 0000000..5905aba --- /dev/null +++ b/src/tests/WSPeripherals.h @@ -0,0 +1,66 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created the : 30/05/2021 + * This classe exposes all the methods necessary to init the WSPeripherals (Weather Station Peripherals) and + * to retrieve various data and measurements like battery voltage, Temperatures etc... + */ +#ifndef WSPERIPHERALS_H +#define WSPERIPHERALS_H + +#include +#include +#include +#include + +#include "BoardConfig.h" + +class WSPeripherals +{ + public: + enum State {OFF, ON}; + + WSPeripherals(const BoardConfig &boardConfig); + /* + * Returns 7 if all the external devices are working properly, or an other value if it is not the case. + */ + uint8_t init(); + /* + * After calling this methode , you need to execute initExternalPeripherals() to init the peripherals. + */ + void externalPeripherals(State state){_3V3PowerRail(state);} + + /* + * Used to init devices after each LDO powerup, external devices need to be turned one externalPeripherals(ON) before calling this function. + */ + uint8_t initExternalPeripherals(); + + float batteryVoltage(); + int sunlightMeasurement(); + float temperatureFromBMP280(); + float ATMPressure(); + float temperatureFromHTU21(); + float humidity(); + float compensatedHumidity(); + + /* + * Before calling this method, externalPeripherals(ON) and initExternalPeripherals() must be called respectively + */ + void applyRadioConfig(uint8_t channel, uint8_t paLevel, rf24_datarate_e datarate) + { + _NRF.setChannel(channel); + _NRF.setPALevel(paLevel); + _NRF.setDataRate(rf24_datarate_e); + } + const RF24 &getRadio(); + + protected: + private: + void _3V3PowerRail(State state); + const BoardConfig &_boardConfig; + const Adafruit_BMP280 _BMP280; + const HTU21D _HTU21; + const RF24 _NRF; + +}; + +#endif //WSPERIPHERALS_H diff --git a/src/tests/definition.h b/src/tests/definition.h new file mode 100644 index 0000000..357ab32 --- /dev/null +++ b/src/tests/definition.h @@ -0,0 +1,43 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created the : 29/05/2021 + * This file contains all the config used by the other classes and sources files. + */ +#ifndef DEFINITION_H +#define DEFINITION_H + +#include +#include + +//Serial debug config part +#define SERIAL_DEBUG_ENABLED 0 +#define SERIAL_BAUD_RATE 115200 + +//Battery config part +#define ADC_QUANTUM 0.00323632812 //ADC_VREF / ADC_RESOLUTION -> 3.314 and 10 bits (1024) in my case +#define VOLTAGE_DIV_COEFF 1.3125 //(R1 + R2)/R2 + +//NRF Radio config part +#define RADIO_CHANNEL 108 +#define RADIO_NODE_ADDRESS "WEST1" //Weather Station 1 +#define RADIO_PA_LEVEL RF24_PA_MAX +#define RADIO_DATARATE RF24_250KBPS + +//Pin config part +typedef enum +{ + D2_LDO_EN = 2, + D3_NRF_CE = 3, + A4_SDA = A4, + A5_SCL = A5, + D4_LDR_V_SENS_EN = 4, + D5_BAT_V_SENS_EN = 5, + D10_NRF_CS = 10, + D11_MOSI = 11, + D12_MISO = 12, + D13_SCK = 13, + A0_BAT_V_SENS = A0, + A1_LDR_V_SENS = A1, +} Pin; + +#endif //DEFINITION_H diff --git a/src/tests/tests.ino b/src/tests/tests.ino new file mode 100644 index 0000000..44863cd --- /dev/null +++ b/src/tests/tests.ino @@ -0,0 +1,62 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created the : 29/05/2021 + * This TEST program aims to verify all basic functions of the station like by displaying some values to a serial console : + * - The battery level sensing + * - The light level with the LDR + * - The Temperature + * - The pressure + * - The humidity + */ +#include "definition.h" +#include "BoardConfig.h" +#include "WSPeripherals.h" + +BoardConfig defaultBC; //Default config is applied +WSPeripherals WSP(defaultBC); + +void setup() +{ + Serial.begin(SERIAL_BAUD_RATE); + Serial.println("Setup begin"); + uint8_t code = WSP.init(); + Serial.print("WSP init returned : ");Serial.print(code); + WSP.externalPeripherals(WSPeripherals::ON); + code = WSP.initExternalPeripherals(); + Serial.print("WSP init external peripheral returned : ");Serial.print(code); + Serial.println("Setup end"); +} + +void loop() +{ + Serial.println("##############DATA##############"); + Serial.print("BATT : "); + Serial.print(WSP.batteryVoltage()); + Serial.println(" V"); + + Serial.print("LDR : "); + Serial.println(WSP.sunlightMeasurement()); + + Serial.print("BMP TEMP : "); + Serial.print(WSP.temperatureFromBMP280()); + Serial.println(" *C"); + + Serial.print("BMP PRESS : "); + Serial.print(WSP.ATMPressure()); + Serial.println(" Pa"); + + Serial.print("HUM : ");Serial.println(WSP.humidity()); + Serial.print("COM HUM : ");Serial.println(WSP.compensatedHumidity()); + Serial.print("HTU TEMP : ");Serial.println(WSP.temperatureFromHTU21()); + + if(!WSP.getRadio().isChipConnected()) + { + Serial.println("Chip is missing"); + } + else + { + Serial.println("Chip is present"); + } + + delay(1000); +}