-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
304 lines (233 loc) · 7.3 KB
/
main.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "Arduino.h"
#include "SC16IS7X0.h"
#include "sb_services.h"
//SC16IS7X0 I2C_UART;
#include "LIS3DH.h"
#include "SIM908.h"
#include "utils.h"
#include "BG_serial.h"
#include "eeprom_store.h"
#define LIGHT_DEBOUNCE_MS 30000
#define RSSI_DEBOUNCE_MS 30000
#define BATTERY_UPDATE_INTERVAL 60000
int16_t lis_x;
int16_t lis_y;
int16_t lis_z;
int lightSensorPin = A0;
int lightSensorValue = 0;
unsigned long light_debounce_start = 0;
unsigned long RSSI_debounce_start = 0;
unsigned long battery_update_start = 0;
uint8_t move_enabled = FALSE;
uint8_t push_enabled = FALSE;
uint16_t accel_thr_move = 300;
uint16_t accel_thr_impact = 1000;
uint16_t light_thr = 100;
char received[64], numchar;
unsigned long ledDelay;
extern SC16IS7X0 BG_UART;
extern uint8_t data_2_send;
extern uint8_t hour, minute, second, year, month, day;
extern int8_t BATT_level;
LIS3DH lis;
void setup();
void loop();
//TODO
/*
//gestire gli stati APERTO / CHIUSO per il sensore di lucw
in futuro bisogna gestire correttamente gli interrupt dei due convertitore seriale I2C
*/
void setup()
{
char tmpStr[120];
int command_status;
//led setting
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
Serial.begin(115200);
blinkLed(BLINK_ORANGE, 5, 500);
dbg_print_P(PSTR("START!"));
// enable LIS3DH
lis.begin();
blinkLed(BLINK_GREEN, 3, 300);
SIM908_init();
blinkLed(BLINK_GREEN, 4, 300);
SIM908_GPS_power(OFF);
// delay(1000);
SIM908_GPS_power(ON);
blinkLed(BLINK_GREEN, 5, 300);
dbg_print_P(PSTR("Init BG ...\n"));
BG_sapi_init();
delay(3000);
blinkLed(BLINK_GREEN, 6, 300);
dbg_print_P(PSTR("set_connection_mode_connectable_discoverable ...\n"));
command_status = set_connection_mode_connectable_discoverable();
if (command_status != 0 ){
dbg_print_P(PSTR("Error!!!\n"));
blinkLed(BLINK_RED, 5, 500);
}
blinkLed(BLINK_GREEN, 7, 300);
dbg_print_P(PSTR("READ SETTINGS FROM EEPROM\n"));
init_eeprom();
ledDelay = millis();
dbg_print_P(PSTR("MAIN LOOP\n"));
}
void ble_task()
{
//char debugString[40];
//short cmdStat;
//uint16_t handle = 3;
//char attribute[64];
uint8_t not_in_proximity = 1;
int event_status;
//delay(1000); // wait for a second
if ( is_connection_established() == 1 ) {
// get_connection_RSSI( &rssi );
if (!is_in_proximity() && (millis() - RSSI_debounce_start > RSSI_DEBOUNCE_MS)) {
RSSI_debounce_start = millis();
// if( !is_in_proximity() ){
if (write_att_request_by_handle_wo_offset(0x0017, 1, ¬_in_proximity, WAIT_RESPONSE) != 0 )
dbg_print_P(PSTR("Error writing the attribute\n"));
}
// evaluated_rssi = rssi;// ^ 0xFF;
// sprintf(debugString, "the RSSI is %d\n\n", evaluated_rssi);
// Serial.println(debugString);
//send battery data to iphone
if (millis() - battery_update_start >= BATTERY_UPDATE_INTERVAL){
if (write_att_request_by_handle_wo_offset(BATTERY_LEVEL_HANDLE, 1, (uint8_t *) &BATT_level, WAIT_RESPONSE) != 0 )
dbg_print_P(PSTR("Error updating batt level\n"));
battery_update_start = millis();
}
}
// numchar = I2C_UART.readBytes ((unsigned char *)received, 64);
// if (numchar > 0)
// Serial.write((const uint8_t*)received, numchar);
// if (BG_UART.available())
event_status = check_for_incoming_events();
if ( event_status == BG_PACKET_EVENT_NOT_RECOGNIZED )
dbg_print_P(PSTR("Event not recognized\n"));
}
void LIS3DH_task()
{
uint16_t threshold;
if (move_enabled == TRUE)
threshold = accel_thr_move;
else
threshold = accel_thr_impact;
lis.getXValue(&lis_x);
lis.getYValue(&lis_y);
lis.getZValue(&lis_z);
lis_x = abs(lis_x);
lis_y = abs(lis_y);
lis_z = abs(lis_z);
/*
Serial.print("acc X ");
Serial.println(lis_x);
Serial.print(" acc Y ");
Serial.println(lis_y);
Serial.print(" acc Z ");
Serial.println(lis_z);
*/
//<!-- example 50g 15/11/83 19:00 is 50 15 11 83 19 00 -->
if ( (lis_x > threshold) || (lis_y > threshold) || (lis_z > threshold) ){
dbg_print_P(PSTR("THR exceded:"));
int16_t max_impact = lis_x;
if (lis_y > max_impact)
max_impact = lis_y;
if (lis_z > max_impact)
max_impact = lis_z;
Serial.print(max_impact);
Serial.print("BT STATUS: ");
Serial.println(is_connection_established() );
if ( is_connection_established() == 1 ) {
dbg_print_P(PSTR("BT SEND ---->\n"));
uint8_t message[7];
//uint8_t hour, minute, second, year, month, day;
memcpy(message, (uint8_t *)&max_impact, 2);
message[2] = day;
message[3] = month;
message[4] = year;
message[5] = hour;
message[6] = minute;
if (move_enabled == TRUE){
if (write_att_request_by_handle_wo_offset(SHOCK_SERVICE_HANDLE, 7, message, WAIT_RESPONSE) != 0 )
dbg_print_P(PSTR("Error writing shock service\n"));
}else{
if (write_att_request_by_handle_wo_offset(SHOCK_SERVICE_HANDLE, 7, message, WAIT_RESPONSE) != 0 )
dbg_print_P(PSTR("Error writing shock service\n"));
}
}else{
if(push_enabled == FALSE ){
dbg_print_P(PSTR("gprs SEND ---->\n"));
if (move_enabled == TRUE)
sbi(data_2_send,MOVE_ALARM);
else
sbi(data_2_send,IMPACT_ALARM);
}else{
dbg_print_P(PSTR("EEPROM LOG -->\n"));
store_data_event_eeprom(SHOCK, max_impact);
}
}
}
}
void opening_detection()
{
lightSensorValue = analogRead(lightSensorPin);
//Serial.print("light value is: ");
//Serial.println(lightSensorValue);
//return;
if ((lightSensorValue > light_thr) && (millis() - light_debounce_start > LIGHT_DEBOUNCE_MS)) {
dbg_print_P(PSTR("Light THR exceded!\n"));
light_debounce_start = millis();
if ( is_connection_established() == 1 )
{
dbg_print_P(PSTR("BT SEND ---->\n"));
uint8_t message[6];
memcpy(message, "1", 1);
message[1] = day;
message[2] = month;
message[3] = year;
message[4] = hour;
message[5] = minute;
if (write_att_request_by_handle_wo_offset(LIGHT_SERVICE_HANDLE, 6, message, WAIT_RESPONSE) != 0 )
dbg_print_P(PSTR("Error writing accel z\n"));
}else{
if(push_enabled == FALSE ){
dbg_print_P(PSTR("gprs SEND ---->\n"));
sbi(data_2_send,OPEN_ALARM);
}else{
dbg_print_P(PSTR("EEPROM LOG -->\n"));
store_data_event_eeprom(OPEN, 0);
}
}
}
}
uint8_t red_led_status = LOW;
uint8_t green_led_status = LOW;
void loop()
{
LIS3DH_task();
//opening_detection();
ble_task();
SIM908_task();
//export_log();
if (millis() - ledDelay < 1000)
return;
ledDelay = millis();
if (BATT_level <= 70){
if (red_led_status == LOW){
digitalWrite(RED_LED, HIGH);
red_led_status = HIGH;
}else{
digitalWrite(RED_LED, LOW);
red_led_status = LOW;
}
}
if (green_led_status == LOW){
digitalWrite(GREEN_LED, HIGH);
green_led_status = HIGH;
}else{
digitalWrite(GREEN_LED, LOW);
green_led_status = LOW;
}
}