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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Mon Jan 18 06:23:53 2021 +0000
Revision:
19:1d4d31c23953
Parent:
18:11e82e17446d
last_one

Who changed what in which revision?

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