hello
Dependencies: C027_SupportMMA7455
main.cpp@0:c6e1fe84dc9a, 2017-12-12 (annotated)
- Committer:
- david8251
- Date:
- Tue Dec 12 12:32:16 2017 +0000
- Revision:
- 0:c6e1fe84dc9a
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
david8251 | 0:c6e1fe84dc9a | 1 | #include "mbed.h" |
david8251 | 0:c6e1fe84dc9a | 2 | #include "MMA7455.h" |
david8251 | 0:c6e1fe84dc9a | 3 | #include "MDM.h" |
david8251 | 0:c6e1fe84dc9a | 4 | #include "GPS.h" |
david8251 | 0:c6e1fe84dc9a | 5 | |
david8251 | 0:c6e1fe84dc9a | 6 | #define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253 |
david8251 | 0:c6e1fe84dc9a | 7 | |
david8251 | 0:c6e1fe84dc9a | 8 | #define SIMPIN "1922" |
david8251 | 0:c6e1fe84dc9a | 9 | |
david8251 | 0:c6e1fe84dc9a | 10 | #define APN NULL |
david8251 | 0:c6e1fe84dc9a | 11 | //! Set the user name for your APN, or NULL if not needed |
david8251 | 0:c6e1fe84dc9a | 12 | #define USERNAME NULL |
david8251 | 0:c6e1fe84dc9a | 13 | //! Set the password for your APN, or NULL if not needed |
david8251 | 0:c6e1fe84dc9a | 14 | #define PASSWORD NULL |
david8251 | 0:c6e1fe84dc9a | 15 | |
david8251 | 0:c6e1fe84dc9a | 16 | |
david8251 | 0:c6e1fe84dc9a | 17 | Serial wind(P0_2, P0_3); |
david8251 | 0:c6e1fe84dc9a | 18 | MMA7455 acc(p32,p31); |
david8251 | 0:c6e1fe84dc9a | 19 | AnalogIn adc(p16); |
david8251 | 0:c6e1fe84dc9a | 20 | |
david8251 | 0:c6e1fe84dc9a | 21 | |
david8251 | 0:c6e1fe84dc9a | 22 | void task_wind(); |
david8251 | 0:c6e1fe84dc9a | 23 | void task_MMA(); |
david8251 | 0:c6e1fe84dc9a | 24 | void task_GPS(); |
david8251 | 0:c6e1fe84dc9a | 25 | void task_extend(); |
david8251 | 0:c6e1fe84dc9a | 26 | int task_3G(); |
david8251 | 0:c6e1fe84dc9a | 27 | |
david8251 | 0:c6e1fe84dc9a | 28 | |
david8251 | 0:c6e1fe84dc9a | 29 | double a,b,c,f,g,h; |
david8251 | 0:c6e1fe84dc9a | 30 | float d; |
david8251 | 0:c6e1fe84dc9a | 31 | int e; |
david8251 | 0:c6e1fe84dc9a | 32 | char str[90]; |
david8251 | 0:c6e1fe84dc9a | 33 | char str1[90]; |
david8251 | 0:c6e1fe84dc9a | 34 | char str2[90]; |
david8251 | 0:c6e1fe84dc9a | 35 | |
david8251 | 0:c6e1fe84dc9a | 36 | char str3[90]; |
david8251 | 0:c6e1fe84dc9a | 37 | char str4[90]; |
david8251 | 0:c6e1fe84dc9a | 38 | char str5[90]; |
david8251 | 0:c6e1fe84dc9a | 39 | char str6[90]; |
david8251 | 0:c6e1fe84dc9a | 40 | char str7[90]; |
david8251 | 0:c6e1fe84dc9a | 41 | |
david8251 | 0:c6e1fe84dc9a | 42 | Timer timer; |
david8251 | 0:c6e1fe84dc9a | 43 | Timer timer2; |
david8251 | 0:c6e1fe84dc9a | 44 | |
david8251 | 0:c6e1fe84dc9a | 45 | struct tm t; |
david8251 | 0:c6e1fe84dc9a | 46 | struct tm t1; |
david8251 | 0:c6e1fe84dc9a | 47 | int main(void) |
david8251 | 0:c6e1fe84dc9a | 48 | { |
david8251 | 0:c6e1fe84dc9a | 49 | t.tm_sec = 37; // 0-59 |
david8251 | 0:c6e1fe84dc9a | 50 | t.tm_min = 58; // 0-59 |
david8251 | 0:c6e1fe84dc9a | 51 | t.tm_hour = 23; // 0-23 |
david8251 | 0:c6e1fe84dc9a | 52 | t.tm_mday = 28; // 1-31 |
david8251 | 0:c6e1fe84dc9a | 53 | t.tm_mon = 1; // 0-11 |
david8251 | 0:c6e1fe84dc9a | 54 | t.tm_year = 117; // year since 1900 |
david8251 | 0:c6e1fe84dc9a | 55 | |
david8251 | 0:c6e1fe84dc9a | 56 | t1.tm_sec = 37; // 0-59 |
david8251 | 0:c6e1fe84dc9a | 57 | t1.tm_min = 58; // 0-59 |
david8251 | 0:c6e1fe84dc9a | 58 | t1.tm_hour = 23; // 0-23 |
david8251 | 0:c6e1fe84dc9a | 59 | t1.tm_mday = 28; // 1-31 |
david8251 | 0:c6e1fe84dc9a | 60 | t1.tm_mon = 1; // 0-11 |
david8251 | 0:c6e1fe84dc9a | 61 | t1.tm_year = 117; // year since 1900 |
david8251 | 0:c6e1fe84dc9a | 62 | |
david8251 | 0:c6e1fe84dc9a | 63 | time_t seconds = mktime(&t); |
david8251 | 0:c6e1fe84dc9a | 64 | set_time(seconds); |
david8251 | 0:c6e1fe84dc9a | 65 | |
david8251 | 0:c6e1fe84dc9a | 66 | time_t second = mktime(&t1); |
david8251 | 0:c6e1fe84dc9a | 67 | set_time(second); |
david8251 | 0:c6e1fe84dc9a | 68 | |
david8251 | 0:c6e1fe84dc9a | 69 | timer.start(); |
david8251 | 0:c6e1fe84dc9a | 70 | timer2.start(); |
david8251 | 0:c6e1fe84dc9a | 71 | |
david8251 | 0:c6e1fe84dc9a | 72 | acc.setMode(MMA7455::ModeMeasurement); |
david8251 | 0:c6e1fe84dc9a | 73 | acc.setRange(MMA7455::Range_2g); |
david8251 | 0:c6e1fe84dc9a | 74 | acc.calibrate(); |
david8251 | 0:c6e1fe84dc9a | 75 | |
david8251 | 0:c6e1fe84dc9a | 76 | while(1){ |
david8251 | 0:c6e1fe84dc9a | 77 | if(timer.read()>3){ |
david8251 | 0:c6e1fe84dc9a | 78 | for(int k=0;k<89;k++) |
david8251 | 0:c6e1fe84dc9a | 79 | { |
david8251 | 0:c6e1fe84dc9a | 80 | str[k]='\0'; |
david8251 | 0:c6e1fe84dc9a | 81 | } |
david8251 | 0:c6e1fe84dc9a | 82 | task_wind(); |
david8251 | 0:c6e1fe84dc9a | 83 | task_GPS(); |
david8251 | 0:c6e1fe84dc9a | 84 | task_3G(); |
david8251 | 0:c6e1fe84dc9a | 85 | timer.reset(); |
david8251 | 0:c6e1fe84dc9a | 86 | } |
david8251 | 0:c6e1fe84dc9a | 87 | if(timer2.read()> 10){ |
david8251 | 0:c6e1fe84dc9a | 88 | for(int k=0;k<89;k++) |
david8251 | 0:c6e1fe84dc9a | 89 | { |
david8251 | 0:c6e1fe84dc9a | 90 | str[k]='\0'; |
david8251 | 0:c6e1fe84dc9a | 91 | } |
david8251 | 0:c6e1fe84dc9a | 92 | task_MMA(); |
david8251 | 0:c6e1fe84dc9a | 93 | task_extend(); |
david8251 | 0:c6e1fe84dc9a | 94 | task_3G(); |
david8251 | 0:c6e1fe84dc9a | 95 | timer2.reset(); |
david8251 | 0:c6e1fe84dc9a | 96 | } |
david8251 | 0:c6e1fe84dc9a | 97 | } |
david8251 | 0:c6e1fe84dc9a | 98 | } |
david8251 | 0:c6e1fe84dc9a | 99 | |
david8251 | 0:c6e1fe84dc9a | 100 | void task_MMA(){ |
david8251 | 0:c6e1fe84dc9a | 101 | |
david8251 | 0:c6e1fe84dc9a | 102 | |
david8251 | 0:c6e1fe84dc9a | 103 | char filename[64]; |
david8251 | 0:c6e1fe84dc9a | 104 | int32_t x,y,z; |
david8251 | 0:c6e1fe84dc9a | 105 | double x1,y1,z1; |
david8251 | 0:c6e1fe84dc9a | 106 | double xg,yg,zg; |
david8251 | 0:c6e1fe84dc9a | 107 | double xd,yd,zd; |
david8251 | 0:c6e1fe84dc9a | 108 | acc.read(x,y,z); |
david8251 | 0:c6e1fe84dc9a | 109 | |
david8251 | 0:c6e1fe84dc9a | 110 | xg=x*0.016; //實際g值 |
david8251 | 0:c6e1fe84dc9a | 111 | yg=y*0.016; //實際g值 |
david8251 | 0:c6e1fe84dc9a | 112 | zg=z*0.016; //實際g值 |
david8251 | 0:c6e1fe84dc9a | 113 | |
david8251 | 0:c6e1fe84dc9a | 114 | x1=xg/(sqrt(pow(yg,2.0)+pow(zg,2.0))); |
david8251 | 0:c6e1fe84dc9a | 115 | y1=yg/(sqrt(pow(xg,2.0)+pow(zg,2.0))); |
david8251 | 0:c6e1fe84dc9a | 116 | z1=(sqrt(pow(yg,2.0)+pow(xg,2.0)))/zg; |
david8251 | 0:c6e1fe84dc9a | 117 | |
david8251 | 0:c6e1fe84dc9a | 118 | xd=atan(x1); //徑度 |
david8251 | 0:c6e1fe84dc9a | 119 | yd=atan(y1); //徑度 |
david8251 | 0:c6e1fe84dc9a | 120 | zd=atan(z1); //徑度 |
david8251 | 0:c6e1fe84dc9a | 121 | |
david8251 | 0:c6e1fe84dc9a | 122 | a=xd*180.0/PI; |
david8251 | 0:c6e1fe84dc9a | 123 | b=yd*180.0/PI; |
david8251 | 0:c6e1fe84dc9a | 124 | c=zd*180.0/PI; |
david8251 | 0:c6e1fe84dc9a | 125 | |
david8251 | 0:c6e1fe84dc9a | 126 | sprintf(str,"%.2f\t",a); |
david8251 | 0:c6e1fe84dc9a | 127 | sprintf(str1,"%.2f\t",b); |
david8251 | 0:c6e1fe84dc9a | 128 | sprintf(str2,"%.2f",c); |
david8251 | 0:c6e1fe84dc9a | 129 | |
david8251 | 0:c6e1fe84dc9a | 130 | strcat(str,str1); |
david8251 | 0:c6e1fe84dc9a | 131 | strcat(str,str2); |
david8251 | 0:c6e1fe84dc9a | 132 | |
david8251 | 0:c6e1fe84dc9a | 133 | puts(str); |
david8251 | 0:c6e1fe84dc9a | 134 | |
david8251 | 0:c6e1fe84dc9a | 135 | printf("xd: %.2f ",xd*180.0/PI); |
david8251 | 0:c6e1fe84dc9a | 136 | printf("yd: %.2f ",yd*180.0/PI); |
david8251 | 0:c6e1fe84dc9a | 137 | printf("zd: %.2f ",zd*180.0/PI); |
david8251 | 0:c6e1fe84dc9a | 138 | printf("\r\n\n"); |
david8251 | 0:c6e1fe84dc9a | 139 | } |
david8251 | 0:c6e1fe84dc9a | 140 | |
david8251 | 0:c6e1fe84dc9a | 141 | void task_wind(){ |
david8251 | 0:c6e1fe84dc9a | 142 | char filename[64]; |
david8251 | 0:c6e1fe84dc9a | 143 | char command[17]; |
david8251 | 0:c6e1fe84dc9a | 144 | int i = 0; |
david8251 | 0:c6e1fe84dc9a | 145 | int j=1; |
david8251 | 0:c6e1fe84dc9a | 146 | float windSpeed; |
david8251 | 0:c6e1fe84dc9a | 147 | int windDirection; |
david8251 | 0:c6e1fe84dc9a | 148 | for(j=1;j<17;j++) |
david8251 | 0:c6e1fe84dc9a | 149 | { |
david8251 | 0:c6e1fe84dc9a | 150 | command[i]= wind.getc(); |
david8251 | 0:c6e1fe84dc9a | 151 | i++; |
david8251 | 0:c6e1fe84dc9a | 152 | } |
david8251 | 0:c6e1fe84dc9a | 153 | printf("\r\n"); |
david8251 | 0:c6e1fe84dc9a | 154 | if(command[0]==0x3A){ |
david8251 | 0:c6e1fe84dc9a | 155 | windSpeed=((command[8]-48)*10000+(command[9]-48)*1000+(command[10]-48)*100+(command[12]-48)*10+(command[13]-48))/100.0; |
david8251 | 0:c6e1fe84dc9a | 156 | windDirection= (command[4]-48)*100+(command[5]-48)*10+(command[6]-48); |
david8251 | 0:c6e1fe84dc9a | 157 | printf("The Wind Speed is %.2f (m/s)\r\n",windSpeed); |
david8251 | 0:c6e1fe84dc9a | 158 | printf("The Wind Direction is %d",windDirection); |
david8251 | 0:c6e1fe84dc9a | 159 | d=windSpeed; |
david8251 | 0:c6e1fe84dc9a | 160 | e=windDirection; |
david8251 | 0:c6e1fe84dc9a | 161 | sprintf(str3,"%.2f\t",d); |
david8251 | 0:c6e1fe84dc9a | 162 | sprintf(str4,"%.2f\t",e); |
david8251 | 0:c6e1fe84dc9a | 163 | strcat(str,str3); |
david8251 | 0:c6e1fe84dc9a | 164 | strcat(str,str4); |
david8251 | 0:c6e1fe84dc9a | 165 | puts(str); |
david8251 | 0:c6e1fe84dc9a | 166 | } |
david8251 | 0:c6e1fe84dc9a | 167 | printf("\r\n"); |
david8251 | 0:c6e1fe84dc9a | 168 | } |
david8251 | 0:c6e1fe84dc9a | 169 | |
david8251 | 0:c6e1fe84dc9a | 170 | void task_GPS(){ |
david8251 | 0:c6e1fe84dc9a | 171 | int ret; |
david8251 | 0:c6e1fe84dc9a | 172 | char filename[64]; |
david8251 | 0:c6e1fe84dc9a | 173 | #ifdef LARGE_DATA |
david8251 | 0:c6e1fe84dc9a | 174 | char buf[2048] = ""; |
david8251 | 0:c6e1fe84dc9a | 175 | #else |
david8251 | 0:c6e1fe84dc9a | 176 | char buf[512] = ""; |
david8251 | 0:c6e1fe84dc9a | 177 | #endif |
david8251 | 0:c6e1fe84dc9a | 178 | #if 1 |
david8251 | 0:c6e1fe84dc9a | 179 | GPSI2C gps; |
david8251 | 0:c6e1fe84dc9a | 180 | #else |
david8251 | 0:c6e1fe84dc9a | 181 | GPSSerial gps; |
david8251 | 0:c6e1fe84dc9a | 182 | #endif |
david8251 | 0:c6e1fe84dc9a | 183 | int count=0; |
david8251 | 0:c6e1fe84dc9a | 184 | |
david8251 | 0:c6e1fe84dc9a | 185 | while (count==0) { |
david8251 | 0:c6e1fe84dc9a | 186 | #ifndef CELLOCATE |
david8251 | 0:c6e1fe84dc9a | 187 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) |
david8251 | 0:c6e1fe84dc9a | 188 | { |
david8251 | 0:c6e1fe84dc9a | 189 | int len = LENGTH(ret); |
david8251 | 0:c6e1fe84dc9a | 190 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) |
david8251 | 0:c6e1fe84dc9a | 191 | { |
david8251 | 0:c6e1fe84dc9a | 192 | if ((buf[0] == '$') || buf[1] == 'G') { |
david8251 | 0:c6e1fe84dc9a | 193 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
david8251 | 0:c6e1fe84dc9a | 194 | if (_CHECK_TALKER("GLL")) { |
david8251 | 0:c6e1fe84dc9a | 195 | double la = 0, lo = 0; |
david8251 | 0:c6e1fe84dc9a | 196 | char ch; |
david8251 | 0:c6e1fe84dc9a | 197 | if (gps.getNmeaAngle(1,buf,len,la) && |
david8251 | 0:c6e1fe84dc9a | 198 | gps.getNmeaAngle(3,buf,len,lo) && |
david8251 | 0:c6e1fe84dc9a | 199 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') |
david8251 | 0:c6e1fe84dc9a | 200 | { |
david8251 | 0:c6e1fe84dc9a | 201 | f=la; |
david8251 | 0:c6e1fe84dc9a | 202 | g=lo; |
david8251 | 0:c6e1fe84dc9a | 203 | sprintf(str5,"%.2f\t",f); |
david8251 | 0:c6e1fe84dc9a | 204 | sprintf(str6,"%.2f\t",g); |
david8251 | 0:c6e1fe84dc9a | 205 | strcat(str,str5); |
david8251 | 0:c6e1fe84dc9a | 206 | strcat(str,str6); |
david8251 | 0:c6e1fe84dc9a | 207 | puts(str); |
david8251 | 0:c6e1fe84dc9a | 208 | printf("GPS Location: %.5f %.5f\r\n", la, lo); |
david8251 | 0:c6e1fe84dc9a | 209 | } |
david8251 | 0:c6e1fe84dc9a | 210 | } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) { |
david8251 | 0:c6e1fe84dc9a | 211 | double a = 0; |
david8251 | 0:c6e1fe84dc9a | 212 | if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] |
david8251 | 0:c6e1fe84dc9a | 213 | { |
david8251 | 0:c6e1fe84dc9a | 214 | printf("GPS Altitude: %.1f\r\n", a); |
david8251 | 0:c6e1fe84dc9a | 215 | } |
david8251 | 0:c6e1fe84dc9a | 216 | } |
david8251 | 0:c6e1fe84dc9a | 217 | } |
david8251 | 0:c6e1fe84dc9a | 218 | count++; |
david8251 | 0:c6e1fe84dc9a | 219 | } |
david8251 | 0:c6e1fe84dc9a | 220 | } |
david8251 | 0:c6e1fe84dc9a | 221 | if((ret = gps.getMessage(buf, sizeof(buf))) < 0) |
david8251 | 0:c6e1fe84dc9a | 222 | { |
david8251 | 0:c6e1fe84dc9a | 223 | |
david8251 | 0:c6e1fe84dc9a | 224 | } |
david8251 | 0:c6e1fe84dc9a | 225 | #endif |
david8251 | 0:c6e1fe84dc9a | 226 | } |
david8251 | 0:c6e1fe84dc9a | 227 | } |
david8251 | 0:c6e1fe84dc9a | 228 | |
david8251 | 0:c6e1fe84dc9a | 229 | void task_extend(){ |
david8251 | 0:c6e1fe84dc9a | 230 | |
david8251 | 0:c6e1fe84dc9a | 231 | h=adc.read_u16()*3.3/65535/0.29; |
david8251 | 0:c6e1fe84dc9a | 232 | |
david8251 | 0:c6e1fe84dc9a | 233 | sprintf(str7,"%.2f\t",h); |
david8251 | 0:c6e1fe84dc9a | 234 | strcat(str,str7); |
david8251 | 0:c6e1fe84dc9a | 235 | puts(str); |
david8251 | 0:c6e1fe84dc9a | 236 | } |
david8251 | 0:c6e1fe84dc9a | 237 | |
david8251 | 0:c6e1fe84dc9a | 238 | int task_3G(){ |
david8251 | 0:c6e1fe84dc9a | 239 | |
david8251 | 0:c6e1fe84dc9a | 240 | int ret; |
david8251 | 0:c6e1fe84dc9a | 241 | #ifdef LARGE_DATA |
david8251 | 0:c6e1fe84dc9a | 242 | char buf[2048] = ""; |
david8251 | 0:c6e1fe84dc9a | 243 | #else |
david8251 | 0:c6e1fe84dc9a | 244 | char buf[512] = ""; |
david8251 | 0:c6e1fe84dc9a | 245 | #endif |
david8251 | 0:c6e1fe84dc9a | 246 | |
david8251 | 0:c6e1fe84dc9a | 247 | MDMSerial mdm; |
david8251 | 0:c6e1fe84dc9a | 248 | MDMParser::DevStatus devStatus = {}; |
david8251 | 0:c6e1fe84dc9a | 249 | MDMParser::NetStatus netStatus = {}; |
david8251 | 0:c6e1fe84dc9a | 250 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
david8251 | 0:c6e1fe84dc9a | 251 | mdm.dumpDevStatus(&devStatus); |
david8251 | 0:c6e1fe84dc9a | 252 | |
david8251 | 0:c6e1fe84dc9a | 253 | |
david8251 | 0:c6e1fe84dc9a | 254 | if (mdmOk) { |
david8251 | 0:c6e1fe84dc9a | 255 | #if 0 |
david8251 | 0:c6e1fe84dc9a | 256 | // file system API |
david8251 | 0:c6e1fe84dc9a | 257 | const char* filename = "File"; |
david8251 | 0:c6e1fe84dc9a | 258 | char buf[] = "Hello World"; |
david8251 | 0:c6e1fe84dc9a | 259 | printf("writeFile \"%s\"\r\n", buf); |
david8251 | 0:c6e1fe84dc9a | 260 | if (mdm.writeFile(filename, buf, sizeof(buf))) |
david8251 | 0:c6e1fe84dc9a | 261 | { |
david8251 | 0:c6e1fe84dc9a | 262 | memset(buf, 0, sizeof(buf)); |
david8251 | 0:c6e1fe84dc9a | 263 | int len = mdm.readFile(filename, buf, sizeof(buf)); |
david8251 | 0:c6e1fe84dc9a | 264 | if (len >= 0) |
david8251 | 0:c6e1fe84dc9a | 265 | printf("readFile %d \"%.*s\"\r\n", len, len, buf); |
david8251 | 0:c6e1fe84dc9a | 266 | mdm.delFile(filename); |
david8251 | 0:c6e1fe84dc9a | 267 | } |
david8251 | 0:c6e1fe84dc9a | 268 | #endif |
david8251 | 0:c6e1fe84dc9a | 269 | mdmOk = mdm.registerNet(&netStatus); |
david8251 | 0:c6e1fe84dc9a | 270 | mdm.dumpNetStatus(&netStatus); |
david8251 | 0:c6e1fe84dc9a | 271 | } |
david8251 | 0:c6e1fe84dc9a | 272 | if (mdmOk) |
david8251 | 0:c6e1fe84dc9a | 273 | { |
david8251 | 0:c6e1fe84dc9a | 274 | // join the internet connection |
david8251 | 0:c6e1fe84dc9a | 275 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
david8251 | 0:c6e1fe84dc9a | 276 | if (ip == NOIP) |
david8251 | 0:c6e1fe84dc9a | 277 | printf("Not able to join network"); |
david8251 | 0:c6e1fe84dc9a | 278 | else |
david8251 | 0:c6e1fe84dc9a | 279 | { |
david8251 | 0:c6e1fe84dc9a | 280 | mdm.dumpIp(ip); |
david8251 | 0:c6e1fe84dc9a | 281 | printf("Make a Http Post Request\r\n"); |
david8251 | 0:c6e1fe84dc9a | 282 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
david8251 | 0:c6e1fe84dc9a | 283 | if (socket >= 0) |
david8251 | 0:c6e1fe84dc9a | 284 | { |
david8251 | 0:c6e1fe84dc9a | 285 | mdm.socketSetBlocking(socket, 10000); |
david8251 | 0:c6e1fe84dc9a | 286 | if (mdm.socketConnect(socket, "140.118.206.213", 555)) |
david8251 | 0:c6e1fe84dc9a | 287 | { |
david8251 | 0:c6e1fe84dc9a | 288 | mdm.socketSend(socket, str, sizeof(str)-1); |
david8251 | 0:c6e1fe84dc9a | 289 | |
david8251 | 0:c6e1fe84dc9a | 290 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
david8251 | 0:c6e1fe84dc9a | 291 | if (ret > 0) |
david8251 | 0:c6e1fe84dc9a | 292 | printf("Socket Recv \"%*s\"\r\n", ret, buf); |
david8251 | 0:c6e1fe84dc9a | 293 | mdm.socketClose(socket); |
david8251 | 0:c6e1fe84dc9a | 294 | } |
david8251 | 0:c6e1fe84dc9a | 295 | mdm.socketFree(socket); |
david8251 | 0:c6e1fe84dc9a | 296 | } |
david8251 | 0:c6e1fe84dc9a | 297 | |
david8251 | 0:c6e1fe84dc9a | 298 | int port = 7; |
david8251 | 0:c6e1fe84dc9a | 299 | const char* host = "echo.u-blox.com"; |
david8251 | 0:c6e1fe84dc9a | 300 | MDMParser::IP ip = mdm.gethostbyname(host); |
david8251 | 0:c6e1fe84dc9a | 301 | char data[] = "\r\nxxx Socket Hello World\r\n" |
david8251 | 0:c6e1fe84dc9a | 302 | #ifdef LARGE_DATA |
david8251 | 0:c6e1fe84dc9a | 303 | "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 304 | "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 305 | "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 306 | "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 307 | "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 308 | |
david8251 | 0:c6e1fe84dc9a | 309 | "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 310 | "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 311 | "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 312 | "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 313 | "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 314 | |
david8251 | 0:c6e1fe84dc9a | 315 | "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 316 | "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 317 | "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 318 | "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 319 | "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 320 | |
david8251 | 0:c6e1fe84dc9a | 321 | "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 322 | "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 323 | "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 324 | "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 325 | "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
david8251 | 0:c6e1fe84dc9a | 326 | #endif |
david8251 | 0:c6e1fe84dc9a | 327 | "End\r\n"; |
david8251 | 0:c6e1fe84dc9a | 328 | |
david8251 | 0:c6e1fe84dc9a | 329 | printf("Testing TCP sockets with ECHO server\r\n"); |
david8251 | 0:c6e1fe84dc9a | 330 | socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
david8251 | 0:c6e1fe84dc9a | 331 | if (socket >= 0) |
david8251 | 0:c6e1fe84dc9a | 332 | { |
david8251 | 0:c6e1fe84dc9a | 333 | mdm.socketSetBlocking(socket, 10000); |
david8251 | 0:c6e1fe84dc9a | 334 | if (mdm.socketConnect(socket, host, port)) { |
david8251 | 0:c6e1fe84dc9a | 335 | memcpy(data, "\r\nTCP", 5); |
david8251 | 0:c6e1fe84dc9a | 336 | ret = mdm.socketSend(socket, data, sizeof(data)-1); |
david8251 | 0:c6e1fe84dc9a | 337 | if (ret == sizeof(data)-1) { |
david8251 | 0:c6e1fe84dc9a | 338 | printf("Socket Send %d \"%s\"\r\n", ret, data); |
david8251 | 0:c6e1fe84dc9a | 339 | } |
david8251 | 0:c6e1fe84dc9a | 340 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
david8251 | 0:c6e1fe84dc9a | 341 | if (ret >= 0) { |
david8251 | 0:c6e1fe84dc9a | 342 | printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf); |
david8251 | 0:c6e1fe84dc9a | 343 | } |
david8251 | 0:c6e1fe84dc9a | 344 | mdm.socketClose(socket); |
david8251 | 0:c6e1fe84dc9a | 345 | } |
david8251 | 0:c6e1fe84dc9a | 346 | mdm.socketFree(socket); |
david8251 | 0:c6e1fe84dc9a | 347 | } |
david8251 | 0:c6e1fe84dc9a | 348 | |
david8251 | 0:c6e1fe84dc9a | 349 | printf("Testing UDP sockets with ECHO server\r\n"); |
david8251 | 0:c6e1fe84dc9a | 350 | socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port); |
david8251 | 0:c6e1fe84dc9a | 351 | if (socket >= 0) |
david8251 | 0:c6e1fe84dc9a | 352 | { |
david8251 | 0:c6e1fe84dc9a | 353 | mdm.socketSetBlocking(socket, 10000); |
david8251 | 0:c6e1fe84dc9a | 354 | memcpy(data, "\r\nUDP", 5); |
david8251 | 0:c6e1fe84dc9a | 355 | ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1); |
david8251 | 0:c6e1fe84dc9a | 356 | if (ret == sizeof(data)-1) { |
david8251 | 0:c6e1fe84dc9a | 357 | printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data); |
david8251 | 0:c6e1fe84dc9a | 358 | } |
david8251 | 0:c6e1fe84dc9a | 359 | ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1); |
david8251 | 0:c6e1fe84dc9a | 360 | if (ret >= 0) { |
david8251 | 0:c6e1fe84dc9a | 361 | printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf); |
david8251 | 0:c6e1fe84dc9a | 362 | } |
david8251 | 0:c6e1fe84dc9a | 363 | mdm.socketFree(socket); |
david8251 | 0:c6e1fe84dc9a | 364 | } |
david8251 | 0:c6e1fe84dc9a | 365 | |
david8251 | 0:c6e1fe84dc9a | 366 | // disconnect |
david8251 | 0:c6e1fe84dc9a | 367 | mdm.disconnect(); |
david8251 | 0:c6e1fe84dc9a | 368 | } |
david8251 | 0:c6e1fe84dc9a | 369 | |
david8251 | 0:c6e1fe84dc9a | 370 | const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" |
david8251 | 0:c6e1fe84dc9a | 371 | printf("Ussd Send Command %s\r\n", ussd); |
david8251 | 0:c6e1fe84dc9a | 372 | ret = mdm.ussdCommand(ussd, buf); |
david8251 | 0:c6e1fe84dc9a | 373 | if (ret > 0) |
david8251 | 0:c6e1fe84dc9a | 374 | printf("Ussd Got Answer: \"%s\"\r\n", buf); |
david8251 | 0:c6e1fe84dc9a | 375 | } |
david8251 | 0:c6e1fe84dc9a | 376 | #ifdef CELLOCATE |
david8251 | 0:c6e1fe84dc9a | 377 | const int sensorMask = 3; // Hybrid: GNSS + CellLocate |
david8251 | 0:c6e1fe84dc9a | 378 | const int timeoutMargin = 5; // seconds |
david8251 | 0:c6e1fe84dc9a | 379 | const int submitPeriod = 60; // 1 minutes in seconds |
david8251 | 0:c6e1fe84dc9a | 380 | const int targetAccuracy = 1; // meters |
david8251 | 0:c6e1fe84dc9a | 381 | unsigned int j = submitPeriod * 1000/wait; |
david8251 | 0:c6e1fe84dc9a | 382 | bool cellLocWait = false; |
david8251 | 0:c6e1fe84dc9a | 383 | MDMParser::CellLocData loc; |
david8251 | 0:c6e1fe84dc9a | 384 | |
david8251 | 0:c6e1fe84dc9a | 385 | if (!mdm.cellLocSrvHttp("TOKEN")) |
david8251 | 0:c6e1fe84dc9a | 386 | mdm.cellLocSrvUdp(); |
david8251 | 0:c6e1fe84dc9a | 387 | mdm.cellLocConfigSensor(1); // Deep scan mode |
david8251 | 0:c6e1fe84dc9a | 388 | |
david8251 | 0:c6e1fe84dc9a | 389 | #endif |
david8251 | 0:c6e1fe84dc9a | 390 | mdm.powerOff(); |
david8251 | 0:c6e1fe84dc9a | 391 | return 0; |
david8251 | 0:c6e1fe84dc9a | 392 | |
david8251 | 0:c6e1fe84dc9a | 393 | } |
david8251 | 0:c6e1fe84dc9a | 394 | |
david8251 | 0:c6e1fe84dc9a | 395 |