Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TOTAL 3 // Total number of analog sensors
- #define SIREN_PORT PORTH // Siren Port
- #define SIREN_PIN 3 // Siren Pin Number
- #define PHONE "+13472603586" // Operator Phone Number
- #define FOREMAN "+12052604026"
- #define OPEN_VALUE 266 // 1.9 V
- #define CLOSE_VALUE 978 // 4.8V
- #define AN_PIN 5
- unsigned char IsOpened_Flag = 0;
- unsigned int sec_counter = 0;
- unsigned int valve_counter =0;
- unsigned int avg = 0;
- unsigned char Was_Sent=255;
- unsigned char GLOBAL_FLAG=0;
- unsigned int temp[TOTAL];
- unsigned int GetTemp(unsigned char w);
- void Send_Valve_Report( char phone_number[]);
- void Check_Valves(void);
- void CheckForCommand(char phone[], char message[]);
- void SuperControl(void);
- void Send_Valve_Report( char phone_number[]) {
- char alrt_msg[140]="Valve Report: ";
- char part[50];
- char ret = sprintf(part,"Valve was opened %d times. Avg time of %d , ",valve_counter,avg);
- strcat(alrt_msg, part);
- int ErrorCode = DroneCell_Text(phone_number,alrt_msg ); // text alert
- }
- void Check_Valves(void) {
- if (IsOpened_Flag) {
- if ( a2dConvert10bit(AN_PIN) <= OPEN_VALUE ) {
- sec_counter = sec_counter + 4;
- }
- else if ( a2dConvert10bit(AN_PIN) >= CLOSE_VALUE ) {
- IsOpened_Flag = 0;
- if ( avg == 0) {
- avg = sec_counter;
- }
- else {
- avg = (sec_counter + avg) /2;
- }
- sec_counter =0;
- valve_counter++;
- }
- }
- else if ( a2dConvert10bit(AN_PIN) <= OPEN_VALUE ) {
- IsOpened_Flag = 1;
- }
- signed char hours_now = Hours_Now();
- if (Was_Sent != hours_now) {
- Send_Valve_Report(FOREMAN);
- valve_counter = 0;
- avg = 0;
- Was_Sent = hours_now;
- }
- }
- void CheckForTrigger(void) {
- unsigned char flag=0;
- char alrt_msg[140]="Alert: ";
- int ErrorCode = 0;
- rprintfInit(DebugPrint); rprintf("Reading Values: \n");
- for (unsigned char w=0;w<TOTAL;w++) {
- // Set Criteria for Alert
- temp[w] = GetTemp(w);
- rprintf("Door #%d = %d ,",w,temp[w] );
- if (temp[w] == 0) { // Check Condition
- char part[50];
- char ret = sprintf(part,"Door %d is %d , ",w,temp[w]);
- strcat(alrt_msg, part);
- flag = 1;
- }
- }
- if (flag==1 && GLOBAL_FLAG==0) {
- rprintfInit(DebugPrint); rprintf("Triggered! Sending Alert Message\n");
- rprintfStr(alrt_msg); rprintf("\n");
- ErrorCode = DroneCell_Text(PHONE,alrt_msg ); // text alert
- GLOBAL_FLAG = 1;
- }
- }
- void CheckForCommand(char phone[], char message[]) {
- int ErrorCode=0;
- char status_msg[140]="Status: ";
- //********** Status Report **********//
- if (message[0] == 'S' || message[0] == 's') {
- rprintfInit(DebugPrint); rprintf(" -> Status\n");
- //******* Read all Analog Inputs *******//
- for (unsigned char w=0;w<TOTAL;w++) {
- temp[w] = GetTemp(w);
- char part[100];
- char ret = sprintf(part,"Door %d is %d deg, ",w,temp[w]);
- strcat(status_msg, part);
- delay_ms(5);
- }
- rprintfInit(DebugPrint); rprintf("Status Msg is ");rprintfStr(status_msg);rprintf("\n");
- ErrorCode = DroneCell_Text(phone,status_msg);
- }
- //********** Activate Siren **********//
- else if (message[0] == 'A' || message[0] == 'a') {
- rprintfInit(DebugPrint); rprintf(" -> Activate\n");
- PORT_ON(SIREN_PORT,SIREN_PIN);
- ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Siren Activated."); // Delete SMS Message #1 that was just received
- }
- //********** Quiet Siren **********//
- else if (message[0] == 'Q' || message[0] == 'q') {
- rprintfInit(DebugPrint); rprintf(" -> Quiet\n");
- PORT_OFF(SIREN_PORT,SIREN_PIN);
- ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Siren Quieted."); // Delete SMS Message #1 that was just received
- }
- //********** Reset **********//
- else if (message[0] == 'R' || message[0] == 'r') {
- rprintfInit(DebugPrint); rprintf(" -> Reset\n");
- PORT_OFF(SIREN_PORT,SIREN_PIN);
- GLOBAL_FLAG= 0;
- ErrorCode = DroneCell_Text(phone,"Command Acknowledged, Resetting System."); // Delete SMS Message #1 that was just received
- }
- //********** Reset **********//
- else if (message[0] == 'V' || message[0] == 'v') {
- rprintfInit(DebugPrint); rprintf(" -> Valve Report\n");
- Send_Valve_Report(phone);
- }
- ErrorCode = DroneCell_DeleteMsg(1);
- rprintfInit(DebugPrint); rprintf("\nDeleted SMS Message #1\n");
- }
- unsigned int GetTemp(unsigned char w) {
- return a2dConvert10bit(w);
- }
- void SuperControl(void) {
- int ErrorCode=0;
- //********** Startup Message **********//
- ErrorCode = DroneCell_Text(PHONE,"System Initated. Commands: Status, Alarm, Quiet, Reset");
- rprintfInit(DebugPrint); rprintf(" Sent Text Message Error Code %d \n",ErrorCode);
- delay_ms(2000);
- while(1) {
- rprintfInit(DebugPrint); rprintf("\n");
- /* -------------- Read Message Index # 1 ------------ */
- char message[30];
- char phone_num[13];
- ErrorCode = DroneCell_MsgRead(1,phone_num,message);
- // No SMS Message in Index #1
- if (ErrorCode == NO_MESSAGE) {
- rprintfInit(DebugPrint); rprintf("1 %d - No SMS Message\n", ErrorCode);
- }
- else {
- // Print the SMS Message that I received
- rprintfInit(DebugPrint);
- rprintf("Message is : ");
- for (unsigned char i=0;i<ErrorCode;i++) {
- DebugPrint(message[i]);
- }
- // Print the Phone Number the Message is From
- rprintf("\nPhone # is : ");
- char myphone[12];
- for (unsigned char i=0;i<12;i++) {
- myphone[i] = phone_num[i];
- DebugPrint(phone_num[i]);
- }
- CheckForCommand(myphone,message);
- }
- Check_Valves();
- CheckForTrigger();
- delay_ms(3500);
- }
- }
Add Comment
Please, Sign In to add comment