-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pde
157 lines (123 loc) · 2.87 KB
/
main.pde
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <WaspSensorGas.h>
float value, press, hum, temp;
byte d, M, Y, h, m, even[4] = {4,6,9,11}, i, battery, temper;
int sum;
byte ALd=1,ALh=14,ALm=0;
char pressstr[10], humstr[10], data[20];
void setup(){
// Set SD ON
SD.ON();
// Powers RTC up, init I2C bus and read initial values
RTC.ON();
// Turn on the sensor board
SensorGas.setBoardMode(SENS_ON);
// Turn on pressure sensor
SensorGas.setSensorMode(SENS_ON, SENS_PRESSURE);
delay(100); //waiting for the stabilization of the power supply
// Turn on humidity sensor
SensorGas.setSensorMode(SENS_ON, SENS_HUMIDITY);
delay(100); //waiting for the stabilization of the power supply
}
void loop()
{
getTime();
setAlarm();
// Setting Waspmote to Low-Power Consumption Mode
PWR.sleep(ALL_OFF);
if( intFlag & RTC_INT )
{
setup();
getTime();
intFlag &= ~(RTC_INT); // Clear flag
meanPressure();
meanHumidity();
meanBattery();
meanTemper();
logData();
Utils.blinkLEDs(200);
}
}
void getTime() //Getting current date & time
{
RTC.getTime();
d = RTC.date,DEC;
M = RTC.month,DEC;
Y = RTC.year,DEC;
h = RTC.hour,DEC;
m = RTC.minute,DEC;
}
void setAlarm()
{
RTC.setAlarm2(ALd,ALh,ALm,RTC_ABSOLUTE,RTC_ALM2_MODE2);
USB.print("Alarm2: ");
USB.println(RTC.getAlarm2());
ALh++;
if (ALh == 24)
{
ALh=0;
ALd++;
if (ALd == 31)
{
for (i=0; i<4; i++)
{
if (M == even[i])
{
ALd = 1;
}
}
}
else if (ALd == 32)
{
ALd = 1;
}
else if (ALd == 28 && M == 2)
{
ALd = 1;
}
}
}
void meanBattery()
{
sum=0;
for (i=0; i<100; i++)
{
sum += PWR.getBatteryLevel(),DEC;
}
battery = sum/i;
}
void meanTemper()
{
sum=0;
for (i=0; i<100; i++)
{
sum += RTC.getTemperature(),DEC;
}
temper = sum/i;
}
void meanHumidity()
{
temp=0;
for (i=0; i<100; i++)
{
value = SensorGas.readValue(SENS_HUMIDITY);
temp += (value - 0.48)/(0.0186);
}
hum = temp/i;
Utils.float2String(hum,humstr,2);
}
void meanPressure()
{
temp=0;
for (i=0; i<100; i++)
{
temp += ((((SensorGas.readValue(SENS_PRESSURE)) * (0.2/0.12)) / (5*0.009)) + (0.095/0.009)) * 10;
}
press = temp/i;
Utils.float2String(press,pressstr,2);
}
void logData()
{
sprintf(data,"%d/%d/%d %d:%d%c%s%c%s%c%d%c%d%c",d,M,Y,h,m,'\t',humstr, '\t', pressstr, '\t', battery, '\t', temper, '\n');
if(!SD.create("datalog.txt")) SD.create("datalog.txt");
SD.appendln("datalog.txt",data);
}