пропажа слешей

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Fri Jan 08 12:20:48 2021 +0000
Revision:
11:57fa27cb533e
Parent:
10:51960145754a
Child:
12:7fe416cdac08
08.01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:0279e8c1f111 1 #include "stm32f103c8t6.h"
hudakz 0:0279e8c1f111 2 #include "mbed.h"
spin7ion 6:70d0218c2a28 3 #include "DS1820.h"
spin7ion 6:70d0218c2a28 4 #include "MLX90614.h"
spin7ion 6:70d0218c2a28 5 #include "config.h"
spin7ion 6:70d0218c2a28 6 #include "ATCmdParser.h"
spin7ion 6:70d0218c2a28 7 #include "UARTSerial.h"
spin7ion 6:70d0218c2a28 8 #include "Watchdog.h"
spin7ion 6:70d0218c2a28 9
spin7ion 6:70d0218c2a28 10 Watchdog wd;
spin7ion 6:70d0218c2a28 11 DigitalOut myled(PC_13);
astartes 11:57fa27cb533e 12 DigitalOut simPWR(PA_8); // (active low level) PWRKEY pin to power on or off module
spin7ion 6:70d0218c2a28 13
spin7ion 6:70d0218c2a28 14 //SIM7000
spin7ion 6:70d0218c2a28 15 UARTSerial *_serial;
spin7ion 6:70d0218c2a28 16 ATCmdParser *_parser;
spin7ion 10:51960145754a 17 int rssiDB,rxQual;
spin7ion 10:51960145754a 18
astartes 11:57fa27cb533e 19 bool from_sleep = false;
astartes 11:57fa27cb533e 20
astartes 11:57fa27cb533e 21
astartes 11:57fa27cb533e 22 // ID
astartes 11:57fa27cb533e 23
astartes 11:57fa27cb533e 24 char device_id[] = {'M','I','E','M','H','S','E','-','T','E','S','T','\0'};
spin7ion 6:70d0218c2a28 25
spin7ion 6:70d0218c2a28 26 //Termometers
spin7ion 6:70d0218c2a28 27 OneWire oneWire(PIN_ONEWIRE);
spin7ion 6:70d0218c2a28 28 const int SENSORS_COUNT = 10;
spin7ion 6:70d0218c2a28 29 DS1820* ds1820[SENSORS_COUNT];
spin7ion 6:70d0218c2a28 30 int sensors_found = 0;
spin7ion 6:70d0218c2a28 31 const char sensorsOrder[]={4,2,5,8,0,3,6,1,9,7};
spin7ion 6:70d0218c2a28 32 float stickTemperatures[SENSORS_COUNT];
spin7ion 6:70d0218c2a28 33
spin7ion 6:70d0218c2a28 34 int i=0;
spin7ion 6:70d0218c2a28 35
spin7ion 6:70d0218c2a28 36 //IR termometer
spin7ion 6:70d0218c2a28 37 I2C i2c(PIN_SDA, PIN_SCL); //sda,scl
spin7ion 6:70d0218c2a28 38 MLX90614 thermometer(&i2c);
spin7ion 6:70d0218c2a28 39 float IRtemp;
hudakz 0:0279e8c1f111 40
astartes 11:57fa27cb533e 41 // GPS
astartes 11:57fa27cb533e 42 int Fix_st;
astartes 11:57fa27cb533e 43
astartes 11:57fa27cb533e 44 float B_l;
astartes 11:57fa27cb533e 45 float L_l;
astartes 11:57fa27cb533e 46 float Alt;
astartes 11:57fa27cb533e 47
astartes 11:57fa27cb533e 48 bool cold_start = 0;
astartes 11:57fa27cb533e 49 int step_p = 0;
spin7ion 6:70d0218c2a28 50 #if DEBUG_PC
spin7ion 6:70d0218c2a28 51 Serial pc(PIN_TX, PIN_RX); // TX, RX
spin7ion 6:70d0218c2a28 52 #endif
spin7ion 6:70d0218c2a28 53
spin7ion 6:70d0218c2a28 54 int index;
spin7ion 6:70d0218c2a28 55 char bufferString[2048];
spin7ion 6:70d0218c2a28 56
spin7ion 6:70d0218c2a28 57 int h_time, m_time, s_time;
spin7ion 6:70d0218c2a28 58 int fq, nst, fix, date; // fix quality, Number of satellites being tracked, 3D fix
astartes 11:57fa27cb533e 59 //float latitude, longitude, timefix, speed, altitude;
spin7ion 6:70d0218c2a28 60
spin7ion 6:70d0218c2a28 61 char state=STATE_INIT;
spin7ion 6:70d0218c2a28 62 int sleepTimer=0;
spin7ion 6:70d0218c2a28 63 int fixTries=0;
astartes 11:57fa27cb533e 64 float a = 66;
astartes 11:57fa27cb533e 65 void waste_time(int x)
astartes 11:57fa27cb533e 66 {
astartes 11:57fa27cb533e 67
astartes 11:57fa27cb533e 68 for(int i; i < 10000000; i++)
astartes 11:57fa27cb533e 69 {
astartes 11:57fa27cb533e 70 for(int k; k < 10000000 * x * 5; k++)
astartes 11:57fa27cb533e 71 {
astartes 11:57fa27cb533e 72 a = a / 33;
astartes 11:57fa27cb533e 73 a = a * 33;
astartes 11:57fa27cb533e 74 }
astartes 11:57fa27cb533e 75 }
astartes 11:57fa27cb533e 76
astartes 11:57fa27cb533e 77
astartes 11:57fa27cb533e 78
astartes 11:57fa27cb533e 79 }
astartes 11:57fa27cb533e 80
hudakz 0:0279e8c1f111 81
spin7ion 6:70d0218c2a28 82 void parseTime (float timeval)
spin7ion 6:70d0218c2a28 83 {
spin7ion 6:70d0218c2a28 84 //format utc time to beijing time,add 8 time zone
spin7ion 6:70d0218c2a28 85 float time = timeval + 80000.00f;
spin7ion 6:70d0218c2a28 86 h_time = int(time) / 10000;
spin7ion 6:70d0218c2a28 87 m_time = (int(time) % 10000) / 100;
spin7ion 6:70d0218c2a28 88 s_time = int(time) % 100;
spin7ion 6:70d0218c2a28 89 }
spin7ion 6:70d0218c2a28 90
hudakz 0:0279e8c1f111 91
spin7ion 6:70d0218c2a28 92 bool checkIfOk() {
spin7ion 6:70d0218c2a28 93 if(_parser->recv("OK")) {
spin7ion 6:70d0218c2a28 94 #if DEBUG_PC
spin7ion 6:70d0218c2a28 95 pc.printf("Done\r\n");
spin7ion 6:70d0218c2a28 96 #endif
spin7ion 6:70d0218c2a28 97
spin7ion 6:70d0218c2a28 98 return true;
spin7ion 6:70d0218c2a28 99 } else {
spin7ion 6:70d0218c2a28 100 #if DEBUG_PC
spin7ion 6:70d0218c2a28 101 pc.printf("Fail\r\n");
spin7ion 6:70d0218c2a28 102 #endif
spin7ion 6:70d0218c2a28 103
spin7ion 6:70d0218c2a28 104 return false;
spin7ion 6:70d0218c2a28 105 }
spin7ion 6:70d0218c2a28 106 }
astartes 11:57fa27cb533e 107 void blink_fast()
astartes 11:57fa27cb533e 108 {
astartes 11:57fa27cb533e 109 for(i=0;i<5;i++)
astartes 11:57fa27cb533e 110 {
astartes 11:57fa27cb533e 111 myled = 1;
astartes 11:57fa27cb533e 112 wait(0.5);
astartes 11:57fa27cb533e 113 myled = 0;
astartes 11:57fa27cb533e 114 wait(0.5);
astartes 11:57fa27cb533e 115 myled = 1;
astartes 11:57fa27cb533e 116 }
astartes 11:57fa27cb533e 117 }
astartes 11:57fa27cb533e 118 void blink_slow()
astartes 11:57fa27cb533e 119 {
astartes 11:57fa27cb533e 120 for(i=0;i<5;i++)
astartes 11:57fa27cb533e 121 {
astartes 11:57fa27cb533e 122 myled = 1;
astartes 11:57fa27cb533e 123 wait(1);
astartes 11:57fa27cb533e 124 myled = 0;
astartes 11:57fa27cb533e 125 wait(1);
astartes 11:57fa27cb533e 126 myled = 1;
astartes 11:57fa27cb533e 127 }
astartes 11:57fa27cb533e 128 }
astartes 11:57fa27cb533e 129
astartes 11:57fa27cb533e 130
spin7ion 6:70d0218c2a28 131
spin7ion 6:70d0218c2a28 132 bool enableGPS(bool powerUp) {
spin7ion 10:51960145754a 133 #if DEBUG_PC
astartes 11:57fa27cb533e 134 pc.printf("Powering GPS %s:\r\n",powerUp?"up":"down");
spin7ion 10:51960145754a 135 #endif
spin7ion 6:70d0218c2a28 136 if(powerUp){
spin7ion 6:70d0218c2a28 137 _parser->send("AT+CGNSPWR=1"); //GPS power on
spin7ion 6:70d0218c2a28 138 } else {
spin7ion 6:70d0218c2a28 139 _parser->send("AT+CGNSPWR=0"); //GPS power off
spin7ion 6:70d0218c2a28 140 }
astartes 11:57fa27cb533e 141 wait(0.5);
spin7ion 10:51960145754a 142 return checkIfOk();
spin7ion 10:51960145754a 143 }
spin7ion 10:51960145754a 144
spin7ion 10:51960145754a 145 bool enableRF(bool powerUp){
spin7ion 10:51960145754a 146 #if DEBUG_PC
astartes 11:57fa27cb533e 147 pc.printf("Powering RF %s: \r\n",powerUp?"up":"down");
spin7ion 10:51960145754a 148 #endif
spin7ion 10:51960145754a 149 if(powerUp){
astartes 11:57fa27cb533e 150 _parser->send("AT+CFUN=1");
spin7ion 10:51960145754a 151 } else {
spin7ion 10:51960145754a 152 _parser->send("AT+CFUN=0"); //GPS power off
spin7ion 10:51960145754a 153 }
spin7ion 6:70d0218c2a28 154 return checkIfOk();
spin7ion 6:70d0218c2a28 155 }
spin7ion 6:70d0218c2a28 156
astartes 11:57fa27cb533e 157 bool setPowerSavingMode()
astartes 11:57fa27cb533e 158 {
astartes 11:57fa27cb533e 159 _parser->send("AT+CPSMS=1");//power save on
astartes 11:57fa27cb533e 160 wait(0.2);
spin7ion 10:51960145754a 161 return checkIfOk();
spin7ion 10:51960145754a 162 }
spin7ion 10:51960145754a 163
spin7ion 6:70d0218c2a28 164 bool setSatSystems(){
spin7ion 10:51960145754a 165 #if DEBUG_PC
spin7ion 10:51960145754a 166 pc.printf("Setting sats:");
spin7ion 10:51960145754a 167 #endif
spin7ion 6:70d0218c2a28 168 _parser->send("AT+CGNSMOD=1,1,1,1");
spin7ion 6:70d0218c2a28 169 return checkIfOk();
spin7ion 6:70d0218c2a28 170 }
spin7ion 6:70d0218c2a28 171
astartes 11:57fa27cb533e 172 bool getSignalQuality()
astartes 11:57fa27cb533e 173 {
spin7ion 10:51960145754a 174 _parser->send("AT+CSQ");
spin7ion 10:51960145754a 175
spin7ion 10:51960145754a 176 if(_parser->recv("+CSQ: %d,%d", &rssiDB,&rxQual) && _parser->recv("OK")) {
spin7ion 10:51960145754a 177 return true;
spin7ion 10:51960145754a 178 }
spin7ion 10:51960145754a 179 return false;
spin7ion 10:51960145754a 180 }
spin7ion 10:51960145754a 181
spin7ion 10:51960145754a 182 bool setAPN() {
astartes 11:57fa27cb533e 183 _parser->send("AT+CNACT=1,\"iot\"");
spin7ion 10:51960145754a 184 return checkIfOk();
spin7ion 10:51960145754a 185 }
spin7ion 10:51960145754a 186
spin7ion 10:51960145754a 187 bool disconnectNetwork() {
spin7ion 10:51960145754a 188 _parser->send("AT+CNACT=0");
spin7ion 10:51960145754a 189 return checkIfOk();
spin7ion 10:51960145754a 190 }
spin7ion 10:51960145754a 191
astartes 11:57fa27cb533e 192 int initSIM()
astartes 11:57fa27cb533e 193 {
astartes 11:57fa27cb533e 194 fixTries=0;
astartes 11:57fa27cb533e 195 while(fixTries<COMD_EXE_TRIES && !enableRF(1))
astartes 11:57fa27cb533e 196 {
astartes 11:57fa27cb533e 197
astartes 11:57fa27cb533e 198 wait(2);
astartes 11:57fa27cb533e 199 wd.Service();
astartes 11:57fa27cb533e 200 fixTries++;
astartes 11:57fa27cb533e 201 if (fixTries >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 202 {
astartes 11:57fa27cb533e 203 return 0;
astartes 11:57fa27cb533e 204 }
astartes 11:57fa27cb533e 205 }
astartes 11:57fa27cb533e 206
astartes 11:57fa27cb533e 207 fixTries=0;
astartes 11:57fa27cb533e 208 while(fixTries<COMD_EXE_TRIES && !setSatSystems())
astartes 11:57fa27cb533e 209 {
astartes 11:57fa27cb533e 210
astartes 11:57fa27cb533e 211 wait(2);
astartes 11:57fa27cb533e 212 wd.Service();
astartes 11:57fa27cb533e 213 fixTries++;
astartes 11:57fa27cb533e 214 if (fixTries >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 215 {
astartes 11:57fa27cb533e 216 return 0;
astartes 11:57fa27cb533e 217 }
astartes 11:57fa27cb533e 218 }
spin7ion 10:51960145754a 219 wd.Service();
astartes 11:57fa27cb533e 220
astartes 11:57fa27cb533e 221
astartes 11:57fa27cb533e 222
astartes 11:57fa27cb533e 223 return true;
spin7ion 10:51960145754a 224 }
spin7ion 10:51960145754a 225
spin7ion 6:70d0218c2a28 226 bool getGPS() {
spin7ion 10:51960145754a 227 wd.Service();
astartes 11:57fa27cb533e 228 char GNSSrunstatus_c[2];
astartes 11:57fa27cb533e 229 char Fixstatus_c[2];
astartes 11:57fa27cb533e 230 char UTCdatetime_c[19];
astartes 11:57fa27cb533e 231 char latitude_c[11];
astartes 11:57fa27cb533e 232 char logitude_c[12];
astartes 11:57fa27cb533e 233 char altitude_c[9];
astartes 11:57fa27cb533e 234 char speedOTG_c[7];
astartes 11:57fa27cb533e 235 char course_c[7];
astartes 11:57fa27cb533e 236 char fixmode_c[2];
astartes 11:57fa27cb533e 237 char reserved_1[1];
astartes 11:57fa27cb533e 238 char HDOP_c[5];
astartes 11:57fa27cb533e 239 char PDOP_c[5];
astartes 11:57fa27cb533e 240 char VDOP_c[5];
astartes 11:57fa27cb533e 241 char reserved_2[1];
astartes 11:57fa27cb533e 242 char satellitesinview_c[3];
astartes 11:57fa27cb533e 243 char GNSSsatellitesused_c[3];
astartes 11:57fa27cb533e 244 char GLONASSsatellitesused_c[3];
astartes 11:57fa27cb533e 245 char reserved_3[1];
astartes 11:57fa27cb533e 246 char cn0max_c[3];
astartes 11:57fa27cb533e 247 char HPA_c[7];
astartes 11:57fa27cb533e 248 char VPA_c[7];
astartes 11:57fa27cb533e 249
astartes 11:57fa27cb533e 250 const int numChars = 120; // spec says up to 94 characters
astartes 11:57fa27cb533e 251 char receivedChars[ numChars ];
astartes 11:57fa27cb533e 252 receivedChars[0] = 0;
astartes 11:57fa27cb533e 253
astartes 11:57fa27cb533e 254
astartes 11:57fa27cb533e 255
astartes 11:57fa27cb533e 256 _parser->send("AT+CGNSINF");
astartes 11:57fa27cb533e 257
astartes 11:57fa27cb533e 258 int nmeaStrLen=_parser->read(receivedChars, 120);
astartes 11:57fa27cb533e 259 _parser->flush();
astartes 11:57fa27cb533e 260 char *curLine = receivedChars;
astartes 11:57fa27cb533e 261
astartes 11:57fa27cb533e 262 char frame[150];
astartes 11:57fa27cb533e 263 for (int i = 0; i< 115; i++)
astartes 11:57fa27cb533e 264 {
astartes 11:57fa27cb533e 265 if(curLine[i]==':')
astartes 11:57fa27cb533e 266 {
astartes 11:57fa27cb533e 267 i++;
astartes 11:57fa27cb533e 268 int j = 0;
astartes 11:57fa27cb533e 269 while(i < 125)
astartes 11:57fa27cb533e 270 {
astartes 11:57fa27cb533e 271 if(curLine[i]=='O' && curLine[i+1]=='K')
astartes 11:57fa27cb533e 272 {
astartes 11:57fa27cb533e 273 break;
astartes 11:57fa27cb533e 274 }
astartes 11:57fa27cb533e 275 else
astartes 11:57fa27cb533e 276 {
astartes 11:57fa27cb533e 277 frame[j] = curLine[i];
astartes 11:57fa27cb533e 278 #if DEBUG_PC
astartes 11:57fa27cb533e 279 pc.printf("cahr %c\n", frame[j]);
astartes 11:57fa27cb533e 280 #endif
astartes 11:57fa27cb533e 281 i++;
astartes 11:57fa27cb533e 282 j++;
astartes 11:57fa27cb533e 283 }
astartes 11:57fa27cb533e 284 }
astartes 11:57fa27cb533e 285 break;
astartes 11:57fa27cb533e 286 }
astartes 11:57fa27cb533e 287 }
astartes 11:57fa27cb533e 288 // Parses the string
astartes 11:57fa27cb533e 289 char * pch = strtok (frame,",");
astartes 11:57fa27cb533e 290 strcpy(GNSSrunstatus_c, pch);
astartes 11:57fa27cb533e 291 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 292 strcpy(Fixstatus_c, pch);
astartes 11:57fa27cb533e 293 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 294 strcpy(UTCdatetime_c, pch);
astartes 11:57fa27cb533e 295 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 296 strcpy(latitude_c, pch);
astartes 11:57fa27cb533e 297 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 298 strcpy(logitude_c, pch);
astartes 11:57fa27cb533e 299 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 300 strcpy(altitude_c, pch);
astartes 11:57fa27cb533e 301 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 302 strcpy(speedOTG_c, pch);
astartes 11:57fa27cb533e 303 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 304 strcpy(course_c, pch);
astartes 11:57fa27cb533e 305 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 306 strcpy(fixmode_c, pch);
astartes 11:57fa27cb533e 307 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 308 strcpy(reserved_1, pch);
astartes 11:57fa27cb533e 309 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 310 strcpy(HDOP_c, pch);
astartes 11:57fa27cb533e 311 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 312 strcpy(PDOP_c, pch);
astartes 11:57fa27cb533e 313 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 314 strcpy(VDOP_c, pch);
astartes 11:57fa27cb533e 315 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 316 strcpy(reserved_2, pch);
astartes 11:57fa27cb533e 317 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 318 strcpy(satellitesinview_c, pch);
astartes 11:57fa27cb533e 319 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 320 strcpy(GNSSsatellitesused_c, pch);
astartes 11:57fa27cb533e 321 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 322 strcpy(GLONASSsatellitesused_c, pch);
astartes 11:57fa27cb533e 323 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 324 strcpy(reserved_3, pch);
astartes 11:57fa27cb533e 325 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 326 strcpy(cn0max_c, pch);
astartes 11:57fa27cb533e 327 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 328 strcpy(HPA_c, pch);
astartes 11:57fa27cb533e 329 pch = strtok(NULL, ",");
astartes 11:57fa27cb533e 330 strcpy(VPA_c, pch);
spin7ion 10:51960145754a 331
astartes 11:57fa27cb533e 332
astartes 11:57fa27cb533e 333
astartes 11:57fa27cb533e 334 int GNSS_st = atof(GNSSrunstatus_c);
astartes 11:57fa27cb533e 335 Fix_st = atof(Fixstatus_c);
astartes 11:57fa27cb533e 336 int sat_count = atof(satellitesinview_c);
astartes 11:57fa27cb533e 337
astartes 11:57fa27cb533e 338 B_l = atof(latitude_c);
astartes 11:57fa27cb533e 339 L_l = atof(logitude_c);
astartes 11:57fa27cb533e 340 Alt = atof(altitude_c);
astartes 11:57fa27cb533e 341
astartes 11:57fa27cb533e 342 #if DEBUG_PC
astartes 11:57fa27cb533e 343 pc.printf("GNSS st %d, FIX %d,SAT Count %d, Latitude: %f , Longitude: %f, Altitude: %f \n",GNSS_st, Fix_st, sat_count, B_l, L_l, Alt);
astartes 11:57fa27cb533e 344 #endif
astartes 11:57fa27cb533e 345
astartes 11:57fa27cb533e 346 wd.Service();
astartes 11:57fa27cb533e 347
astartes 11:57fa27cb533e 348 if(Fix_st != 0)
astartes 11:57fa27cb533e 349 {
astartes 11:57fa27cb533e 350 if (step_p > 5 )
spin7ion 6:70d0218c2a28 351 {
astartes 11:57fa27cb533e 352 return true;
spin7ion 6:70d0218c2a28 353 }
astartes 11:57fa27cb533e 354 else
astartes 11:57fa27cb533e 355 {
astartes 11:57fa27cb533e 356 step_p++;
spin7ion 10:51960145754a 357 }
astartes 11:57fa27cb533e 358 }
spin7ion 6:70d0218c2a28 359 return false;
spin7ion 6:70d0218c2a28 360 }
spin7ion 6:70d0218c2a28 361
astartes 11:57fa27cb533e 362 int terminateCOAPSession() {
astartes 11:57fa27cb533e 363
astartes 11:57fa27cb533e 364 for(int try_c = 0; try_c < 5; try_c++)
astartes 11:57fa27cb533e 365 {
astartes 11:57fa27cb533e 366 _parser->flush();
spin7ion 10:51960145754a 367 _parser->send("AT+CCOAPTERM");
astartes 11:57fa27cb533e 368 int r1 = checkIfOk();
spin7ion 10:51960145754a 369 wait(0.5);
spin7ion 10:51960145754a 370 _parser->send("AT+CNACT=0");
astartes 11:57fa27cb533e 371 int r2 = checkIfOk();
astartes 11:57fa27cb533e 372 if (r1 == r2 == true)
astartes 11:57fa27cb533e 373 {
astartes 11:57fa27cb533e 374 return true;
astartes 11:57fa27cb533e 375 }
astartes 11:57fa27cb533e 376 else
astartes 11:57fa27cb533e 377 {
astartes 11:57fa27cb533e 378 if(try_c >=5)
astartes 11:57fa27cb533e 379 {
astartes 11:57fa27cb533e 380 return false;
astartes 11:57fa27cb533e 381 }
astartes 11:57fa27cb533e 382 }
astartes 11:57fa27cb533e 383 }
astartes 11:57fa27cb533e 384
spin7ion 10:51960145754a 385 }
spin7ion 10:51960145754a 386
astartes 11:57fa27cb533e 387
astartes 11:57fa27cb533e 388
astartes 11:57fa27cb533e 389 bool sendTelemetry()
astartes 11:57fa27cb533e 390 {
astartes 11:57fa27cb533e 391 int check = 0;
spin7ion 10:51960145754a 392 wd.Service();
astartes 11:57fa27cb533e 393 int tr = 0;
astartes 11:57fa27cb533e 394
spin7ion 10:51960145754a 395 setAPN();
astartes 11:57fa27cb533e 396
astartes 11:57fa27cb533e 397 wd.Service();
astartes 11:57fa27cb533e 398 check = tr = 0;
astartes 11:57fa27cb533e 399 wait(2);
astartes 11:57fa27cb533e 400
astartes 11:57fa27cb533e 401 _parser->flush();
spin7ion 10:51960145754a 402 _parser->send("AT+CCOAPINIT");
astartes 11:57fa27cb533e 403
astartes 11:57fa27cb533e 404
spin7ion 10:51960145754a 405 wait(1);
astartes 11:57fa27cb533e 406 _parser->flush();
spin7ion 10:51960145754a 407 _parser->send("AT+CCOAPURL=\"" MTS_TELEMETRY_URL_STRING);
astartes 11:57fa27cb533e 408 checkIfOk();
astartes 11:57fa27cb533e 409 //if(!checkIfOk()){ terminateCOAPSession();return false;}
spin7ion 10:51960145754a 410 wait(1);
astartes 11:57fa27cb533e 411 _parser->flush();
astartes 11:57fa27cb533e 412 //_parser->printf("AT+CCOAPPARA=code,2,token,0,\"%s\",payload,1,",xstr(MTS_COAP_TOKEN));
astartes 11:57fa27cb533e 413 _parser->printf("AT+CCOAPPARA=code,2,payload,1,");
astartes 11:57fa27cb533e 414
spin7ion 10:51960145754a 415 for (size_t i = 0; i < strlen(bufferString); i++) {
spin7ion 10:51960145754a 416 _parser->printf("%x",bufferString[i]);
spin7ion 10:51960145754a 417 }
spin7ion 10:51960145754a 418
spin7ion 10:51960145754a 419 _parser->printf("\r\n");
astartes 11:57fa27cb533e 420 checkIfOk();
astartes 11:57fa27cb533e 421 //if(!checkIfOk()){ terminateCOAPSession();return false;}
spin7ion 10:51960145754a 422 wait(0.5);
astartes 11:57fa27cb533e 423 _parser->flush();
spin7ion 10:51960145754a 424 _parser->send("AT+CCOAPACTION");
astartes 11:57fa27cb533e 425 //checkIfOk();
astartes 11:57fa27cb533e 426 if(!checkIfOk()){ return false;}
spin7ion 10:51960145754a 427 wait(0.5);
astartes 11:57fa27cb533e 428
spin7ion 10:51960145754a 429 terminateCOAPSession();
spin7ion 10:51960145754a 430
spin7ion 10:51960145754a 431 return true;
spin7ion 10:51960145754a 432 }
spin7ion 6:70d0218c2a28 433 int main() {
spin7ion 6:70d0218c2a28 434 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
astartes 11:57fa27cb533e 435
astartes 11:57fa27cb533e 436 #if ENABLE_WATCHDOG
astartes 11:57fa27cb533e 437 wd.Configure(WATCHDOG_INTERVAL_S); // sets the timeout interval
astartes 11:57fa27cb533e 438 #endif
astartes 11:57fa27cb533e 439
astartes 11:57fa27cb533e 440 simPWR = 0;
astartes 11:57fa27cb533e 441 wait(2);
astartes 11:57fa27cb533e 442 simPWR = 1;
astartes 11:57fa27cb533e 443
astartes 11:57fa27cb533e 444 //blink_slow();
astartes 11:57fa27cb533e 445 wd.Service();
astartes 11:57fa27cb533e 446 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 447 {
astartes 11:57fa27cb533e 448 //myled = 1;
spin7ion 10:51960145754a 449 wait(0.5);
astartes 11:57fa27cb533e 450 //myled = 0;
spin7ion 10:51960145754a 451 wait(0.5);
astartes 11:57fa27cb533e 452 wd.Service();
astartes 11:57fa27cb533e 453 //myled = 1;
astartes 11:57fa27cb533e 454 }
astartes 11:57fa27cb533e 455
astartes 11:57fa27cb533e 456 //wait(7);
astartes 11:57fa27cb533e 457 //toggle_sim_power();
astartes 11:57fa27cb533e 458 /*
astartes 11:57fa27cb533e 459 * Generally, the NET indicator will fast flash firstly (1 time per second), which means that the
astartes 11:57fa27cb533e 460 * module has not logged in the Network. After logging in, the indicator become to flash slowly (1
astartes 11:57fa27cb533e 461 * time every three seconds).
astartes 11:57fa27cb533e 462 */
astartes 11:57fa27cb533e 463
spin7ion 10:51960145754a 464 //wait(WATCHDOG_INTERVAL_S-15);//Let SIM7000 wake up
astartes 11:57fa27cb533e 465 wd.Service();
spin7ion 6:70d0218c2a28 466 #if DEBUG_PC
spin7ion 6:70d0218c2a28 467 pc.baud(115200);
spin7ion 6:70d0218c2a28 468 pc.printf("Starting\r\n");
spin7ion 9:e8a07983289f 469 if (wd.WatchdogCausedReset()) {
spin7ion 6:70d0218c2a28 470 pc.printf("Watchdog caused reset.\r\n");
spin7ion 9:e8a07983289f 471 }
spin7ion 6:70d0218c2a28 472 #endif
astartes 11:57fa27cb533e 473
spin7ion 6:70d0218c2a28 474
astartes 11:57fa27cb533e 475
astartes 11:57fa27cb533e 476
astartes 11:57fa27cb533e 477
spin7ion 6:70d0218c2a28 478
spin7ion 6:70d0218c2a28 479 //Initiate SIM7000 SERIAL
spin7ion 6:70d0218c2a28 480 _serial = new UARTSerial(PIN_SIM_TX, PIN_SIM_RX, 115200);
spin7ion 6:70d0218c2a28 481 _parser = new ATCmdParser(_serial);
spin7ion 6:70d0218c2a28 482 _parser->debug_on( DEBUG_SIM );
spin7ion 6:70d0218c2a28 483 _parser->set_delimiter( "\r\n" );
astartes 11:57fa27cb533e 484 _parser->set_timeout(PARSER_TIMEOUT_S);
astartes 11:57fa27cb533e 485 wait(2);
astartes 11:57fa27cb533e 486
astartes 11:57fa27cb533e 487 wd.Service();
astartes 11:57fa27cb533e 488 wait(2);
astartes 11:57fa27cb533e 489 wd.Service();
astartes 11:57fa27cb533e 490 wait(2);
astartes 11:57fa27cb533e 491
astartes 11:57fa27cb533e 492 fixTries=0;
astartes 11:57fa27cb533e 493
astartes 11:57fa27cb533e 494 #if DEBUG_PC
astartes 11:57fa27cb533e 495 pc.printf("INIT START\r\n");
astartes 11:57fa27cb533e 496 #endif
astartes 11:57fa27cb533e 497
spin7ion 10:51960145754a 498
astartes 11:57fa27cb533e 499
astartes 11:57fa27cb533e 500
astartes 11:57fa27cb533e 501 int res = initSIM();
astartes 11:57fa27cb533e 502 if(!res)
astartes 11:57fa27cb533e 503 {
astartes 11:57fa27cb533e 504 #if DEBUG_PC
astartes 11:57fa27cb533e 505 pc.printf("Init Failed\r\n");
astartes 11:57fa27cb533e 506 #endif
astartes 11:57fa27cb533e 507
astartes 11:57fa27cb533e 508 wait(2);
astartes 11:57fa27cb533e 509 wait(2);
astartes 11:57fa27cb533e 510 wait(2);
astartes 11:57fa27cb533e 511 if(!initSIM());
astartes 11:57fa27cb533e 512 {
astartes 11:57fa27cb533e 513 wait(WATCHDOG_INTERVAL_S+10); // reset by dog
astartes 11:57fa27cb533e 514 }
astartes 11:57fa27cb533e 515 }
astartes 11:57fa27cb533e 516
astartes 11:57fa27cb533e 517 #if DEBUG_PC
astartes 11:57fa27cb533e 518 pc.printf("INIT finished\r\n");
spin7ion 10:51960145754a 519 #endif
astartes 11:57fa27cb533e 520
astartes 11:57fa27cb533e 521 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 522 {
astartes 11:57fa27cb533e 523 myled = 1;
astartes 11:57fa27cb533e 524 wait(0.5);
astartes 11:57fa27cb533e 525 myled = 0;
astartes 11:57fa27cb533e 526 wait(0.5);
astartes 11:57fa27cb533e 527 wd.Service();
astartes 11:57fa27cb533e 528 myled = 1;
spin7ion 10:51960145754a 529 }
spin7ion 6:70d0218c2a28 530
astartes 11:57fa27cb533e 531
astartes 11:57fa27cb533e 532 wd.Service();
astartes 11:57fa27cb533e 533
spin7ion 10:51960145754a 534 /*_parser->send("AT+CPSI?");
spin7ion 10:51960145754a 535 checkIfOk();
spin7ion 10:51960145754a 536 _parser->send("AT+CGNAPN");
spin7ion 10:51960145754a 537 checkIfOk();
spin7ion 10:51960145754a 538 _parser->send("AT+CAPNMODE=1");
spin7ion 10:51960145754a 539 checkIfOk();
spin7ion 10:51960145754a 540 _parser->send("AT+CREG?");
spin7ion 10:51960145754a 541 checkIfOk();
spin7ion 10:51960145754a 542 */
spin7ion 10:51960145754a 543 getSignalQuality();
spin7ion 10:51960145754a 544 #if DEBUG_PC
spin7ion 10:51960145754a 545 pc.printf("Signal quality: %d\r\n",rssiDB);
spin7ion 10:51960145754a 546 #endif
spin7ion 10:51960145754a 547
spin7ion 10:51960145754a 548 wd.Service();
spin7ion 6:70d0218c2a28 549 //Initiate termal stick
spin7ion 6:70d0218c2a28 550 for(i = 0; i < SENSORS_COUNT; i++) {
spin7ion 6:70d0218c2a28 551 ds1820[i] = new DS1820(&oneWire);
spin7ion 6:70d0218c2a28 552 if(!ds1820[i]->begin()) {
spin7ion 6:70d0218c2a28 553 delete ds1820[i];
spin7ion 6:70d0218c2a28 554 break;
spin7ion 6:70d0218c2a28 555 }
spin7ion 6:70d0218c2a28 556 }
spin7ion 6:70d0218c2a28 557
spin7ion 6:70d0218c2a28 558 sensors_found = i;
spin7ion 6:70d0218c2a28 559 #if DEBUG_PC
spin7ion 6:70d0218c2a28 560 pc.printf("Found %d sensors\r\n",sensors_found);
spin7ion 6:70d0218c2a28 561 if (sensors_found==0)
spin7ion 6:70d0218c2a28 562 pc.printf("No devices found");
spin7ion 6:70d0218c2a28 563 #endif
spin7ion 6:70d0218c2a28 564
spin7ion 6:70d0218c2a28 565 //Feed the watchdog
spin7ion 6:70d0218c2a28 566 wd.Service();
astartes 11:57fa27cb533e 567
spin7ion 6:70d0218c2a28 568 state=STATE_STARTING_GPS;
spin7ion 6:70d0218c2a28 569
astartes 11:57fa27cb533e 570 int off = 0;
astartes 11:57fa27cb533e 571
spin7ion 6:70d0218c2a28 572 while(1) {
spin7ion 10:51960145754a 573 wd.Service();
astartes 11:57fa27cb533e 574 if (state==STATE_STARTING_GPS)
astartes 11:57fa27cb533e 575 {
astartes 11:57fa27cb533e 576 wd.Service();
astartes 11:57fa27cb533e 577
astartes 11:57fa27cb533e 578
spin7ion 6:70d0218c2a28 579 #if DEBUG_PC
spin7ion 10:51960145754a 580 pc.printf("STATE=STARTING GPS\r\n");
spin7ion 6:70d0218c2a28 581 #endif
astartes 11:57fa27cb533e 582
astartes 11:57fa27cb533e 583 fixTries=0;
astartes 11:57fa27cb533e 584 while(fixTries<COMD_EXE_TRIES && !enableGPS(1))
astartes 11:57fa27cb533e 585 {
astartes 11:57fa27cb533e 586 #if DEBUG_PC
astartes 11:57fa27cb533e 587 pc.printf("GPS enable Fail\r\n");
astartes 11:57fa27cb533e 588 #endif
astartes 11:57fa27cb533e 589 wait(3);
astartes 11:57fa27cb533e 590 wd.Service();
astartes 11:57fa27cb533e 591 fixTries++;
astartes 11:57fa27cb533e 592 if (fixTries >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 593 {
astartes 11:57fa27cb533e 594 wait(WATCHDOG_INTERVAL_S+5);
astartes 11:57fa27cb533e 595 }
astartes 11:57fa27cb533e 596 }
astartes 11:57fa27cb533e 597
astartes 11:57fa27cb533e 598
astartes 11:57fa27cb533e 599 for(int test = 0; test < FIX_MAX_TRIES; test ++)
astartes 11:57fa27cb533e 600 {
astartes 11:57fa27cb533e 601 #if DEBUG_PC
astartes 11:57fa27cb533e 602 pc.printf("WAITNG gps to turn on \r\n");
astartes 11:57fa27cb533e 603 #endif
astartes 11:57fa27cb533e 604 wd.Service();
astartes 11:57fa27cb533e 605 wait(5);
astartes 11:57fa27cb533e 606 wd.Service();
astartes 11:57fa27cb533e 607 _parser->send("AT+CGNSINF");
astartes 11:57fa27cb533e 608 if(checkIfOk())
astartes 11:57fa27cb533e 609 {
astartes 11:57fa27cb533e 610 if(cold_start)
astartes 11:57fa27cb533e 611 {
astartes 11:57fa27cb533e 612 off = 0;
astartes 11:57fa27cb533e 613 while(off<COMD_EXE_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 614 {
astartes 11:57fa27cb533e 615 wd.Service();
astartes 11:57fa27cb533e 616 off++;
astartes 11:57fa27cb533e 617 }
astartes 11:57fa27cb533e 618 break;
astartes 11:57fa27cb533e 619 }
astartes 11:57fa27cb533e 620 else
astartes 11:57fa27cb533e 621 {
astartes 11:57fa27cb533e 622 break;
astartes 11:57fa27cb533e 623 }
astartes 11:57fa27cb533e 624 }
astartes 11:57fa27cb533e 625 else
astartes 11:57fa27cb533e 626 {
astartes 11:57fa27cb533e 627 if (test > 2)
astartes 11:57fa27cb533e 628 {
astartes 11:57fa27cb533e 629 cold_start = true;
astartes 11:57fa27cb533e 630 }
astartes 11:57fa27cb533e 631 wd.Service();
astartes 11:57fa27cb533e 632 wait(5);
astartes 11:57fa27cb533e 633 wd.Service();
astartes 11:57fa27cb533e 634 }
astartes 11:57fa27cb533e 635
astartes 11:57fa27cb533e 636
astartes 11:57fa27cb533e 637
astartes 11:57fa27cb533e 638 }
astartes 11:57fa27cb533e 639
astartes 11:57fa27cb533e 640
astartes 11:57fa27cb533e 641
spin7ion 6:70d0218c2a28 642 state=STATE_WAITING_FIX;
spin7ion 6:70d0218c2a28 643 fixTries=0;
spin7ion 6:70d0218c2a28 644 wd.Service();
astartes 11:57fa27cb533e 645 step_p = 0;
astartes 11:57fa27cb533e 646 //wait(FIX_CHECK_TIME_S);
astartes 11:57fa27cb533e 647
astartes 11:57fa27cb533e 648 }
astartes 11:57fa27cb533e 649 else if(state==STATE_WAITING_FIX) {
spin7ion 10:51960145754a 650
spin7ion 10:51960145754a 651 #if DEBUG_PC
spin7ion 10:51960145754a 652 pc.printf("STATE=WAITNG FIX\r\n");
spin7ion 10:51960145754a 653 #endif
astartes 11:57fa27cb533e 654
astartes 11:57fa27cb533e 655 off = 0;
astartes 11:57fa27cb533e 656 if (cold_start)
astartes 11:57fa27cb533e 657 {
astartes 11:57fa27cb533e 658 while(off<COMD_EXE_TRIES && !enableGPS(1))
astartes 11:57fa27cb533e 659 {
astartes 11:57fa27cb533e 660 wd.Service();
astartes 11:57fa27cb533e 661 off++;
astartes 11:57fa27cb533e 662 if (off >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 663 {
astartes 11:57fa27cb533e 664 wait(WATCHDOG_INTERVAL_S+5);
astartes 11:57fa27cb533e 665 }
astartes 11:57fa27cb533e 666 }
astartes 11:57fa27cb533e 667 cold_start = false;
astartes 11:57fa27cb533e 668 }
astartes 11:57fa27cb533e 669
astartes 11:57fa27cb533e 670
astartes 11:57fa27cb533e 671 if(getGPS())
astartes 11:57fa27cb533e 672 {
spin7ion 10:51960145754a 673 wd.Service();
astartes 11:57fa27cb533e 674 off = 0;
astartes 11:57fa27cb533e 675 while(off<FIX_MAX_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 676 {
astartes 11:57fa27cb533e 677 #if DEBUG_PC
astartes 11:57fa27cb533e 678 pc.printf("GPS disable Fail\r\n");
astartes 11:57fa27cb533e 679 #endif
astartes 11:57fa27cb533e 680 wait(3);
astartes 11:57fa27cb533e 681 wd.Service();
astartes 11:57fa27cb533e 682 off++;
astartes 11:57fa27cb533e 683 }
astartes 11:57fa27cb533e 684
spin7ion 6:70d0218c2a28 685 state=STATE_COLLECTING_TELEMETRY;
astartes 11:57fa27cb533e 686 }
astartes 11:57fa27cb533e 687 else
astartes 11:57fa27cb533e 688 {
spin7ion 10:51960145754a 689 wd.Service();
spin7ion 9:e8a07983289f 690 #if DEBUG_PC
spin7ion 10:51960145754a 691 pc.printf("No fix(%d) at %d/%d try\r\n", fix, fixTries, FIX_MAX_TRIES);
spin7ion 9:e8a07983289f 692 #endif
spin7ion 6:70d0218c2a28 693 fixTries++;
astartes 11:57fa27cb533e 694 if (fixTries>FIX_MAX_TRIES)
astartes 11:57fa27cb533e 695 {
spin7ion 6:70d0218c2a28 696 //fix not achieved in given tries, send as is
spin7ion 10:51960145754a 697 #if DEBUG_PC
spin7ion 10:51960145754a 698 pc.printf("No fix but continue\r\n");
spin7ion 10:51960145754a 699 #endif
astartes 11:57fa27cb533e 700
astartes 11:57fa27cb533e 701 off = 0;
astartes 11:57fa27cb533e 702 while(off<FIX_MAX_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 703 {
astartes 11:57fa27cb533e 704 #if DEBUG_PC
astartes 11:57fa27cb533e 705 pc.printf("GPS disable Fail\r\n");
astartes 11:57fa27cb533e 706 #endif
astartes 11:57fa27cb533e 707 wait(3);
astartes 11:57fa27cb533e 708 wd.Service();
astartes 11:57fa27cb533e 709 off++;
astartes 11:57fa27cb533e 710 }
astartes 11:57fa27cb533e 711
astartes 11:57fa27cb533e 712
spin7ion 9:e8a07983289f 713 wd.Service();
spin7ion 6:70d0218c2a28 714 state=STATE_COLLECTING_TELEMETRY;
spin7ion 10:51960145754a 715 fixTries=0;
astartes 11:57fa27cb533e 716 }
astartes 11:57fa27cb533e 717 else
astartes 11:57fa27cb533e 718 {
spin7ion 10:51960145754a 719 #if DEBUG_PC
spin7ion 10:51960145754a 720 pc.printf("Waiting %d sec\r\n",FIX_CHECK_TIME_S);
spin7ion 10:51960145754a 721 #endif
spin7ion 6:70d0218c2a28 722 wd.Service();
spin7ion 6:70d0218c2a28 723 wait(FIX_CHECK_TIME_S);
spin7ion 6:70d0218c2a28 724 }
spin7ion 6:70d0218c2a28 725 }
spin7ion 6:70d0218c2a28 726 } else if(state==STATE_COLLECTING_TELEMETRY) {
spin7ion 10:51960145754a 727 #if DEBUG_PC
spin7ion 10:51960145754a 728 pc.printf("STATE=COLLECTING TELEMETRY\r\n");
spin7ion 10:51960145754a 729 #endif
spin7ion 6:70d0218c2a28 730 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 6:70d0218c2a28 731 if(sensorsOrder[i]<sensors_found){
spin7ion 6:70d0218c2a28 732 ds1820[sensorsOrder[i]]->startConversion();
spin7ion 6:70d0218c2a28 733 }
spin7ion 6:70d0218c2a28 734 }
spin7ion 6:70d0218c2a28 735 wait(1.0);
spin7ion 6:70d0218c2a28 736 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 6:70d0218c2a28 737 if(sensorsOrder[i]<sensors_found){
spin7ion 6:70d0218c2a28 738 if(ds1820[sensorsOrder[i]]->isPresent()){
spin7ion 6:70d0218c2a28 739 stickTemperatures[i]=ds1820[i]->read();
spin7ion 6:70d0218c2a28 740 } else {
spin7ion 6:70d0218c2a28 741 stickTemperatures[i]=-273.f; // Sensor is offline
spin7ion 6:70d0218c2a28 742 }
spin7ion 6:70d0218c2a28 743 }
spin7ion 6:70d0218c2a28 744 }
spin7ion 6:70d0218c2a28 745
spin7ion 6:70d0218c2a28 746 IRtemp=thermometer.read_temp(1);
spin7ion 6:70d0218c2a28 747
astartes 11:57fa27cb533e 748 float bat_v = 100;
spin7ion 6:70d0218c2a28 749 //Form JSON as {"tempIR":1,"temps":[1,...,10],"latitude":37,"longitude":51,"altitude":21,"validGeo":true}
astartes 11:57fa27cb533e 750 snprintf(bufferString,2048,"{\"ID\":%s,\"tempIR\":%f,\"latitude\":%f,\"longitude\":%f,\"altitude\":%f,\"validGeo\":%s,\"battery\":%f,\"netlvl\":%d,temps:[",device_id, IRtemp, B_l, L_l, Alt, Fix_st ? "true" : "false", bat_v, rssiDB);
astartes 11:57fa27cb533e 751 // device_id
spin7ion 6:70d0218c2a28 752 index = strlen(bufferString);
spin7ion 6:70d0218c2a28 753
spin7ion 6:70d0218c2a28 754 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 9:e8a07983289f 755 index += snprintf(&bufferString[index], 2048-index, i==0?"%f":",%f", stickTemperatures[i]);
spin7ion 6:70d0218c2a28 756 }
spin7ion 9:e8a07983289f 757 strcat (bufferString,"]}");
spin7ion 6:70d0218c2a28 758
spin7ion 10:51960145754a 759 wd.Service();
spin7ion 6:70d0218c2a28 760 state=STATE_SENDING_TELEMETRY;
astartes 11:57fa27cb533e 761 }
astartes 11:57fa27cb533e 762 else if(state==STATE_SENDING_TELEMETRY)
astartes 11:57fa27cb533e 763 {
spin7ion 6:70d0218c2a28 764 #if DEBUG_PC
spin7ion 10:51960145754a 765 pc.printf("STATE=SENDING TELEMETRY\r\n");
spin7ion 6:70d0218c2a28 766 pc.printf(bufferString);
spin7ion 6:70d0218c2a28 767 #endif
astartes 11:57fa27cb533e 768
astartes 11:57fa27cb533e 769 for(int try_c = 0; try_c < COMD_EXE_TRIES; try_c ++)
astartes 11:57fa27cb533e 770 {
astartes 11:57fa27cb533e 771 if(getSignalQuality())
astartes 11:57fa27cb533e 772 {
astartes 11:57fa27cb533e 773 break;
astartes 11:57fa27cb533e 774 }
spin7ion 10:51960145754a 775 wd.Service();
spin7ion 10:51960145754a 776 }
astartes 11:57fa27cb533e 777
astartes 11:57fa27cb533e 778
spin7ion 10:51960145754a 779 fixTries=0;
astartes 11:57fa27cb533e 780
astartes 11:57fa27cb533e 781 while(fixTries<FIX_MAX_TRIES && !sendTelemetry())
astartes 11:57fa27cb533e 782 {
astartes 11:57fa27cb533e 783 wait(1);
astartes 11:57fa27cb533e 784 terminateCOAPSession();
astartes 11:57fa27cb533e 785 wait(3);
astartes 11:57fa27cb533e 786 wd.Service();
astartes 11:57fa27cb533e 787 fixTries++;
astartes 11:57fa27cb533e 788 }
astartes 11:57fa27cb533e 789
astartes 11:57fa27cb533e 790 //disconnectNetwork();
astartes 11:57fa27cb533e 791 //initSIM();
astartes 11:57fa27cb533e 792 //enableGPS(0);
astartes 11:57fa27cb533e 793 //setPowerSavingMode();
astartes 11:57fa27cb533e 794 //fixTries++;
spin7ion 10:51960145754a 795 wd.Service();
astartes 11:57fa27cb533e 796 off = 0;
astartes 11:57fa27cb533e 797 while(off<COMD_EXE_TRIES && !setPowerSavingMode())
astartes 11:57fa27cb533e 798 {
astartes 11:57fa27cb533e 799 wd.Service();
astartes 11:57fa27cb533e 800 off++;
astartes 11:57fa27cb533e 801 }
astartes 11:57fa27cb533e 802 #if DEBUG_PC
astartes 11:57fa27cb533e 803 pc.printf("Sim 7000 Off\r\n");
astartes 11:57fa27cb533e 804
astartes 11:57fa27cb533e 805 #endif
astartes 11:57fa27cb533e 806
astartes 11:57fa27cb533e 807 simPWR = 0;
astartes 11:57fa27cb533e 808 wait(2);
astartes 11:57fa27cb533e 809 simPWR = 1;
astartes 11:57fa27cb533e 810 _parser->flush();
astartes 11:57fa27cb533e 811
astartes 11:57fa27cb533e 812
astartes 11:57fa27cb533e 813 //wait(SLEEP_CHECK_TIME);
astartes 11:57fa27cb533e 814 wd.Service();
astartes 11:57fa27cb533e 815
astartes 11:57fa27cb533e 816
spin7ion 6:70d0218c2a28 817 state=STATE_SLEEPING;
spin7ion 6:70d0218c2a28 818 } else if(state==STATE_SLEEPING){
spin7ion 10:51960145754a 819 #if DEBUG_PC
spin7ion 10:51960145754a 820 pc.printf("STATE=SLEEPING already for %d\r\n",sleepTimer);
spin7ion 10:51960145754a 821 #endif
spin7ion 6:70d0218c2a28 822 wd.Service();
spin7ion 6:70d0218c2a28 823 sleepTimer+=SLEEP_CHECK_TIME;
spin7ion 6:70d0218c2a28 824
spin7ion 6:70d0218c2a28 825 if(sleepTimer>SLEEP_TIME_S){
astartes 11:57fa27cb533e 826 simPWR = 0;
astartes 11:57fa27cb533e 827 wait(3);
astartes 11:57fa27cb533e 828 simPWR = 1;
astartes 11:57fa27cb533e 829 _parser->flush();
astartes 11:57fa27cb533e 830 #if DEBUG_PC
astartes 11:57fa27cb533e 831 pc.printf("from sleep\r\n");
astartes 11:57fa27cb533e 832 #endif
astartes 11:57fa27cb533e 833 wait(3);
astartes 11:57fa27cb533e 834 wd.Service();
astartes 11:57fa27cb533e 835 wait(3);
astartes 11:57fa27cb533e 836 wd.Service();
astartes 11:57fa27cb533e 837
astartes 11:57fa27cb533e 838 _parser->flush();
astartes 11:57fa27cb533e 839
astartes 11:57fa27cb533e 840
astartes 11:57fa27cb533e 841 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 842 {
astartes 11:57fa27cb533e 843 myled = 1;
astartes 11:57fa27cb533e 844 wait(1);
astartes 11:57fa27cb533e 845 myled = 0;
astartes 11:57fa27cb533e 846 wait(1);
astartes 11:57fa27cb533e 847 wd.Service();
astartes 11:57fa27cb533e 848 myled = 1;
astartes 11:57fa27cb533e 849 }
astartes 11:57fa27cb533e 850 #if DEBUG_PC
astartes 11:57fa27cb533e 851 pc.printf("boot\r\n");
astartes 11:57fa27cb533e 852 #endif
astartes 11:57fa27cb533e 853 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 854 {
astartes 11:57fa27cb533e 855 myled = 1;
astartes 11:57fa27cb533e 856 wait(1);
astartes 11:57fa27cb533e 857 myled = 0;
astartes 11:57fa27cb533e 858 wait(1);
astartes 11:57fa27cb533e 859 wd.Service();
astartes 11:57fa27cb533e 860 myled = 1;
astartes 11:57fa27cb533e 861 }
astartes 11:57fa27cb533e 862
astartes 11:57fa27cb533e 863 int res = initSIM();
astartes 11:57fa27cb533e 864 if(!res)
astartes 11:57fa27cb533e 865 {
astartes 11:57fa27cb533e 866 #if DEBUG_PC
astartes 11:57fa27cb533e 867 pc.printf("Init Failed\r\n");
astartes 11:57fa27cb533e 868 #endif
astartes 11:57fa27cb533e 869 wait(5);
astartes 11:57fa27cb533e 870 if(!initSIM());
astartes 11:57fa27cb533e 871 {
astartes 11:57fa27cb533e 872 wait(WATCHDOG_INTERVAL_S+10); // reset by dog
astartes 11:57fa27cb533e 873 }
astartes 11:57fa27cb533e 874
astartes 11:57fa27cb533e 875
astartes 11:57fa27cb533e 876
astartes 11:57fa27cb533e 877 }
astartes 11:57fa27cb533e 878
spin7ion 6:70d0218c2a28 879 state=STATE_STARTING_GPS;
spin7ion 6:70d0218c2a28 880 sleepTimer=0;
spin7ion 6:70d0218c2a28 881 } else {
spin7ion 6:70d0218c2a28 882 wait(SLEEP_CHECK_TIME);
spin7ion 6:70d0218c2a28 883 }
spin7ion 10:51960145754a 884 wd.Service();
spin7ion 10:51960145754a 885 #if DEBUG_PC
spin7ion 10:51960145754a 886 pc.printf("After sleep\r\n");
spin7ion 10:51960145754a 887 #endif
astartes 11:57fa27cb533e 888
astartes 11:57fa27cb533e 889 //from_sleep = true;
spin7ion 6:70d0218c2a28 890 }
spin7ion 6:70d0218c2a28 891
spin7ion 6:70d0218c2a28 892 //Feed the watchdog
spin7ion 6:70d0218c2a28 893 wd.Service();
spin7ion 6:70d0218c2a28 894 wait(0.5);
spin7ion 6:70d0218c2a28 895 }
spin7ion 6:70d0218c2a28 896 }
spin7ion 6:70d0218c2a28 897