-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTempImp.device.nut
140 lines (120 loc) · 3.27 KB
/
TempImp.device.nut
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
temp <- hardware.pin1
temp.configure(ANALOG_IN);
led <- hardware.pin2;
led.configure(DIGITAL_OUT);
temperatureSamples <- 10; //number of temperature samples per measurement
periodShort <- 10; //minutes
periodLong <- 3; //Hours
maxTemp <- 50;
minTemp <- -20;
d <- date();
//Get non volatile variables
function loadNonVolatileVariables()
{
if (!("nv" in getroottable())) {
nv <- { currentEnabled = true, trendEnabled = false , lastTempValue = -999};
}
}
// 1. Load NonVolatileVariables
// 2. Flash the LED
// 3. When the time has come, read the temperature
// 4. Schedule next work appointment
function doWork() {
//1.
loadNonVolatileVariables();
//2.
flashLed();
//3.
//TREND Temp: Check if the time is on the 3rd hour (0-3-6...21)
//and if the temperature for this period has not been send already (trendEnabled)
if ((d.hour % periodLong) == 0 && nv.trendEnabled == true)
{
nv.trendEnabled = false;
sendTemperature(true);
}
else if (!((d.hour % periodLong) == 0))
{
nv.trendEnabled = true;
}
//CURRENT Temp: Check if the time is on the 10th minute (0-10-20...50)
//and if the temperature for this period has not been send already (currentEnabled)
if((d.min % periodShort) == 0 && nv.currentEnabled == true)
{
nv.currentEnabled = false;
sendTemperature(false);
}
else if (!((d.min % periodShort) == 0))
{
nv.currentEnabled = true;
}
//4.
imp.onidle(function() {
imp.deepsleepfor(10 - (time() % 10)); //Sleep for 10 seconds
});
}
//Send temperature to the agent
function sendTemperature(trend) {
local temperature = getAverageTemperature();
if (temperature == -999)
{
server.log("Invalid temperature found.");
}
else
{
nv.lastTempValue = temperature;
local formattedTemperature = format("%.01f", temperature);
if (trend)
{
agent.send("updateTemp", [formattedTemperature, "Trend"]);
imp.sleep(15);
agent.send("updateTemp", [formattedTemperature, "Current"]);
}
else
{
agent.send("updateTemp", [formattedTemperature, "Current"]);
}
}
server.expectonlinein(periodShort * 60);
}
//Get the average temperature from X samples
function getAverageTemperature()
{
local temperatureArray = [];
for(local i = 0; i < temperatureSamples; i++)
{
temperatureArray.push(getTemperatureSample());
imp.sleep(0.1);
}
local validValues = 0;
local sum = 0;
for(local i = 0; i < temperatureSamples; i++)
{
local value = temperatureArray[i];
if(nv.lastTempValue == -999) nv.lastTempValue = value; //Initialize the first stored value
if ( (value < maxTemp)
&& (value > minTemp)
&& (value < (nv.lastTempValue + 10))
&& (value > (nv.lastTempValue - 10)))
{
sum += value;
validValues++;
}
}
if (validValues == 0) return -999; //no valid temperature found
else return (sum / validValues);
}
//Get temperature from sensor, formula: ºC = 100 * V - 50.
function getTemperatureSample()
{
local supplyVoltage = hardware.voltage();
local voltageRead = supplyVoltage * (temp.read() / 65535.0); //Get millivolts reading
return (voltageRead - 0.5) * 100; //Celcius
}
function flashLed()
{
led.write(1);
imp.sleep(0.1);
led.write(0);
}
loadNonVolatileVariables();
imp.onidle(doWork);