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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Sat Jan 16 06:54:49 2021 +0000
Revision:
15:b0d74907b9c1
Parent:
14:b4f8020f0c4a
Child:
16:319eece233ff
new config

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