works through pushing box to log data to google spreadsheet
Dependencies: MBed_Adafruit-GPS-Library SDFileSystem mbed
Fork of GPR_Interface by
GPRS/modem/modem.cpp@11:045cb766d9a5, 2015-10-21 (annotated)
- Committer:
- DeWayneDennis
- Date:
- Wed Oct 21 19:41:42 2015 +0000
- Revision:
- 11:045cb766d9a5
- Parent:
- 10:8c55dfcc9a7c
Changed GPR Interface to work with pushingbox
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:8ccbd963e74d | 1 | /* |
lawliet | 0:8ccbd963e74d | 2 | modem.cpp |
lawliet | 0:8ccbd963e74d | 3 | 2014 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 0:8ccbd963e74d | 4 | |
lawliet | 0:8ccbd963e74d | 5 | Author:lawliet zou(lawliet.zou@gmail.com) |
lawliet | 0:8ccbd963e74d | 6 | 2014-2-24 |
lawliet | 0:8ccbd963e74d | 7 | |
lawliet | 0:8ccbd963e74d | 8 | This library is free software; you can redistribute it and/or |
lawliet | 0:8ccbd963e74d | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 0:8ccbd963e74d | 10 | License as published by the Free Software Foundation; either |
lawliet | 0:8ccbd963e74d | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 0:8ccbd963e74d | 12 | |
lawliet | 0:8ccbd963e74d | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 0:8ccbd963e74d | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 0:8ccbd963e74d | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 0:8ccbd963e74d | 16 | Lesser General Public License for more details. |
lawliet | 0:8ccbd963e74d | 17 | |
lawliet | 0:8ccbd963e74d | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 0:8ccbd963e74d | 19 | License along with this library; if not, write to the Free Software |
lawliet | 0:8ccbd963e74d | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 0:8ccbd963e74d | 21 | */ |
lawliet | 0:8ccbd963e74d | 22 | |
lawliet | 0:8ccbd963e74d | 23 | #include "modem.h" |
noahmilam | 10:8c55dfcc9a7c | 24 | //Serial pc(USBTX,USBRX); |
noahmilam | 10:8c55dfcc9a7c | 25 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. Tested on K64F, correct pins. |
noahmilam | 10:8c55dfcc9a7c | 26 | AnalogIn LM35(PTB2); |
noahmilam | 10:8c55dfcc9a7c | 27 | // added by Noah Milam |
noahmilam | 10:8c55dfcc9a7c | 28 | void Modem::gprs_response() |
noahmilam | 10:8c55dfcc9a7c | 29 | { |
noahmilam | 10:8c55dfcc9a7c | 30 | printf("\nlistening for client\n"); |
noahmilam | 10:8c55dfcc9a7c | 31 | char buffer[100]; |
noahmilam | 10:8c55dfcc9a7c | 32 | int count = 0; |
noahmilam | 10:8c55dfcc9a7c | 33 | |
noahmilam | 10:8c55dfcc9a7c | 34 | mkdir("/sd/mydir", 0777); // makes directory if needed |
noahmilam | 10:8c55dfcc9a7c | 35 | FILE *fp = fopen("/sd/mydir/chemData.csv", "w"); // creats new file to write |
noahmilam | 10:8c55dfcc9a7c | 36 | fprintf(fp,"phone Number,chem data, latitude,longitude\n"); // writes in a header for the table |
noahmilam | 10:8c55dfcc9a7c | 37 | fclose(fp); // closes file |
noahmilam | 10:8c55dfcc9a7c | 38 | |
noahmilam | 10:8c55dfcc9a7c | 39 | while(1) |
noahmilam | 10:8c55dfcc9a7c | 40 | { if(serialModem.readable()) { |
noahmilam | 10:8c55dfcc9a7c | 41 | |
noahmilam | 10:8c55dfcc9a7c | 42 | while(serialModem.readable()) { |
noahmilam | 10:8c55dfcc9a7c | 43 | char c = serialModem.getc(); |
noahmilam | 10:8c55dfcc9a7c | 44 | buffer[count++] = c; |
noahmilam | 10:8c55dfcc9a7c | 45 | //pc.puts(&c); |
noahmilam | 10:8c55dfcc9a7c | 46 | if(count == 64) break; |
noahmilam | 10:8c55dfcc9a7c | 47 | } |
noahmilam | 10:8c55dfcc9a7c | 48 | |
noahmilam | 10:8c55dfcc9a7c | 49 | if(buffer[0] != '\0'){ |
noahmilam | 10:8c55dfcc9a7c | 50 | buffer[count] = '\0'; |
noahmilam | 10:8c55dfcc9a7c | 51 | FILE *fp = fopen("/sd/mydir/chemicalData.csv", "a"); // opens file to append it |
noahmilam | 10:8c55dfcc9a7c | 52 | fprintf(fp,"%s\n",buffer);//writes to file |
noahmilam | 10:8c55dfcc9a7c | 53 | fclose(fp); // closes file |
noahmilam | 10:8c55dfcc9a7c | 54 | |
noahmilam | 10:8c55dfcc9a7c | 55 | printf("%s \n",buffer); |
noahmilam | 10:8c55dfcc9a7c | 56 | for(int i = 0; i < count+2; i++) { |
noahmilam | 10:8c55dfcc9a7c | 57 | buffer[i] = NULL; |
noahmilam | 10:8c55dfcc9a7c | 58 | } |
noahmilam | 10:8c55dfcc9a7c | 59 | } |
noahmilam | 10:8c55dfcc9a7c | 60 | count = 0; |
noahmilam | 10:8c55dfcc9a7c | 61 | } |
noahmilam | 10:8c55dfcc9a7c | 62 | } |
noahmilam | 10:8c55dfcc9a7c | 63 | } |
lawliet | 0:8ccbd963e74d | 64 | |
noahmilam | 10:8c55dfcc9a7c | 65 | void Modem::wait_for_sms(){ |
noahmilam | 10:8c55dfcc9a7c | 66 | printf("waiting for message\n"); |
noahmilam | 10:8c55dfcc9a7c | 67 | while(1){ |
noahmilam | 10:8c55dfcc9a7c | 68 | if(serialModem.readable()){ |
noahmilam | 10:8c55dfcc9a7c | 69 | return; |
noahmilam | 10:8c55dfcc9a7c | 70 | } |
noahmilam | 10:8c55dfcc9a7c | 71 | } |
noahmilam | 10:8c55dfcc9a7c | 72 | } |
noahmilam | 10:8c55dfcc9a7c | 73 | void Modem::get_message(){ |
noahmilam | 10:8c55dfcc9a7c | 74 | char tempStr[30]; |
noahmilam | 10:8c55dfcc9a7c | 75 | int count= 0; |
noahmilam | 10:8c55dfcc9a7c | 76 | int line_count = 0; |
noahmilam | 10:8c55dfcc9a7c | 77 | for(int i= 0;i < strlen(IPAdd);i++){ |
noahmilam | 10:8c55dfcc9a7c | 78 | if(line_count == 3){ |
noahmilam | 10:8c55dfcc9a7c | 79 | tempStr[count++] = IPAdd[i]; |
noahmilam | 10:8c55dfcc9a7c | 80 | } |
noahmilam | 10:8c55dfcc9a7c | 81 | if(IPAdd[i] == '\n'){ |
noahmilam | 10:8c55dfcc9a7c | 82 | line_count++; |
noahmilam | 10:8c55dfcc9a7c | 83 | } |
noahmilam | 10:8c55dfcc9a7c | 84 | } |
noahmilam | 10:8c55dfcc9a7c | 85 | tempStr[count - 2] = '\0'; |
noahmilam | 10:8c55dfcc9a7c | 86 | count++; |
noahmilam | 10:8c55dfcc9a7c | 87 | strncpy(IPAdd, tempStr, count); |
noahmilam | 10:8c55dfcc9a7c | 88 | printf("IP Addr > %s \n", IPAdd); |
noahmilam | 10:8c55dfcc9a7c | 89 | printf("text size > %d\n",strlen(IPAdd)); |
noahmilam | 10:8c55dfcc9a7c | 90 | } |
noahmilam | 10:8c55dfcc9a7c | 91 | void Modem::storeResp(){ |
noahmilam | 10:8c55dfcc9a7c | 92 | |
noahmilam | 10:8c55dfcc9a7c | 93 | int line_count = 0; |
noahmilam | 10:8c55dfcc9a7c | 94 | int read = -1; |
noahmilam | 10:8c55dfcc9a7c | 95 | char buffer[100]; |
noahmilam | 10:8c55dfcc9a7c | 96 | int count = 0; |
noahmilam | 10:8c55dfcc9a7c | 97 | timeCnt.start(); |
noahmilam | 10:8c55dfcc9a7c | 98 | while(timeCnt.read() < 5) |
noahmilam | 10:8c55dfcc9a7c | 99 | { |
noahmilam | 10:8c55dfcc9a7c | 100 | while(serialModem.readable()) { |
noahmilam | 10:8c55dfcc9a7c | 101 | char c = serialModem.getc(); |
noahmilam | 10:8c55dfcc9a7c | 102 | buffer[count++] = c; |
noahmilam | 10:8c55dfcc9a7c | 103 | } |
noahmilam | 10:8c55dfcc9a7c | 104 | } |
noahmilam | 10:8c55dfcc9a7c | 105 | timeCnt.stop(); |
noahmilam | 10:8c55dfcc9a7c | 106 | timeCnt.reset(); |
noahmilam | 10:8c55dfcc9a7c | 107 | buffer[count] = '\0'; |
noahmilam | 10:8c55dfcc9a7c | 108 | |
noahmilam | 10:8c55dfcc9a7c | 109 | strncpy(IPAdd, buffer, count); |
noahmilam | 10:8c55dfcc9a7c | 110 | printf("original>> %s",IPAdd); |
noahmilam | 10:8c55dfcc9a7c | 111 | printf("size of text > %d",strlen(IPAdd)); |
noahmilam | 10:8c55dfcc9a7c | 112 | count = 0; |
noahmilam | 10:8c55dfcc9a7c | 113 | get_message(); |
noahmilam | 10:8c55dfcc9a7c | 114 | } |
noahmilam | 10:8c55dfcc9a7c | 115 | |
noahmilam | 10:8c55dfcc9a7c | 116 | char* Modem::get_server_IP(){ |
noahmilam | 10:8c55dfcc9a7c | 117 | return IPAdd; |
noahmilam | 10:8c55dfcc9a7c | 118 | } |
noahmilam | 10:8c55dfcc9a7c | 119 | //end added by Noah Milam |
lawliet | 0:8ccbd963e74d | 120 | char Modem::readByte(void) |
lawliet | 0:8ccbd963e74d | 121 | { |
lawliet | 0:8ccbd963e74d | 122 | return serialModem.getc(); |
lawliet | 0:8ccbd963e74d | 123 | } |
lawliet | 0:8ccbd963e74d | 124 | |
lawliet | 0:8ccbd963e74d | 125 | bool Modem::readable() |
lawliet | 0:8ccbd963e74d | 126 | { |
lawliet | 0:8ccbd963e74d | 127 | return serialModem.readable(); |
lawliet | 0:8ccbd963e74d | 128 | } |
lawliet | 0:8ccbd963e74d | 129 | |
lawliet | 0:8ccbd963e74d | 130 | int Modem::readBuffer(char *buffer,int count, unsigned int timeOut) |
lawliet | 0:8ccbd963e74d | 131 | { |
lawliet | 0:8ccbd963e74d | 132 | int i = 0; |
lawliet | 0:8ccbd963e74d | 133 | timeCnt.start(); |
lawliet | 0:8ccbd963e74d | 134 | while(1) { |
lawliet | 0:8ccbd963e74d | 135 | while (serialModem.readable()) { |
lawliet | 0:8ccbd963e74d | 136 | char c = serialModem.getc(); |
lawliet | 0:8ccbd963e74d | 137 | buffer[i++] = c; |
lawliet | 0:8ccbd963e74d | 138 | if(i >= count)break; |
lawliet | 0:8ccbd963e74d | 139 | } |
lawliet | 0:8ccbd963e74d | 140 | if(i >= count)break; |
lawliet | 0:8ccbd963e74d | 141 | if(timeCnt.read() > timeOut) { |
lawliet | 0:8ccbd963e74d | 142 | timeCnt.stop(); |
lawliet | 0:8ccbd963e74d | 143 | timeCnt.reset(); |
lawliet | 0:8ccbd963e74d | 144 | break; |
lawliet | 0:8ccbd963e74d | 145 | } |
lawliet | 0:8ccbd963e74d | 146 | } |
lawliet | 0:8ccbd963e74d | 147 | return 0; |
lawliet | 0:8ccbd963e74d | 148 | } |
lawliet | 0:8ccbd963e74d | 149 | |
lawliet | 0:8ccbd963e74d | 150 | void Modem::cleanBuffer(char *buffer, int count) |
lawliet | 0:8ccbd963e74d | 151 | { |
lawliet | 0:8ccbd963e74d | 152 | for(int i=0; i < count; i++) { |
lawliet | 0:8ccbd963e74d | 153 | buffer[i] = '\0'; |
lawliet | 0:8ccbd963e74d | 154 | } |
lawliet | 0:8ccbd963e74d | 155 | } |
lawliet | 0:8ccbd963e74d | 156 | |
lawliet | 0:8ccbd963e74d | 157 | void Modem::sendCmd(const char* cmd) |
lawliet | 0:8ccbd963e74d | 158 | { |
lawliet | 0:8ccbd963e74d | 159 | serialModem.puts(cmd); |
lawliet | 0:8ccbd963e74d | 160 | } |
noahmilam | 10:8c55dfcc9a7c | 161 | void Modem::sendCmdResp(const char* cmd) |
noahmilam | 10:8c55dfcc9a7c | 162 | { |
noahmilam | 10:8c55dfcc9a7c | 163 | serialModem.puts(cmd); |
noahmilam | 10:8c55dfcc9a7c | 164 | } |
noahmilam | 10:8c55dfcc9a7c | 165 | void Modem::getResp() |
noahmilam | 10:8c55dfcc9a7c | 166 | { |
noahmilam | 10:8c55dfcc9a7c | 167 | char buffer[1000]; |
noahmilam | 10:8c55dfcc9a7c | 168 | int count = 0; |
noahmilam | 10:8c55dfcc9a7c | 169 | timeCnt.start(); |
noahmilam | 10:8c55dfcc9a7c | 170 | while(timeCnt.read() < 5) |
noahmilam | 10:8c55dfcc9a7c | 171 | { while(serialModem.readable()) { |
noahmilam | 10:8c55dfcc9a7c | 172 | char c = serialModem.getc(); |
noahmilam | 10:8c55dfcc9a7c | 173 | buffer[count++] = c; |
noahmilam | 10:8c55dfcc9a7c | 174 | } |
noahmilam | 10:8c55dfcc9a7c | 175 | } |
noahmilam | 10:8c55dfcc9a7c | 176 | timeCnt.stop(); |
noahmilam | 10:8c55dfcc9a7c | 177 | timeCnt.reset(); |
noahmilam | 10:8c55dfcc9a7c | 178 | buffer[count] = '\0'; |
DeWayneDennis | 11:045cb766d9a5 | 179 | printf("Response:--%s--\r\n",buffer); |
noahmilam | 10:8c55dfcc9a7c | 180 | for(int i = 0; i < count+2; i++) { |
noahmilam | 10:8c55dfcc9a7c | 181 | buffer[i] = NULL; |
noahmilam | 10:8c55dfcc9a7c | 182 | } |
noahmilam | 10:8c55dfcc9a7c | 183 | count = 0; |
noahmilam | 10:8c55dfcc9a7c | 184 | } |
noahmilam | 10:8c55dfcc9a7c | 185 | |
lawliet | 0:8ccbd963e74d | 186 | |
lawliet | 0:8ccbd963e74d | 187 | void Modem::sendATTest(void) |
lawliet | 0:8ccbd963e74d | 188 | { |
lawliet | 0:8ccbd963e74d | 189 | sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD); |
lawliet | 0:8ccbd963e74d | 190 | } |
lawliet | 0:8ccbd963e74d | 191 | |
lawliet | 0:8ccbd963e74d | 192 | bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout) |
lawliet | 0:8ccbd963e74d | 193 | { |
lawliet | 0:8ccbd963e74d | 194 | int sum=0; |
lawliet | 0:8ccbd963e74d | 195 | timeCnt.start(); |
lawliet | 0:8ccbd963e74d | 196 | |
lawliet | 0:8ccbd963e74d | 197 | while(1) { |
lawliet | 0:8ccbd963e74d | 198 | if(serialModem.readable()) { |
lawliet | 0:8ccbd963e74d | 199 | char c = serialModem.getc(); |
lawliet | 0:8ccbd963e74d | 200 | sum = (c==resp[sum]) ? sum+1 : 0; |
lawliet | 0:8ccbd963e74d | 201 | if(sum == len)break; |
lawliet | 0:8ccbd963e74d | 202 | } |
lawliet | 0:8ccbd963e74d | 203 | if(timeCnt.read() > timeout) { |
lawliet | 0:8ccbd963e74d | 204 | timeCnt.stop(); |
lawliet | 0:8ccbd963e74d | 205 | timeCnt.reset(); |
lawliet | 0:8ccbd963e74d | 206 | return false; |
lawliet | 0:8ccbd963e74d | 207 | } |
lawliet | 0:8ccbd963e74d | 208 | } |
lawliet | 0:8ccbd963e74d | 209 | timeCnt.stop(); |
lawliet | 0:8ccbd963e74d | 210 | timeCnt.reset(); |
lawliet | 0:8ccbd963e74d | 211 | |
lawliet | 0:8ccbd963e74d | 212 | return true; |
lawliet | 0:8ccbd963e74d | 213 | } |
lawliet | 0:8ccbd963e74d | 214 | |
lawliet | 0:8ccbd963e74d | 215 | int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type) |
lawliet | 0:8ccbd963e74d | 216 | { |
lawliet | 0:8ccbd963e74d | 217 | int len = strlen(resp); |
lawliet | 0:8ccbd963e74d | 218 | int sum=0; |
lawliet | 0:8ccbd963e74d | 219 | timeCnt.start(); |
lawliet | 0:8ccbd963e74d | 220 | |
lawliet | 0:8ccbd963e74d | 221 | while(1) { |
lawliet | 0:8ccbd963e74d | 222 | if(serialModem.readable()) { |
DeWayneDennis | 11:045cb766d9a5 | 223 | char c = serialModem.getc(); |
lawliet | 0:8ccbd963e74d | 224 | sum = (c==resp[sum]) ? sum+1 : 0; |
lawliet | 0:8ccbd963e74d | 225 | if(sum == len)break; |
lawliet | 0:8ccbd963e74d | 226 | } |
lawliet | 0:8ccbd963e74d | 227 | if(timeCnt.read() > timeout) { |
lawliet | 0:8ccbd963e74d | 228 | timeCnt.stop(); |
lawliet | 0:8ccbd963e74d | 229 | timeCnt.reset(); |
lawliet | 0:8ccbd963e74d | 230 | return -1; |
lawliet | 0:8ccbd963e74d | 231 | } |
lawliet | 0:8ccbd963e74d | 232 | } |
DeWayneDennis | 11:045cb766d9a5 | 233 | |
lawliet | 0:8ccbd963e74d | 234 | timeCnt.stop(); |
lawliet | 0:8ccbd963e74d | 235 | timeCnt.reset(); |
DeWayneDennis | 11:045cb766d9a5 | 236 | |
lawliet | 0:8ccbd963e74d | 237 | if(type == CMD) { |
lawliet | 0:8ccbd963e74d | 238 | while(serialModem.readable()) { |
lawliet | 0:8ccbd963e74d | 239 | char c = serialModem.getc(); |
lawliet | 0:8ccbd963e74d | 240 | } |
DeWayneDennis | 11:045cb766d9a5 | 241 | |
lawliet | 0:8ccbd963e74d | 242 | } |
lawliet | 0:8ccbd963e74d | 243 | return 0; |
lawliet | 0:8ccbd963e74d | 244 | } |
lawliet | 0:8ccbd963e74d | 245 | |
lawliet | 0:8ccbd963e74d | 246 | int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type) |
lawliet | 0:8ccbd963e74d | 247 | { |
lawliet | 0:8ccbd963e74d | 248 | sendCmd(data); |
lawliet | 0:8ccbd963e74d | 249 | return waitForResp(resp,timeout,type); |
lawliet | 0:8ccbd963e74d | 250 | } |