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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Committer:
astartes
Date:
Sun Jan 10 17:02:07 2021 +0000
Revision:
14:b4f8020f0c4a
Parent:
13:6dbc5383b7e0
Child:
15:b0d74907b9c1
udp2

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