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:
33:2ae9a4eb6433
Parent:
32:424896b5adbe
Child:
34:5345174bfb30
--- a/GSMLibrary.cpp	Wed Oct 21 19:44:15 2015 +0000
+++ b/GSMLibrary.cpp	Fri Nov 06 20:49:23 2015 +0000
@@ -5,13 +5,14 @@
 #include "GPRSInterface.h"
 #include "gsmqueue.h"
 //Global defines
-#define TIMEOUTLIMIT SECONDS_TIMEOUT/TIME_CONST  //Defines how many "ticks" of the GSM will constitute "timeout" of our "watchdog timer"
-#define NUM_SIZE 250
+#define TIMEOUTLIMIT (SECONDS_TIMEOUT / 1) //Defines how many "ticks" of the GSM will constitute "timeout" of our "watchdog timer"
+#define NUM_SIZE 500
 
 //External variables
 extern Serial pc;   //To print output to computer
 //extern Serial gsm;  //To communicate with GSM
 extern uint8_t buffer[BUFFER_LENGTH];   //DMA queue
+GPRSInterface eth(D1,D0, 19200, "ndo","","");
 /**************************************************
  **          GPRS                                 **
  **************************************************/
@@ -23,7 +24,7 @@
  * "username" - APN username
  * "password" - APN passowrd
  */
-GPRSInterface eth(D1,D0, 19200, "ndo","","");
+
 
 //Internal variables
 gsm_states gsm_current_state = GSM_INITIALIZE;
@@ -34,17 +35,17 @@
 
 //Extras for transmitter
 char gsm_msg[250]; //String storing SMS message that will be sent (add 250 length to give leeway)
-char gsm_header[250]; //for the http header information
+char gsm_header[500]; //for the http header information
 char num[NUM_SIZE];   //Temporary string storage to help with concatenation of strings
 char send = false;  //if true => we will send something (only if send_enable is true)
 char send_enable = false;   //Sending start and stop commands to GSM via SMS changes this variable. If true, we will send SMS messages of our data received
-
-
+int contentLength;
+int waitTicks = 0;
 //"Tick" of the GSM (i.e. this is a state machine)
 void gsm_tick()
 {
     //Don't do anything, unless i) we received a response from the GSM, ii) the watchdog timer timed out, or iii) or we are initializing the GSM
-    if (queueHasResponse() || gsm_timeOut() || gsm_current_state == GSM_INITIALIZE)
+    //if (queueHasResponse() || gsm_timeOut() || gsm_current_state == GSM_INITIALIZE)
     {
         //gsm_printState();   //&debug
         //printQueue();   //&debug
@@ -77,51 +78,66 @@
     //Concatenate data
     gsm_msg[0] = NULL;
     gsm_header[0] = NULL;
-    int contentLength = 0;
-    snprintf(num, NUM_SIZE, "&phone=%s", "3852368101");
+    contentLength = 0;
+    //entry.453067166=$phone$
+    snprintf(num, NUM_SIZE, "entry.453067166=%s", eth.getPhoneNumber());   
+    //snprintf(num, NUM_SIZE, "phone=%s", "3852368101");
     contentLength += strlen(num);
     strcat(gsm_msg, num);
-    snprintf(num, NUM_SIZE, "&data=%f", L);
+    //&entry.724220743=$data$
+    snprintf(num, NUM_SIZE, "&entry.724220743=%f", L);
+    //snprintf(num, NUM_SIZE, "&data=%f", L);
     contentLength += strlen(num);
     strcat(gsm_msg, num);
-    snprintf(num, NUM_SIZE, "&dataRef=%f", Lref);
+    //&entry.1590868051=$dataRef$
+    //snprintf(num, NUM_SIZE, "&dataRef=%f", Lref);
+    snprintf(num, NUM_SIZE, "&entry.1590868051=%f", Lref);
     contentLength += strlen(num);
     strcat(gsm_msg, num);
-    snprintf(num, NUM_SIZE, "&dataRatio=%f", (Lref ? (L/Lref) : 0));
+    //&entry.44817253=$dataRatio$
+    snprintf(num, NUM_SIZE, "&entry.44817253=%f", (Lref ? (L/Lref) : 0));
+    //snprintf(num, NUM_SIZE, "&dataRatio=%f", (Lref ? (L/Lref) : 0));
     contentLength += strlen(num);
     strcat(gsm_msg, num);
-    snprintf(num, NUM_SIZE, "&time=%02d:%02d:%02d", hh, mm, ss);   //If there is no data from GPS, the time will just be "00:00:00" (that is okay)
+    //&entry.142778814=$time$
+    snprintf(num, NUM_SIZE, "&entry.142778814=%02d:%02d:%02d", hh, mm, ss);
+    //snprintf(num, NUM_SIZE, "&time=%02d:%02d:%02d", hh, mm, ss);   //If there is no data from GPS, the time will just be "00:00:00" (that is okay)
     contentLength += strlen(num);
     strcat(gsm_msg, num);
     if (ns != NULL) //If there is a gps fix (i.e. the gps has data on our location), ns will be set
     { 
-        snprintf(num, NUM_SIZE, "&latitude=%.4f&longitude=%.4f", (ns == 'N') ? lat : -lat, (we == 'E') ? lon : -lon);   //Use + or - rather than N/S, E/W
+        //&entry.894229969=$latitude$&entry.1266703316=$longitude$
+        snprintf(num, NUM_SIZE, "&entry.894229969=%.4f&entry.1266703316=%.4f", (ns == 'N') ? lat : -lat, (we == 'E') ? lon : -lon);   //Use + or - rather than N/S, E/W
         contentLength += strlen(num);
         strcat(gsm_msg, num);
     }
     else {
-        snprintf(num, NUM_SIZE,"&latitude=0&longitude=0");
+        //&entry.894229969=$latitude$&entry.1266703316=$longitude$
+        snprintf(num, NUM_SIZE,"&entry.894229969=0&entry.1266703316=0");
         strcat(gsm_msg, num);    //Otherwise just send 0's for latitude and longitude
         contentLength += strlen(num);
     }
-
+    
+    //header information
     
-    //header information    
-    snprintf(num, NUM_SIZE, "%s", "POST /pushingbox?devid=v941C443DE0C7B14");
+    //begin get request
+    snprintf(num, NUM_SIZE, "%s", "\r\nGET /index.html?");
     strcat(gsm_header, num);
+    
+    //add query parameters
     strcat(gsm_header, gsm_msg);
+    
+    //add necessary headers
     snprintf(num, NUM_SIZE, "%s"," HTTP/1.1\r\n");
     strcat(gsm_header, num);
-    snprintf(num, NUM_SIZE, "%s","Host: api.pushingbox.com\r\n");
-    strcat(gsm_header, num);
-    snprintf(num, NUM_SIZE, "%s","Connection: close\r\n");
-    strcat(gsm_header, num);
-    snprintf(num, NUM_SIZE, "%s","User-Agent: FRDM-KD64\r\n");
+    snprintf(num, NUM_SIZE, "%s","Host: 23.251.149.114\r\n");
     strcat(gsm_header, num);
     //must have two blank lines after so the server knows that this is the end of headers
-    snprintf(num, NUM_SIZE, "Content-Length: %d\r\n\r\n", contentLength);
+    snprintf(num, NUM_SIZE, "%s","Connection: Keep-Alive\r\n\r\n");
     strcat(gsm_header, num);
-    send = true;    //Mark that we are currently sending a message
+    
+    
+    send = true;
 }
  
 //Return true if gsm is ready to send via tcp
@@ -168,6 +184,7 @@
             int join = eth.connect();
             if (join == false || join < 0){
                 //stay here
+                gsm_current_state = GSM_JOIN;
             }
             else{
                 //possibly send this sms to the main box at the lab
@@ -177,7 +194,7 @@
             break;
         case GSM_SERVER_IP:
             printf(">>>SERVER IP\r\n");
-            serverIP = "api.pushingbox.com";
+            serverIP = "23.251.149.114";
             if(serverIP != NULL)
             {
                 gsm_current_state = GSM_CONNECT;
@@ -188,33 +205,37 @@
             break;
         case GSM_CONNECT:
             printf("\r\n>>>CONNECT TO: %s\r\n", serverIP);
-            //sock.set_blocking(true,5000);
-            if (sock.connect(serverIP,80) == true){
-                gsm_current_state = GSM_SEND;
-            }
-            else{
-                gsm_current_state = GSM_CONNECT;   
-            }
+                if(sock.connect(serverIP,80)){
+                    printf("Connected\r\n");
+                    gsm_current_state = GSM_SEND;
+                }
+                else{
+                     gsm_current_state = GSM_JOIN;
+                } 
             break;  
         case GSM_SEND:
             printf(">>>READY TO SEND\r\n");
-            if(send){
-            if(sock.send_all(gsm_header, sizeof(gsm_header)-1)){
+            waitTicks = 6;
+            if(sock.send_all(gsm_header, contentLength)){
                 printf("Data succesfully sent to server\r\n");
-                //close the connection so others can send too
-                //wait(2);
-                //eth.disconnect();
+                gsm_current_state = GSM_WAIT;
             }
             else{
                 printf("Reconnecting to Server...\r\n");
-                send = false;
                 gsm_current_state = GSM_CONNECT;
             }
-            }
+            
+            
+            //gsm_current_state = GSM_SEND;               
             break;
         case GSM_WAIT:
-            //check for text message from server asking for data
-            gsm_current_state = GSM_SEND;
+            //WAIT BETWEEN CONSECUTIVE SENDS
+            printf("Wait Tick...\r\n");
+            //eth.close(0);
+            //eth.disconnect();
+            waitTicks--;
+            if(waitTicks == 0)
+                gsm_current_state = GSM_SEND;
             break;
         default:
             pc.printf("This is a state error\r\n");
@@ -252,12 +273,12 @@
      // Number of bytes to be transfered in each service request of the channel
      DMA_TCD0_NBYTES_MLNO = 0x01;
      // Current major iteration count
-    DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
-    DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
+    //DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(BUFFER_LENGTH);
+    //DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH);
     // Adjustment value used to restore the source and destiny address to the initial value
     // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the original address)
     DMA_TCD0_SLAST = 0;   // Source address adjustment
-    DMA_TCD0_DLASTSGA = -BUFFER_LENGTH;  // Destination address adjustment   
+    //DMA_TCD0_DLASTSGA = -BUFFER_LENGTH;  // Destination address adjustment   
     // Setup control and status register
     DMA_TCD0_CSR = 0;
        
@@ -269,8 +290,8 @@
     UART_C5_REG(UART3) |= UART_C5_RDMAS_MASK | UART_C5_ILDMAS_MASK | UART_C5_LBKDDMAS_MASK;
     //activate p fifo register
     UART_PFIFO_REG(UART3) |= UART_PFIFO_RXFE_MASK; //RXFE and buffer size of 1 word
-    queueInit();
-    //pc.printf("done...\n\r");
+    //queueInit();
+    pc.printf("done...\n\r");
 }
  
 //For debugging: print registers related to DMA and UART setup