/
main.c
384 lines (347 loc) · 8.77 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "stm32f10x.h"
#include <string.h>
#define EVER (;;)
#include "delay.h"
#include "ds1307.h"
#include "flash.h"
#include "sim_hal.h"
#if DEBUG
#include "usart.h"
#endif
#include "one_wire.h"
#include "sensors.h"
#include "enrf24.h"
#include "thesis.h"
#include "rs485.h"
#include "output.h"
#include "sim900.h"
uint8_t enrf24_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x0A };
#define BUFFER_SIZE 128
uint8_t led_state = 0;
void led_toggle(void)
{
if (led_state)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET); // OFF
led_state = 0;
}
else
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET);
led_state = 1;
}
}
#define BUFFER_SIZE 128
#define GAS_LIMIT 1500
#define LIGHT_LIMIT 100
#define TEMPC_LIMIT 40
int main()
{
RCC_ClocksTypeDef RCC_Clocks;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
int rf_len = 0;
int rs485_len = 0;
#if DEBUG
// int usart_len = 0;
// char buff_usart[BUFFER_SIZE];
#endif
char buff_rf[BUFFER_SIZE];
char buff_rs485[BUFFER_SIZE];
unsigned int sensors_time_poll = 0;
// int temp_time_poll = 0;
// int sms_test_time = 0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET); // off
Delay_Init();
Enrf24_init(CE_PIN, CSN_PIN, IRQ_PIN);
Enrf24_begin(1000000, 0); // Defaults 1Mbps, channel 0, max TX power
Enrf24_setTXaddress((void*)enrf24_addr);
Enrf24_setRXaddress((void*)enrf24_addr);
Enrf24_enableRX(); // Start listening
#if DEBUG
USART1_Init(115200);
#endif
DS1307_Init();
Sensors_Init();
RS485_Init(115200);
sim_hal_init(115200);
OutputInit();
if (ThesisInit() == THESIS_FLASH_ERROR)
{
#if DEBUG
USART1_SendStr("\nFlash write error.\n");
#endif
TurnBuzzerOn();
Delay(1000);
}
#if DEBUG
RCC_GetClocksFreq(&RCC_Clocks);
USART1_SendStr("System Clock: ");
USART1_SendNum(RCC_Clocks.SYSCLK_Frequency);
USART1_SendStr("\r\n");
USART1_SendStr("Device ID: ");
USART1_SendByte(__flash_data.id, HEX);
USART1_SendStr("\r\n");
USART1_SendStr("Device Unique Number: ");
USART1_SendByte(__flash_data.unique_number[0], HEX);
USART1_SendByte(__flash_data.unique_number[1], HEX);
USART1_SendByte(__flash_data.unique_number[2], HEX);
USART1_SendByte(__flash_data.unique_number[3], HEX);
USART1_SendStr("\r\n");
#endif
OneWire_Init();
for EVER
{
if (millis() - sensors_time_poll > 100)
{
led_toggle();
Sensors_Poll();
sensors_time_poll = millis();
// buzzer_toggle();
// output_toggle();
if (millis() > 10000)
{
if (sensors.Gas >= GAS_LIMIT)
{
#if DEBUG
USART1_SendStr("Gas detected.\r\n");
USART1_SendStr("Current Gas: ");
USART1_SendFloat(sensors.Gas);
USART1_SendStr("Limited Gas: ");
USART1_SendFloat(GAS_LIMIT);
USART1_SendStr("\r\n");
#endif
TurnBuzzerOn();
TurnSpeakerOn();
TurnRelayOn();
}
else if (sensors.Lighting >= LIGHT_LIMIT)
{
#if DEBUG
USART1_SendStr("Light detected.\r\n");
USART1_SendStr("Current Light: ");
USART1_SendFloat(sensors.Lighting);
USART1_SendStr("Limited Light: ");
USART1_SendFloat(LIGHT_LIMIT);
USART1_SendStr("\r\n");
#endif
TurnBuzzerOn();
TurnSpeakerOn();
TurnRelayOn();
}
else if (sensors.TempC >= TEMPC_LIMIT)
{
#if DEBUG
USART1_SendStr("Tempc detected.\r\n");
USART1_SendStr("Current Tempc: ");
USART1_SendFloat(sensors.TempC);
USART1_SendStr("Limited Tempc: ");
USART1_SendFloat(TEMPC_LIMIT);
USART1_SendStr("\r\n");
#endif
TurnBuzzerOn();
TurnSpeakerOn();
TurnRelayOn();
}
else
{
TurnBuzzerOff();
TurnSpeakerOff();
TurnRelayOff();
}
}
// Sim900_Process();
}
// if (millis() - sms_test_time > 10000)
// {
// Sim900_SendSMS("Hi kieu", "01677880531");
// sms_test_time = millis();
// }
// usart_len = USART1_Available();
//
// // usart: for test
// if (usart_len > 4)
// {
// int i;
// USART1_SendStr("\nUSART1 received packet: \n");
// USART1_GetData(buff_usart, usart_len);
// for (i = 0; i < usart_len; i++)
// USART1_SendByte(buff_usart[i], HEX);
// USART1_SendChar('\n');
// if (ThesisProcess(buff_usart, usart_len) == THESIS_OK)
// {
// memset(buff_usart, 0, usart_len);
// USART1_Flush();
// if (thesis_need_to_send)
// {
// int i;
// USART1_SendStr("\nNeed to send packet: ");
// for (i = 0; i < thesis_msg_len; i++)
// {
// USART1_SendByte(thesis_sent_msg[i], HEX);
// }
// USART1_SendStr("\nNeed to send packet length: ");
// USART1_SendNum(thesis_msg_len);
// USART1_SendStr("\n");
// thesis_msg_len = 0;
// thesis_need_to_send = 0;
// }
// USART1_SendStr("\nPacket processed.\n");
// }
// else if (thesis_errn == THESIS_FLASH_ERROR)
// {
// USART1_SendStr("\n");
// USART1_SendStr(thesis_err_msg);
// USART1_SendStr("\n");
// led_toggle();
// for(;;);
// }
// else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH)
// {
// memset(buff_usart, 0, usart_len);
// USART1_Flush();
// USART1_SendStr("Packet processing fail.\n");
// }
//
// USART1_SendStr("\n");
// USART1_SendStr(thesis_err_msg);
// USART1_SendStr("\n");
// }
// rf
if (Enrf24_available(1))
{
int i;
rf_len = Enrf24_read(buff_rf + rf_len, BUFFER_SIZE - 1 - rf_len);
#if DEBUG
USART1_SendStr("\nRF received packet.\n");
for (i = 0; i < rf_len; i++)
USART1_SendByte(buff_rf[i], HEX);
USART1_SendChar('\n');
#endif
if (ThesisProcess(buff_rf, rf_len) == THESIS_OK)
{
memset(buff_rf, 0, rf_len);
rf_len = 0;
if (thesis_need_to_send)
{
int i;
#if DEBUG
USART1_SendStr("\nNeed to send packet: ");
for (i = 0; i < thesis_msg_len; i++)
{
// Enrf24_write(thesis_sent_msg[i]);
USART1_SendByte(thesis_sent_msg[i], HEX);
}
#endif
Enrf24_write_buff(thesis_sent_msg, thesis_msg_len);
Enrf24_flush();
#if DEBUG
USART1_SendStr("\nNeed to send packet length: ");
USART1_SendNum(thesis_msg_len);
USART1_SendStr("\n");
#endif
thesis_msg_len = 0;
thesis_need_to_send = 0;
}
#if DEBUG
USART1_SendStr("\nPacket processed.\n");
#endif
}
else if (thesis_errn == THESIS_FLASH_ERROR)
{
#if DEBUG
USART1_SendStr("\n");
USART1_SendStr(thesis_err_msg);
USART1_SendStr("\n");
#endif
led_toggle();
for(;;);
}
else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH)
{
memset(buff_rf, 0, rf_len);
// RF_Flush();
rf_len = 0;
#if DEBUG
USART1_SendStr("Packet processing fail.\n");
#endif
}
#if DEBUG
USART1_SendStr("\n");
USART1_SendStr(thesis_err_msg);
USART1_SendStr("\n");
#endif
}
// rs485
rs485_len = RS485_Available();
if (rs485_len > 4)
{
int i;
RS485_GetData(buff_rs485, rs485_len);
#if DEBUG
USART1_SendStr("\nUSART1 received packet: \n");
for (i = 0; i < rs485_len; i++)
USART1_SendByte(buff_rs485[i], HEX);
USART1_SendChar('\n');
#endif
if (ThesisProcess(buff_rs485, rs485_len) == THESIS_OK)
{
memset(buff_rs485, 0, rs485_len);
RS485_Flush();
if (thesis_need_to_send)
{
int i;
#if DEBUG
USART1_SendStr("\nNeed to send packet: ");
#endif
RS485_DIR_Output();
for (i = 0; i < thesis_msg_len; i++)
{
RS485_SendChar(thesis_sent_msg[i]);
#if DEBUG
USART1_SendByte(thesis_sent_msg[i], HEX);
#endif
}
RS485_DIR_Input();
#if DEBUG
USART1_SendStr("\nNeed to send packet length: ");
USART1_SendNum(thesis_msg_len);
USART1_SendStr("\n");
#endif
thesis_msg_len = 0;
thesis_need_to_send = 0;
}
#if DEBUG
USART1_SendStr("\nPacket processed.\n");
#endif
}
else if (thesis_errn == THESIS_FLASH_ERROR)
{
#if DEBUG
USART1_SendStr("\n");
USART1_SendStr(thesis_err_msg);
USART1_SendStr("\n");
#endif
led_toggle();
for(;;);
}
else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH)
{
memset(buff_rs485, 0, rs485_len);
RS485_Flush();
#if DEBUG
USART1_SendStr("Packet processing fail.\n");
#endif
}
#if DEBUG
USART1_SendStr("\n");
USART1_SendStr(thesis_err_msg);
USART1_SendStr("\n");
#endif
}
}
}