works through pushing box to log data to google spreadsheet
Dependencies: MBed_Adafruit-GPS-Library SDFileSystem mbed
Fork of GPR_Interface by
main.cpp@11:045cb766d9a5, 2015-10-21 (annotated)
- Committer:
- DeWayneDennis
- Date:
- Wed Oct 21 19:41:42 2015 +0000
- Revision:
- 11:045cb766d9a5
Changed GPR Interface to work with pushingbox
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DeWayneDennis | 11:045cb766d9a5 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
DeWayneDennis | 11:045cb766d9a5 | 2 | * |
DeWayneDennis | 11:045cb766d9a5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
DeWayneDennis | 11:045cb766d9a5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
DeWayneDennis | 11:045cb766d9a5 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
DeWayneDennis | 11:045cb766d9a5 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
DeWayneDennis | 11:045cb766d9a5 | 7 | * Software is furnished to do so, subject to the following conditions: |
DeWayneDennis | 11:045cb766d9a5 | 8 | * |
DeWayneDennis | 11:045cb766d9a5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
DeWayneDennis | 11:045cb766d9a5 | 10 | * substantial portions of the Software. |
DeWayneDennis | 11:045cb766d9a5 | 11 | * |
DeWayneDennis | 11:045cb766d9a5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
DeWayneDennis | 11:045cb766d9a5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
DeWayneDennis | 11:045cb766d9a5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
DeWayneDennis | 11:045cb766d9a5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DeWayneDennis | 11:045cb766d9a5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
DeWayneDennis | 11:045cb766d9a5 | 17 | */ |
DeWayneDennis | 11:045cb766d9a5 | 18 | #pragma once |
DeWayneDennis | 11:045cb766d9a5 | 19 | #include "mbed.h" |
DeWayneDennis | 11:045cb766d9a5 | 20 | #include "math.h" |
DeWayneDennis | 11:045cb766d9a5 | 21 | #include "MBed_Adafruit_GPS.h" |
DeWayneDennis | 11:045cb766d9a5 | 22 | #include "SDFileSystem.h" |
DeWayneDennis | 11:045cb766d9a5 | 23 | #include "QAM.h" |
DeWayneDennis | 11:045cb766d9a5 | 24 | #include "GPRSInterface.h" |
DeWayneDennis | 11:045cb766d9a5 | 25 | #include "param.h" |
DeWayneDennis | 11:045cb766d9a5 | 26 | #include "stdlib.h" |
DeWayneDennis | 11:045cb766d9a5 | 27 | |
DeWayneDennis | 11:045cb766d9a5 | 28 | #define NUM_SIZE 250 |
DeWayneDennis | 11:045cb766d9a5 | 29 | /** On many platforms USBTX/USBRX overlap with serial on D1/D0 pins and enabling the below will interrupt the communication. |
DeWayneDennis | 11:045cb766d9a5 | 30 | * You can use an LCD display to print the values or store them on an SD card etc. |
DeWayneDennis | 11:045cb766d9a5 | 31 | */ |
DeWayneDennis | 11:045cb766d9a5 | 32 | DigitalOut led_red(LED_RED); |
DeWayneDennis | 11:045cb766d9a5 | 33 | Serial pc(USBTX, USBRX); |
DeWayneDennis | 11:045cb766d9a5 | 34 | //Serial gsm(D1,D0); |
DeWayneDennis | 11:045cb766d9a5 | 35 | |
DeWayneDennis | 11:045cb766d9a5 | 36 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 37 | ** SD FILE SYSTEM ** |
DeWayneDennis | 11:045cb766d9a5 | 38 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 39 | //SDFileSystem sdModem(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. Tested on K64F, correct pins. |
DeWayneDennis | 11:045cb766d9a5 | 40 | FILE *fpData; |
DeWayneDennis | 11:045cb766d9a5 | 41 | FILE *fpLog; |
DeWayneDennis | 11:045cb766d9a5 | 42 | |
DeWayneDennis | 11:045cb766d9a5 | 43 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 44 | ** GPS ** |
DeWayneDennis | 11:045cb766d9a5 | 45 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 46 | Serial * gps_Serial; |
DeWayneDennis | 11:045cb766d9a5 | 47 | |
DeWayneDennis | 11:045cb766d9a5 | 48 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 49 | ** GPRS ** |
DeWayneDennis | 11:045cb766d9a5 | 50 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 51 | /** |
DeWayneDennis | 11:045cb766d9a5 | 52 | * D1 - TX pin (RX on the WiFi side) |
DeWayneDennis | 11:045cb766d9a5 | 53 | * D0 - RX pin (TX on the WiFi side) |
DeWayneDennis | 11:045cb766d9a5 | 54 | * 19200 - Baud rate |
DeWayneDennis | 11:045cb766d9a5 | 55 | * "apn" - APN name |
DeWayneDennis | 11:045cb766d9a5 | 56 | * "username" - APN username |
DeWayneDennis | 11:045cb766d9a5 | 57 | * "password" - APN passowrd |
DeWayneDennis | 11:045cb766d9a5 | 58 | */ |
DeWayneDennis | 11:045cb766d9a5 | 59 | GPRSInterface eth(D1,D0, 19200, "ndo","",""); |
DeWayneDennis | 11:045cb766d9a5 | 60 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 61 | ** SENSOR INPUTS ** |
DeWayneDennis | 11:045cb766d9a5 | 62 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 63 | AnalogIn An(A0); |
DeWayneDennis | 11:045cb766d9a5 | 64 | AnalogIn AnRef(A2); |
DeWayneDennis | 11:045cb766d9a5 | 65 | Ticker sample_tick; |
DeWayneDennis | 11:045cb766d9a5 | 66 | bool takeSample = false; |
DeWayneDennis | 11:045cb766d9a5 | 67 | void tick(){takeSample = true;} |
DeWayneDennis | 11:045cb766d9a5 | 68 | |
DeWayneDennis | 11:045cb766d9a5 | 69 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 70 | ** SIN OUTPUT ** |
DeWayneDennis | 11:045cb766d9a5 | 71 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 72 | AnalogOut dac0(DAC0_OUT); |
DeWayneDennis | 11:045cb766d9a5 | 73 | int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST); |
DeWayneDennis | 11:045cb766d9a5 | 74 | float sinWave[SIN_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 75 | int sinIndex = 0; |
DeWayneDennis | 11:045cb766d9a5 | 76 | |
DeWayneDennis | 11:045cb766d9a5 | 77 | |
DeWayneDennis | 11:045cb766d9a5 | 78 | /************************************************** |
DeWayneDennis | 11:045cb766d9a5 | 79 | ** QAM ** |
DeWayneDennis | 11:045cb766d9a5 | 80 | **************************************************/ |
DeWayneDennis | 11:045cb766d9a5 | 81 | float sQ[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 82 | float sI[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 83 | float sRefQ[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 84 | float sRefI[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 85 | |
DeWayneDennis | 11:045cb766d9a5 | 86 | float Iarray[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 87 | float Qarray[SAMPLE_LENGTH] = {}; |
DeWayneDennis | 11:045cb766d9a5 | 88 | int sampleIndex = 0; |
DeWayneDennis | 11:045cb766d9a5 | 89 | float I = 0; |
DeWayneDennis | 11:045cb766d9a5 | 90 | float Q = 0; |
DeWayneDennis | 11:045cb766d9a5 | 91 | float lon = 0; |
DeWayneDennis | 11:045cb766d9a5 | 92 | float lonRef = 0; |
DeWayneDennis | 11:045cb766d9a5 | 93 | |
DeWayneDennis | 11:045cb766d9a5 | 94 | /*Global Variables */ |
DeWayneDennis | 11:045cb766d9a5 | 95 | char c; |
DeWayneDennis | 11:045cb766d9a5 | 96 | int s; |
DeWayneDennis | 11:045cb766d9a5 | 97 | bool run = 0; |
DeWayneDennis | 11:045cb766d9a5 | 98 | bool fil = 0; |
DeWayneDennis | 11:045cb766d9a5 | 99 | float filtered = 0; |
DeWayneDennis | 11:045cb766d9a5 | 100 | float filteredRef = 0; |
DeWayneDennis | 11:045cb766d9a5 | 101 | char gsm_header[250]; //String storing SMS message that will be sent (add 250 length to give leeway) |
DeWayneDennis | 11:045cb766d9a5 | 102 | char gsm_msg[250]; |
DeWayneDennis | 11:045cb766d9a5 | 103 | char num[NUM_SIZE]; //Temporary string storage to help with concatenation of strings |
DeWayneDennis | 11:045cb766d9a5 | 104 | |
DeWayneDennis | 11:045cb766d9a5 | 105 | void buildIQ(){ |
DeWayneDennis | 11:045cb766d9a5 | 106 | for(int i = 0; i < SAMPLE_LENGTH; i++){ |
DeWayneDennis | 11:045cb766d9a5 | 107 | Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST); |
DeWayneDennis | 11:045cb766d9a5 | 108 | Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST); |
DeWayneDennis | 11:045cb766d9a5 | 109 | } |
DeWayneDennis | 11:045cb766d9a5 | 110 | } |
DeWayneDennis | 11:045cb766d9a5 | 111 | |
DeWayneDennis | 11:045cb766d9a5 | 112 | void create_sinWave(){ |
DeWayneDennis | 11:045cb766d9a5 | 113 | int i = 0; |
DeWayneDennis | 11:045cb766d9a5 | 114 | for(i = 0; i < SIN_LENGTH; i++){ |
DeWayneDennis | 11:045cb766d9a5 | 115 | sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75; |
DeWayneDennis | 11:045cb766d9a5 | 116 | } |
DeWayneDennis | 11:045cb766d9a5 | 117 | } |
DeWayneDennis | 11:045cb766d9a5 | 118 | void init(Adafruit_GPS myGPS){ |
DeWayneDennis | 11:045cb766d9a5 | 119 | |
DeWayneDennis | 11:045cb766d9a5 | 120 | //fprintf(fpLog, "%s\r\n", ":)\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 121 | |
DeWayneDennis | 11:045cb766d9a5 | 122 | //fprintf(fpLog, "%s\r\n", "Initializing the GPS\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 123 | |
DeWayneDennis | 11:045cb766d9a5 | 124 | myGPS.begin(9600); |
DeWayneDennis | 11:045cb766d9a5 | 125 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
DeWayneDennis | 11:045cb766d9a5 | 126 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
DeWayneDennis | 11:045cb766d9a5 | 127 | myGPS.sendCommand(PGCMD_ANTENNA); |
DeWayneDennis | 11:045cb766d9a5 | 128 | //fprintf(fpLog, "%s\r\n", "GPS Initialized\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 129 | |
DeWayneDennis | 11:045cb766d9a5 | 130 | // Initialize filtering structures. |
DeWayneDennis | 11:045cb766d9a5 | 131 | buildIQ(); |
DeWayneDennis | 11:045cb766d9a5 | 132 | create_sinWave(); |
DeWayneDennis | 11:045cb766d9a5 | 133 | |
DeWayneDennis | 11:045cb766d9a5 | 134 | |
DeWayneDennis | 11:045cb766d9a5 | 135 | |
DeWayneDennis | 11:045cb766d9a5 | 136 | //Intialize the dhcp (or die trying) |
DeWayneDennis | 11:045cb766d9a5 | 137 | s = eth.init(); |
DeWayneDennis | 11:045cb766d9a5 | 138 | if (s != NULL) { |
DeWayneDennis | 11:045cb766d9a5 | 139 | printf(">>> Could not initialise. Halting!\n"); |
DeWayneDennis | 11:045cb766d9a5 | 140 | exit(0); |
DeWayneDennis | 11:045cb766d9a5 | 141 | } |
DeWayneDennis | 11:045cb766d9a5 | 142 | |
DeWayneDennis | 11:045cb766d9a5 | 143 | //Check the SIM card |
DeWayneDennis | 11:045cb766d9a5 | 144 | s= eth.preInit(); |
DeWayneDennis | 11:045cb766d9a5 | 145 | if(s == true) { |
DeWayneDennis | 11:045cb766d9a5 | 146 | //fprintf(fpLog, "%s\r\n", "SIM Card is good!\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 147 | } else { |
DeWayneDennis | 11:045cb766d9a5 | 148 | //fprintf(fpLog, "%s\r\n", "check your baud rate\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 149 | } |
DeWayneDennis | 11:045cb766d9a5 | 150 | } |
DeWayneDennis | 11:045cb766d9a5 | 151 | void gatherData(Adafruit_GPS myGPS){ |
DeWayneDennis | 11:045cb766d9a5 | 152 | //sample |
DeWayneDennis | 11:045cb766d9a5 | 153 | if(takeSample){ |
DeWayneDennis | 11:045cb766d9a5 | 154 | |
DeWayneDennis | 11:045cb766d9a5 | 155 | dac0 = sinWave[sinIndex]; |
DeWayneDennis | 11:045cb766d9a5 | 156 | |
DeWayneDennis | 11:045cb766d9a5 | 157 | lon = An.read(); |
DeWayneDennis | 11:045cb766d9a5 | 158 | lonRef = AnRef.read(); |
DeWayneDennis | 11:045cb766d9a5 | 159 | |
DeWayneDennis | 11:045cb766d9a5 | 160 | I = Iarray[sampleIndex]; |
DeWayneDennis | 11:045cb766d9a5 | 161 | Q = Qarray[sampleIndex]; |
DeWayneDennis | 11:045cb766d9a5 | 162 | sI[sampleIndex] = lon*I; |
DeWayneDennis | 11:045cb766d9a5 | 163 | sQ[sampleIndex] = lon*Q; |
DeWayneDennis | 11:045cb766d9a5 | 164 | sRefI[sampleIndex] = lonRef*I; |
DeWayneDennis | 11:045cb766d9a5 | 165 | sRefQ[sampleIndex] = lonRef*Q; |
DeWayneDennis | 11:045cb766d9a5 | 166 | |
DeWayneDennis | 11:045cb766d9a5 | 167 | takeSample = false; |
DeWayneDennis | 11:045cb766d9a5 | 168 | sinIndex++; |
DeWayneDennis | 11:045cb766d9a5 | 169 | if((sinIndex+1) > SIN_LENGTH){ |
DeWayneDennis | 11:045cb766d9a5 | 170 | sinIndex = 0; |
DeWayneDennis | 11:045cb766d9a5 | 171 | } |
DeWayneDennis | 11:045cb766d9a5 | 172 | |
DeWayneDennis | 11:045cb766d9a5 | 173 | sampleIndex++; |
DeWayneDennis | 11:045cb766d9a5 | 174 | if(sampleIndex+1 > SAMPLE_LENGTH){ |
DeWayneDennis | 11:045cb766d9a5 | 175 | fil = 1; |
DeWayneDennis | 11:045cb766d9a5 | 176 | } |
DeWayneDennis | 11:045cb766d9a5 | 177 | } |
DeWayneDennis | 11:045cb766d9a5 | 178 | //Filter |
DeWayneDennis | 11:045cb766d9a5 | 179 | if(fil==1){ |
DeWayneDennis | 11:045cb766d9a5 | 180 | |
DeWayneDennis | 11:045cb766d9a5 | 181 | fil = 0; |
DeWayneDennis | 11:045cb766d9a5 | 182 | run = 1; |
DeWayneDennis | 11:045cb766d9a5 | 183 | sampleIndex = 0; |
DeWayneDennis | 11:045cb766d9a5 | 184 | filtered = 15*QAM(sI, sQ); |
DeWayneDennis | 11:045cb766d9a5 | 185 | filteredRef = QAM(sRefI, sRefQ); |
DeWayneDennis | 11:045cb766d9a5 | 186 | } |
DeWayneDennis | 11:045cb766d9a5 | 187 | |
DeWayneDennis | 11:045cb766d9a5 | 188 | c = myGPS.read(); |
DeWayneDennis | 11:045cb766d9a5 | 189 | if ( myGPS.newNMEAreceived() ) { |
DeWayneDennis | 11:045cb766d9a5 | 190 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
DeWayneDennis | 11:045cb766d9a5 | 191 | //fprintf(fpLog, "%s\r\n", "Couldn't parse the GPS Data\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 192 | } |
DeWayneDennis | 11:045cb766d9a5 | 193 | } |
DeWayneDennis | 11:045cb766d9a5 | 194 | if(run){ |
DeWayneDennis | 11:045cb766d9a5 | 195 | run = 0; |
DeWayneDennis | 11:045cb766d9a5 | 196 | printf("Logged Data\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 197 | /*fprintf(fpLog, "15CM: %f, ", filtered); |
DeWayneDennis | 11:045cb766d9a5 | 198 | fprintf(fpLog, "15CM Reference: %f\r\n", filteredRef); |
DeWayneDennis | 11:045cb766d9a5 | 199 | fprintf(fpLog, "15CM / 15CM Refernce: %f\r\n", (filteredRef ? (filtered/filteredRef) : 0)); |
DeWayneDennis | 11:045cb766d9a5 | 200 | fprintf(fpLog, "GPS TIME: %d:%d:%d \r\n", myGPS.hour-6, myGPS.minute, myGPS.seconds);*/ |
DeWayneDennis | 11:045cb766d9a5 | 201 | |
DeWayneDennis | 11:045cb766d9a5 | 202 | if (myGPS.fix){ |
DeWayneDennis | 11:045cb766d9a5 | 203 | printf("Lat: %5.2f%c, Long: %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
DeWayneDennis | 11:045cb766d9a5 | 204 | //fprintf(fpLog, "Lat: %5.2f%c, Long: %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
DeWayneDennis | 11:045cb766d9a5 | 205 | } |
DeWayneDennis | 11:045cb766d9a5 | 206 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 207 | printf("No GPS FIX\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 208 | /*fprintf(fpLog, "%s\r\n", "No GPS fix\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 209 | fprintf(fpLog, "%s\r\n", "--------------------------------\r\n");*/ |
DeWayneDennis | 11:045cb766d9a5 | 210 | } |
DeWayneDennis | 11:045cb766d9a5 | 211 | |
DeWayneDennis | 11:045cb766d9a5 | 212 | /*fpData = fopen("/sd/data.txt", "a"); |
DeWayneDennis | 11:045cb766d9a5 | 213 | if (fpData != NULL){ |
DeWayneDennis | 11:045cb766d9a5 | 214 | |
DeWayneDennis | 11:045cb766d9a5 | 215 | fprintf(fpData, "%f, ", filtered); |
DeWayneDennis | 11:045cb766d9a5 | 216 | fprintf(fpData, "%f, ", filteredRef); |
DeWayneDennis | 11:045cb766d9a5 | 217 | fprintf(fpData, "%d:%d:%d\r\n", myGPS.hour-6, myGPS.minute, myGPS.seconds); |
DeWayneDennis | 11:045cb766d9a5 | 218 | if (myGPS.fix) fprintf(fpData, "%5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
DeWayneDennis | 11:045cb766d9a5 | 219 | else fprintf(fpData, "No_GPS_fix\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 220 | |
DeWayneDennis | 11:045cb766d9a5 | 221 | fclose(fpData); |
DeWayneDennis | 11:045cb766d9a5 | 222 | }*/ |
DeWayneDennis | 11:045cb766d9a5 | 223 | |
DeWayneDennis | 11:045cb766d9a5 | 224 | } |
DeWayneDennis | 11:045cb766d9a5 | 225 | |
DeWayneDennis | 11:045cb766d9a5 | 226 | } |
DeWayneDennis | 11:045cb766d9a5 | 227 | int main() |
DeWayneDennis | 11:045cb766d9a5 | 228 | { |
DeWayneDennis | 11:045cb766d9a5 | 229 | //set the baud rates |
DeWayneDennis | 11:045cb766d9a5 | 230 | //gsm.baud(19200); |
DeWayneDennis | 11:045cb766d9a5 | 231 | |
DeWayneDennis | 11:045cb766d9a5 | 232 | gps_Serial = new Serial(PTC4,PTC3); ////Serial gsm(D1,D0); |
DeWayneDennis | 11:045cb766d9a5 | 233 | Adafruit_GPS myGPS(gps_Serial); |
DeWayneDennis | 11:045cb766d9a5 | 234 | |
DeWayneDennis | 11:045cb766d9a5 | 235 | //Initialize SD Card with log and data |
DeWayneDennis | 11:045cb766d9a5 | 236 | /*fpLog = fopen("/sd/log.txt", "a"); |
DeWayneDennis | 11:045cb766d9a5 | 237 | if (fpLog != NULL){ |
DeWayneDennis | 11:045cb766d9a5 | 238 | fprintf(fpLog, "%s\r\n", "--------------- DCS LOG ------------------"); |
DeWayneDennis | 11:045cb766d9a5 | 239 | } |
DeWayneDennis | 11:045cb766d9a5 | 240 | fpData = fopen("/sd/data.txt", "a"); |
DeWayneDennis | 11:045cb766d9a5 | 241 | if (fpData != NULL){ |
DeWayneDennis | 11:045cb766d9a5 | 242 | fprintf(fpData, "%s", "--------------- DCS DATA ------------------"); |
DeWayneDennis | 11:045cb766d9a5 | 243 | } |
DeWayneDennis | 11:045cb766d9a5 | 244 | |
DeWayneDennis | 11:045cb766d9a5 | 245 | //Successful Connection Message |
DeWayneDennis | 11:045cb766d9a5 | 246 | fprintf(fpLog, "%s\r\n", "---DPG Connection Successful!---\r\n");*/ |
DeWayneDennis | 11:045cb766d9a5 | 247 | |
DeWayneDennis | 11:045cb766d9a5 | 248 | //initialize gps, gprs, sd card etc... |
DeWayneDennis | 11:045cb766d9a5 | 249 | init(myGPS); |
DeWayneDennis | 11:045cb766d9a5 | 250 | |
DeWayneDennis | 11:045cb766d9a5 | 251 | //fprintf(fpLog, "%s\r\n", ">>> Get IP address...\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 252 | while (1) { |
DeWayneDennis | 11:045cb766d9a5 | 253 | s = eth.connect(); // Connect to network |
DeWayneDennis | 11:045cb766d9a5 | 254 | if (s == false || s < 0) { |
DeWayneDennis | 11:045cb766d9a5 | 255 | printf(">>> Could not connect to network. Retrying!\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 256 | wait(3); |
DeWayneDennis | 11:045cb766d9a5 | 257 | } else { |
DeWayneDennis | 11:045cb766d9a5 | 258 | break; |
DeWayneDennis | 11:045cb766d9a5 | 259 | } |
DeWayneDennis | 11:045cb766d9a5 | 260 | } |
DeWayneDennis | 11:045cb766d9a5 | 261 | //fprintf(fpLog, ">>> Got IP address: %s\r\n", eth.getIPAddress()); |
DeWayneDennis | 11:045cb766d9a5 | 262 | |
DeWayneDennis | 11:045cb766d9a5 | 263 | //fprintf(fpLog, "%s\r\n", ">>> Create a TCP Socket Connection\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 264 | TCPSocketConnection sock; |
DeWayneDennis | 11:045cb766d9a5 | 265 | sock.set_blocking(true,5000); |
DeWayneDennis | 11:045cb766d9a5 | 266 | |
DeWayneDennis | 11:045cb766d9a5 | 267 | sock.connect("api.pushingbox.com",80); |
DeWayneDennis | 11:045cb766d9a5 | 268 | |
DeWayneDennis | 11:045cb766d9a5 | 269 | //fprintf(fpLog, "%s\r\n", "Gathering Data...\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 270 | while(1){ |
DeWayneDennis | 11:045cb766d9a5 | 271 | |
DeWayneDennis | 11:045cb766d9a5 | 272 | //take a sample |
DeWayneDennis | 11:045cb766d9a5 | 273 | tick(); |
DeWayneDennis | 11:045cb766d9a5 | 274 | gatherData(myGPS); |
DeWayneDennis | 11:045cb766d9a5 | 275 | //if(!myGPS.fix){ |
DeWayneDennis | 11:045cb766d9a5 | 276 | printf("Done Gathering Data\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 277 | |
DeWayneDennis | 11:045cb766d9a5 | 278 | //fprintf(fpLog, "%s\r\n", "Begin Sending data...\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 279 | //Concatenate data |
DeWayneDennis | 11:045cb766d9a5 | 280 | gsm_msg[0] = NULL; |
DeWayneDennis | 11:045cb766d9a5 | 281 | gsm_header[0] = NULL; |
DeWayneDennis | 11:045cb766d9a5 | 282 | int contentLength = 0; |
DeWayneDennis | 11:045cb766d9a5 | 283 | snprintf(num, NUM_SIZE, "&phone=%s", "3852368101"); |
DeWayneDennis | 11:045cb766d9a5 | 284 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 285 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 286 | snprintf(num, NUM_SIZE, "&data=%f", filtered); |
DeWayneDennis | 11:045cb766d9a5 | 287 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 288 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 289 | snprintf(num, NUM_SIZE, "&dataRef=%f", filteredRef); |
DeWayneDennis | 11:045cb766d9a5 | 290 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 291 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 292 | snprintf(num, NUM_SIZE, "&dataRatio=%f", (filteredRef ? (filtered/filteredRef) : 0)); |
DeWayneDennis | 11:045cb766d9a5 | 293 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 294 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 295 | snprintf(num, NUM_SIZE, "&time=%02d:%02d:%02d", myGPS.hour, myGPS.minute, myGPS.seconds); //If there is no data from GPS, the time will just be "00:00:00" (that is okay) |
DeWayneDennis | 11:045cb766d9a5 | 296 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 297 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 298 | if (myGPS.lat != NULL) //If there is a gps fix (i.e. the gps has data on our location), ns will be set |
DeWayneDennis | 11:045cb766d9a5 | 299 | { |
DeWayneDennis | 11:045cb766d9a5 | 300 | snprintf(num, NUM_SIZE, "&latitude=%.4f&longitude=%.4f", (myGPS.lat == 'N') ? myGPS.latitude : -myGPS.latitude, (myGPS.lon == 'E') ? myGPS.longitude : -myGPS.longitude); //Use + or - rather than N/S, E/W |
DeWayneDennis | 11:045cb766d9a5 | 301 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 302 | strcat(gsm_msg, num); |
DeWayneDennis | 11:045cb766d9a5 | 303 | } |
DeWayneDennis | 11:045cb766d9a5 | 304 | else { |
DeWayneDennis | 11:045cb766d9a5 | 305 | snprintf(num, NUM_SIZE,"&latitude=0&longitude=0"); |
DeWayneDennis | 11:045cb766d9a5 | 306 | strcat(gsm_msg, num); //Otherwise just send 0's for latitude and longitude |
DeWayneDennis | 11:045cb766d9a5 | 307 | contentLength += strlen(num); |
DeWayneDennis | 11:045cb766d9a5 | 308 | } |
DeWayneDennis | 11:045cb766d9a5 | 309 | |
DeWayneDennis | 11:045cb766d9a5 | 310 | |
DeWayneDennis | 11:045cb766d9a5 | 311 | //header information |
DeWayneDennis | 11:045cb766d9a5 | 312 | snprintf(num, NUM_SIZE, "%s", "POST /pushingbox?devid=v941C443DE0C7B14"); |
DeWayneDennis | 11:045cb766d9a5 | 313 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 314 | strcat(gsm_header, gsm_msg); |
DeWayneDennis | 11:045cb766d9a5 | 315 | snprintf(num, NUM_SIZE, "%s\r\n"," HTTP/1.1"); |
DeWayneDennis | 11:045cb766d9a5 | 316 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 317 | snprintf(num, NUM_SIZE, "%s\r\n","Host: api.pushingbox.com"); |
DeWayneDennis | 11:045cb766d9a5 | 318 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 319 | snprintf(num, NUM_SIZE, "%s\r\n","Connection: Keep-Alive"); |
DeWayneDennis | 11:045cb766d9a5 | 320 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 321 | snprintf(num, NUM_SIZE, "%s\r\n","User-Agent: FRDM-KD64"); |
DeWayneDennis | 11:045cb766d9a5 | 322 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 323 | //must have two blank lines after so the server knows that this is the end of headers |
DeWayneDennis | 11:045cb766d9a5 | 324 | snprintf(num, NUM_SIZE, "Content-Length: %d\r\n\r\n\r\n", contentLength); |
DeWayneDennis | 11:045cb766d9a5 | 325 | strcat(gsm_header, num); |
DeWayneDennis | 11:045cb766d9a5 | 326 | |
DeWayneDennis | 11:045cb766d9a5 | 327 | //fprintf(fpLog, "%s\r\n", ">>> Sending data to the server: %s\r\n",gsm_header); |
DeWayneDennis | 11:045cb766d9a5 | 328 | int result = sock.send_all(gsm_header, contentLength); |
DeWayneDennis | 11:045cb766d9a5 | 329 | //sock.set_blocking(false); |
DeWayneDennis | 11:045cb766d9a5 | 330 | if (result > 0) { |
DeWayneDennis | 11:045cb766d9a5 | 331 | //fprintf(fpLog, "%s\r\n", ">>> Successfully Sent Data to Server\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 332 | printf("Successfully send data\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 333 | } else { |
DeWayneDennis | 11:045cb766d9a5 | 334 | //fprintf(fpLog, "%s\r\n", ">>> Failed to Send Data, Error Code: %d \r\n",result); |
DeWayneDennis | 11:045cb766d9a5 | 335 | printf("Data Not Sent!! Content-Length:%d\r\n", contentLength); |
DeWayneDennis | 11:045cb766d9a5 | 336 | } |
DeWayneDennis | 11:045cb766d9a5 | 337 | |
DeWayneDennis | 11:045cb766d9a5 | 338 | //check that we are still connected (if not reconnect) |
DeWayneDennis | 11:045cb766d9a5 | 339 | //sock.close(); |
DeWayneDennis | 11:045cb766d9a5 | 340 | //printf("end of program\n"); |
DeWayneDennis | 11:045cb766d9a5 | 341 | // Disconnect from network |
DeWayneDennis | 11:045cb766d9a5 | 342 | eth.disconnect(); |
DeWayneDennis | 11:045cb766d9a5 | 343 | //return 0; |
DeWayneDennis | 11:045cb766d9a5 | 344 | /*} |
DeWayneDennis | 11:045cb766d9a5 | 345 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 346 | fprintf(fpLog, "%s\r\n", ">>>No GPS Fix\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 347 | }*/ |
DeWayneDennis | 11:045cb766d9a5 | 348 | //wait(4); |
DeWayneDennis | 11:045cb766d9a5 | 349 | } |
DeWayneDennis | 11:045cb766d9a5 | 350 | |
DeWayneDennis | 11:045cb766d9a5 | 351 | } |
DeWayneDennis | 11:045cb766d9a5 | 352 |