-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotsaSensor.cpp
106 lines (90 loc) · 2.79 KB
/
iotsaSensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "iotsaSensor.h"
#include "iotsaConfigFile.h"
#ifdef IOTSA_WITH_WEB
void
IotsaSensorMod::handler() {
bool anyChanged = false;
if( server->hasArg("interval")) {
if (needsAuthentication()) return;
String sInterval = server->arg("interval");
interval = sInterval.toInt();
anyChanged = true;
}
if (anyChanged) configSave();
String message = "<html><head><title>Timed Sensor Module</title></head><body><h1>Timed Sensor Module</h1>";
message += "<form method='get'>Interval (ms): <input name='interval' value='";
message += String(interval);
message += "'><br><input type='submit'></form>";
server->send(200, "text/html", message);
}
String IotsaSensorMod::info() {
String message = "<p>Timed sensor readings. See <a href=\"/sensor\">/sensor</a> for configuration, <a href=\"/api\">/api</a> for readings.</p>";
return message;
}
#endif // IOTSA_WITH_WEB
bool IotsaSensorMod::getHandler(const char *path, JsonObject& reply) {
buffer.toJSON(reply);
reply["interval"] = interval;
return true;
}
bool IotsaSensorMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
if (!request.is<JsonObject>()) return false;
JsonObject reqObj = request.as<JsonObject>();
if (!reqObj.containsKey("interval")) return false;
interval = reqObj["interval"];
configSave();
return true;
}
void IotsaSensorMod::setup() {
configLoad();
}
void IotsaSensorMod::serverSetup() {
#ifdef IOTSA_WITH_WEB
server->on("/sensor", std::bind(&IotsaSensorMod::handler, this));
#endif
api.setup("/api/sensor", true, true);
name = "sensor";
}
void IotsaSensorMod::configLoad() {
IotsaConfigFileLoad cf("/config/sensor.cfg");
cf.get("interval", interval, 1000);
}
void IotsaSensorMod::configSave() {
IotsaConfigFileSave cf("/config/sensor.cfg");
cf.put("interval", interval);
}
void IotsaSensorMod::loop() {
if (millis() >= lastReading + interval) {
lastReading = millis();
int value = analogRead(A0);
buffer.add(value);
}
}
void SensorBuffer::add(SensorBufferItemValueType value)
{
if (nItem >= SENSORBUFFERSIZE) compact();
items[nItem].value = value;
items[nItem].timestamp = millis();
nItem++;
}
void SensorBuffer::compact()
{
int toRemove = nItem - SENSORBUFFERMINSIZE;
if (toRemove <= 0) return;
memmove(items, items+toRemove, SENSORBUFFERMINSIZE*sizeof(SensorBufferItem));
nItem -= toRemove;
}
void SensorBuffer::toJSON(JsonObject &replyObj)
{
if (nItem == 0) return;
uint32_t curTime = items[0].timestamp;
replyObj["timestamp"] = curTime;
JsonArray values = replyObj.createNestedArray("data");
for (int i=0; i<nItem; i++) {
uint32_t delta = items[i].timestamp-curTime;
JsonObject curValue = values.createNestedObject();
curValue["dt"] = delta;
curValue["v"] = items[i].value;
curTime = items[i].timestamp;
}
}