uses pushing box to publish to google spreadsheets with a state machine instead of a while loop

Dependents:   DCS_FINAL_CODE

Fork of GSM_PUSHING_BOX_STATE_MACHINE by DCS_TEAM

Revision:
12:f3ccc43c4d3c
Parent:
7:6c0b6ab3cafe
Child:
13:9ac5ff131214
diff -r 000ee5c0087c -r f3ccc43c4d3c GSMLibrary.cpp
--- a/GSMLibrary.cpp	Thu Mar 05 23:24:18 2015 +0000
+++ b/GSMLibrary.cpp	Fri Mar 06 00:30:41 2015 +0000
@@ -3,7 +3,7 @@
 #include <string.h>
 
 #define TIME_CONST 1
-#define SECONDS_TIMEOUT 10
+#define SECONDS_TIMEOUT 100
 #define TIMEOUTLIMIT TIME_CONST //$change check with main code this will set up condition fior timeout.
 
 //definition for AT comands
@@ -13,12 +13,13 @@
 #define AT_CMGF "AT+CMGF=1"
 #define RECEIVER_PHONE_NUMBER "\"+18014722842\""
 #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER 
+#define MESSAGE_BODY "hi joseph"
 
 //Definition for at repsonses
 //Please notice that after ":" the gsm will usually send aditional information
 #define AT_OK_RESPONSE "OK" //Response after sending "AT" message
 #define AT_CSQ_RESPONSE "+CSQ:" //+CSQ: <arg1>,<arg2> where <arg1> is signal strength arg1 = 0-30 where a number below 10 means low signal strength and 99 is not knwn or detectable signal and arg2 is bit error rate form 0-7, 99 will represent error 
-#define AT_CREG_RESPONSE "+CREG:"//+CREG: <arg1>,<arg2> where <arg1> = 0-2(see AT command descriptions), <arg2> =  0-5, 0 not registered to nework and not looking for one. 1 is conected to network, 2 is not conected but searching
+#define AT_CREG_RESPONSE "+CREG: 0"//+CREG: <arg1>,<arg2> where <arg1> = 0-2(see AT command descriptions), <arg2> =  0-5, 0 not registered to nework and not looking for one. 1 is conected to network, 2 is not conected but searching
 #define AT_CMGF_RESPONSE "OK"
 #define AT_CMGS_RESPONSE ">" //Message is written aftersymbol
 #define AT_SENDSMS_RESPONSE "+CMGS:" // +CMGS: <id> this will include the message id. CMGS ERROR for error and 
@@ -32,73 +33,158 @@
 
 char correct = 0;
 char send = 0;
-char timeout_count = 0;
+int timeout_count = 0;
 char received = 0;
 char timeout_limit = TIMEOUTLIMIT;
 
 void gsm_tick(){
     
     //post action
+    //if(++timeout_count >= timeout_limit){
+     //   timeout_count=0; 
+     //   gsm_current_state = GSM_INITIALIZE;
+    //}
     switch(gsm_current_state){
         //when send flag is on , send AT_OK message to gsm.
         case GSM_INITIALIZE:
             pc.printf("gsm_initilize state\r\n");//&debug
-            correct = 0;
             timeout_count = 0;
-            received = 0;
             if(send){ //send first at_ok message
                  resetGSMIdleBit();
                  pc.printf("sending AT_OK\r\n");//&debug
                  gsm.puts(AT_OK);
-                 gsm.puts("\r\n"); 
+                 gsm.puts("\r\n");
                  gsm_current_state = GSM_AT_OK;
             }
             else
                 gsm_current_state = GSM_INITIALIZE;
             break;
+         
          // check for repsonse to AT and if correct send AT+CSQ message   
         case GSM_AT_OK:
-        timeout_count++;
-        pc.printf("inside AT_OK state\r\n");//&debug
+            pc.printf("inside AT_OK state\r\n");//&debug
             if(getGSMIdleBit()){
                 printQueue(); //$debug 
-                if(findInQueue(AT_OK)){
-                    timeout_count = 0;
+                if(findInQueue(AT_OK_RESPONSE)){
                     resetGSMIdleBit();
                     pc.printf("sending AT_CSQ\r\n");//&debug
                     gsm.puts(AT_CSQ);
                     gsm.puts("\r\n"); 
                     gsm_current_state = GSM_AT_CSQ;
                 }
-                else
-                    gsm_current_state = GSM_INITIALIZE;
+                else{
+                    gsm.puts(AT_OK);
+                    gsm.puts("\r\n");
+                }
             }
-            if(timeout_count >= timeout_limit)
-                gsm_current_state = GSM_INITIALIZE;
+            break;
+        
+        //CHECK FOR RESPOSE TO at+csq AND SEND at+creg    
+        case GSM_AT_CSQ:
+            pc.printf("inside AT_CSQ state \r\n");//&debug
+            if(getGSMIdleBit()){
+                printQueue(); //$debug 
+                if(findInQueue(AT_CSQ_RESPONSE)){
+                    if(parseInt() > 9){
+                        resetGSMIdleBit();
+                        pc.printf("sending AT_CREG\r\n");//&debug
+                        gsm.puts(AT_CREG);
+                        gsm.puts("\r\n"); 
+                        gsm_current_state = GSM_AT_CREG;
+                    }
+                }
+                else{
+                    gsm.puts(AT_CSQ);
+                    gsm.puts("\r\n");
+                }
+            }
             break;
             
-        case GSM_AT_CSQ:
-            pc.printf("gsm_csq state\r\n");//&debug
-            gsm_current_state = GSM_AT_CREG;
-            break;
+         //check for AT creg and if correct send AT+CMGF   
         case GSM_AT_CREG:
             pc.printf("gsm_creg state\r\n");//&debug
-            gsm_current_state = GSM_AT_CMGF;
+            if(getGSMIdleBit()){
+                printQueue(); //$debug 
+                if(findInQueue(AT_CREG_RESPONSE)){
+                    if(parseInt() == 1){
+                        resetGSMIdleBit();
+                        pc.printf("sending AT_CMGF\r\n");//&debug
+                        gsm.puts(AT_CMGF);
+                        gsm.puts("\r\n"); 
+                        gsm_current_state = GSM_AT_CMGF;
+                    }
+                }
+                 if(gsm_current_state == GSM_AT_CREG){
+                    gsm.puts(AT_CREG);
+                    gsm.puts("\r\n"); 
+                 }
+            } 
             break;
+            
+        //check for cmgf esponse and if correct go to at_cmgs    
         case GSM_AT_CMGF:
-        pc.printf("gsm_cmgf state\r\n");//&debug
-            gsm_current_state = GSM_AT_CMGS;
+            pc.printf("gsm_cmgf state\r\n");//&debug
+            if(getGSMIdleBit()){
+                printQueue(); //$debug 
+                if(findInQueue(AT_CMGF_RESPONSE)){
+                    resetGSMIdleBit();
+                    pc.printf("sending AT_CMGS\r\n");//&debug
+                    gsm.puts(AT_CMGS);
+                    gsm.puts("\r\n"); 
+                    gsm_current_state = GSM_AT_CMGS;
+                }
+                else{
+                     gsm.puts(AT_CMGF);
+                    gsm.puts("\r\n"); 
+                }
+            }
             break;
+            
+        //check cmgs response if correct send SMS    
         case GSM_AT_CMGS:
         pc.printf("gsm_cmgs state\r\n");//&debug
-            gsm_current_state = GSM_AT_SENDSMS;
+        if(getGSMIdleBit()){
+                printQueue(); //$debug 
+                if(findInQueue(AT_CMGS_RESPONSE)){
+                    resetGSMIdleBit();
+                    pc.printf("sending MESSAGE_BODY\r\n");//&debug
+                    gsm.puts(MESSAGE_BODY); //substitute
+                    gsm.putc((char)26);
+                    gsm.puts("\r\n");
+                    gsm_current_state = GSM_AT_SENDSMS;
+                }
+                else
+                {
+                    gsm.puts(AT_CMGS);
+                    gsm.puts("\r\n"); 
+                }
+            }
             break;
+         
+        //check if message was sent correctly and if so    
         case GSM_AT_SENDSMS:
-        pc.printf("gsm_send_sms state\r\n");//&debug
-            gsm_current_state = GSM_SUCCESS;
+            pc.printf("gsm_send_sms state\r\n");//&debug
+            if(getGSMIdleBit()){
+                printQueue(); //$debug 
+                if(findInQueue(AT_SENDSMS_RESPONSE)){
+                    resetGSMIdleBit();
+                    pc.printf("sending message ID: %iY\r\n",parseInt());//&debug
+                    gsm_current_state = GSM_SUCCESS;
+                }
+                else
+                {
+                    gsm.puts(AT_CMGF);
+                    gsm.puts("\r\n"); 
+                    gsm_current_state = GSM_AT_CMGF;
+                }
+            }
             break;
+            
+            
         case GSM_SUCCESS:
             pc.printf("gsm_success state\r\n");//&debug
+            if(findInQueue(AT_SENDSMS_RESPONSE))
+                pc.printf("Message SENT!\r\n");//&debug
             gsm_current_state = GSM_INITIALIZE;
             break;
         default:
@@ -117,7 +203,7 @@
 //
 void gsm_initialize(){    
       SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
-      //SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  // enebaling dma clock
+      SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  // enebaling dma clock
       pc.printf("initializing tregisters...!\r\n");
      // control register mux, enabling uart3 receive        
      DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8);