Skip to content
Snippets Groups Projects
Verified Commit f44e7cdf authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

WIP: Move MeasurementType, SensorProtocol, and HardwareName strings to enums

parent 3b43bb90
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!27Move from json-string based transfer between client and host to C struct / class based transfer
#include "scd30.hpp"
void ForteSCD30 ::setup()
{
Wire.begin(I2C_SDA, I2C_SCL);
if (!airSensor.begin()) {
// Sensor init went wrong
ESP_LOGW(sensorInformation.getHardwareName().c_str(), "Initialization failed.");
return;
}
void ForteSCD30::setup() {
Wire.begin(I2C_SDA, I2C_SCL);
if (!airSensor.begin()) {
// Sensor init went wrong
ESP_LOGW(sensorInformation.getHardwareNameString().c_str(),
"Initialization failed.");
return;
}
}
out_data_scd30 ForteSCD30 ::readData()
{
if (airSensor.dataAvailable()) {
data.C02 = airSensor.getCO2();
data.Temperature = airSensor.getTemperature();
data.Humidity = airSensor.getHumidity();
out_data_scd30 ForteSCD30::readData() {
if (airSensor.dataAvailable()) {
data.C02 = airSensor.getCO2();
data.Temperature = airSensor.getTemperature();
data.Humidity = airSensor.getHumidity();
return data;
}
throw NoDataAvailableException();
return data;
}
throw NoDataAvailableException();
}
std::list<Message> ForteSCD30::buildMessages()
{
std::list<Message> messages;
std::list<Message> ForteSCD30::buildMessages() {
std::list<Message> messages;
out_data_scd30 data = readData();
MeasurementData CO2Data{data.C02, 0, {}, "CO2"};
MeasurementData TempData{data.Temperature, 0, {}, "Temperature"};
MeasurementData HumidData{data.Humidity, 0, {}, "Humidity"};
data = readData(); // TODO: catch exception
MeasurementData CO2Data{data.C02, 0, {}, MeasurementType::CO2};
MeasurementData
TempData{data.Temperature, 0, {}, MeasurementType::TEMPERATURE};
MeasurementData HumidData{data.Humidity, 0, {}, MeasurementType::HUMIDITY};
messages.emplace_back(Message(CO2Data, sensorInformation, Time::getInstance().getEpochSeconds()));
messages.emplace_back(Message(TempData, sensorInformation, Time::getInstance().getEpochSeconds()));
messages.emplace_back(Message(HumidData, sensorInformation, Time::getInstance().getEpochSeconds()));
return messages;
messages.emplace_back(CO2Data,
sensorInformation,
Time::getInstance().getEpochSeconds());
messages.emplace_back(TempData,
sensorInformation,
Time::getInstance().getEpochSeconds());
messages.emplace_back(HumidData,
sensorInformation,
Time::getInstance().getEpochSeconds());
return messages;
}
SensorInformation ForteSCD30::getSensorInformation() const
{
return sensorInformation;
SensorInformation ForteSCD30::getSensorInformation() const {
return sensorInformation;
}
......@@ -10,22 +10,23 @@
#include <Wire.h>
struct out_data_scd30 {
float C02;
float Temperature;
float Humidity;
float C02;
float Temperature;
float Humidity;
};
class ForteSCD30 : public ForteSensor<out_data_scd30> {
public:
void setup() override;
out_data_scd30 readData() override;
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
class ForteSCD30: public ForteSensor<out_data_scd30> {
public:
void setup() override;
out_data_scd30 readData() override;
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
private:
SCD30 airSensor;
out_data_scd30 data;
const SensorInformation sensorInformation{"SCD30", SensorProtocol::I2C};
private:
SCD30 airSensor;
out_data_scd30 data;
const SensorInformation
sensorInformation{HardwareName::SCD30, SensorProtocol::I2C};
};
#endif
\ No newline at end of file
......@@ -3,70 +3,68 @@
//
#include "Sht85.hpp"
void Sht85::setup()
{
this->sht = SHTSensor(SHTSensor::SHT85);
readData();
void Sht85::setup() {
this->sht = SHTSensor(SHTSensor::SHT85);
readData();
}
void Sht85::readSHT()
{
// Read data from the initialized SHT85
// TODO send via esp now instead of printing
if (sht.readSample()) {
Serial.print("T: ");
Serial.println(sht.getTemperature(), 2);
Serial.print("RH: ");
Serial.println(sht.getHumidity(), 2);
} else {
Serial.print("Error in readSample()\n");
}
void Sht85::readSHT() {
// Read data from the initialized SHT85
// TODO send via esp now instead of printing
if (sht.readSample()) {
Serial.print("T: ");
Serial.println(sht.getTemperature(), 2);
Serial.print("RH: ");
Serial.println(sht.getHumidity(), 2);
} else {
Serial.print("Error in readSample()\n");
}
}
out_data_sht85 Sht85::readData()
{
// Start the I2C bus
Wire.begin(SDA, SCL);
delay(100); // let serial console settle
out_data_sht85 Sht85::readData() {
// Start the I2C bus
Wire.begin(SDA, SCL);
delay(100); // let serial console settle
// initiate the SHT85 sensor
Serial.println("");
if (sht.init(Wire)) {
Serial.println("SHT initialization successful");
} else {
Serial.println("SHT initialization failed");
}
// initiate the SHT85 sensor
Serial.println("");
if (sht.init(Wire)) {
Serial.println("SHT initialization successful");
} else {
Serial.println("SHT initialization failed");
}
// T + RH reading
readSHT();
// T + RH reading
readSHT();
delay(100);
delay(100);
// end I2C to free the pins to be used by the SD card
Wire.end();
// end I2C to free the pins to be used by the SD card
Wire.end();
out_data_sht85 sht85Data;
sht85Data.temperature = sht.getTemperature();
sht85Data.humidity = sht.getHumidity();
return sht85Data;
out_data_sht85 sht85Data;
sht85Data.temperature = sht.getTemperature();
sht85Data.humidity = sht.getHumidity();
return sht85Data;
}
std::list<Message> Sht85::buildMessages()
{
std::list<Message> messages;
auto data = readData();
MeasurementData temperatureMeasurementData{
data.temperature, {}, 0x44, measurementTypeToString.at(MeasurementType::TEMPERATURE)};
MeasurementData humidityMeasurementData{
data.humidity, {}, 0x44, measurementTypeToString.at(MeasurementType::HUMIDITY)};
std::list<Message> Sht85::buildMessages() {
std::list<Message> messages;
auto data = readData();
MeasurementData temperatureMeasurementData{
data.temperature, {}, 0x44, MeasurementType::TEMPERATURE};
MeasurementData humidityMeasurementData{
data.humidity, {}, 0x44, MeasurementType::HUMIDITY};
messages.emplace_back(
Message{temperatureMeasurementData, sensorInformation, Time::getInstance().getEpochSeconds()});
messages.emplace_back(Message{humidityMeasurementData, sensorInformation, Time::getInstance().getEpochSeconds()});
messages.emplace_back(
temperatureMeasurementData,
sensorInformation,
Time::getInstance().getEpochSeconds());
messages.emplace_back(humidityMeasurementData,
sensorInformation,
Time::getInstance().getEpochSeconds());
return messages;
return messages;
}
SensorInformation Sht85::getSensorInformation() const
{
return sensorInformation;
SensorInformation Sht85::getSensorInformation() const {
return sensorInformation;
}
......@@ -23,28 +23,24 @@
#define SCL 12
struct out_data_sht85 {
float temperature = 0.0;
float humidity = 0.0;
float temperature = 0.0;
float humidity = 0.0;
};
class Sht85 : public ForteSensor<out_data_sht85> {
public:
void setup() override;
out_data_sht85 readData() override;
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
class Sht85: public ForteSensor<out_data_sht85> {
public:
void setup() override;
out_data_sht85 readData() override;
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
private:
SHTSensor sht; // I2C address: 0x44
out_data_sht85 data;
const SensorInformation sensorInformation{"SHT85", SensorProtocol::I2C};
void readSHT();
private:
SHTSensor sht; // I2C address: 0x44
out_data_sht85 data;
const SensorInformation
sensorInformation{HardwareName::SHT85, SensorProtocol::I2C};
void readSHT();
enum class MeasurementType { TEMPERATURE, HUMIDITY };
// enum to string
std::map<MeasurementType, const char *> measurementTypeToString = {{MeasurementType::TEMPERATURE, "TEMPERATURE"},
{MeasurementType::HUMIDITY, "HUMIDITY"}};
};
#endif // ESPCAM_SHT85_HPP
......@@ -13,47 +13,51 @@
constexpr double ERROR_VALUE = -999.99;
constexpr unsigned long NULL_TIMESTAMP = 0; // null timestamp
enum ErrorTypes : short {
SENSOR_NOT_FOUND,
SENSOR_NOT_CONNECTED,
NO_DATA,
DATA_OK,
NULL_MESSAGE, // message that is sent as padding, should be thrown away
enum ErrorTypes: char {
SENSOR_NOT_FOUND,
SENSOR_NOT_CONNECTED,
NO_DATA,
DATA_OK,
NULL_MESSAGE, // message that is sent as padding, should be thrown away
};
struct CompressedDataPackage {
int channel;
int i2cAddress;
double value;
unsigned long timestamp;
ErrorTypes errorType;
char hardwareName[6];
char sensorProtocol[6];
char measurementType[18];
// tostring
[[nodiscard]] std::string toString() const {
StaticJsonDocument<250> document; // 250 byte is the max send size of espnow
document["hardwareName"] = hardwareName;
document["timestamp"] = timestamp;
document["sensorProtocol"] = sensorProtocol;
document["value"] = value;
if (channel != -1) {
document["channel"] = channel;
}
if (i2cAddress != -1) {
document["i2cAddress"] = i2cAddress;
}
document["measurementType"] = measurementType;
std::string jsonString;
serializeJson(document, jsonString);
return jsonString;
int channel; // can this be short?
int i2cAddress; // can this be short?
double value;
unsigned long timestamp;
ErrorTypes errorType;
HardwareName hardwareName;
SensorProtocol sensorProtocol;
MeasurementType measurementType;
// same could be done for hardwarename and sensorprotocol
// tostring
[[nodiscard]] std::string toString() const {
StaticJsonDocument<250> document; // 250 byte is the max send size of espnow
document["hardwareName"] =
HardwareNames::hardwareNameToString(hardwareName);
document["timestamp"] = timestamp;
document["sensorProtocol"] =
SensorProtocols::sensorProtocolToString(sensorProtocol);
document["value"] = value;
if (channel != -1) {
document["channel"] = channel;
}
if (i2cAddress != -1) {
document["i2cAddress"] = i2cAddress;
}
document["measurementType"] =
MeasurementTypes::measurementTypeToString(measurementType);
std::string jsonString;
serializeJson(document, jsonString);
return jsonString;
}
};
#endif // CLIENT_MOCK_COMPRESSEDDATAPACKAGE_HPP
//
// Created by zoe on 2/2/23.
//
#include "HardwareNames.h"
namespace HardwareNames {
std::string hardwareNameToString(HardwareName hardwareName) {
// switch
switch (hardwareName) {
case HardwareName::RS485:
return "RS485";
case HardwareName::INA219:
return "INA219";
case HardwareName::SCD30:
return "SCD30";
case HardwareName::RAIN_GAUGE:
return "RAIN_GAUGE";
case HardwareName::SOIL_MOISTURE_SENSOR:
return "SOIL_MOISTURE_SENSOR";
case HardwareName::SOIL_TEMPERATURE_SENSOR:
return "SOIL_TEMPERATURE_SENSOR";
case HardwareName::SOLAR_RADIATION_SENSOR:
return "SOLAR_RADIATION_SENSOR";
case HardwareName::SHT85:
return "SHT85";
case HardwareName::DRS26:
return "DRS26";
case HardwareName::DR26:
return "DR26";
case HardwareName::MOCK:
return "MOCK";
case HardwareName::NONE:
return "NONE";
case HardwareName::LC709203:
break;
}
return "UNKNOWN HARDWARE NAME";
}
}
\ No newline at end of file
//
// Created by zoe on 2/2/23.
//
#ifndef CLIENT_MOCK_HARDWARENAMES_H
#define CLIENT_MOCK_HARDWARENAMES_H
#include <map>
// 32,767 possible values
enum class HardwareName : short {
RS485, // TODO: THIS IS THE PROTOCOL NAME NOT THE HARDWARE NAME
INA219,
SCD30,
RAIN_GAUGE,
SOIL_MOISTURE_SENSOR,
SOIL_TEMPERATURE_SENSOR,
SOLAR_RADIATION_SENSOR,
SHT85,
DRS26,
DR26,
MOCK,
NONE,
LC709203,
};
// hardware name to string function
namespace HardwareNames {
std::string hardwareNameToString(HardwareName hardwareName);
}
#endif //CLIENT_MOCK_HARDWARENAMES_H
//
// Created by zoe on 2/2/23.
//
# include "MeasurementTypes.h"
namespace MeasurementTypes {
std::string measurementTypeToString(MeasurementType measurementType) {
switch (measurementType) {
case MeasurementType::CIRCUMFERENCE_INCREMENT:
return "CIRCUMFERENCE_INCREMENT";
case MeasurementType::TEMPERATURE:
return "TEMPERATURE";
case MeasurementType::HUMIDITY:
return "HUMIDITY";
case MeasurementType::SHUNT_VOLTAGE:
return "SHUNT_VOLTAGE";
case MeasurementType::BUS_VOLTAGE:
return "BUS_VOLTAGE";
case MeasurementType::CURRENT_mA:
return "CURRENT_mA";
case MeasurementType::POWER_mA:
return "POWER_mA";
case MeasurementType::LOAD_VOLTAGE_V:
return "LOAD_VOLTAGE_V";
case MeasurementType::INA219_OVERFLOW:
return "INA219_OVERFLOW";
case MeasurementType::BATTERY_VOLTAGE:
return "BATTERY_VOLTAGE";
case MeasurementType::MOCK:
return "MOCK";
case MeasurementType::SOLAR_RADIATION:
return "SOLAR_RADIATION";
case MeasurementType::SOIL_MOISTURE_3:
return "SOIL_MOISTURE_3";
case MeasurementType::SOIL_TEMPERATURE_3:
return "SOIL_TEMPERATURE_3";
case MeasurementType::SOIL_MOISTURE_4:
return "SOIL_MOISTURE_4";
case MeasurementType::SOIL_TEMPERATURE_4:
return "SOIL_TEMPERATURE_4";
case MeasurementType::SOIL_MOISTURE_5:
return "SOIL_MOISTURE_5";
case MeasurementType::SOIL_TEMPERATURE_5:
return "SOIL_TEMPERATURE_5";
case MeasurementType::PRECIPITATION:
return "PRECIPITATION";
case MeasurementType::CO2:
return "CO2";
case MeasurementType::NULL_MEASUREMENT:
return "NULL_MEASUREMENT";
}
return "UNKNOWN";
}
}
\ No newline at end of file
//
// Created by zoe on 2/2/23.
//
#ifndef CLIENT_MOCK_MEASUREMENTTYPES_H
#define CLIENT_MOCK_MEASUREMENTTYPES_H
#include <map>
enum class MeasurementType {
CIRCUMFERENCE_INCREMENT,
TEMPERATURE,
HUMIDITY,
SHUNT_VOLTAGE,
BUS_VOLTAGE,
CURRENT_mA,
POWER_mA,
LOAD_VOLTAGE_V,
INA219_OVERFLOW,
BATTERY_VOLTAGE,
MOCK,
SOLAR_RADIATION,
SOIL_MOISTURE_3,
SOIL_TEMPERATURE_3,
SOIL_MOISTURE_4,
SOIL_TEMPERATURE_4,
SOIL_MOISTURE_5,
SOIL_TEMPERATURE_5,
PRECIPITATION,
CO2,
NULL_MEASUREMENT,
};
// convert below to a function
// measurement type to string function
namespace MeasurementTypes {
std::string measurementTypeToString(MeasurementType measurementType);
}
#endif //CLIENT_MOCK_MEASUREMENTTYPES_H
//
// Created by zoe on 2/2/23.
//
#include "SensorProtocol.hpp"
namespace SensorProtocols {
std::string sensorProtocolToString(SensorProtocol sensorProtocol) {
switch (sensorProtocol) {
case SensorProtocol::I2C:
return "I2C";
case SensorProtocol::RS485:
return "RS485";
case SensorProtocol::Analog:
return "Analog";
case SensorProtocol::Mock:
return "Mock";
case SensorProtocol::NULL_PROTOCOL:
return "NULL_PROTOCOL";
}
return "UNKNOWN";
}
}
\ No newline at end of file
//
// Created by zoe on 10/5/22.
//
#ifndef CLIENT_PROTOCOL_HPP
#define CLIENT_PROTOCOL_HPP
#include <map>
enum class SensorProtocol : short { I2C, RS485, Analog, Mock, NULL_PROTOCOL };
namespace SensorProtocols {
std::string sensorProtocolToString(SensorProtocol sensorProtocol);
}
#endif // CLIENT_PROTOCOL_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment