Add sensor value pushing
parent
dad701045c
commit
53b97e20c3
|
@ -1,8 +1,9 @@
|
||||||
#include <SpejsNode.h>
|
#include <SpejsNode.h>
|
||||||
#include <Endpoint.h>
|
#include <Endpoint.h>
|
||||||
|
|
||||||
void Endpoint::bind(String key, SpejsNode* _parent) {
|
void Endpoint::bind(String _key, SpejsNode* _parent) {
|
||||||
parent = _parent;
|
parent = _parent;
|
||||||
|
key = _key;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Endpoint::notify(String value) {
|
void Endpoint::notify(String value) {
|
||||||
|
@ -10,10 +11,16 @@ void Endpoint::notify(String value) {
|
||||||
parent->notify(this, value);*/
|
parent->notify(this, value);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T> void InputEndpoint<T>::updateValue(T newValue) {
|
template <class T> void ValueEndpoint<T>::updateValue(T newValue) {
|
||||||
value = newValue;
|
value = newValue;
|
||||||
//if(parent)
|
|
||||||
// parent->notify(String("xD"), String(value));
|
// TODO parent->notify(this, String(value)) ?
|
||||||
|
if(parent)
|
||||||
|
parent->notify(key, String(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T> void ValueEndpoint<T>::fillValue(JsonObject& obj) {
|
||||||
|
obj["value"] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
EndpointResult ControlEndpoint::onValue(String key, String value) {
|
EndpointResult ControlEndpoint::onValue(String key, String value) {
|
||||||
|
@ -46,3 +53,24 @@ EndpointResult OutputEndpoint::onValue(String key, String value) {
|
||||||
digitalWrite(pin, inverted ^ currentValue);
|
digitalWrite(pin, inverted ^ currentValue);
|
||||||
return 200;
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DHTEndpoint::bind(String _key, SpejsNode* _parent) {
|
||||||
|
parent = _parent;
|
||||||
|
key = _key;
|
||||||
|
|
||||||
|
sensor.begin();
|
||||||
|
samplingTimer.initializeMs(samplingRate, TimerDelegate(&DHTEndpoint::sample, this)).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DHTEndpoint::sample() {
|
||||||
|
TempAndHumidity th;
|
||||||
|
if(sensor.readTempAndHumidity(th))
|
||||||
|
{
|
||||||
|
updateValue(th.temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Failed to read from DHT: ");
|
||||||
|
Serial.print(sensor.getLastError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,14 +17,15 @@ public:
|
||||||
|
|
||||||
class Endpoint {
|
class Endpoint {
|
||||||
protected:
|
protected:
|
||||||
const SpejsNode* parent;
|
SpejsNode* parent;
|
||||||
|
String key;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
String type;
|
String type;
|
||||||
|
|
||||||
Endpoint(String _type = "unknown") : type(_type) { }
|
Endpoint(String _type = "unknown") : type(_type) { }
|
||||||
|
|
||||||
virtual void bind(String key, SpejsNode* _parent);
|
virtual void bind(String _key, SpejsNode* _parent);
|
||||||
void notify(String value);
|
void notify(String value);
|
||||||
|
|
||||||
virtual EndpointResult onValue(String key, String value) {
|
virtual EndpointResult onValue(String key, String value) {
|
||||||
|
@ -57,26 +58,33 @@ public:
|
||||||
void fillValue(JsonObject& obj);
|
void fillValue(JsonObject& obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T> class InputEndpoint : public Endpoint {
|
template <class T> class ValueEndpoint : public Endpoint {
|
||||||
protected:
|
protected:
|
||||||
T value;
|
T value;
|
||||||
void updateValue(T newValue);
|
void updateValue(T newValue);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InputEndpoint(String _type) : Endpoint(_type) {}
|
ValueEndpoint(String _type) : Endpoint(_type) {}
|
||||||
|
|
||||||
|
virtual void fillValue(JsonObject& obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <Libraries/DHT/DHT.h>
|
#include <Libraries/DHT/DHT.h>
|
||||||
|
|
||||||
class DHT22Endpoint : public InputEndpoint<int> {
|
class DHTEndpoint : public ValueEndpoint<float> {
|
||||||
private:
|
private:
|
||||||
DHT sensor;
|
DHT sensor;
|
||||||
Timer samplingTimer;
|
Timer samplingTimer;
|
||||||
int samplingRate;
|
int samplingRate;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void sample();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DHT22Endpoint(int _pin, int _samplingRate = 10000) :
|
DHTEndpoint(int _pin, int _samplingRate = 10000) :
|
||||||
InputEndpoint("dht22"), sensor(_pin), samplingRate(_samplingRate) {}
|
ValueEndpoint("dht"), sensor(_pin, DHT11), samplingRate(_samplingRate) {}
|
||||||
|
|
||||||
|
void bind(String key, SpejsNode* _parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue