Guest User

Untitled

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