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

WIP: Initial capability to read out terros10

parent 442fb378
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!32Implement Teros Sensors
Pipeline #104054 failed
#include "ESPNow.hpp"
#include <Arduino.h>
#include <DS18B20.h>
#include <Terros11.h>
#include <Terros10.h>
static const char *TAG = "MAIN";
// Data wire is plugged into port 2 on the Arduino
constexpr int ONE_WIRE_BUS = 33;
DS18B20 ds18b20{ONE_WIRE_BUS};
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
// Pass our oneWire reference to Dallas Temperature.
Terros11 terros11(4);
Terros10 terros10_a0;
Terros10 terros10_a3;
void setup() {
// start serial port
Serial.begin(115200);
gpio_install_isr_service(0);
ds18b20.setup();
terros10_a0.setup();
terros10_a3.setup();
terros10_a0.setChannel(0);
terros10_a3.setChannel(3);
terros11.setup();
}
......@@ -26,9 +35,14 @@ void setup() {
*/
void loop() {
terros11.readData();
// ESP_LOGD(TAG, "Sending %d messages", data.size());
// for (auto const &message : data) {
// ESP_LOGD(TAG, "%s", message.getMessageAsMinifiedJsonString().c_str());
// }
delay(1000);
terros10_a0.readData();
terros10_a3.readData();
auto data = ds18b20.buildMessages();
ESP_LOGD(TAG, "Sending %d messages", data.size());
for (auto const &message : data) {
ESP_LOGD(TAG, "%s", message.getMessageAsMinifiedJsonString().c_str());
}
delay(2000);
}
#include "Terros10.h"
static const char *TAG = "DR26";
void Terros10::setup() {
if (sensorConnected) {
ESP_LOGD(TAG, "ADS already initialized");
return;
}
Wire.begin();
ads.setGain(GAIN_ONE);
ads.setDataRate(RATE_ADS1115_8SPS);
if (ads.begin()) {
ESP_LOGD(TAG, "ADS initialized");
sensorConnected = true;
} else {
ESP_LOGE(TAG, "failed to initialize ADS");
sensorConnected = false;
}
channel = 0;
}
Measurement Terros10::readData() {
float volts = 0;
int16_t adc = 0;
if (!sensorConnected) {
return {ERROR_VALUE, channel, NO_I2C_ADDRESS, MeasurementType::SOIL_MOISTURE,
ErrorType::SENSOR_INIT_FAILED};
}
// TODO: This function might never return if conversionComplete is never true. Check if this is the case
// FIXME: How to notice from the dr if a value is incorrect?
adc = ads.readADC_SingleEnded(channel);
volts = ads.computeVolts(adc);
ESP_LOGD("Terros10 readData", "volts (ch: %d): %f", channel, volts);
// map the voltage (1 to 2.5 V to a vwc of 1 to 80)
long mv = volts * 1000; // cuts off the decimal
auto apparentDielectricPermittivity = map(mv, 1000, 2500, 1, 80);
ESP_LOGD("Terros10 readData", "apparentDielectricPermittivity (ch: %d): %d", channel, apparentDielectricPermittivity);
return {volts, channel, NO_I2C_ADDRESS, MeasurementType::SOIL_MOISTURE, ErrorType::DATA_OK};
}
// 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 Terros10::changeGain(adsGain_t gain) {
ads.setGain(gain);
}
void Terros10::setChannel(int c) {
channel = c;
}
std::list<Message> Terros10::buildMessages() {
std::list<Message> messages;
auto data = readData();
messages.emplace_back(data, sensorInformation, Time::getInstance().getEpochSeconds());
return messages;
}
SensorInformation Terros10::getSensorInformation() const {
return sensorInformation;
}
#ifndef Terros10_HEADERGUARD
#define Terros10_HEADERGUARD
#include "Adafruit_ADS1X15.h"
#include "ForteSensor.hpp"
#include "Message.hpp"
#include "Pinout.hpp"
#include "esp_log.h"
#include <Wire.h>
#include "NoDataAvailableException.hpp"
class Terros10: public ForteSensor<Measurement> {
public:
void setup() override;
Measurement readData() override;
void changeGain(adsGain_t gain);
void setChannel(int channel);
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
Adafruit_ADS1115 ads;
private:
const SensorInformation
sensorInformation{HardwareName::Terros10, SensorProtocol::Analog};
int channel;
bool sensorConnected = false;
};
#endif
\ No newline at end of file
......@@ -17,7 +17,7 @@ class ForteDR26: public ForteSensor<Measurement> {
void setChannel(int channel);
std::list<Message> buildMessages() override;
[[nodiscard]] SensorInformation getSensorInformation() const override;
static Adafruit_ADS1115 ads;
Adafruit_ADS1115 ads;
private:
const SensorInformation
......
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