LoraComms/SensorTransceiverNode/Sensors.h
2025-03-10 18:58:58 -05:00

127 lines
3.2 KiB
C++

#ifndef SENSORS_H
#define SENSORS_H
#include <RHReliableDatagram.h>
#include <RHDatagram.h>
#include <RH_RF95.h>
#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_SCD30.h>
#include <SPI.h>
#include <SparkFun_SCD30_Arduino_Library.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS28E17.h>
//https://github.com/hardwario/SoilSensor/tree/master
// Sensor definitions
#define MESSAGE_TYPE_SENSOR_DATA 2
#define SENSOR_TYPE_BAROMETER 10
#define SENSOR_TYPE_SCD30_TEMPERATURE 11
#define SENSOR_TYPE_SCD30_HUMIDITY 12
#define SENSOR_TYPE_SCD30_CO2 13
#define SENSOR_TYPE_RAIN 14
#define SENSOR_TYPE_WINDSPEED 15
#define SENSOR_TYPE_SOIL_TEMPERATURE 16//ds18b20
#define SENSOR_TYPE_SOIL_HUMIDITY A2
#define SENSOR_TYPE_LIGHT_DIODE 18
#define MASTER_NODE_ID 0
// https://github.com/adafruit/Adafruit_BME280_Library/blob/master/examples/bme280test/bme280test.ino
// https://lastminuteengineers.com/rain-sensor-arduino-tutorial/
// Add a 4k7 pull-up resistor to this pin
// https://how2electronics.com/measure-wind-speed-using-anemometer-arduino/
//https://store-usa.arduino.cc/products/gravity-analog-capacitive-soil-moisture-sensor-corrosion-resistant?selectedStore=us
// GL5528
// Sensor bitmask
#define BAROMETER_BIT 0x01
#define SCD30_TEMPERATURE_BIT 0x02
#define SCD30_HUMIDITY_BIT 0x04
#define SCD30_CO2_BIT 0x08
#define RAIN_BIT 0x10
#define WINDSPEED_BIT 0x20
#define SOIL_TEMPERATURE_BIT 0x40
#define SOIL_HUMIDITY_BIT 0x80
#define LIGHT_BIT 0x100
//Check if Pin is in USe
// https://arduino.stackexchange.com/questions/14647/how-can-i-detect-a-disconnected-pin
#define WIND_PIN A0
#define LIGHT_PIN A1
#define RAIN_PWR_PIN 1
#define RAIN_PIN SENSOR_TYPE_RAIN
#define SOIL_HUMIDITY_PWR_PIN 2
#define SOIL_HUMIDITY_PIN SENSOR_TYPE_SOIL_HUMIDITY
struct Message {
uint8_t type;
uint8_t senderID;
uint8_t lastRelayID;
uint8_t targetID;
char id[10];
uint8_t hops;
char route[10];
char masterRoute[10];
char data[20];
uint8_t sensorType;
uint16_t sensorValue;
};
struct AckMessage {
uint8_t type; // Should be MESSAGE_TYPE_ACK
uint8_t consumed; // MESSAGE_CONSUMED or MESSAGE_NOT_CONSUMED
};
struct Neighbor {
uint8_t nodeID;
unsigned long lastSeen;
};
class Sensors {
public:
Sensors();
uint8_t NODE_ID;
void initialize();
int readSensorsAndSend();
Message* messages = nullptr;
int messageArraySize = 0;
~Sensors() {
if (messages) delete[] messages;
}
void setAddress(uint8_t id);
private:
Adafruit_BME280 bme;
OneWire wire;
SCD30 scd30;
uint16_t sensorFlags;
void initBarometer();
void initSCD30();
void initRainSensor();
void initWindSensor();
void initSoilSensors();
void initLightSensor();
char* generateMessageID();
float readBarometer();
float readSCD30Temperature();
float readSCD30Humidity();
float readSCD30CO2();
float readRain();
float readWindSpeed();
float readSoilTemperature();
float readSoilHumidity();
float readLight();
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
Message sendSensorData(uint8_t sensorType, uint16_t value);
};
#endif