![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
t3
Fork of C027_SupportTest by
main.cpp@35:ead6b88cebeb, 2017-01-09 (annotated)
- Committer:
- PetriI2
- Date:
- Mon Jan 09 12:29:58 2017 +0000
- Revision:
- 35:ead6b88cebeb
- Parent:
- 34:bc82b26ec43b
t3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:4e3cb26f6019 | 1 | #include "mbed.h" |
mazgch | 9:26f694bc31b4 | 2 | |
mazgch | 19:f022ff746eb8 | 3 | //------------------------------------------------------------------------------------ |
mazgch | 11:b8505cbbd55c | 4 | /* This example was tested on C027-U20 and C027-G35 with the on board modem. |
mazgch | 11:b8505cbbd55c | 5 | |
mazgch | 18:50e6c4ed8a4a | 6 | Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON |
mazgch | 11:b8505cbbd55c | 7 | is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this |
mazgch | 11:b8505cbbd55c | 8 | configuration the following platforms were tested (it is likely that others |
mazgch | 11:b8505cbbd55c | 9 | will work as well) |
mazgch | 19:f022ff746eb8 | 10 | - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD) |
mazgch | 18:50e6c4ed8a4a | 11 | - NXP: LPC1549v2, LPC4088qsb |
mazgch | 18:50e6c4ed8a4a | 12 | - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F |
mazgch | 12:96c7b62c7aaf | 13 | - STM: NUCLEO-F401RE, NUCLEO-F030R8 |
mazgch | 18:50e6c4ed8a4a | 14 | mount resistors SB13/14 1k, SB62/63 0R |
mazgch | 11:b8505cbbd55c | 15 | */ |
mazgch | 19:f022ff746eb8 | 16 | #include "GPS.h" |
mazgch | 19:f022ff746eb8 | 17 | #include "MDM.h" |
mazgch | 19:f022ff746eb8 | 18 | //------------------------------------------------------------------------------------ |
mazgch | 19:f022ff746eb8 | 19 | // You need to configure these cellular modem / SIM parameters. |
mazgch | 19:f022ff746eb8 | 20 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
mazgch | 19:f022ff746eb8 | 21 | //------------------------------------------------------------------------------------ |
mazgch | 19:f022ff746eb8 | 22 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
PetriI2 | 34:bc82b26ec43b | 23 | #define SIMPIN "1234" |
mazgch | 19:f022ff746eb8 | 24 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
mazgch | 19:f022ff746eb8 | 25 | contract with the network operator. You can also try to look-up your settings in |
mazgch | 19:f022ff746eb8 | 26 | google: https://www.google.de/search?q=APN+list */ |
PetriI2 | 34:bc82b26ec43b | 27 | #define APN "internet" |
mazgch | 19:f022ff746eb8 | 28 | //! Set the user name for your APN, or NULL if not needed |
PetriI2 | 34:bc82b26ec43b | 29 | #define USERNAME "dna" |
mazgch | 19:f022ff746eb8 | 30 | //! Set the password for your APN, or NULL if not needed |
PetriI2 | 34:bc82b26ec43b | 31 | #define PASSWORD "internet" |
mazgch | 19:f022ff746eb8 | 32 | //------------------------------------------------------------------------------------ |
lawliet | 0:4e3cb26f6019 | 33 | |
msinig | 29:16f1037626e3 | 34 | //#define CELLOCATE |
msinig | 29:16f1037626e3 | 35 | |
PetriI2 | 34:bc82b26ec43b | 36 | AnalogIn temperature(A5); |
PetriI2 | 34:bc82b26ec43b | 37 | DigitalOut led1(LED1); |
PetriI2 | 34:bc82b26ec43b | 38 | |
PetriI2 | 35:ead6b88cebeb | 39 | DigitalOut ledD1(D10); //inverted |
PetriI2 | 35:ead6b88cebeb | 40 | DigitalOut ledD9(D11); //inverted |
PetriI2 | 35:ead6b88cebeb | 41 | DigitalOut ledD2(D12); |
PetriI2 | 35:ead6b88cebeb | 42 | DigitalOut ledD3(D13); |
PetriI2 | 35:ead6b88cebeb | 43 | DigitalOut ledD11(D9); //inverted |
PetriI2 | 35:ead6b88cebeb | 44 | |
PetriI2 | 35:ead6b88cebeb | 45 | DigitalIn S1(D5); |
PetriI2 | 35:ead6b88cebeb | 46 | DigitalIn S2(D3); |
PetriI2 | 35:ead6b88cebeb | 47 | DigitalIn S3(D7); |
PetriI2 | 35:ead6b88cebeb | 48 | DigitalIn S4(D8); |
PetriI2 | 35:ead6b88cebeb | 49 | DigitalIn S5(D2); |
PetriI2 | 35:ead6b88cebeb | 50 | |
PetriI2 | 35:ead6b88cebeb | 51 | DigitalIn A(D4); |
PetriI2 | 35:ead6b88cebeb | 52 | DigitalIn B(D6); |
PetriI2 | 35:ead6b88cebeb | 53 | |
PetriI2 | 35:ead6b88cebeb | 54 | /*another thread to read IOs*/ |
PetriI2 | 35:ead6b88cebeb | 55 | void btn_thread(void const *args) { |
PetriI2 | 35:ead6b88cebeb | 56 | |
PetriI2 | 35:ead6b88cebeb | 57 | //d4.mode(PullUp); |
PetriI2 | 35:ead6b88cebeb | 58 | |
PetriI2 | 35:ead6b88cebeb | 59 | while(true) { |
PetriI2 | 35:ead6b88cebeb | 60 | |
PetriI2 | 35:ead6b88cebeb | 61 | if(S1 == 1) { |
PetriI2 | 35:ead6b88cebeb | 62 | //printf("S1 OFF\r\n"); |
PetriI2 | 35:ead6b88cebeb | 63 | ledD11 = 1; |
PetriI2 | 35:ead6b88cebeb | 64 | } |
PetriI2 | 35:ead6b88cebeb | 65 | else { |
PetriI2 | 35:ead6b88cebeb | 66 | //printf("S1 ON\r\n"); |
PetriI2 | 35:ead6b88cebeb | 67 | ledD11 = 0; |
PetriI2 | 35:ead6b88cebeb | 68 | } |
PetriI2 | 35:ead6b88cebeb | 69 | |
PetriI2 | 35:ead6b88cebeb | 70 | if(S2 == 0) { |
PetriI2 | 35:ead6b88cebeb | 71 | printf("Down\r\n"); |
PetriI2 | 35:ead6b88cebeb | 72 | } |
PetriI2 | 35:ead6b88cebeb | 73 | |
PetriI2 | 35:ead6b88cebeb | 74 | if(S3 == 0) { |
PetriI2 | 35:ead6b88cebeb | 75 | printf("Right\r\n"); |
PetriI2 | 35:ead6b88cebeb | 76 | } |
PetriI2 | 35:ead6b88cebeb | 77 | |
PetriI2 | 35:ead6b88cebeb | 78 | if(S4 == 0) { |
PetriI2 | 35:ead6b88cebeb | 79 | printf("Up\r\n"); |
PetriI2 | 35:ead6b88cebeb | 80 | } |
PetriI2 | 35:ead6b88cebeb | 81 | |
PetriI2 | 35:ead6b88cebeb | 82 | if(S5 == 0) { |
PetriI2 | 35:ead6b88cebeb | 83 | printf("Left\r\n"); |
PetriI2 | 35:ead6b88cebeb | 84 | } |
PetriI2 | 35:ead6b88cebeb | 85 | |
PetriI2 | 35:ead6b88cebeb | 86 | if(A == 0) { |
PetriI2 | 35:ead6b88cebeb | 87 | ledD3 = 0; |
PetriI2 | 35:ead6b88cebeb | 88 | } |
PetriI2 | 35:ead6b88cebeb | 89 | else { |
PetriI2 | 35:ead6b88cebeb | 90 | ledD3=1; |
PetriI2 | 35:ead6b88cebeb | 91 | } |
PetriI2 | 35:ead6b88cebeb | 92 | |
PetriI2 | 35:ead6b88cebeb | 93 | if(B == 0) { |
PetriI2 | 35:ead6b88cebeb | 94 | printf("b0\r\n"); |
PetriI2 | 35:ead6b88cebeb | 95 | } |
PetriI2 | 35:ead6b88cebeb | 96 | |
PetriI2 | 35:ead6b88cebeb | 97 | wait(1); |
PetriI2 | 35:ead6b88cebeb | 98 | } |
PetriI2 | 35:ead6b88cebeb | 99 | } |
PetriI2 | 34:bc82b26ec43b | 100 | |
lawliet | 0:4e3cb26f6019 | 101 | int main(void) |
lawliet | 0:4e3cb26f6019 | 102 | { |
mazgch | 2:b77151f111a9 | 103 | int ret; |
mazgch | 19:f022ff746eb8 | 104 | #ifdef LARGE_DATA |
mazgch | 16:43f6de7bc38b | 105 | char buf[2048] = ""; |
mazgch | 17:c293780a40ac | 106 | #else |
mazgch | 17:c293780a40ac | 107 | char buf[512] = ""; |
mazgch | 17:c293780a40ac | 108 | #endif |
lawliet | 0:4e3cb26f6019 | 109 | |
PetriI2 | 34:bc82b26ec43b | 110 | char buffer[500]; |
PetriI2 | 34:bc82b26ec43b | 111 | char csrfMiddleware[40]={0}; |
PetriI2 | 34:bc82b26ec43b | 112 | char csrfMiddleware2[50]={0}; |
PetriI2 | 34:bc82b26ec43b | 113 | char* buff; |
PetriI2 | 34:bc82b26ec43b | 114 | |
PetriI2 | 34:bc82b26ec43b | 115 | unsigned int macAddr[6] = {0x00,0x1d,0xc9,0x1b,0x94,0x3d}; |
PetriI2 | 34:bc82b26ec43b | 116 | int tlsData[256]; |
PetriI2 | 34:bc82b26ec43b | 117 | unsigned int TLSdata[2] = {1000,2000}; |
PetriI2 | 34:bc82b26ec43b | 118 | float lat=65.3256, lon=27.462; |
PetriI2 | 34:bc82b26ec43b | 119 | unsigned int dataLen=0; |
PetriI2 | 34:bc82b26ec43b | 120 | int contentLenStr[10]; |
PetriI2 | 34:bc82b26ec43b | 121 | int len=0, opcode=0, sleepTime=0; |
PetriI2 | 34:bc82b26ec43b | 122 | char a[2]; |
PetriI2 | 34:bc82b26ec43b | 123 | char http_cmd[700] = {""}; |
PetriI2 | 34:bc82b26ec43b | 124 | |
PetriI2 | 34:bc82b26ec43b | 125 | double la = 0, lo = 0; |
PetriI2 | 34:bc82b26ec43b | 126 | bool mdmOk; |
PetriI2 | 35:ead6b88cebeb | 127 | |
PetriI2 | 35:ead6b88cebeb | 128 | |
PetriI2 | 35:ead6b88cebeb | 129 | /*start another thread to read IOs*/ |
PetriI2 | 35:ead6b88cebeb | 130 | Thread thread(btn_thread); |
PetriI2 | 35:ead6b88cebeb | 131 | |
PetriI2 | 35:ead6b88cebeb | 132 | //Set all leds ON |
PetriI2 | 35:ead6b88cebeb | 133 | ledD1 = 0; |
PetriI2 | 35:ead6b88cebeb | 134 | ledD9 = 0; |
PetriI2 | 35:ead6b88cebeb | 135 | ledD2 = 0; |
PetriI2 | 35:ead6b88cebeb | 136 | ledD3 = 0; |
PetriI2 | 35:ead6b88cebeb | 137 | ledD11 = 0; |
PetriI2 | 35:ead6b88cebeb | 138 | |
PetriI2 | 35:ead6b88cebeb | 139 | wait(2); //5s |
PetriI2 | 34:bc82b26ec43b | 140 | |
PetriI2 | 34:bc82b26ec43b | 141 | printf("Temperature1: %f \r\n", temperature.read()); |
PetriI2 | 34:bc82b26ec43b | 142 | printf("Temperature2: %3.3f \r\n", temperature.read()*100.0f); |
PetriI2 | 34:bc82b26ec43b | 143 | led1 = (temperature > 0.5f) ? 1 : 0; |
PetriI2 | 34:bc82b26ec43b | 144 | |
PetriI2 | 34:bc82b26ec43b | 145 | printf("Voltage1: 0x%04X \r\n", temperature.read_u16()); |
PetriI2 | 34:bc82b26ec43b | 146 | printf("Voltage2: %f \r\n", temperature.read_u16()*(3.3/65536)); |
PetriI2 | 34:bc82b26ec43b | 147 | //float voltage = (f*(3.3/4096.0)); |
PetriI2 | 35:ead6b88cebeb | 148 | |
PetriI2 | 35:ead6b88cebeb | 149 | //Set all leds OFF |
PetriI2 | 35:ead6b88cebeb | 150 | ledD1 = ~ledD1; |
PetriI2 | 35:ead6b88cebeb | 151 | ledD9 = ~ledD9; |
PetriI2 | 35:ead6b88cebeb | 152 | ledD2 = ~ledD2; |
PetriI2 | 35:ead6b88cebeb | 153 | ledD3 = ~ledD3; |
PetriI2 | 35:ead6b88cebeb | 154 | ledD11 = ~ledD11; |
PetriI2 | 35:ead6b88cebeb | 155 | |
PetriI2 | 34:bc82b26ec43b | 156 | |
mazgch | 19:f022ff746eb8 | 157 | // Create the GPS object |
mazgch | 19:f022ff746eb8 | 158 | #if 1 // use GPSI2C class |
mazgch | 19:f022ff746eb8 | 159 | GPSI2C gps; |
mazgch | 19:f022ff746eb8 | 160 | #else // or GPSSerial class |
mazgch | 19:f022ff746eb8 | 161 | GPSSerial gps; |
mazgch | 10:d2da2028a233 | 162 | #endif |
mazgch | 10:d2da2028a233 | 163 | // Create the modem object |
mazgch | 32:b838fcaba45e | 164 | MDMSerial mdm; // use mdm(D1,D0) if you connect the cellular shield to a C027 |
PetriI2 | 34:bc82b26ec43b | 165 | // mdm.setDebug(4); // enable this for debugging issues |
PetriI2 | 34:bc82b26ec43b | 166 | // mdm.setDebug(1); // enable this for debugging issues |
mazgch | 2:b77151f111a9 | 167 | // initialize the modem |
mazgch | 19:f022ff746eb8 | 168 | MDMParser::DevStatus devStatus = {}; |
mazgch | 19:f022ff746eb8 | 169 | MDMParser::NetStatus netStatus = {}; |
PetriI2 | 34:bc82b26ec43b | 170 | |
PetriI2 | 34:bc82b26ec43b | 171 | do { |
PetriI2 | 34:bc82b26ec43b | 172 | mdmOk = mdm.init(SIMPIN, &devStatus); |
PetriI2 | 34:bc82b26ec43b | 173 | wait(5); |
PetriI2 | 34:bc82b26ec43b | 174 | mdm.dumpDevStatus(&devStatus); |
PetriI2 | 34:bc82b26ec43b | 175 | |
PetriI2 | 34:bc82b26ec43b | 176 | printf("mdmOk status=%d\r\n", mdmOk); |
PetriI2 | 34:bc82b26ec43b | 177 | //wait(2); |
PetriI2 | 34:bc82b26ec43b | 178 | }while (!mdmOk); |
PetriI2 | 34:bc82b26ec43b | 179 | |
mazgch | 19:f022ff746eb8 | 180 | if (mdmOk) { |
mazgch | 20:52f0e5de8c3d | 181 | // wait until we are connected |
mazgch | 20:52f0e5de8c3d | 182 | mdmOk = mdm.registerNet(&netStatus); |
mazgch | 20:52f0e5de8c3d | 183 | mdm.dumpNetStatus(&netStatus); |
mazgch | 20:52f0e5de8c3d | 184 | } |
mazgch | 20:52f0e5de8c3d | 185 | if (mdmOk) |
mazgch | 20:52f0e5de8c3d | 186 | { |
PetriI2 | 35:ead6b88cebeb | 187 | ledD1 = 0; |
mazgch | 4:90ab1ec64b0e | 188 | // join the internet connection |
mazgch | 19:f022ff746eb8 | 189 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
mazgch | 32:b838fcaba45e | 190 | if (ip == NOIP) |
mazgch | 32:b838fcaba45e | 191 | printf("Not able to join network"); |
mazgch | 32:b838fcaba45e | 192 | else |
mazgch | 2:b77151f111a9 | 193 | { |
mazgch | 19:f022ff746eb8 | 194 | mdm.dumpIp(ip); |
mazgch | 19:f022ff746eb8 | 195 | printf("Make a Http Post Request\r\n"); |
mazgch | 4:90ab1ec64b0e | 196 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
PetriI2 | 34:bc82b26ec43b | 197 | printf("Socket created: %d\r\n", socket); |
mazgch | 4:90ab1ec64b0e | 198 | if (socket >= 0) |
mazgch | 2:b77151f111a9 | 199 | { |
PetriI2 | 34:bc82b26ec43b | 200 | mdm.socketSetBlocking(socket, 10000); |
PetriI2 | 34:bc82b26ec43b | 201 | //if (mdm.socketConnect(socket, "mbed.org", 80)) |
PetriI2 | 34:bc82b26ec43b | 202 | if (mdm.socketConnect(socket, "212.116.37.180", 81)) |
mazgch | 4:90ab1ec64b0e | 203 | { |
PetriI2 | 34:bc82b26ec43b | 204 | //const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; |
PetriI2 | 34:bc82b26ec43b | 205 | //mdm.socketSend(socket, http, sizeof(http)-1); |
PetriI2 | 34:bc82b26ec43b | 206 | |
PetriI2 | 34:bc82b26ec43b | 207 | char http_cmdg[100] = "GET /cloudmgr/sensor/data/"; |
PetriI2 | 34:bc82b26ec43b | 208 | strcat(http_cmdg, " HTTP/1.1"); |
PetriI2 | 34:bc82b26ec43b | 209 | strcat(http_cmdg, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 210 | strcat(http_cmdg, "Connection: keep-alive"); |
PetriI2 | 34:bc82b26ec43b | 211 | strcat(http_cmdg, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 212 | strcat(http_cmdg, "Host: 212.116.37.180:81"); |
PetriI2 | 34:bc82b26ec43b | 213 | strcat(http_cmdg, "\r\n\r\n"); |
PetriI2 | 34:bc82b26ec43b | 214 | strcat(http_cmdg, "\0"); |
PetriI2 | 34:bc82b26ec43b | 215 | int leng = strlen(http_cmdg); |
PetriI2 | 34:bc82b26ec43b | 216 | printf("Sending %d chars to server:\r\n%s\r\n", leng, http_cmdg); |
PetriI2 | 34:bc82b26ec43b | 217 | //sock.send_all(http_cmdg, leng); |
PetriI2 | 34:bc82b26ec43b | 218 | mdm.socketSend(socket, http_cmdg, leng); |
mazgch | 4:90ab1ec64b0e | 219 | |
PetriI2 | 34:bc82b26ec43b | 220 | ret = mdm.socketRecv(socket, buffer, sizeof(buffer)-1); |
PetriI2 | 34:bc82b26ec43b | 221 | if (ret > 0) { |
PetriI2 | 34:bc82b26ec43b | 222 | buffer[ret] = '\0'; |
PetriI2 | 34:bc82b26ec43b | 223 | printf("Socket Recv \"%*s\"\r\n", ret, buffer); |
PetriI2 | 34:bc82b26ec43b | 224 | if(ret > 100) { |
PetriI2 | 34:bc82b26ec43b | 225 | buff=strstr(buffer, "csrftoken="); |
PetriI2 | 34:bc82b26ec43b | 226 | strncpy(csrfMiddleware, &buff[10], 32); |
PetriI2 | 34:bc82b26ec43b | 227 | strncpy(csrfMiddleware2, &buff[0], 42); |
PetriI2 | 34:bc82b26ec43b | 228 | printf("Received csrfMiddleware= %s\r\n", csrfMiddleware); |
PetriI2 | 34:bc82b26ec43b | 229 | printf("Received csrfMiddleware2= %s\r\n", csrfMiddleware2); |
PetriI2 | 34:bc82b26ec43b | 230 | //break; |
PetriI2 | 34:bc82b26ec43b | 231 | } |
PetriI2 | 34:bc82b26ec43b | 232 | } |
PetriI2 | 34:bc82b26ec43b | 233 | |
PetriI2 | 34:bc82b26ec43b | 234 | /*Prepare data to send to server*/ |
PetriI2 | 34:bc82b26ec43b | 235 | printf("Prepare to POST\r\n"); |
PetriI2 | 34:bc82b26ec43b | 236 | sprintf((char *)tlsData, "csrfmiddlewaretoken=%s&serial=%x%x%x%x%x%x&value=%d&gpslat=%f&gpslon=%f", |
PetriI2 | 34:bc82b26ec43b | 237 | csrfMiddleware, macAddr[0],macAddr[1],macAddr[2],macAddr[3],macAddr[4],macAddr[5], 4096-TLSdata[1], lat, lon); //req_value for POST |
PetriI2 | 34:bc82b26ec43b | 238 | |
PetriI2 | 34:bc82b26ec43b | 239 | dataLen = strlen((char const*)tlsData); |
PetriI2 | 34:bc82b26ec43b | 240 | sprintf((char *)&contentLenStr[0],"%d", dataLen); |
PetriI2 | 34:bc82b26ec43b | 241 | memset(buffer, 0, sizeof(buffer)); |
PetriI2 | 34:bc82b26ec43b | 242 | |
PetriI2 | 34:bc82b26ec43b | 243 | /*Send a POST message to server*/ |
PetriI2 | 34:bc82b26ec43b | 244 | //char http_cmd[700] = "POST /cloudmgr/sensor/data/"; |
PetriI2 | 34:bc82b26ec43b | 245 | strcat(http_cmd, "POST /cloudmgr/sensor/data/"); |
PetriI2 | 34:bc82b26ec43b | 246 | strcat(http_cmd, " HTTP/1.1"); |
PetriI2 | 34:bc82b26ec43b | 247 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 248 | strcat(http_cmd, "Connection: keep-alive"); |
PetriI2 | 34:bc82b26ec43b | 249 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 250 | strcat(http_cmd, "Host: 212.116.37.180:81"); |
PetriI2 | 34:bc82b26ec43b | 251 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 252 | strcat(http_cmd, "Content-Type: application/x-www-form-urlencoded"); |
PetriI2 | 34:bc82b26ec43b | 253 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 254 | strcat(http_cmd, "Content-Length: "); |
PetriI2 | 34:bc82b26ec43b | 255 | strcat(http_cmd, (char const*)contentLenStr); |
PetriI2 | 34:bc82b26ec43b | 256 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 257 | strcat(http_cmd, "Cookie: "); |
PetriI2 | 34:bc82b26ec43b | 258 | strcat(http_cmd, csrfMiddleware2); |
PetriI2 | 34:bc82b26ec43b | 259 | strcat(http_cmd, "\r\n\r\n"); |
PetriI2 | 34:bc82b26ec43b | 260 | strcat(http_cmd, (char const*)tlsData); |
PetriI2 | 34:bc82b26ec43b | 261 | strcat(http_cmd, "\0"); |
PetriI2 | 34:bc82b26ec43b | 262 | len = strlen(http_cmd); |
PetriI2 | 34:bc82b26ec43b | 263 | printf("Sending %d chars to server:\n%s\r\n", len, http_cmd); |
PetriI2 | 34:bc82b26ec43b | 264 | //sock.send_all(http_cmd, len); |
PetriI2 | 34:bc82b26ec43b | 265 | mdm.socketSend(socket, http_cmd, len); |
PetriI2 | 34:bc82b26ec43b | 266 | ret=1; |
PetriI2 | 34:bc82b26ec43b | 267 | |
PetriI2 | 34:bc82b26ec43b | 268 | /*reveive answer message (sleeptime) from server*/ |
PetriI2 | 34:bc82b26ec43b | 269 | while (true) { |
PetriI2 | 34:bc82b26ec43b | 270 | ret = mdm.socketRecv(socket, buffer, sizeof(buffer)-1); |
PetriI2 | 34:bc82b26ec43b | 271 | if (ret <= 0) { |
PetriI2 | 34:bc82b26ec43b | 272 | printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
PetriI2 | 34:bc82b26ec43b | 273 | break; |
PetriI2 | 34:bc82b26ec43b | 274 | } |
PetriI2 | 34:bc82b26ec43b | 275 | buffer[ret] = '\0'; |
PetriI2 | 34:bc82b26ec43b | 276 | |
PetriI2 | 34:bc82b26ec43b | 277 | printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
PetriI2 | 34:bc82b26ec43b | 278 | |
PetriI2 | 34:bc82b26ec43b | 279 | if((strncmp((char const*)buffer, "200", 3))) { |
PetriI2 | 34:bc82b26ec43b | 280 | strncpy(a, &buffer[ret-1], 1); |
PetriI2 | 34:bc82b26ec43b | 281 | opcode = atoi(a); |
PetriI2 | 34:bc82b26ec43b | 282 | //printf("Received opcode= %d, %s\r\n", opcode, a); |
PetriI2 | 34:bc82b26ec43b | 283 | switch (opcode) { |
PetriI2 | 34:bc82b26ec43b | 284 | case 1 : |
PetriI2 | 34:bc82b26ec43b | 285 | sleepTime = 5; |
PetriI2 | 34:bc82b26ec43b | 286 | break; |
PetriI2 | 34:bc82b26ec43b | 287 | case 2 : |
PetriI2 | 34:bc82b26ec43b | 288 | sleepTime = 30; |
PetriI2 | 34:bc82b26ec43b | 289 | break; |
PetriI2 | 34:bc82b26ec43b | 290 | case 3 : |
PetriI2 | 34:bc82b26ec43b | 291 | sleepTime = 300; |
PetriI2 | 34:bc82b26ec43b | 292 | break; |
PetriI2 | 34:bc82b26ec43b | 293 | default : |
PetriI2 | 34:bc82b26ec43b | 294 | sleepTime = 5; |
PetriI2 | 34:bc82b26ec43b | 295 | break; |
PetriI2 | 34:bc82b26ec43b | 296 | } |
PetriI2 | 34:bc82b26ec43b | 297 | printf("Received sleeptime= %d\r\n", sleepTime); |
PetriI2 | 34:bc82b26ec43b | 298 | break; |
PetriI2 | 34:bc82b26ec43b | 299 | |
PetriI2 | 34:bc82b26ec43b | 300 | } |
PetriI2 | 34:bc82b26ec43b | 301 | else { |
PetriI2 | 34:bc82b26ec43b | 302 | break; |
PetriI2 | 34:bc82b26ec43b | 303 | } |
PetriI2 | 34:bc82b26ec43b | 304 | } |
PetriI2 | 34:bc82b26ec43b | 305 | |
mazgch | 4:90ab1ec64b0e | 306 | mdm.socketClose(socket); |
mazgch | 4:90ab1ec64b0e | 307 | } |
mazgch | 4:90ab1ec64b0e | 308 | mdm.socketFree(socket); |
mazgch | 4:90ab1ec64b0e | 309 | } |
mazgch | 4:90ab1ec64b0e | 310 | // disconnect |
mazgch | 4:90ab1ec64b0e | 311 | mdm.disconnect(); |
PetriI2 | 35:ead6b88cebeb | 312 | ledD1 = ~ledD1; |
mazgch | 2:b77151f111a9 | 313 | } |
PetriI2 | 34:bc82b26ec43b | 314 | |
mazgch | 10:d2da2028a233 | 315 | } |
PetriI2 | 34:bc82b26ec43b | 316 | |
PetriI2 | 34:bc82b26ec43b | 317 | |
mazgch | 30:062717f25e41 | 318 | |
mazgch | 19:f022ff746eb8 | 319 | printf("SMS and GPS Loop\r\n"); |
mazgch | 10:d2da2028a233 | 320 | char link[128] = ""; |
mazgch | 10:d2da2028a233 | 321 | unsigned int i = 0xFFFFFFFF; |
mazgch | 10:d2da2028a233 | 322 | const int wait = 100; |
mazgch | 10:d2da2028a233 | 323 | bool abort = false; |
PetriI2 | 34:bc82b26ec43b | 324 | |
mazgch | 11:b8505cbbd55c | 325 | //DigitalOut led(LED1); |
mazgch | 10:d2da2028a233 | 326 | while (!abort) { |
PetriI2 | 35:ead6b88cebeb | 327 | //printf("GPS Loop Start\r\n"); |
mazgch | 19:f022ff746eb8 | 328 | // led = !led; |
msinig | 29:16f1037626e3 | 329 | #ifndef CELLOCATE |
mazgch | 10:d2da2028a233 | 330 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) |
mazgch | 10:d2da2028a233 | 331 | { |
PetriI2 | 35:ead6b88cebeb | 332 | ledD9 = 0; |
mazgch | 10:d2da2028a233 | 333 | int len = LENGTH(ret); |
PetriI2 | 35:ead6b88cebeb | 334 | //printf("GPS len: %d\r\n", len); |
mazgch | 19:f022ff746eb8 | 335 | //printf("NMEA: %.*s\r\n", len-2, msg); |
mazgch | 19:f022ff746eb8 | 336 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) |
mazgch | 4:90ab1ec64b0e | 337 | { |
PetriI2 | 35:ead6b88cebeb | 338 | //printf("GPS len: %s\r\n", buf); |
mazgch | 27:fe3b383dd4ec | 339 | // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS |
mazgch | 27:fe3b383dd4ec | 340 | if ((buf[0] == '$') || buf[1] == 'G') { |
mazgch | 27:fe3b383dd4ec | 341 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
mazgch | 27:fe3b383dd4ec | 342 | if (_CHECK_TALKER("GLL")) { |
PetriI2 | 34:bc82b26ec43b | 343 | |
mazgch | 27:fe3b383dd4ec | 344 | char ch; |
mazgch | 27:fe3b383dd4ec | 345 | if (gps.getNmeaAngle(1,buf,len,la) && |
mazgch | 27:fe3b383dd4ec | 346 | gps.getNmeaAngle(3,buf,len,lo) && |
mazgch | 27:fe3b383dd4ec | 347 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') |
mazgch | 27:fe3b383dd4ec | 348 | { |
mazgch | 27:fe3b383dd4ec | 349 | printf("GPS Location: %.5f %.5f\r\n", la, lo); |
mazgch | 27:fe3b383dd4ec | 350 | sprintf(link, "I am here!\n" |
mazgch | 27:fe3b383dd4ec | 351 | "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
mazgch | 27:fe3b383dd4ec | 352 | } |
mazgch | 27:fe3b383dd4ec | 353 | } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) { |
mazgch | 27:fe3b383dd4ec | 354 | double a = 0; |
mazgch | 27:fe3b383dd4ec | 355 | if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] |
mazgch | 27:fe3b383dd4ec | 356 | printf("GPS Altitude: %.1f\r\n", a); |
mazgch | 27:fe3b383dd4ec | 357 | } else if (_CHECK_TALKER("VTG")) { |
mazgch | 27:fe3b383dd4ec | 358 | double s = 0; |
mazgch | 27:fe3b383dd4ec | 359 | if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] |
mazgch | 27:fe3b383dd4ec | 360 | printf("GPS Speed: %.1f\r\n", s); |
mazgch | 19:f022ff746eb8 | 361 | } |
mazgch | 4:90ab1ec64b0e | 362 | } |
mazgch | 4:90ab1ec64b0e | 363 | } |
PetriI2 | 35:ead6b88cebeb | 364 | ledD9 = ~ledD9; |
mazgch | 10:d2da2028a233 | 365 | } |
msinig | 29:16f1037626e3 | 366 | #endif |
PetriI2 | 34:bc82b26ec43b | 367 | |
mazgch | 19:f022ff746eb8 | 368 | if (mdmOk && (i++ == 5000/wait)) { |
mazgch | 10:d2da2028a233 | 369 | i = 0; |
PetriI2 | 35:ead6b88cebeb | 370 | ledD2 = 0; |
PetriI2 | 35:ead6b88cebeb | 371 | |
mazgch | 10:d2da2028a233 | 372 | // check the network status |
mazgch | 15:ea10b6cf8c85 | 373 | if (mdm.checkNetStatus(&netStatus)) { |
mazgch | 19:f022ff746eb8 | 374 | mdm.dumpNetStatus(&netStatus, fprintf, stdout); |
mazgch | 15:ea10b6cf8c85 | 375 | } |
mazgch | 10:d2da2028a233 | 376 | |
PetriI2 | 34:bc82b26ec43b | 377 | |
PetriI2 | 34:bc82b26ec43b | 378 | printf("Voltage A5: %f \r\n", temperature.read_u16()*(3.3/65536)); |
PetriI2 | 34:bc82b26ec43b | 379 | led1 = (temperature > 0.5f) ? 1 : 0; |
PetriI2 | 35:ead6b88cebeb | 380 | |
PetriI2 | 34:bc82b26ec43b | 381 | |
PetriI2 | 34:bc82b26ec43b | 382 | if (mdmOk) |
mazgch | 10:d2da2028a233 | 383 | { |
PetriI2 | 34:bc82b26ec43b | 384 | // join the internet connection |
PetriI2 | 34:bc82b26ec43b | 385 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
PetriI2 | 34:bc82b26ec43b | 386 | if (ip == NOIP) |
PetriI2 | 34:bc82b26ec43b | 387 | printf("Not able to join network"); |
PetriI2 | 34:bc82b26ec43b | 388 | else |
PetriI2 | 34:bc82b26ec43b | 389 | { |
PetriI2 | 35:ead6b88cebeb | 390 | ledD1 = 0; |
PetriI2 | 34:bc82b26ec43b | 391 | mdm.dumpIp(ip); |
PetriI2 | 34:bc82b26ec43b | 392 | printf("Make a Http Post Request\r\n"); |
PetriI2 | 34:bc82b26ec43b | 393 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
PetriI2 | 34:bc82b26ec43b | 394 | printf("Socket created: %d\r\n", socket); |
PetriI2 | 34:bc82b26ec43b | 395 | if (socket >= 0) |
PetriI2 | 34:bc82b26ec43b | 396 | { |
PetriI2 | 34:bc82b26ec43b | 397 | mdm.socketSetBlocking(socket, 10000); |
PetriI2 | 34:bc82b26ec43b | 398 | //if (mdm.socketConnect(socket, "mbed.org", 80)) |
PetriI2 | 34:bc82b26ec43b | 399 | if (mdm.socketConnect(socket, "212.116.37.180", 81)) |
PetriI2 | 34:bc82b26ec43b | 400 | { |
PetriI2 | 34:bc82b26ec43b | 401 | |
PetriI2 | 34:bc82b26ec43b | 402 | printf("Prepare to POST\r\n"); |
PetriI2 | 34:bc82b26ec43b | 403 | sprintf((char *)tlsData, "csrfmiddlewaretoken=%s&serial=%x%x%x%x%x%x&value=%d&gpslat=%f&gpslon=%f", |
PetriI2 | 34:bc82b26ec43b | 404 | csrfMiddleware, macAddr[0],macAddr[1],macAddr[2],macAddr[3],macAddr[4],macAddr[5], 65536-temperature.read_u16(), la, lo); //req_value for POST |
PetriI2 | 34:bc82b26ec43b | 405 | |
PetriI2 | 34:bc82b26ec43b | 406 | dataLen = strlen((char const*)tlsData); |
PetriI2 | 34:bc82b26ec43b | 407 | sprintf((char *)&contentLenStr[0],"%d", dataLen); |
PetriI2 | 34:bc82b26ec43b | 408 | memset(buffer, 0, sizeof(buffer)); |
PetriI2 | 34:bc82b26ec43b | 409 | memset(http_cmd, 0, sizeof(http_cmd)); |
PetriI2 | 34:bc82b26ec43b | 410 | |
PetriI2 | 34:bc82b26ec43b | 411 | /*Send a POST message to server*/ |
PetriI2 | 34:bc82b26ec43b | 412 | strcat(http_cmd, "POST /cloudmgr/sensor/data/"); |
PetriI2 | 34:bc82b26ec43b | 413 | strcat(http_cmd, " HTTP/1.1"); |
PetriI2 | 34:bc82b26ec43b | 414 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 415 | strcat(http_cmd, "Connection: keep-alive"); |
PetriI2 | 34:bc82b26ec43b | 416 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 417 | strcat(http_cmd, "Host: 212.116.37.180:81"); |
PetriI2 | 34:bc82b26ec43b | 418 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 419 | strcat(http_cmd, "Content-Type: application/x-www-form-urlencoded"); |
PetriI2 | 34:bc82b26ec43b | 420 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 421 | strcat(http_cmd, "Content-Length: "); |
PetriI2 | 34:bc82b26ec43b | 422 | strcat(http_cmd, (char const*)contentLenStr); |
PetriI2 | 34:bc82b26ec43b | 423 | strcat(http_cmd, "\r\n"); |
PetriI2 | 34:bc82b26ec43b | 424 | strcat(http_cmd, "Cookie: "); |
PetriI2 | 34:bc82b26ec43b | 425 | strcat(http_cmd, csrfMiddleware2); |
PetriI2 | 34:bc82b26ec43b | 426 | strcat(http_cmd, "\r\n\r\n"); |
PetriI2 | 34:bc82b26ec43b | 427 | strcat(http_cmd, (char const*)tlsData); |
PetriI2 | 34:bc82b26ec43b | 428 | strcat(http_cmd, "\0"); |
PetriI2 | 34:bc82b26ec43b | 429 | len = strlen(http_cmd); |
PetriI2 | 34:bc82b26ec43b | 430 | printf("Sending %d chars to server:\r\n%s\r\n", len, http_cmd); |
PetriI2 | 34:bc82b26ec43b | 431 | //sock.send_all(http_cmd, len); |
PetriI2 | 34:bc82b26ec43b | 432 | mdm.socketSend(socket, http_cmd, len); |
PetriI2 | 34:bc82b26ec43b | 433 | ret=1; |
PetriI2 | 34:bc82b26ec43b | 434 | |
PetriI2 | 34:bc82b26ec43b | 435 | /*reveive answer message (sleeptime) from server*/ |
PetriI2 | 34:bc82b26ec43b | 436 | while (true) { |
PetriI2 | 34:bc82b26ec43b | 437 | ret = mdm.socketRecv(socket, buffer, sizeof(buffer)-1); |
PetriI2 | 34:bc82b26ec43b | 438 | if (ret <= 0) { |
PetriI2 | 34:bc82b26ec43b | 439 | printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
PetriI2 | 34:bc82b26ec43b | 440 | break; |
PetriI2 | 34:bc82b26ec43b | 441 | } |
PetriI2 | 34:bc82b26ec43b | 442 | buffer[ret] = '\0'; |
PetriI2 | 34:bc82b26ec43b | 443 | |
PetriI2 | 34:bc82b26ec43b | 444 | printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
PetriI2 | 34:bc82b26ec43b | 445 | |
PetriI2 | 34:bc82b26ec43b | 446 | if((strncmp((char const*)buffer, "200", 3))) { |
PetriI2 | 34:bc82b26ec43b | 447 | strncpy(a, &buffer[ret-1], 1); |
PetriI2 | 34:bc82b26ec43b | 448 | opcode = atoi(a); |
PetriI2 | 34:bc82b26ec43b | 449 | //printf("Received opcode= %d, %s\r\n", opcode, a); |
PetriI2 | 34:bc82b26ec43b | 450 | switch (opcode) { |
PetriI2 | 34:bc82b26ec43b | 451 | case 1 : |
PetriI2 | 34:bc82b26ec43b | 452 | sleepTime = 5; |
PetriI2 | 34:bc82b26ec43b | 453 | break; |
PetriI2 | 34:bc82b26ec43b | 454 | case 2 : |
PetriI2 | 34:bc82b26ec43b | 455 | sleepTime = 30; |
PetriI2 | 34:bc82b26ec43b | 456 | break; |
PetriI2 | 34:bc82b26ec43b | 457 | case 3 : |
PetriI2 | 34:bc82b26ec43b | 458 | sleepTime = 300; |
PetriI2 | 34:bc82b26ec43b | 459 | break; |
PetriI2 | 34:bc82b26ec43b | 460 | default : |
PetriI2 | 34:bc82b26ec43b | 461 | sleepTime = 5; |
PetriI2 | 34:bc82b26ec43b | 462 | break; |
PetriI2 | 34:bc82b26ec43b | 463 | } |
PetriI2 | 34:bc82b26ec43b | 464 | printf("Received sleeptime= %d\r\n", sleepTime); |
PetriI2 | 34:bc82b26ec43b | 465 | break; |
PetriI2 | 34:bc82b26ec43b | 466 | |
PetriI2 | 34:bc82b26ec43b | 467 | } |
PetriI2 | 34:bc82b26ec43b | 468 | else { |
PetriI2 | 34:bc82b26ec43b | 469 | break; |
PetriI2 | 34:bc82b26ec43b | 470 | } |
PetriI2 | 34:bc82b26ec43b | 471 | } |
PetriI2 | 34:bc82b26ec43b | 472 | |
PetriI2 | 34:bc82b26ec43b | 473 | mdm.socketClose(socket); |
PetriI2 | 34:bc82b26ec43b | 474 | } |
PetriI2 | 34:bc82b26ec43b | 475 | mdm.socketFree(socket); |
PetriI2 | 34:bc82b26ec43b | 476 | } |
PetriI2 | 34:bc82b26ec43b | 477 | // disconnect |
PetriI2 | 34:bc82b26ec43b | 478 | mdm.disconnect(); |
PetriI2 | 35:ead6b88cebeb | 479 | ledD1 = ~ledD1; |
mazgch | 4:90ab1ec64b0e | 480 | } |
PetriI2 | 35:ead6b88cebeb | 481 | ledD2 = ~ledD2; |
mazgch | 4:90ab1ec64b0e | 482 | } |
PetriI2 | 34:bc82b26ec43b | 483 | |
PetriI2 | 34:bc82b26ec43b | 484 | |
PetriI2 | 34:bc82b26ec43b | 485 | |
mazgch | 9:26f694bc31b4 | 486 | } |
mazgch | 31:a07d0f76dc81 | 487 | #ifdef RTOS_H |
mazgch | 31:a07d0f76dc81 | 488 | Thread::wait(wait); |
mazgch | 31:a07d0f76dc81 | 489 | #else |
mazgch | 31:a07d0f76dc81 | 490 | ::wait_ms(wait); |
mazgch | 31:a07d0f76dc81 | 491 | #endif |
lawliet | 0:4e3cb26f6019 | 492 | } |
mazgch | 19:f022ff746eb8 | 493 | gps.powerOff(); |
mazgch | 10:d2da2028a233 | 494 | mdm.powerOff(); |
lawliet | 0:4e3cb26f6019 | 495 | return 0; |
lawliet | 0:4e3cb26f6019 | 496 | } |