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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Sun Jan 10 16:29:35 2021 +0000
Revision:
13:6dbc5383b7e0
Parent:
12:7fe416cdac08
Child:
14:b4f8020f0c4a
udp;

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