Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #define TOTAL 3 // Total number of analog sensors
  2.  
  3. #define SIREN_PORT PORTH // Siren Port
  4. #define SIREN_PIN 3 // Siren Pin Number
  5.  
  6. #define PHONE "+13472603586" // Operator Phone Number
  7. #define FOREMAN "+12052604026"
  8. #define OPEN_VALUE 266 // 1.9 V
  9. #define CLOSE_VALUE 978 // 4.8V
  10. #define AN_PIN 5
  11.  
  12. unsigned char IsOpened_Flag = 0;
  13. unsigned int sec_counter = 0;
  14. unsigned int valve_counter =0;
  15. unsigned int avg = 0;
  16.  
  17. unsigned char Was_Sent=255;
  18.  
  19.  
  20. unsigned char GLOBAL_FLAG=0;
  21.  
  22. unsigned int temp[TOTAL];
  23.  
  24. unsigned int GetTemp(unsigned char w);
  25. void Send_Valve_Report( char phone_number[]);
  26. void Check_Valves(void);
  27. void CheckForCommand(char phone[], char message[]);
  28. void SuperControl(void);
  29.  
  30.  
  31. void Send_Valve_Report( char phone_number[]) {
  32. char alrt_msg[140]="Valve Report: ";
  33. char part[50];
  34. char ret = sprintf(part,"Valve was opened %d times. Avg time of %d , ",valve_counter,avg);
  35. strcat(alrt_msg, part);
  36.  
  37. int ErrorCode = DroneCell_Text(phone_number,alrt_msg ); // text alert
  38. }
  39.  
  40. void Check_Valves(void) {
  41. if (IsOpened_Flag) {
  42. if ( a2dConvert10bit(AN_PIN) <= OPEN_VALUE ) {
  43. sec_counter = sec_counter + 4;
  44. }
  45. else if ( a2dConvert10bit(AN_PIN) >= CLOSE_VALUE ) {
  46. IsOpened_Flag = 0;
  47. if ( avg == 0) {
  48. avg = sec_counter;
  49. }
  50. else {
  51. avg = (sec_counter + avg) /2;
  52. }
  53. sec_counter =0;
  54. valve_counter++;
  55. }
  56. }
  57.  
  58. else if ( a2dConvert10bit(AN_PIN) <= OPEN_VALUE ) {
  59. IsOpened_Flag = 1;
  60. }
  61.  
  62. signed char hours_now = Hours_Now();
  63. if (Was_Sent != hours_now) {
  64. Send_Valve_Report(FOREMAN);
  65. Was_Sent = hours_now;
  66. }
  67. }
  68. void CheckForTrigger(void) {
  69. unsigned char flag=0;
  70. char alrt_msg[140]="Alert: ";
  71. int ErrorCode = 0;
  72.  
  73. rprintfInit(DebugPrint); rprintf("Reading Values: \n");
  74. for (unsigned char w=0;w<TOTAL;w++) {
  75. // Set Criteria for Alert
  76. temp[w] = GetTemp(w);
  77. rprintf("Door #%d = %d ,",w,temp[w] );
  78. if (temp[w] == 0) { // Check Condition
  79. char part[50];
  80. char ret = sprintf(part,"Door %d is %d , ",w,temp[w]);
  81. strcat(alrt_msg, part);
  82. flag = 1;
  83. }
  84. }
  85. if (flag==1 && GLOBAL_FLAG==0) {
  86. rprintfInit(DebugPrint); rprintf("Triggered! Sending Alert Message\n");
  87. rprintfStr(alrt_msg); rprintf("\n");
  88. ErrorCode = DroneCell_Text(PHONE,alrt_msg ); // text alert
  89. GLOBAL_FLAG = 1;
  90. }
  91. }
  92.  
  93.  
  94. void CheckForCommand(char phone[], char message[]) {
  95. int ErrorCode=0;
  96.  
  97. char status_msg[140]="Status: ";
  98.  
  99. //********** Status Report **********//
  100. if (message[0] == 'S' || message[0] == 's') {
  101. rprintfInit(DebugPrint); rprintf(" -> Status\n");
  102.  
  103. //******* Read all Analog Inputs *******//
  104. for (unsigned char w=0;w<TOTAL;w++) {
  105. temp[w] = GetTemp(w);
  106. char part[100];
  107. char ret = sprintf(part,"Door %d is %d deg, ",w,temp[w]);
  108. strcat(status_msg, part);
  109. delay_ms(5);
  110. }
  111. rprintfInit(DebugPrint); rprintf("Status Msg is ");rprintfStr(status_msg);rprintf("\n");
  112. ErrorCode = DroneCell_Text(phone,status_msg);
  113. }
  114.  
  115. //********** Activate Siren **********//
  116. else if (message[0] == 'A' || message[0] == 'a') {
  117. rprintfInit(DebugPrint); rprintf(" -> Activate\n");
  118. PORT_ON(SIREN_PORT,SIREN_PIN);
  119. ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Siren Activated."); // Delete SMS Message #1 that was just received
  120. }
  121.  
  122. //********** Quiet Siren **********//
  123. else if (message[0] == 'Q' || message[0] == 'q') {
  124. rprintfInit(DebugPrint); rprintf(" -> Quiet\n");
  125. PORT_OFF(SIREN_PORT,SIREN_PIN);
  126. ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Siren Quieted."); // Delete SMS Message #1 that was just received
  127. }
  128.  
  129. //********** Reset **********//
  130. else if (message[0] == 'R' || message[0] == 'r') {
  131. rprintfInit(DebugPrint); rprintf(" -> Reset\n");
  132. valve_counter = 0;
  133. avg = 0;
  134. PORT_OFF(SIREN_PORT,SIREN_PIN);
  135. // GLOBAL_FLAG= 0; // disabled trigger here
  136. ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Resetting System."); // Delete SMS Message #1 that was just received
  137. }
  138.  
  139. //********** Valve **********//
  140. else if (message[0] == 'V' || message[0] == 'v') {
  141. rprintfInit(DebugPrint); rprintf(" -> Valve Report\n");
  142. Send_Valve_Report(phone);
  143. }
  144.  
  145. ErrorCode = DroneCell_DeleteMsg(1);
  146. rprintfInit(DebugPrint); rprintf("\nDeleted SMS Message #1\n");
  147. }
  148.  
  149.  
  150. unsigned int GetTemp(unsigned char w) {
  151. return a2dConvert10bit(w);
  152. }
  153.  
  154. void SuperControl(void) {
  155. int ErrorCode=0;
  156. //********** Startup Message **********//
  157. ErrorCode = DroneCell_Text(PHONE,"System Initated. Commands: Status, Alarm, Quiet, Reset");
  158. rprintfInit(DebugPrint); rprintf(" Sent Text Message Error Code %d \n",ErrorCode);
  159. delay_ms(2000);
  160.  
  161. while(1) {
  162. rprintfInit(DebugPrint); rprintf("\n");
  163. /* -------------- Read Message Index # 1 ------------ */
  164. char message[30];
  165. char phone_num[13];
  166. ErrorCode = DroneCell_MsgRead(1,phone_num,message);
  167. // No SMS Message in Index #1
  168. if (ErrorCode == NO_MESSAGE) {
  169. rprintfInit(DebugPrint); rprintf("1 %d - No SMS Message\n", ErrorCode);
  170. }
  171. else {
  172. // Print the SMS Message that I received
  173. rprintfInit(DebugPrint);
  174. rprintf("Message is : ");
  175. for (unsigned char i=0;i<ErrorCode;i++) {
  176. DebugPrint(message[i]);
  177. }
  178. // Print the Phone Number the Message is From
  179. rprintf("\nPhone # is : ");
  180. char myphone[12];
  181. for (unsigned char i=0;i<12;i++) {
  182. myphone[i] = phone_num[i];
  183. DebugPrint(phone_num[i]);
  184. }
  185. CheckForCommand(myphone,message);
  186. }
  187. Check_Valves();
  188. CheckForTrigger();
  189. delay_ms(3500);
  190. }
  191.  
  192. }
Add Comment
Please, Sign In to add comment