2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
#include <htc.h> |
#include <htc.h> |
4 |
#include <string.h> |
#include <string.h> |
5 |
|
#include <stdlib.h> |
6 |
|
|
7 |
#include "lcd.h" |
#include "lcd.h" |
8 |
#include "Delay.h" |
#include "Delay.h" |
18 |
|
|
19 |
|
|
20 |
unsigned char global_Pot_Hi, global_Pot_Lo; |
unsigned char global_Pot_Hi, global_Pot_Lo; |
|
unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; |
|
21 |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
22 |
bit global_recieve_done = 0, global_interval_changed = 0; |
bit global_recieve_done = 0, global_interval_changed = 0; |
23 |
unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0; |
unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0; |
24 |
unsigned int global_emergency_counter = 600, global_time_interval = 3600; |
unsigned int global_emergency_counter = 600, global_time_interval = 3600; |
25 |
unsigned char global_temp = 0; |
unsigned char global_temp = 0; |
26 |
|
|
27 |
unsigned char global_message_buffer[BUFFER]; |
unsigned char global_temp_update_display = 0; |
28 |
unsigned char global_message_buffer_length; |
|
29 |
|
unsigned char global_message_buffer1[BUFFER]; |
30 |
|
unsigned char global_message_buffer2[BUFFER]; |
31 |
|
unsigned char global_message_buffer_length1; |
32 |
|
unsigned char global_message_buffer_length2; |
33 |
|
|
34 |
unsigned char global_lcd_buf[16]; |
unsigned char global_lcd_buf[16]; |
35 |
|
|
36 |
unsigned short global_imei_tversum; |
unsigned short global_imei_tversum; |
37 |
|
|
38 |
unsigned char cell_nr[15] = ""; // = "21681784"; |
unsigned char global_cell_nr[15] = ""; // = "21681784"; |
39 |
bit global_modem_init = 0; |
bit global_modem_init = 0; |
40 |
|
bit global_has_imei = 0; |
41 |
|
|
42 |
unsigned char global_sms_recieve_number[3]; |
unsigned char global_sms_recieve_number[3]; |
43 |
|
|
52 |
void on_recieve(void); |
void on_recieve(void); |
53 |
void on_initial_recieve(void); |
void on_initial_recieve(void); |
54 |
void sms_recieved(void); |
void sms_recieved(void); |
55 |
|
void send_sms(const unsigned char* payload); |
56 |
|
|
57 |
|
|
58 |
void reset_recieve_buffer(void) |
void reset_recieve_buffer(void) |
59 |
{ |
{ |
60 |
global_recieve_done = 0; |
global_recieve_done = 0; |
61 |
|
|
62 |
global_serial_byte_counter=0; |
global_message_buffer_length1 = 0; |
63 |
global_serial_recieve_buffer[0] = 0; |
//global_message_buffer1[0] = 0; |
64 |
|
global_message_buffer_length2 = 0; |
65 |
|
//global_message_buffer2[0] = 0; |
66 |
|
|
67 |
global_message_buffer_length = 0; |
memset(global_message_buffer1, 0, BUFFER); |
68 |
global_message_buffer[0] = 0; |
memset(global_message_buffer2, 0, BUFFER); |
69 |
} |
} |
70 |
|
|
71 |
|
void pic18_io_init(void) |
72 |
|
{ |
73 |
|
TRISA0 = 1; // analog input |
74 |
|
TRISA5 = 0; // Output |
75 |
|
TRISB1 = 1; // TRISB1-4 Digital input |
76 |
|
TRISB2 = 1; |
77 |
|
TRISB3 = 1; |
78 |
|
TRISB4 = 1; |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
void ad_init(void) // Nicked from H7 |
void ad_init(void) // Nicked from H7 |
84 |
{ |
{ |
142 |
T1OSCEN = 1; //enable oscillator circuit |
T1OSCEN = 1; //enable oscillator circuit |
143 |
RD16 = 0; //normal 8 bit writes |
RD16 = 0; //normal 8 bit writes |
144 |
TMR1ON = 1; |
TMR1ON = 1; |
|
} |
|
|
|
|
|
void pic18_io_init(void) |
|
|
{ |
|
|
TRISA0 = 1; // analog input |
|
|
TRISA1 = 0; // Output |
|
|
TRISB1 = 1; // TRISB1-4 Digital input |
|
|
TRISB2 = 1; |
|
|
TRISB3 = 1; |
|
|
TRISB4 = 1; |
|
145 |
} |
} |
146 |
|
|
147 |
void sms_init(void) |
void sms_init(void) |
148 |
{ |
{ |
149 |
int i; |
int i; |
150 |
|
char buf[2]; |
151 |
|
|
152 |
|
|
153 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
154 |
sprintf(global_serial_send,"at+cgsn\r"); |
sprintf(global_serial_send,"at+cgsn\r"); |
155 |
serial_send(); |
serial_send(); |
156 |
DelaySek(1); |
DelaySek(1); |
157 |
while(!global_recieve_done) ; |
//while(!global_recieve_done) ; |
158 |
|
|
159 |
|
while (global_has_imei == 0) |
160 |
|
{ |
161 |
|
if (strstr(global_message_buffer1,"OK") != 0) |
162 |
|
{ |
163 |
|
global_imei_tversum = 0; |
164 |
|
for (i=0; i<15; ++i) |
165 |
|
{ |
166 |
|
buf[0] = global_message_buffer2[i]; |
167 |
|
buf[1] = 0; |
168 |
|
global_imei_tversum += atoi(buf); |
169 |
|
//global_imei_tversum += (global_message_buffer2[i] - '0'); |
170 |
|
} |
171 |
|
|
172 |
|
global_has_imei = 1; |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
sprintf(global_serial_send,"%s", "at+cpin=8043\r"); |
sprintf(global_serial_send,"%s", "at+cpin=8043\r"); |
181 |
{ |
{ |
182 |
on_initial_recieve(); |
on_initial_recieve(); |
183 |
} |
} |
184 |
|
DelayMs(5); |
185 |
|
|
186 |
|
reset_recieve_buffer(); |
187 |
|
sprintf(global_serial_send, "at+cmgd=1,4\r"); |
188 |
|
serial_send(); |
189 |
|
while ( strstr(global_message_buffer1,"OK") == 0) ; |
190 |
} |
} |
191 |
|
|
192 |
|
|
220 |
while(TRMT==0) ; |
while(TRMT==0) ; |
221 |
DelayMs(10); |
DelayMs(10); |
222 |
} |
} |
223 |
DelayMs(250); |
DelayMs(150); |
224 |
DelayMs(250); |
global_serial_send[0] = 0; |
225 |
|
DelayMs(150); |
226 |
} |
} |
227 |
|
|
228 |
void serial_recieved(void) |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
229 |
|
// Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from, |
230 |
|
// global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done. |
231 |
|
void serial_recieved(void) |
232 |
{ |
{ |
233 |
char data_byte, saved_data[LCD_LENGTH]; |
char data_byte; |
234 |
|
|
235 |
data_byte = RCREG; |
data_byte = RCREG; |
236 |
|
|
237 |
if (data_byte == '\n') |
if (data_byte == '\n') // Cant be bothered to do anyting if the byte is a '\n'. |
238 |
return; |
return; |
239 |
|
|
240 |
if (global_serial_byte_counter == 0 && data_byte == '\r') |
if (global_serial_byte_counter == 0 && data_byte == '\r') // don't care about '\r', if it's the first byte we recieve. |
241 |
return; |
return; |
242 |
|
|
243 |
global_serial_recieve_buffer[global_serial_byte_counter] = data_byte; |
|
244 |
|
if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun |
245 |
if (data_byte == '\r') |
global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer. |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of |
250 |
|
// global_message_buffer1 into global_message_buffer2 our main buffer into |
251 |
|
// global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer |
252 |
|
// into global_message_buffer1. |
253 |
{ |
{ |
254 |
global_recieve_done = 1; |
global_recieve_done = 1; // indicates the recieve transmission is done. |
|
//global_serial_byte_counter = 0; |
|
255 |
global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate |
global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate |
256 |
|
|
257 |
strcpy(global_message_buffer, global_serial_recieve_buffer); |
// global_message_buffer1 -> global_message_buffer2 |
258 |
global_message_buffer_length = global_serial_byte_counter; |
strcpy(global_message_buffer2, global_message_buffer1); |
259 |
|
global_message_buffer_length2 = global_message_buffer_length1; |
260 |
|
|
261 |
|
// global_serial_recieve_buffer -> global_message_buffer1 |
262 |
|
strcpy(global_message_buffer1, global_serial_recieve_buffer); |
263 |
|
global_message_buffer_length1 = global_serial_byte_counter; |
264 |
|
|
265 |
|
|
266 |
|
global_serial_byte_counter = 0; |
267 |
} |
} |
|
else |
|
|
{ |
|
|
global_serial_byte_counter++; |
|
|
} |
|
|
|
|
268 |
} |
} |
269 |
|
|
270 |
void timer1_interrupt(void) |
void timer1_interrupt(void) |
278 |
|
|
279 |
void update_lcd(void) |
void update_lcd(void) |
280 |
{ |
{ |
281 |
lcd_clear(); |
if(global_temp_update_display != global_time_counter) |
282 |
lcd_goto(0x00); |
{ |
283 |
|
lcd_clear(); |
284 |
|
lcd_goto(0x00); |
285 |
|
|
286 |
sprintf(global_lcd_buf, "%d", global_temp); |
sprintf(global_lcd_buf, "Temp: %3d", global_temp); |
287 |
lcd_puts(global_lcd_buf); |
lcd_puts(global_lcd_buf); |
288 |
|
global_temp_update_display = global_time_counter; |
289 |
|
} |
290 |
|
|
291 |
} |
} |
292 |
|
|
293 |
void send_update(void) |
void send_update(void) |
294 |
{ |
{ |
295 |
sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr); |
char update[40]; |
296 |
|
sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL); |
297 |
|
send_sms(update); |
298 |
|
|
299 |
|
global_sms_counter++; |
300 |
|
} |
301 |
|
|
302 |
|
void send_sms(const unsigned char* payload) |
303 |
|
{ |
304 |
|
sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr); |
305 |
serial_send(); |
serial_send(); |
306 |
sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A); |
sprintf(global_serial_send, "%s%c", payload, 0x1A); |
|
lcd_goto(40); |
|
|
lcd_puts(global_serial_send); |
|
307 |
serial_send(); |
serial_send(); |
308 |
DelayMs(250); |
DelayMs(150); |
309 |
global_sms_counter++; |
|
310 |
} |
} |
311 |
|
|
312 |
void convertTemp() |
void convertTemp() |
316 |
if( adVal >=840 ) |
if( adVal >=840 ) |
317 |
global_temp = 100; |
global_temp = 100; |
318 |
else |
else |
319 |
global_temp = (adVal / 8.3886); |
global_temp = (unsigned char) (adVal / 8.3886); |
320 |
} |
} |
321 |
|
|
322 |
|
|
325 |
{ |
{ |
326 |
char len,i; |
char len,i; |
327 |
|
|
328 |
len = strlen(cell_nr); |
len = strlen(global_cell_nr); |
329 |
eeprom_write(0, (global_time_interval/60)); |
eeprom_write(0, (global_time_interval/60)); |
330 |
eeprom_write(1, global_sms_counter>>8); |
eeprom_write(1, global_sms_counter>>8); |
331 |
eeprom_write(2, global_sms_counter); |
eeprom_write(2, global_sms_counter); |
333 |
|
|
334 |
for (i=0; i<len; ++i) |
for (i=0; i<len; ++i) |
335 |
{ |
{ |
336 |
eeprom_write(i+4, cell_nr[i] ); |
eeprom_write(i+4, global_cell_nr[i] ); |
337 |
} |
} |
338 |
} |
} |
339 |
|
|
348 |
|
|
349 |
for (i=0; i<len; ++i) |
for (i=0; i<len; ++i) |
350 |
{ |
{ |
351 |
cell_nr[i] = eeprom_read(i+4); |
global_cell_nr[i] = eeprom_read(i+4); |
352 |
} |
} |
353 |
|
|
354 |
cell_nr[i] = 0; //zero terminated! |
global_cell_nr[i] = 0; //zero terminated! |
355 |
} |
} |
356 |
|
|
357 |
void on_initial_recieve(void) |
void on_initial_recieve(void) |
359 |
char imei[16]; |
char imei[16]; |
360 |
char* ptr; |
char* ptr; |
361 |
char i; |
char i; |
362 |
|
char buf[2]; |
363 |
|
|
364 |
if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) |
if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) |
365 |
{ |
{ |
369 |
ptr +=4; |
ptr +=4; |
370 |
strncpy(imei, ptr,15); |
strncpy(imei, ptr,15); |
371 |
imei[15] = 0; |
imei[15] = 0; |
|
|
|
|
global_imei_tversum = 0; |
|
|
for (i=0; i<15; ++i) |
|
|
{ |
|
|
global_imei_tversum += (imei[i] - '0'); |
|
|
} |
|
|
|
|
372 |
|
|
373 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
374 |
} |
} |
375 |
} |
} |
376 |
|
|
|
|
|
377 |
void on_recieve(void) |
void on_recieve(void) |
378 |
{ |
{ |
379 |
char tmp[17]; |
char tmp[3]; |
380 |
char* ptr; |
char* ptr; |
381 |
tmp[0]=0; |
tmp[0]=0; |
382 |
|
|
383 |
if (global_recieve_done == 0 || global_message_buffer_length == 0) |
if (global_recieve_done == 0 || global_message_buffer_length1 == 0) |
384 |
return; |
return; |
385 |
|
|
386 |
|
if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS |
|
if (strstr(global_serial_recieve_buffer,"CMTI") != 0) |
|
387 |
{ |
{ |
388 |
ptr = strstr(global_serial_recieve_buffer,","); |
ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS. |
389 |
strcat(tmp,ptr+1); |
strcat(tmp,ptr+1); // puts that number in tmp |
390 |
sprintf(global_sms_recieve_number,"%s", tmp); |
global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0] |
391 |
lcd_clear(); |
strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable. |
|
lcd_puts(global_sms_recieve_number); |
|
392 |
sms_recieved(); |
sms_recieved(); |
393 |
} |
} |
|
else if (strstr(global_message_buffer,"CMTI") != 0) |
|
|
{ |
|
|
ptr = strstr(global_message_buffer,","); |
|
|
strcat(tmp,ptr+1); |
|
|
lcd_clear(); |
|
|
lcd_puts(tmp); |
|
|
} |
|
|
|
|
|
|
|
394 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
395 |
} |
} |
396 |
|
|
397 |
void sms_recieved(void) |
void sms_recieved(void) |
398 |
{ |
{ |
399 |
sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); |
char buf[4]; |
400 |
serial_send(); |
char i,imei; |
401 |
|
char pos; |
402 |
|
|
403 |
|
sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem. |
404 |
|
serial_send(); // Sends the command. |
405 |
|
|
406 |
|
while(strstr(global_message_buffer1, "OK") == 0) // stays here until we recieve a "OK" from the modem. |
407 |
|
DelayMs(1); |
408 |
|
|
409 |
|
DelayUs(10); |
410 |
|
|
411 |
|
for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i) |
412 |
|
{ |
413 |
|
buf[i] = global_message_buffer2[i]; |
414 |
|
} |
415 |
|
|
416 |
|
buf[i] = 0; |
417 |
|
imei = atoi(buf); |
418 |
|
|
419 |
|
if (imei == global_imei_tversum) |
420 |
|
{ |
421 |
|
i++; //spring over ':' |
422 |
|
pos = 0; |
423 |
|
for ( ; global_message_buffer2[i] != ':'; ++i, ++pos) |
424 |
|
{ |
425 |
|
global_cell_nr[pos] = global_message_buffer2[i]; |
426 |
|
} |
427 |
|
global_cell_nr[pos] = 0; //zero terminator |
428 |
|
|
429 |
|
i++; //spring over ':' |
430 |
|
pos=0; |
431 |
|
for ( ; global_message_buffer2[i] ; ++i,++pos) |
432 |
|
{ |
433 |
|
buf[pos] = global_message_buffer2[i]; |
434 |
|
} |
435 |
|
buf[pos]=0; |
436 |
|
|
437 |
|
global_time_interval = atoi(buf); |
438 |
|
global_time_interval *= 60; |
439 |
|
eeprom_writer(); // writes the new cell nr. and time interval to the eeprom. |
440 |
|
send_sms("conf ok"); |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
} |
} |
445 |
|
|
446 |
|
|
447 |
|
|
448 |
void main() |
void main() |
449 |
{ |
{ |
450 |
//////////////////// |
//////////////////// |
452 |
|
|
453 |
// Running init for various components. |
// Running init for various components. |
454 |
pic18_io_init(); |
pic18_io_init(); |
455 |
|
RA5 = 1; // Indicates that the board is running inits. |
456 |
|
|
457 |
rs232_init(); |
rs232_init(); |
458 |
ad_init(); |
ad_init(); |
459 |
lcd_init(0); |
lcd_init(0); |
468 |
DelayMs(50); |
DelayMs(50); |
469 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
470 |
|
|
471 |
|
RA5 = 0; // Inits are done, and RA1 will now work as a error notifier. |
472 |
|
|
473 |
while(1) |
while(1) |
474 |
{ |
{ |
475 |
// If there happends to be a critical state on the system, we send a sms. |
// If there happends to be a critical state on the system, we send a sms. |
500 |
global_Pot_Hi = ADRESH; |
global_Pot_Hi = ADRESH; |
501 |
global_Pot_Lo = ADRESL; |
global_Pot_Lo = ADRESL; |
502 |
convertTemp(); |
convertTemp(); |
503 |
//update_lcd(); |
update_lcd(); |
504 |
GODONE = 1; |
GODONE = 1; |
505 |
} |
} |
506 |
// Handels the recieve sms'es. |
// Handels the recieve sms'es. |