sensors + json [UNTESTED]
This commit is contained in:
parent
b6d1d319a8
commit
562ac507cb
@ -9,6 +9,7 @@ lib_deps = milesburton/DallasTemperature
|
||||
https://github.com/volkszaehler/libsml
|
||||
https://github.com/adafruit/MAX6675-library
|
||||
paulstoffregen/OneWire
|
||||
bblanchon/ArduinoJson
|
||||
upload_port = /dev/ttyUSB0
|
||||
upload_speed = 921600
|
||||
monitor_port = /dev/ttyUSB0
|
||||
|
||||
1
src/patrix/Node.cpp
Normal file
1
src/patrix/Node.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "Node.h"
|
||||
20
src/patrix/Node.h
Normal file
20
src/patrix/Node.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class Node {
|
||||
|
||||
public:
|
||||
|
||||
virtual ~Node() = default;
|
||||
|
||||
virtual void setup() = 0;
|
||||
|
||||
virtual void loop() = 0;
|
||||
|
||||
virtual void toJson(JsonDocument& json) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif //NODE_H
|
||||
@ -1,8 +1,9 @@
|
||||
#include "clock.h"
|
||||
|
||||
#include <log.h>
|
||||
#include <WiFi.h>
|
||||
#include <wifi.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#define CLOCK_GMT_OFFSET_SECONDS 3600
|
||||
#define CLOCK_DST_OFFSET_SECONDS 3600
|
||||
@ -1,10 +1,10 @@
|
||||
#include "log.h"
|
||||
|
||||
#include <clock.h>
|
||||
#include <cstdio>
|
||||
#include <HardwareSerial.h>
|
||||
#include <cstdarg>
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
auto logLevel = DEBUG;
|
||||
|
||||
void log(const LogLevel level, const char *format, const va_list args) {
|
||||
@ -1,6 +1,6 @@
|
||||
#include <HardwareSerial.h>
|
||||
#include <mqtt.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "mqtt.h"
|
||||
#include "wifi.h"
|
||||
#include "clock.h"
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
#include "mqtt.h"
|
||||
|
||||
#include <log.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFi.h>
|
||||
#include <wifi.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#define MQTT_RETRY_DELAY_MILLIS 3000
|
||||
|
||||
41
src/patrix/sensor/DHT22.h
Normal file
41
src/patrix/sensor/DHT22.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef DHT22_H
|
||||
#define DHT22_H
|
||||
|
||||
#include "dht_nonblocking.h"
|
||||
|
||||
class DHT22 final : public Sensor {
|
||||
|
||||
DHT_nonblocking sensor;
|
||||
|
||||
double temperatureThreshold;
|
||||
|
||||
double humidityRelativeThreshold;
|
||||
|
||||
double humidityAbsoluteThreshold;
|
||||
|
||||
float temperature = NAN;
|
||||
|
||||
float humidityRelative = NAN;
|
||||
|
||||
double humidityAbsolute = NAN;
|
||||
|
||||
public:
|
||||
|
||||
DHT22(const int pin, const char *name, const double temperatureThreshold, const double humidityRelativeThreshold, const double humidityAbsoluteThreshold) : Sensor(name), sensor(pin, DHT_TYPE_22), temperatureThreshold(temperatureThreshold), humidityRelativeThreshold(humidityRelativeThreshold), humidityAbsoluteThreshold(humidityAbsoluteThreshold) {
|
||||
//
|
||||
}
|
||||
|
||||
bool loop() override {
|
||||
sensor.measure(&temperature, &humidityRelative);
|
||||
humidityAbsolute = 6.112 * exp(17.67 * temperature / (243.5 + temperature)) * humidityRelative * 2.1674 / (temperature + 273.15);
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name]["temperature"] = temperature;
|
||||
json[name]["humidity"]["relative"] = humidityRelative;
|
||||
json[name]["humidity"]["absolute"] = humidityAbsolute;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
84
src/patrix/sensor/Dallas.h
Normal file
84
src/patrix/sensor/Dallas.h
Normal file
@ -0,0 +1,84 @@
|
||||
#ifndef DALLAS_H
|
||||
#define DALLAS_H
|
||||
|
||||
#include "OneWire.h"
|
||||
|
||||
#include "DallasTemperature.h"
|
||||
|
||||
#define DALLAS_TIMEOUT_MILLISECONDS 3000
|
||||
|
||||
class Dallas {
|
||||
|
||||
OneWire oneWire;
|
||||
|
||||
DallasTemperature sensors;
|
||||
|
||||
time_t timestamp = 0;
|
||||
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
bool converting = false;
|
||||
|
||||
bool converted = false;
|
||||
|
||||
bool first = true;
|
||||
|
||||
public:
|
||||
|
||||
explicit Dallas(const int pin) : oneWire(pin), sensors(&oneWire) {
|
||||
sensors.begin();
|
||||
sensors.setWaitForConversion(false);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (converting) {
|
||||
if (sensors.isConversionComplete()) {
|
||||
if (first) {
|
||||
first = false;
|
||||
const auto count = sensors.getDeviceCount();
|
||||
if (count == 0) {
|
||||
Serial.printf("ERROR: No Dallas devices found!\n");
|
||||
} else {
|
||||
Serial.printf("Found %d Dallas devices:\n", count);
|
||||
uint64_t address;
|
||||
for (int index = 0; index < count; ++index) {
|
||||
sensors.getAddress(reinterpret_cast<uint8_t *>(&address), index);
|
||||
Serial.printf(" - 0x%016llX = %5.1f C\n", address, sensors.getTempC(reinterpret_cast<uint8_t *>(&address)));
|
||||
}
|
||||
}
|
||||
}
|
||||
converting = false;
|
||||
converted = true;
|
||||
} else if (millis() - lastMillis > DALLAS_TIMEOUT_MILLISECONDS) {
|
||||
Serial.print("ERROR: Dallas timeout!\n");
|
||||
converting = false;
|
||||
converted = false;
|
||||
}
|
||||
} else {
|
||||
sensors.requestTemperatures();
|
||||
timestamp = time(nullptr);
|
||||
lastMillis = millis();
|
||||
converting = true;
|
||||
converted = false;
|
||||
}
|
||||
}
|
||||
|
||||
double read(uint64_t address) {
|
||||
const auto value = sensors.getTempC(reinterpret_cast<uint8_t *>(&address));
|
||||
if (value <= DEVICE_DISCONNECTED_C) {
|
||||
return NAN;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
time_t getTimestamp() const {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
bool isConverted() const {
|
||||
return converted;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
36
src/patrix/sensor/DallasSensor.h
Normal file
36
src/patrix/sensor/DallasSensor.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef DALLAS_SENSOR_H
|
||||
#define DALLAS_SENSOR_H
|
||||
|
||||
#include "Sensor.h"
|
||||
|
||||
#include "Dallas.h"
|
||||
|
||||
class DallasSensor final : public Sensor {
|
||||
|
||||
Dallas& sensors;
|
||||
|
||||
uint64_t address;
|
||||
|
||||
double threshold;
|
||||
|
||||
double temperature = NAN;
|
||||
|
||||
public:
|
||||
|
||||
DallasSensor(Dallas& sensors, const uint64_t address, const char *name, const double threshold) : Sensor(name), sensors(sensors), address(address), threshold(threshold) {
|
||||
// -
|
||||
}
|
||||
|
||||
bool loop() override {
|
||||
if (sensors.isConverted()) {
|
||||
temperature = sensors.read(address);
|
||||
}
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name]["temperature"] = temperature;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
33
src/patrix/sensor/Max6675Sensor.h
Normal file
33
src/patrix/sensor/Max6675Sensor.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef MAX6675SENSOR_H
|
||||
#define MAX6675SENSOR_H
|
||||
|
||||
#include "max6675.h"
|
||||
|
||||
class Max6675Sensor final : public Sensor {
|
||||
|
||||
MAX6675 sensor;
|
||||
|
||||
double threshold;
|
||||
|
||||
double temperature = NAN;
|
||||
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
public:
|
||||
|
||||
explicit Max6675Sensor(const int8_t pinMISO, const int8_t pinCS, const int8_t pinCLK, const char *name, const double threshold) : Sensor(name), sensor(pinCLK, pinCS, pinMISO), threshold(threshold) {
|
||||
// -
|
||||
}
|
||||
|
||||
bool loop() override {
|
||||
float current = sensor.readCelsius();
|
||||
temperature = current;
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name]["temperature"] = temperature;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
26
src/patrix/sensor/Sensor.h
Normal file
26
src/patrix/sensor/Sensor.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef SENSOR_H
|
||||
#define SENSOR_H
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class Sensor {
|
||||
|
||||
protected:
|
||||
|
||||
const char *name;
|
||||
|
||||
public:
|
||||
|
||||
explicit Sensor(const char *name): name(name) {
|
||||
//
|
||||
}
|
||||
|
||||
virtual ~Sensor() = default;
|
||||
|
||||
virtual bool loop() = 0;
|
||||
|
||||
virtual void toJson(JsonDocument& json) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user