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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Sun Jan 10 13:37:58 2021 +0000
Revision:
12:7fe416cdac08
Parent:
11:57fa27cb533e
Child:
13:6dbc5383b7e0
Hard code

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();
astartes 12:7fe416cdac08 407 //_parser->send("AT+CCOAPURL=\"" MTS_TELEMETRY_URL_STRING);
astartes 12:7fe416cdac08 408 _parser->send("AT+CCOAPURL=\"coap://193.227.232.26:5683/api/v1/MIEMHSE-TEST\"");
astartes 11:57fa27cb533e 409 checkIfOk();
astartes 11:57fa27cb533e 410 //if(!checkIfOk()){ terminateCOAPSession();return false;}
spin7ion 10:51960145754a 411 wait(1);
astartes 11:57fa27cb533e 412 _parser->flush();
astartes 11:57fa27cb533e 413 //_parser->printf("AT+CCOAPPARA=code,2,token,0,\"%s\",payload,1,",xstr(MTS_COAP_TOKEN));
astartes 11:57fa27cb533e 414 _parser->printf("AT+CCOAPPARA=code,2,payload,1,");
astartes 11:57fa27cb533e 415
spin7ion 10:51960145754a 416 for (size_t i = 0; i < strlen(bufferString); i++) {
spin7ion 10:51960145754a 417 _parser->printf("%x",bufferString[i]);
spin7ion 10:51960145754a 418 }
spin7ion 10:51960145754a 419
spin7ion 10:51960145754a 420 _parser->printf("\r\n");
astartes 11:57fa27cb533e 421 checkIfOk();
astartes 11:57fa27cb533e 422 //if(!checkIfOk()){ terminateCOAPSession();return false;}
spin7ion 10:51960145754a 423 wait(0.5);
astartes 11:57fa27cb533e 424 _parser->flush();
spin7ion 10:51960145754a 425 _parser->send("AT+CCOAPACTION");
astartes 11:57fa27cb533e 426 //checkIfOk();
astartes 11:57fa27cb533e 427 if(!checkIfOk()){ return false;}
astartes 12:7fe416cdac08 428 wait(2);
astartes 12:7fe416cdac08 429 wd.Service();
astartes 12:7fe416cdac08 430 wait(2);
astartes 12:7fe416cdac08 431 wd.Service();
astartes 12:7fe416cdac08 432 wait(2);
astartes 12:7fe416cdac08 433 wd.Service();
astartes 12:7fe416cdac08 434 wait(2);
astartes 11:57fa27cb533e 435
spin7ion 10:51960145754a 436 terminateCOAPSession();
spin7ion 10:51960145754a 437
spin7ion 10:51960145754a 438 return true;
spin7ion 10:51960145754a 439 }
spin7ion 6:70d0218c2a28 440 int main() {
spin7ion 6:70d0218c2a28 441 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
astartes 11:57fa27cb533e 442
astartes 11:57fa27cb533e 443 #if ENABLE_WATCHDOG
astartes 11:57fa27cb533e 444 wd.Configure(WATCHDOG_INTERVAL_S); // sets the timeout interval
astartes 11:57fa27cb533e 445 #endif
astartes 11:57fa27cb533e 446
astartes 11:57fa27cb533e 447 simPWR = 0;
astartes 11:57fa27cb533e 448 wait(2);
astartes 11:57fa27cb533e 449 simPWR = 1;
astartes 11:57fa27cb533e 450
astartes 11:57fa27cb533e 451 //blink_slow();
astartes 11:57fa27cb533e 452 wd.Service();
astartes 11:57fa27cb533e 453 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 454 {
astartes 11:57fa27cb533e 455 //myled = 1;
spin7ion 10:51960145754a 456 wait(0.5);
astartes 11:57fa27cb533e 457 //myled = 0;
spin7ion 10:51960145754a 458 wait(0.5);
astartes 11:57fa27cb533e 459 wd.Service();
astartes 11:57fa27cb533e 460 //myled = 1;
astartes 11:57fa27cb533e 461 }
astartes 11:57fa27cb533e 462
astartes 11:57fa27cb533e 463 //wait(7);
astartes 11:57fa27cb533e 464 //toggle_sim_power();
astartes 11:57fa27cb533e 465 /*
astartes 11:57fa27cb533e 466 * Generally, the NET indicator will fast flash firstly (1 time per second), which means that the
astartes 11:57fa27cb533e 467 * module has not logged in the Network. After logging in, the indicator become to flash slowly (1
astartes 11:57fa27cb533e 468 * time every three seconds).
astartes 11:57fa27cb533e 469 */
astartes 11:57fa27cb533e 470
spin7ion 10:51960145754a 471 //wait(WATCHDOG_INTERVAL_S-15);//Let SIM7000 wake up
astartes 11:57fa27cb533e 472 wd.Service();
spin7ion 6:70d0218c2a28 473 #if DEBUG_PC
spin7ion 6:70d0218c2a28 474 pc.baud(115200);
spin7ion 6:70d0218c2a28 475 pc.printf("Starting\r\n");
spin7ion 9:e8a07983289f 476 if (wd.WatchdogCausedReset()) {
spin7ion 6:70d0218c2a28 477 pc.printf("Watchdog caused reset.\r\n");
spin7ion 9:e8a07983289f 478 }
spin7ion 6:70d0218c2a28 479 #endif
astartes 11:57fa27cb533e 480
spin7ion 6:70d0218c2a28 481
astartes 11:57fa27cb533e 482
astartes 11:57fa27cb533e 483
astartes 11:57fa27cb533e 484
spin7ion 6:70d0218c2a28 485
spin7ion 6:70d0218c2a28 486 //Initiate SIM7000 SERIAL
spin7ion 6:70d0218c2a28 487 _serial = new UARTSerial(PIN_SIM_TX, PIN_SIM_RX, 115200);
spin7ion 6:70d0218c2a28 488 _parser = new ATCmdParser(_serial);
spin7ion 6:70d0218c2a28 489 _parser->debug_on( DEBUG_SIM );
spin7ion 6:70d0218c2a28 490 _parser->set_delimiter( "\r\n" );
astartes 11:57fa27cb533e 491 _parser->set_timeout(PARSER_TIMEOUT_S);
astartes 11:57fa27cb533e 492 wait(2);
astartes 11:57fa27cb533e 493
astartes 11:57fa27cb533e 494 wd.Service();
astartes 11:57fa27cb533e 495 wait(2);
astartes 11:57fa27cb533e 496 wd.Service();
astartes 11:57fa27cb533e 497 wait(2);
astartes 11:57fa27cb533e 498
astartes 11:57fa27cb533e 499 fixTries=0;
astartes 11:57fa27cb533e 500
astartes 11:57fa27cb533e 501 #if DEBUG_PC
astartes 11:57fa27cb533e 502 pc.printf("INIT START\r\n");
astartes 11:57fa27cb533e 503 #endif
astartes 11:57fa27cb533e 504
spin7ion 10:51960145754a 505
astartes 11:57fa27cb533e 506
astartes 11:57fa27cb533e 507
astartes 11:57fa27cb533e 508 int res = initSIM();
astartes 11:57fa27cb533e 509 if(!res)
astartes 11:57fa27cb533e 510 {
astartes 11:57fa27cb533e 511 #if DEBUG_PC
astartes 11:57fa27cb533e 512 pc.printf("Init Failed\r\n");
astartes 11:57fa27cb533e 513 #endif
astartes 11:57fa27cb533e 514
astartes 11:57fa27cb533e 515 wait(2);
astartes 11:57fa27cb533e 516 wait(2);
astartes 11:57fa27cb533e 517 wait(2);
astartes 11:57fa27cb533e 518 if(!initSIM());
astartes 11:57fa27cb533e 519 {
astartes 11:57fa27cb533e 520 wait(WATCHDOG_INTERVAL_S+10); // reset by dog
astartes 11:57fa27cb533e 521 }
astartes 11:57fa27cb533e 522 }
astartes 11:57fa27cb533e 523
astartes 11:57fa27cb533e 524 #if DEBUG_PC
astartes 11:57fa27cb533e 525 pc.printf("INIT finished\r\n");
spin7ion 10:51960145754a 526 #endif
astartes 11:57fa27cb533e 527
astartes 11:57fa27cb533e 528 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 529 {
astartes 11:57fa27cb533e 530 myled = 1;
astartes 11:57fa27cb533e 531 wait(0.5);
astartes 11:57fa27cb533e 532 myled = 0;
astartes 11:57fa27cb533e 533 wait(0.5);
astartes 11:57fa27cb533e 534 wd.Service();
astartes 11:57fa27cb533e 535 myled = 1;
spin7ion 10:51960145754a 536 }
spin7ion 6:70d0218c2a28 537
astartes 11:57fa27cb533e 538
astartes 11:57fa27cb533e 539 wd.Service();
astartes 11:57fa27cb533e 540
spin7ion 10:51960145754a 541 /*_parser->send("AT+CPSI?");
spin7ion 10:51960145754a 542 checkIfOk();
spin7ion 10:51960145754a 543 _parser->send("AT+CGNAPN");
spin7ion 10:51960145754a 544 checkIfOk();
spin7ion 10:51960145754a 545 _parser->send("AT+CAPNMODE=1");
spin7ion 10:51960145754a 546 checkIfOk();
spin7ion 10:51960145754a 547 _parser->send("AT+CREG?");
spin7ion 10:51960145754a 548 checkIfOk();
spin7ion 10:51960145754a 549 */
spin7ion 10:51960145754a 550 getSignalQuality();
spin7ion 10:51960145754a 551 #if DEBUG_PC
spin7ion 10:51960145754a 552 pc.printf("Signal quality: %d\r\n",rssiDB);
spin7ion 10:51960145754a 553 #endif
spin7ion 10:51960145754a 554
spin7ion 10:51960145754a 555 wd.Service();
spin7ion 6:70d0218c2a28 556 //Initiate termal stick
spin7ion 6:70d0218c2a28 557 for(i = 0; i < SENSORS_COUNT; i++) {
spin7ion 6:70d0218c2a28 558 ds1820[i] = new DS1820(&oneWire);
spin7ion 6:70d0218c2a28 559 if(!ds1820[i]->begin()) {
spin7ion 6:70d0218c2a28 560 delete ds1820[i];
spin7ion 6:70d0218c2a28 561 break;
spin7ion 6:70d0218c2a28 562 }
spin7ion 6:70d0218c2a28 563 }
spin7ion 6:70d0218c2a28 564
spin7ion 6:70d0218c2a28 565 sensors_found = i;
spin7ion 6:70d0218c2a28 566 #if DEBUG_PC
spin7ion 6:70d0218c2a28 567 pc.printf("Found %d sensors\r\n",sensors_found);
spin7ion 6:70d0218c2a28 568 if (sensors_found==0)
spin7ion 6:70d0218c2a28 569 pc.printf("No devices found");
spin7ion 6:70d0218c2a28 570 #endif
spin7ion 6:70d0218c2a28 571
spin7ion 6:70d0218c2a28 572 //Feed the watchdog
spin7ion 6:70d0218c2a28 573 wd.Service();
astartes 11:57fa27cb533e 574
spin7ion 6:70d0218c2a28 575 state=STATE_STARTING_GPS;
spin7ion 6:70d0218c2a28 576
astartes 11:57fa27cb533e 577 int off = 0;
astartes 11:57fa27cb533e 578
spin7ion 6:70d0218c2a28 579 while(1) {
spin7ion 10:51960145754a 580 wd.Service();
astartes 11:57fa27cb533e 581 if (state==STATE_STARTING_GPS)
astartes 11:57fa27cb533e 582 {
astartes 11:57fa27cb533e 583 wd.Service();
astartes 11:57fa27cb533e 584
astartes 11:57fa27cb533e 585
spin7ion 6:70d0218c2a28 586 #if DEBUG_PC
spin7ion 10:51960145754a 587 pc.printf("STATE=STARTING GPS\r\n");
spin7ion 6:70d0218c2a28 588 #endif
astartes 11:57fa27cb533e 589
astartes 11:57fa27cb533e 590 fixTries=0;
astartes 11:57fa27cb533e 591 while(fixTries<COMD_EXE_TRIES && !enableGPS(1))
astartes 11:57fa27cb533e 592 {
astartes 11:57fa27cb533e 593 #if DEBUG_PC
astartes 11:57fa27cb533e 594 pc.printf("GPS enable Fail\r\n");
astartes 11:57fa27cb533e 595 #endif
astartes 11:57fa27cb533e 596 wait(3);
astartes 11:57fa27cb533e 597 wd.Service();
astartes 11:57fa27cb533e 598 fixTries++;
astartes 11:57fa27cb533e 599 if (fixTries >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 600 {
astartes 11:57fa27cb533e 601 wait(WATCHDOG_INTERVAL_S+5);
astartes 11:57fa27cb533e 602 }
astartes 11:57fa27cb533e 603 }
astartes 11:57fa27cb533e 604
astartes 11:57fa27cb533e 605
astartes 11:57fa27cb533e 606 for(int test = 0; test < FIX_MAX_TRIES; test ++)
astartes 11:57fa27cb533e 607 {
astartes 11:57fa27cb533e 608 #if DEBUG_PC
astartes 11:57fa27cb533e 609 pc.printf("WAITNG gps to turn on \r\n");
astartes 11:57fa27cb533e 610 #endif
astartes 11:57fa27cb533e 611 wd.Service();
astartes 12:7fe416cdac08 612 wait(2);
astartes 12:7fe416cdac08 613 wait(2);
astartes 12:7fe416cdac08 614 wait(1);
astartes 11:57fa27cb533e 615 wd.Service();
astartes 11:57fa27cb533e 616 _parser->send("AT+CGNSINF");
astartes 11:57fa27cb533e 617 if(checkIfOk())
astartes 11:57fa27cb533e 618 {
astartes 11:57fa27cb533e 619 if(cold_start)
astartes 11:57fa27cb533e 620 {
astartes 11:57fa27cb533e 621 off = 0;
astartes 11:57fa27cb533e 622 while(off<COMD_EXE_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 623 {
astartes 11:57fa27cb533e 624 wd.Service();
astartes 11:57fa27cb533e 625 off++;
astartes 11:57fa27cb533e 626 }
astartes 11:57fa27cb533e 627 break;
astartes 11:57fa27cb533e 628 }
astartes 11:57fa27cb533e 629 else
astartes 11:57fa27cb533e 630 {
astartes 11:57fa27cb533e 631 break;
astartes 11:57fa27cb533e 632 }
astartes 11:57fa27cb533e 633 }
astartes 11:57fa27cb533e 634 else
astartes 11:57fa27cb533e 635 {
astartes 11:57fa27cb533e 636 if (test > 2)
astartes 11:57fa27cb533e 637 {
astartes 11:57fa27cb533e 638 cold_start = true;
astartes 11:57fa27cb533e 639 }
astartes 11:57fa27cb533e 640 wd.Service();
astartes 12:7fe416cdac08 641 wait(2);
astartes 12:7fe416cdac08 642 wait(2);
astartes 12:7fe416cdac08 643 wait(1);
astartes 11:57fa27cb533e 644 wd.Service();
astartes 11:57fa27cb533e 645 }
astartes 11:57fa27cb533e 646
astartes 11:57fa27cb533e 647
astartes 11:57fa27cb533e 648
astartes 11:57fa27cb533e 649 }
astartes 11:57fa27cb533e 650
astartes 11:57fa27cb533e 651
astartes 11:57fa27cb533e 652
spin7ion 6:70d0218c2a28 653 state=STATE_WAITING_FIX;
spin7ion 6:70d0218c2a28 654 fixTries=0;
spin7ion 6:70d0218c2a28 655 wd.Service();
astartes 11:57fa27cb533e 656 step_p = 0;
astartes 11:57fa27cb533e 657 //wait(FIX_CHECK_TIME_S);
astartes 11:57fa27cb533e 658
astartes 11:57fa27cb533e 659 }
astartes 11:57fa27cb533e 660 else if(state==STATE_WAITING_FIX) {
spin7ion 10:51960145754a 661
spin7ion 10:51960145754a 662 #if DEBUG_PC
spin7ion 10:51960145754a 663 pc.printf("STATE=WAITNG FIX\r\n");
spin7ion 10:51960145754a 664 #endif
astartes 11:57fa27cb533e 665
astartes 11:57fa27cb533e 666 off = 0;
astartes 11:57fa27cb533e 667 if (cold_start)
astartes 11:57fa27cb533e 668 {
astartes 11:57fa27cb533e 669 while(off<COMD_EXE_TRIES && !enableGPS(1))
astartes 11:57fa27cb533e 670 {
astartes 11:57fa27cb533e 671 wd.Service();
astartes 11:57fa27cb533e 672 off++;
astartes 11:57fa27cb533e 673 if (off >= COMD_EXE_TRIES)
astartes 11:57fa27cb533e 674 {
astartes 11:57fa27cb533e 675 wait(WATCHDOG_INTERVAL_S+5);
astartes 11:57fa27cb533e 676 }
astartes 11:57fa27cb533e 677 }
astartes 11:57fa27cb533e 678 cold_start = false;
astartes 11:57fa27cb533e 679 }
astartes 11:57fa27cb533e 680
astartes 11:57fa27cb533e 681
astartes 11:57fa27cb533e 682 if(getGPS())
astartes 11:57fa27cb533e 683 {
spin7ion 10:51960145754a 684 wd.Service();
astartes 11:57fa27cb533e 685 off = 0;
astartes 11:57fa27cb533e 686 while(off<FIX_MAX_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 687 {
astartes 11:57fa27cb533e 688 #if DEBUG_PC
astartes 11:57fa27cb533e 689 pc.printf("GPS disable Fail\r\n");
astartes 11:57fa27cb533e 690 #endif
astartes 11:57fa27cb533e 691 wait(3);
astartes 11:57fa27cb533e 692 wd.Service();
astartes 11:57fa27cb533e 693 off++;
astartes 11:57fa27cb533e 694 }
astartes 11:57fa27cb533e 695
spin7ion 6:70d0218c2a28 696 state=STATE_COLLECTING_TELEMETRY;
astartes 11:57fa27cb533e 697 }
astartes 11:57fa27cb533e 698 else
astartes 11:57fa27cb533e 699 {
spin7ion 10:51960145754a 700 wd.Service();
spin7ion 9:e8a07983289f 701 #if DEBUG_PC
spin7ion 10:51960145754a 702 pc.printf("No fix(%d) at %d/%d try\r\n", fix, fixTries, FIX_MAX_TRIES);
spin7ion 9:e8a07983289f 703 #endif
spin7ion 6:70d0218c2a28 704 fixTries++;
astartes 11:57fa27cb533e 705 if (fixTries>FIX_MAX_TRIES)
astartes 11:57fa27cb533e 706 {
spin7ion 6:70d0218c2a28 707 //fix not achieved in given tries, send as is
spin7ion 10:51960145754a 708 #if DEBUG_PC
spin7ion 10:51960145754a 709 pc.printf("No fix but continue\r\n");
spin7ion 10:51960145754a 710 #endif
astartes 11:57fa27cb533e 711
astartes 11:57fa27cb533e 712 off = 0;
astartes 11:57fa27cb533e 713 while(off<FIX_MAX_TRIES && !enableGPS(0))
astartes 11:57fa27cb533e 714 {
astartes 11:57fa27cb533e 715 #if DEBUG_PC
astartes 11:57fa27cb533e 716 pc.printf("GPS disable Fail\r\n");
astartes 11:57fa27cb533e 717 #endif
astartes 11:57fa27cb533e 718 wait(3);
astartes 11:57fa27cb533e 719 wd.Service();
astartes 11:57fa27cb533e 720 off++;
astartes 11:57fa27cb533e 721 }
astartes 11:57fa27cb533e 722
astartes 11:57fa27cb533e 723
spin7ion 9:e8a07983289f 724 wd.Service();
spin7ion 6:70d0218c2a28 725 state=STATE_COLLECTING_TELEMETRY;
spin7ion 10:51960145754a 726 fixTries=0;
astartes 11:57fa27cb533e 727 }
astartes 11:57fa27cb533e 728 else
astartes 11:57fa27cb533e 729 {
spin7ion 10:51960145754a 730 #if DEBUG_PC
spin7ion 10:51960145754a 731 pc.printf("Waiting %d sec\r\n",FIX_CHECK_TIME_S);
spin7ion 10:51960145754a 732 #endif
spin7ion 6:70d0218c2a28 733 wd.Service();
spin7ion 6:70d0218c2a28 734 wait(FIX_CHECK_TIME_S);
spin7ion 6:70d0218c2a28 735 }
spin7ion 6:70d0218c2a28 736 }
spin7ion 6:70d0218c2a28 737 } else if(state==STATE_COLLECTING_TELEMETRY) {
spin7ion 10:51960145754a 738 #if DEBUG_PC
spin7ion 10:51960145754a 739 pc.printf("STATE=COLLECTING TELEMETRY\r\n");
spin7ion 10:51960145754a 740 #endif
spin7ion 6:70d0218c2a28 741 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 6:70d0218c2a28 742 if(sensorsOrder[i]<sensors_found){
spin7ion 6:70d0218c2a28 743 ds1820[sensorsOrder[i]]->startConversion();
spin7ion 6:70d0218c2a28 744 }
spin7ion 6:70d0218c2a28 745 }
spin7ion 6:70d0218c2a28 746 wait(1.0);
spin7ion 6:70d0218c2a28 747 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 6:70d0218c2a28 748 if(sensorsOrder[i]<sensors_found){
spin7ion 6:70d0218c2a28 749 if(ds1820[sensorsOrder[i]]->isPresent()){
spin7ion 6:70d0218c2a28 750 stickTemperatures[i]=ds1820[i]->read();
spin7ion 6:70d0218c2a28 751 } else {
spin7ion 6:70d0218c2a28 752 stickTemperatures[i]=-273.f; // Sensor is offline
spin7ion 6:70d0218c2a28 753 }
spin7ion 6:70d0218c2a28 754 }
spin7ion 6:70d0218c2a28 755 }
spin7ion 6:70d0218c2a28 756
spin7ion 6:70d0218c2a28 757 IRtemp=thermometer.read_temp(1);
spin7ion 6:70d0218c2a28 758
astartes 11:57fa27cb533e 759 float bat_v = 100;
spin7ion 6:70d0218c2a28 760 //Form JSON as {"tempIR":1,"temps":[1,...,10],"latitude":37,"longitude":51,"altitude":21,"validGeo":true}
astartes 11:57fa27cb533e 761 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 12:7fe416cdac08 762 // 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 ? ( 1: 0) , bat_v
astartes 11:57fa27cb533e 763 // device_id
spin7ion 6:70d0218c2a28 764 index = strlen(bufferString);
spin7ion 6:70d0218c2a28 765
spin7ion 6:70d0218c2a28 766 for(i=0;i<SENSORS_COUNT;i++){
spin7ion 9:e8a07983289f 767 index += snprintf(&bufferString[index], 2048-index, i==0?"%f":",%f", stickTemperatures[i]);
spin7ion 6:70d0218c2a28 768 }
spin7ion 9:e8a07983289f 769 strcat (bufferString,"]}");
spin7ion 6:70d0218c2a28 770
spin7ion 10:51960145754a 771 wd.Service();
spin7ion 6:70d0218c2a28 772 state=STATE_SENDING_TELEMETRY;
astartes 11:57fa27cb533e 773 }
astartes 11:57fa27cb533e 774 else if(state==STATE_SENDING_TELEMETRY)
astartes 11:57fa27cb533e 775 {
spin7ion 6:70d0218c2a28 776 #if DEBUG_PC
spin7ion 10:51960145754a 777 pc.printf("STATE=SENDING TELEMETRY\r\n");
spin7ion 6:70d0218c2a28 778 pc.printf(bufferString);
spin7ion 6:70d0218c2a28 779 #endif
astartes 11:57fa27cb533e 780
astartes 11:57fa27cb533e 781 for(int try_c = 0; try_c < COMD_EXE_TRIES; try_c ++)
astartes 11:57fa27cb533e 782 {
astartes 11:57fa27cb533e 783 if(getSignalQuality())
astartes 11:57fa27cb533e 784 {
astartes 11:57fa27cb533e 785 break;
astartes 11:57fa27cb533e 786 }
spin7ion 10:51960145754a 787 wd.Service();
spin7ion 10:51960145754a 788 }
astartes 11:57fa27cb533e 789
astartes 11:57fa27cb533e 790
spin7ion 10:51960145754a 791 fixTries=0;
astartes 11:57fa27cb533e 792
astartes 11:57fa27cb533e 793 while(fixTries<FIX_MAX_TRIES && !sendTelemetry())
astartes 11:57fa27cb533e 794 {
astartes 11:57fa27cb533e 795 wait(1);
astartes 11:57fa27cb533e 796 terminateCOAPSession();
astartes 11:57fa27cb533e 797 wait(3);
astartes 11:57fa27cb533e 798 wd.Service();
astartes 11:57fa27cb533e 799 fixTries++;
astartes 11:57fa27cb533e 800 }
astartes 11:57fa27cb533e 801
astartes 11:57fa27cb533e 802 //disconnectNetwork();
astartes 11:57fa27cb533e 803 //initSIM();
astartes 11:57fa27cb533e 804 //enableGPS(0);
astartes 11:57fa27cb533e 805 //setPowerSavingMode();
astartes 11:57fa27cb533e 806 //fixTries++;
spin7ion 10:51960145754a 807 wd.Service();
astartes 11:57fa27cb533e 808 off = 0;
astartes 11:57fa27cb533e 809 while(off<COMD_EXE_TRIES && !setPowerSavingMode())
astartes 11:57fa27cb533e 810 {
astartes 11:57fa27cb533e 811 wd.Service();
astartes 11:57fa27cb533e 812 off++;
astartes 11:57fa27cb533e 813 }
astartes 11:57fa27cb533e 814 #if DEBUG_PC
astartes 11:57fa27cb533e 815 pc.printf("Sim 7000 Off\r\n");
astartes 11:57fa27cb533e 816
astartes 11:57fa27cb533e 817 #endif
astartes 11:57fa27cb533e 818
astartes 11:57fa27cb533e 819 simPWR = 0;
astartes 11:57fa27cb533e 820 wait(2);
astartes 11:57fa27cb533e 821 simPWR = 1;
astartes 11:57fa27cb533e 822 _parser->flush();
astartes 11:57fa27cb533e 823
astartes 11:57fa27cb533e 824
astartes 11:57fa27cb533e 825 //wait(SLEEP_CHECK_TIME);
astartes 11:57fa27cb533e 826 wd.Service();
astartes 11:57fa27cb533e 827
astartes 11:57fa27cb533e 828
spin7ion 6:70d0218c2a28 829 state=STATE_SLEEPING;
astartes 12:7fe416cdac08 830 sleepTimer=0;
astartes 12:7fe416cdac08 831
spin7ion 6:70d0218c2a28 832 } else if(state==STATE_SLEEPING){
spin7ion 10:51960145754a 833 #if DEBUG_PC
spin7ion 10:51960145754a 834 pc.printf("STATE=SLEEPING already for %d\r\n",sleepTimer);
spin7ion 10:51960145754a 835 #endif
spin7ion 6:70d0218c2a28 836 wd.Service();
spin7ion 6:70d0218c2a28 837 sleepTimer+=SLEEP_CHECK_TIME;
spin7ion 6:70d0218c2a28 838
spin7ion 6:70d0218c2a28 839 if(sleepTimer>SLEEP_TIME_S){
astartes 11:57fa27cb533e 840 simPWR = 0;
astartes 11:57fa27cb533e 841 wait(3);
astartes 11:57fa27cb533e 842 simPWR = 1;
astartes 11:57fa27cb533e 843 _parser->flush();
astartes 11:57fa27cb533e 844 #if DEBUG_PC
astartes 11:57fa27cb533e 845 pc.printf("from sleep\r\n");
astartes 11:57fa27cb533e 846 #endif
astartes 11:57fa27cb533e 847 wait(3);
astartes 11:57fa27cb533e 848 wd.Service();
astartes 11:57fa27cb533e 849 wait(3);
astartes 11:57fa27cb533e 850 wd.Service();
astartes 11:57fa27cb533e 851
astartes 11:57fa27cb533e 852 _parser->flush();
astartes 11:57fa27cb533e 853
astartes 11:57fa27cb533e 854
astartes 11:57fa27cb533e 855 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 856 {
astartes 11:57fa27cb533e 857 myled = 1;
astartes 11:57fa27cb533e 858 wait(1);
astartes 11:57fa27cb533e 859 myled = 0;
astartes 11:57fa27cb533e 860 wait(1);
astartes 11:57fa27cb533e 861 wd.Service();
astartes 11:57fa27cb533e 862 myled = 1;
astartes 11:57fa27cb533e 863 }
astartes 11:57fa27cb533e 864 #if DEBUG_PC
astartes 11:57fa27cb533e 865 pc.printf("boot\r\n");
astartes 11:57fa27cb533e 866 #endif
astartes 11:57fa27cb533e 867 for(i=0;i<7;i++)
astartes 11:57fa27cb533e 868 {
astartes 11:57fa27cb533e 869 myled = 1;
astartes 11:57fa27cb533e 870 wait(1);
astartes 11:57fa27cb533e 871 myled = 0;
astartes 11:57fa27cb533e 872 wait(1);
astartes 11:57fa27cb533e 873 wd.Service();
astartes 11:57fa27cb533e 874 myled = 1;
astartes 11:57fa27cb533e 875 }
astartes 12:7fe416cdac08 876 wait(2);
astartes 12:7fe416cdac08 877 wd.Service();
astartes 12:7fe416cdac08 878 wait(2);
astartes 12:7fe416cdac08 879 wd.Service();
astartes 12:7fe416cdac08 880 wait(2);
astartes 12:7fe416cdac08 881 wd.Service();
astartes 11:57fa27cb533e 882
astartes 11:57fa27cb533e 883 int res = initSIM();
astartes 11:57fa27cb533e 884 if(!res)
astartes 11:57fa27cb533e 885 {
astartes 11:57fa27cb533e 886 #if DEBUG_PC
astartes 11:57fa27cb533e 887 pc.printf("Init Failed\r\n");
astartes 11:57fa27cb533e 888 #endif
astartes 12:7fe416cdac08 889 wait(2);
astartes 12:7fe416cdac08 890 wait(2);
astartes 12:7fe416cdac08 891 wd.Service();
astartes 12:7fe416cdac08 892 wait(2);
astartes 12:7fe416cdac08 893 wd.Service();
astartes 11:57fa27cb533e 894 if(!initSIM());
astartes 11:57fa27cb533e 895 {
astartes 11:57fa27cb533e 896 wait(WATCHDOG_INTERVAL_S+10); // reset by dog
astartes 11:57fa27cb533e 897 }
astartes 11:57fa27cb533e 898
astartes 11:57fa27cb533e 899
astartes 11:57fa27cb533e 900
astartes 11:57fa27cb533e 901 }
astartes 11:57fa27cb533e 902
spin7ion 6:70d0218c2a28 903 state=STATE_STARTING_GPS;
spin7ion 6:70d0218c2a28 904 sleepTimer=0;
spin7ion 6:70d0218c2a28 905 } else {
astartes 12:7fe416cdac08 906 wait(2);
astartes 12:7fe416cdac08 907 wait(2);
astartes 12:7fe416cdac08 908 wait(1);
astartes 12:7fe416cdac08 909 // = 5 = SLEEP_CHECK_TIME
spin7ion 6:70d0218c2a28 910 }
spin7ion 10:51960145754a 911 wd.Service();
spin7ion 10:51960145754a 912 #if DEBUG_PC
spin7ion 10:51960145754a 913 pc.printf("After sleep\r\n");
spin7ion 10:51960145754a 914 #endif
astartes 11:57fa27cb533e 915
astartes 11:57fa27cb533e 916 //from_sleep = true;
spin7ion 6:70d0218c2a28 917 }
spin7ion 6:70d0218c2a28 918
spin7ion 6:70d0218c2a28 919 //Feed the watchdog
spin7ion 6:70d0218c2a28 920 wd.Service();
spin7ion 6:70d0218c2a28 921 wait(0.5);
spin7ion 6:70d0218c2a28 922 }
spin7ion 6:70d0218c2a28 923 }
spin7ion 6:70d0218c2a28 924