diff --git a/client/client/include/NoDataAvailableException.hpp b/client/client/include/NoDataAvailableException.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a5de09a1410326f7f6352f4e27a455b3f2b0061f --- /dev/null +++ b/client/client/include/NoDataAvailableException.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include <exception> +#include <iostream> + +struct NoDataAvailableException : public std::exception { + const char *what() const throw() { return "Sensor could not read data"; } +}; \ No newline at end of file diff --git a/client/client/include/forte_sensor.hpp b/client/client/include/forte_sensor.hpp index 8e030b9403653c40c5eecfca88edab96e805631d..36c4e75a451ab562aa5a32c3f62eb01958409f4c 100644 --- a/client/client/include/forte_sensor.hpp +++ b/client/client/include/forte_sensor.hpp @@ -1,13 +1,15 @@ #ifndef _FORTE_SENSOR #define _FORTE_SENSOR +// #include "Message.hpp" +template <class T> class Forte_Sensor { - public: - virtual void* read_data() = 0; - virtual void setup() = 0; - - private: + public: + virtual T read_data() = 0; + virtual void setup() = 0; + // virtual Message build_message() = 0; + private: }; #endif \ No newline at end of file diff --git a/client/client/lib/dr26_analogue/src/dr26.cpp b/client/client/lib/dr26_analogue/src/dr26.cpp index e08a572956a2cabe23ece4ab80a71308105d4b5d..1ed6d198cab4b9674d67a24836a650266afaeadd 100644 --- a/client/client/lib/dr26_analogue/src/dr26.cpp +++ b/client/client/lib/dr26_analogue/src/dr26.cpp @@ -1,43 +1,48 @@ #include "dr26.hpp" - Adafruit_ADS1115 ads; -void Forte_DR26 :: setup(adsGain_t gain){ - Wire.begin(I2C_SDA, I2C_SCL); - ads.setGain(gain); - ads.begin() ? - Serial.println("ADS initialized") : - Serial.println("failed to initialize ADS"); - delay(100); +void Forte_DR26 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + // ads.setGain(0); + ads.begin() ? Serial.println("ADS initialized") : Serial.println("failed to initialize ADS"); + delay(100); +} + +float Forte_DR26 ::read_data() +{ + float volts = 0; + for (int i = 0; i < 10; i++) { + int16_t adc = ads.readADC_SingleEnded(0); + float volt = ads.computeVolts(adc); + volts += volt; + } + volts /= 10; + return volts; } -float* Forte_DR26 :: read_data(){ - float volts = 0; - for(int i=0; i<10; i++){ - int16_t adc = ads.readADC_SingleEnded(0); - float volt = ads.computeVolts(adc); - volts += volt; - } - volts /= 10; - return &volts; +// The following functions change the ADC input range: be careful +// to never to exceed VDD +0.3V max, or to exceed the upper and +// lower limits if you adjust the input range. +// SETTING THE GAIN VALUE INCORRECTLY MAY DESTROY THE ADC! +// The maximum output of the dendrometer is 2.5V, and so a gain of +// one (max 4.096V) or two (max 2.048V) is optimal. Changing the gain +// changes the accuracy of the sensor: higher gain gives a higher +// precision but a smaller range. +// +// GAIN_TWOTHIRDS // 2/3x gain +/- 6.144V 1 bit = 0.1875mV (default) +// GAIN_ONE // 1x gain +/- 4.096V 1 bit = 0.125mV +// GAIN_TWO // 2x gain +/- 2.048V 1 bit = 0.0625mV +// GAIN_FOUR // 4x gain +/- 1.024V 1 bit = 0.03125mV +// GAIN_EIGHT // 8x gain +/- 0.512V 1 bit = 0.015625mV +// GAIN_SIXTEEN // 16x gain +/- 0.256V 1 bit = 0.0078125mV +void Forte_DR26 ::change_Gain(adsGain_t gain) +{ + ads.setGain(gain); } - // The following functions change the ADC input range: be careful - // to never to exceed VDD +0.3V max, or to exceed the upper and - // lower limits if you adjust the input range. - // SETTING THE GAIN VALUE INCORRECTLY MAY DESTROY THE ADC! - // The maximum output of the dendrometer is 2.5V, and so a gain of - // one (max 4.096V) or two (max 2.048V) is optimal. Changing the gain - // changes the accuracy of the sensor: higher gain gives a higher - // precision but a smaller range. - // - // GAIN_TWOTHIRDS // 2/3x gain +/- 6.144V 1 bit = 0.1875mV (default) - // GAIN_ONE // 1x gain +/- 4.096V 1 bit = 0.125mV - // GAIN_TWO // 2x gain +/- 2.048V 1 bit = 0.0625mV - // GAIN_FOUR // 4x gain +/- 1.024V 1 bit = 0.03125mV - // GAIN_EIGHT // 8x gain +/- 0.512V 1 bit = 0.015625mV - // GAIN_SIXTEEN // 16x gain +/- 0.256V 1 bit = 0.0078125mV -void Forte_DR26 :: change_Gain(adsGain_t gain){ - ads.setGain(gain); +Message Forte_DR26::build_message() +{ + throw "Not implemented"; } \ No newline at end of file diff --git a/client/client/lib/dr26_analogue/src/dr26.hpp b/client/client/lib/dr26_analogue/src/dr26.hpp index 0a7c53c59650ddcff26c208d05625defe5cc4b30..6105c296c983266ec86026e0664ed4df6144b02a 100644 --- a/client/client/lib/dr26_analogue/src/dr26.hpp +++ b/client/client/lib/dr26_analogue/src/dr26.hpp @@ -1,20 +1,20 @@ #ifndef _DR26 #define _DR26 +#include "Message.hpp" #include "forte_sensor.hpp" #include "pinout.hpp" -#include <Wire.h> #include <Adafruit_ADS1X15.h> +#include <Wire.h> -class Forte_DR26 : public Forte_Sensor{ - public: - void setup(adsGain_t gain); - float* read_data(); - void change_Gain(adsGain_t gain); - - private: - +class Forte_DR26 : public Forte_Sensor<float> { + public: + void setup(); + float read_data(); + void change_Gain(adsGain_t gain); + Message build_message(); + private: }; #endif \ No newline at end of file diff --git a/client/client/lib/drs26_digital/drs26.cpp b/client/client/lib/drs26_digital/drs26.cpp index 4c3378ae05042fcd0bdc89bc905060be97dcd99e..45f038d6174fcee1be854ee302bff61ed08c7b14 100644 --- a/client/client/lib/drs26_digital/drs26.cpp +++ b/client/client/lib/drs26_digital/drs26.cpp @@ -2,41 +2,47 @@ /* It happens for some reason that the sensor cant get reached every 2 time Because the sensor use sdi12 protocoll we have to wait aproxemettly 1 secound between the commands -It is not known how lond the response takes so we use a while loop which can be a risk wehre the programm can get stuck +It is not known how lond the response takes so we use a while loop which can be a risk wehre the programm can get stuck */ void Forte_DRS26 ::setup() { - drs26.begin(SDI_DATA); + drs26.begin(4); } -out_data_drs26 *Forte_DRS26 ::read_data() +out_data_drs26 Forte_DRS26 ::read_data() { - String sdiResponse = ""; - String measurement_command="1M!"; //The drs26 sensor uses the sdi12 protocoll , in the sdi12 protocoll is the measurement command is specified as 1M!=Sebsir measurement request at adress 1 - String data_command="1D0!"; //and the followed data command 1D0! = Sensor data request at adress 1 + String sdiResponse = ""; + String measurement_command = + "1M!"; // The drs26 sensor uses the sdi12 protocoll , in the sdi12 protocoll is the measurement command is + // specified as 1M!=Sebsir measurement request at adress 1 + String data_command = "1D0!"; // and the followed data command 1D0! = Sensor data request at adress 1 - drs26.sendCommand(measurement_command); - delay(1000); - drs26.sendCommand(data_command); + drs26.sendCommand(measurement_command); + delay(1000); + drs26.sendCommand(data_command); - while (drs26.available()) - { - char next_character = drs26.read(); - if ((next_character != '\n') && (next_character != '\r')) - { - sdiResponse += next_character; - delay(10); // 1 character ~ 7.5ms - } - } + data = {-1, -1, -1}; - if (sdiResponse.length() > 1) - { - data.id = sdiResponse.substring(0, 8).toInt(); - data.circumference = sdiResponse.substring(9, 15).toFloat(); - data.temperatur = sdiResponse.substring(16, 22).toFloat(); - return &data; - } + while (drs26.available()) { + char next_character = drs26.read(); + if ((next_character != '\n') && (next_character != '\r')) { + sdiResponse += next_character; + delay(10); // 1 character ~ 7.5ms + } + } - return 0; + if (sdiResponse.length() > 1) { + data.id = sdiResponse.substring(0, 8).toInt(); + data.circumference = sdiResponse.substring(9, 15).toFloat(); + data.temperatur = sdiResponse.substring(16, 22).toFloat(); + } + return data; +} + +Message Forte_DRS26 ::build_message() +{ + // auto message = Message(); + // message.add_data(data.circumference, data.id); + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/drs26_digital/drs26.hpp b/client/client/lib/drs26_digital/drs26.hpp index 443838716a6187707f85cc24131aed3311a910e0..1c5116dc966403eafbe2b83a2c6ccf14093ee75b 100644 --- a/client/client/lib/drs26_digital/drs26.hpp +++ b/client/client/lib/drs26_digital/drs26.hpp @@ -1,27 +1,27 @@ #ifndef _DRS26 #define _DRS26 +#include "Message.hpp" +#include "Wire.h" #include "forte_sensor.hpp" #include "pinout.hpp" #include <SDI12.h> -#include "Wire.h" - struct out_data_drs26 { - int id; - float circumference; - float temperatur; - }; + int id; + float circumference; + float temperatur; +}; +class Forte_DRS26 : public Forte_Sensor<out_data_drs26> { + public: + void setup(); + out_data_drs26 read_data(); + Message build_message(); -class Forte_DRS26 : public Forte_Sensor{ - public: - void setup(); - out_data_drs26* read_data(); - - private: - SDI12 drs26; - out_data_drs26 data; + private: + SDI12 drs26; + out_data_drs26 data; }; #endif \ No newline at end of file diff --git a/client/client/lib/ina219/ina219.cpp b/client/client/lib/ina219/ina219.cpp index 7f4beb3527965f301f0f9c50679189958275220c..0f710d22de466f065fb5531f50f34cd54bf1b37f 100644 --- a/client/client/lib/ina219/ina219.cpp +++ b/client/client/lib/ina219/ina219.cpp @@ -1,26 +1,30 @@ #include "ina219.hpp" +void Forte_INA219 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + if (!ina219.init()) { + // Sensor init went wrong + return; + } +} + +out_data_ina219 Forte_INA219 ::read_data() +{ + if (!ina219.getOverflow()) { + data.shuntVoltage_mV = ina219.getShuntVoltage_mV(); + data.busVoltage_V = ina219.getBusVoltage_V(); + data.current_mA = ina219.getCurrent_mA(); + data.power_mW = ina219.getBusPower(); + data.loadVoltage_V = data.busVoltage_V + (data.shuntVoltage_mV / 1000); + data.ina219_overflow = ina219.getOverflow(); -void Forte_INA219 :: setup(){ - Wire.begin(I2C_SDA, I2C_SCL); - if(!ina219.init()){ - // Sensor init went wrong - return; - } + return data; + } else + return data; } -out_data_ina219* Forte_INA219 :: read_data(){ - if(!ina219.getOverflow()) - { - data.shuntVoltage_mV = ina219.getShuntVoltage_mV(); - data.busVoltage_V= ina219.getBusVoltage_V(); - data.current_mA= ina219.getCurrent_mA(); - data.power_mW= ina219.getBusPower(); - data.loadVoltage_V = data.busVoltage_V + (data.shuntVoltage_mV/1000); - data.ina219_overflow=ina219.getOverflow(); - - return &data; - } - else - return 0; +Message Forte_INA219::build_message() +{ + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/ina219/ina219.hpp b/client/client/lib/ina219/ina219.hpp index 67217e193308b3145460c357f8480116f0b17c46..cd964cc8c63ff3eac93526484d0d9ca57bdc8ba5 100644 --- a/client/client/lib/ina219/ina219.hpp +++ b/client/client/lib/ina219/ina219.hpp @@ -1,31 +1,30 @@ #ifndef _INA219 #define _INA219 +#include "Message.hpp" +#include "Wire.h" #include "forte_sensor.hpp" #include "pinout.hpp" #include <INA219_WE.h> -#include "Wire.h" - - struct out_data_ina219 { - float shuntVoltage_mV = 0.0; - float loadVoltage_V = 0.0; - float busVoltage_V = 0.0; - float current_mA = 0.0; - float power_mW = 0.0; - bool ina219_overflow = false; - }; + float shuntVoltage_mV = 0.0; + float loadVoltage_V = 0.0; + float busVoltage_V = 0.0; + float current_mA = 0.0; + float power_mW = 0.0; + bool ina219_overflow = false; +}; +class Forte_INA219 : public Forte_Sensor<out_data_ina219> { + public: + void setup(); + out_data_ina219 read_data(); + Message build_message(); -class Forte_INA219 : public Forte_Sensor{ - public: - void setup(); - out_data_ina219* read_data(); - - private: - INA219_WE ina219; - out_data_ina219 data; + private: + INA219_WE ina219; + out_data_ina219 data; }; #endif \ No newline at end of file diff --git a/client/client/lib/scd30/scd30.cpp b/client/client/lib/scd30/scd30.cpp index c7c2084754bfc94aef7b1ca25947ede9c4c03fc1..222089c897d08933cbc3ed8afb3c1418f84a6e88 100644 --- a/client/client/lib/scd30/scd30.cpp +++ b/client/client/lib/scd30/scd30.cpp @@ -1,23 +1,28 @@ #include "scd30.hpp" - -void Forte_SCD30 :: setup(){ - Wire.begin(I2C_SDA, I2C_SCL); - if(!airSensor.begin()){ - // Sensor init went wrong - return; - } +void Forte_SCD30 ::setup() +{ + Wire.begin(I2C_SDA, I2C_SCL); + if (!airSensor.begin()) { + // Sensor init went wrong + return; + } } -out_data_scd30* Forte_SCD30 :: read_data(){ - if(airSensor.dataAvailable()) - { - data.C02= airSensor.getCO2(); - data.Temperature = airSensor.getTemperature(); - data.Humidity = airSensor.getHumidity(); +out_data_scd30 Forte_SCD30 ::read_data() +{ + if (airSensor.dataAvailable()) { + data.C02 = airSensor.getCO2(); + data.Temperature = airSensor.getTemperature(); + data.Humidity = airSensor.getHumidity(); + + return data; + } + throw NoDataAvailableException(); + // return out_data_scd30{-1, -1, -1}; +} - return &data; - } - else - return 0; +Message Forte_SCD30::build_message() +{ + throw "Not yet implemented"; } \ No newline at end of file diff --git a/client/client/lib/scd30/scd30.hpp b/client/client/lib/scd30/scd30.hpp index 82a10cd23df90187cd025251f0d2a73279b8d8c8..c3bf69725f82ad61df06d2ba02643074ff105955 100644 --- a/client/client/lib/scd30/scd30.hpp +++ b/client/client/lib/scd30/scd30.hpp @@ -1,27 +1,28 @@ #ifndef _SCD30 #define _SCD30 +#include "Message.hpp" +#include "NoDataAvailableException.hpp" #include "forte_sensor.hpp" #include "pinout.hpp" -#include <Wire.h> #include <SparkFun_SCD30_Arduino_Library.h> +#include <Wire.h> -struct out_data_scd30 -{ - float C02; - float Temperature; - float Humidity; +struct out_data_scd30 { + float C02; + float Temperature; + float Humidity; }; +class Forte_SCD30 : public Forte_Sensor<out_data_scd30> { + public: + void setup(); + out_data_scd30 read_data(); + Message build_message(); -class Forte_SCD30 : public Forte_Sensor{ - public: - void setup(); - out_data_scd30* read_data(); - - private: - SCD30 airSensor; - out_data_scd30 data; + private: + SCD30 airSensor; + out_data_scd30 data; }; #endif \ No newline at end of file diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index 54220bae97fb193cf0d830b9d04c57a49976f3a4..a9cdb27408f274d9daf802dd981138c2af6ce2a6 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -1,31 +1,37 @@ #include <Arduino.h> -#include <scd30.hpp> #include <dr26.hpp> #include <drs26.hpp> #include <ina219.hpp> +#include <scd30.hpp> -// Forte_SCD30 scd30; +Forte_DRS26 drs26; -void setup() { - Serial.begin(9600); - // scd30.setup(); +void setup() +{ + Serial.begin(9600); + drs26.setup(); } -void loop() { - - void* data; - // data = scd30.read_data(); +void loop() +{ + + out_data_drs26 data; + + try { + data = drs26.read_data(); + // auto message = scd30.build_message(); + // message.send(); + } catch (const NoDataAvailableException &e) { + std::cerr << e.what() << '\n'; + } + + Serial.print("Sensor Circumference: "); + Serial.println(data.circumference); + Serial.print("Temperature "); + Serial.println(data.temperatur); + Serial.print("Id "); + Serial.println(data.id); + Serial.println(); - if(data==0) - { - Serial.println("Waiting for data !"); - Serial.println(); - } - // else{ - // Serial.print("Sensor CO2 "); Serial.println(data->C02); - // Serial.print("Humidity "); Serial.println(data->Humidity); - // Serial.print("Temperature "); Serial.println(data->Temperature); - // Serial.println(); - // } - delay(5000); + delay(5000); }