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

Dependencies:   mbed mbed-STM32F103C8T6 MLX90614 Watchdog DS1820

Revision:
19:1d4d31c23953
Parent:
18:11e82e17446d
--- a/main.cpp	Sun Jan 17 12:09:27 2021 +0000
+++ b/main.cpp	Mon Jan 18 06:23:53 2021 +0000
@@ -1,3 +1,4 @@
+
 #include "stm32f103c8t6.h"
 #include "mbed.h"
 #include "DS1820.h"
@@ -41,9 +42,9 @@
 // GPS
 int Fix_st;
  
-float B_l;
-float L_l;
-float Alt;
+float B_l = 0;
+float L_l = 0;
+float Alt = 0;
  
 bool cold_start  = 0;
 int step_p = 0;
@@ -62,21 +63,7 @@
 int sleepTimer=0;
 int fixTries=0;
 float a = 66;
-void waste_time(int x)
-{ 
-    
-    for(int i; i < 10000000; i++)
-    {
-        for(int k; k < 10000000 * x * 5; k++)
-        {
-            a = a / 33;
-            a = a * 33;
-        }
-    }
- 
- 
- 
-}
+
  
  
 void parseTime (float timeval)
@@ -90,13 +77,15 @@
  
  
 bool checkIfOk() {
-    if(_parser->recv("OK")) {
+    if(_parser->recv("OK")) 
+    {
         #if DEBUG_PC
         pc.printf("Done\r\n");
         #endif
         
         return true;
-    } else {
+    } else 
+    {
         #if DEBUG_PC
         pc.printf("Fail\r\n");
         #endif
@@ -106,24 +95,25 @@
 }
 void blink_fast()
 {
-    for(i=0;i<5;i++)
+    for(int b = 0;b < 3;b++)
     {
-    myled = 1;
-    wait(0.5);
-    myled = 0;
-    wait(0.5);
-    myled = 1;
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+        myled = 1;
     }
 }
 void blink_slow()
 {
-    for(i=0;i<5;i++)
+    for(int b = 0;b < 3;b++)
     {
-    myled = 1;
-    wait(1);
-    myled = 0;
-    wait(1);
-    myled = 1;
+        myled = 1;
+        wait(1);
+        myled = 0;
+        wait(1);
+        myled = 1;
+        wd.Service();
     }
 }
  
@@ -148,7 +138,7 @@
     {
        return false;
     }
-
+ 
     _parser->send("AT+CMNB=2");
     if (!checkIfOk())
     {
@@ -174,7 +164,12 @@
     {
        return false;
     }
-
+    _parser->send("ATE0");
+    if (!checkIfOk())
+    {
+       return false;
+    }
+ 
     return true;
 }
  
@@ -191,7 +186,15 @@
     return checkIfOk();  
     //return true;  
 }
- 
+bool enableRF_RESTART()
+{
+    #if DEBUG_PC
+    pc.printf("Powering RF with restart\r\n");
+    #endif
+    _parser->send("AT+CFUN=1,1"); 
+}
+
+
 bool setPowerSavingMode() 
 {
     _parser->send("AT+CPSMS=1");//power save on 
@@ -253,7 +256,7 @@
             return 0;
         }
     }
-
+ 
     fixTries=0;
     while(fixTries<COMD_EXE_TRIES && !setSatSystems())
     {
@@ -304,7 +307,7 @@
  
  
     _parser->send("AT+CGNSINF");
- 
+    wait(0.2);
     int nmeaStrLen=_parser->read(receivedChars, 120);
     _parser->flush();
     char *curLine = receivedChars;
@@ -384,10 +387,13 @@
     int GNSS_st = atof(GNSSrunstatus_c); 
     Fix_st      = atof(Fixstatus_c); 
     int sat_count = atof(satellitesinview_c); 
- 
-    B_l = atof(latitude_c);
-    L_l = atof(logitude_c);
-    Alt = atof(altitude_c);
+    if(Fix_st)
+    {
+        B_l = atof(latitude_c);
+        L_l = atof(logitude_c);
+        Alt = atof(altitude_c);
+    }
+
  
     #if DEBUG_PC
     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);
@@ -412,27 +418,11 @@
  
 int terminateCOAPSession() {
  
-    for(int try_c = 0; try_c < 5; try_c++)
-    {
-    _parser->flush(); 
-    _parser->send("AT+CCOAPTERM");
-    int r1 = checkIfOk();
-    wait(0.5);
+    //_parser->send("AT+CCOAPTERM");
+    //wait(2);
+    //wd.Service();
+
     _parser->send("AT+CNACT=0");
-    int r2 = checkIfOk();
-    if (r1 == r2 == true)
-    {
-        return true;
-    }
-    else
-    {
-        if(try_c >=5)
-        {
-            return false;
-        }
-    }
-    }
- 
 }
  
  
@@ -450,27 +440,39 @@
     wait(2);
  
     _parser->flush();   
+
     _parser->send("AT+CCOAPINIT");
-  
- 
     wait(1);
-     _parser->flush(); 
+
+    _parser->flush(); 
     //_parser->send("AT+CCOAPURL=\"" MTS_TELEMETRY_URL_STRING);
+    
     _parser->send("AT+CCOAPURL=\"coap://193.227.232.26:5683/multisensors\"");
     checkIfOk();
     //if(!checkIfOk()){ terminateCOAPSession();return false;}
     wait(1);
      _parser->flush(); 
+
     //_parser->printf("AT+CCOAPPARA=code,2,token,0,\"%s\",payload,1,",xstr(MTS_COAP_TOKEN));
     //_parser->printf("AT+CCOAPPARA=code,2,payload,1,");
+    
+    pc.printf("\r\nAT+CCOAPPARA=\"CODE\",2,payload,1,");
+      for (size_t i = 0; i < strlen(bufferString); i++) {
+    pc.printf("%x",bufferString[i]);
+    }
+    pc.printf("\r\n");
 
-    _parser->printf("AT+CCOAPPARA=code,2,payload,1,");
+    _parser->printf("AT+CCOAPPARA=\"CODE\",2,payload,1,");
 
-    for (size_t i = 0; i < strlen(bufferString); i++) {
+    //_parser->send("AT+CCOAPPARA=\"CODE\",2,payload,1,\"7b224944223a20224d49454d4853452d54455354222c2274656d704952223a202d312e3636393938392c226c61746974756465223a202d36392e3337393135382c226c6f6e676974756465223a2037362e3338393339372c22616c746974756465223a20362e342c2276616c696447656f223a20747275652c2262617474657279223a203130302c226e65746c766c223a2033312c2274656d7073223a5b332e37352c312e3837352c332e352c332e333132352c332e343337352c342e32352c322e3632352c302e313837352c342e3132352c302e3837355d7d\"");
+    //_parser->printf(bufferString);
+   
+    for (size_t i = 0; i < strlen(bufferString); i++) 
+    {
     _parser->printf("%x",bufferString[i]);
     }
     _parser->printf("\r\n");
-    //parser->printf("\r\n");
+
     checkIfOk();
     //if(!checkIfOk()){ terminateCOAPSession();return false;}
     wait(0.5);
@@ -481,13 +483,9 @@
     wait(2);
     wd.Service();
     wait(2);
-    wd.Service();
     wait(2);
     wd.Service();
     wait(2);
- 
-    terminateCOAPSession();
-    
     return true;
 }
 int main() {
@@ -497,22 +495,13 @@
     wd.Configure(WATCHDOG_INTERVAL_S);       // sets the timeout interval
     #endif
  
-    simPWR = 0; 
-    wait(2);
-    simPWR = 1;
+    //simPWR = 0; 
+    //wait(2);
+    //simPWR = 1;
  
-    //blink_slow();
-    wd.Service();
-    for(i=0;i<7;i++)
-    {
-    //myled = 1;
-    wait(0.5);
-    //myled = 0;
-    wait(0.5);
-    wd.Service();
-    //myled = 1;
-    }
- 
+    blink_slow();
+   
+   
     //wait(7);
     //toggle_sim_power();
     /*
@@ -540,50 +529,40 @@
     _serial = new UARTSerial(PIN_SIM_TX, PIN_SIM_RX, 115200);
     _parser = new ATCmdParser(_serial);
     _parser->debug_on( DEBUG_SIM );
-    _parser->set_delimiter( "\r\n" );
-    _parser->set_timeout(PARSER_TIMEOUT_S);
-    wait(2);
- 
-    wd.Service();
+    _parser->set_delimiter("\r\n");
+    _parser->set_timeout(10);
+
     wait(2);
     wd.Service();
     wait(2);
-    wd.Service();
+    _parser->flush(); 
+    enableRF_RESTART();
+    
     wait(2);
     wd.Service();
     wait(2);
-    wd.Service();
     wait(2);
     wd.Service();
     wait(2);
- 
+    wait(2);
+
+    
     fixTries=0;
  
     #if DEBUG_PC
-    pc.printf("INIT START\r\n");
+    pc.printf("INIT CONFIG\r\n");
     #endif
  
-    
- 
- 
     int res = initSIM();
     if(!res)
     {
         #if DEBUG_PC
         pc.printf("Init Failed\r\n");
         #endif
-    wd.Service();
-    wait(2);
-    wd.Service();
-    wait(2);
-    wd.Service();
-    wait(2);
-    wd.Service();
-    wait(2);
-    wd.Service();
-    wait(2);
-    wd.Service();
-    wait(2);
+        wd.Service();
+        wait(2);
+        wd.Service();
+        wait(2);
         if(!initSIM());
         {
             wait(WATCHDOG_INTERVAL_S+10); // reset by dog 
@@ -594,29 +573,13 @@
     pc.printf("INIT finished\r\n");
     #endif
  
-    for(i=0;i<7;i++)
-    {
-    myled = 1;
-    wait(0.5);
-    myled = 0;
-    wait(0.5);
-    wd.Service();
-    myled = 1;
-    }
-    
+    blink_fast();
  
     wd.Service();
     
-    /*_parser->send("AT+CPSI?");
-    checkIfOk();
-    _parser->send("AT+CGNAPN");
-    checkIfOk();
-    _parser->send("AT+CAPNMODE=1");
-    checkIfOk();
-    _parser->send("AT+CREG?");
-    checkIfOk(); 
-    */
+
     getSignalQuality();
+
     #if DEBUG_PC
     pc.printf("Signal quality: %d\r\n",rssiDB);
     #endif
@@ -641,12 +604,13 @@
     //Feed the watchdog
     wd.Service();
  
-    //state=STATE_STARTING_GPS;
-    state = STATE_COLLECTING_TELEMETRY;
+    state=STATE_STARTING_GPS;
+    //state = STATE_COLLECTING_TELEMETRY;
     
     int off = 0;
  
-    while(1) {
+    while(1) 
+    {
         wd.Service();
         if (state==STATE_STARTING_GPS)
         {
@@ -816,7 +780,9 @@
                     ds1820[sensorsOrder[i]]->startConversion();
                 }    
             }
+            
             wait(1);
+            pc.printf("startConversion\r\n");
             for(i=0;i<SENSORS_COUNT;i++){
                 if(sensorsOrder[i]<sensors_found){
                     if(ds1820[sensorsOrder[i]]->isPresent()){
@@ -826,31 +792,13 @@
                     }
                 }
             }
-        
+             pc.printf("startConversion\r\n");
+           
             IRtemp=thermometer.read_temp(1);
-            
-            float bat_v = 100;
-            //Form JSON as {"tempIR":1,"temps":[1,...,10],"latitude":37,"longitude":51,"altitude":21,"validGeo":true}
-             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);
-   //  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
-           // device_id
-            index = strlen(bufferString);
-            
-            for(i=0;i<SENSORS_COUNT;i++){
-                index += snprintf(&bufferString[index], 2048-index, i==0?"%4.4f":",%4.4f", stickTemperatures[i]);
-            }
-            strcat (bufferString,"]}");
-            
-            wd.Service();
-            state=STATE_SENDING_TELEMETRY;
-        } 
-        else if(state==STATE_SENDING_TELEMETRY)
-        {
-            #if DEBUG_PC
-            pc.printf("STATE=SENDING TELEMETRY\r\n");
-            pc.printf(bufferString);
-            #endif
-         
+
+            pc.printf("ik done\r\n");
+            float bat_v = 3.2;
+
             for(int try_c = 0; try_c < COMD_EXE_TRIES; try_c ++)
             {
                 if(getSignalQuality())
@@ -859,19 +807,114 @@
                 }
                 wd.Service();
             }
+
+            if (rssiDB > 100)
+            {
+                rssiDB = 100;
+            }
+            else if (rssiDB < 0)
+            {
+                rssiDB = 0;
+            }
+
+            if (IRtemp > 100)
+            {
+                IRtemp = 100;
+            }
+            else if (IRtemp < -300)
+            {
+                IRtemp = -300;
+            }
             
+            if (B_l > 200)
+            {
+                B_l = 200;
+            }
+            else if (B_l < -200)
+            {
+                B_l = -200;
+            }
+            
+            if (L_l > 200)
+            {
+                L_l = 200;
+            }
+            else if (L_l < -200)
+            {
+                L_l = -200;
+            }
+            
+            if (Alt > 9000)
+            {
+                Alt = 9000;
+            }
+            else if (Alt < -200)
+            {
+                Alt = -200;
+            }
+            
+ 
+
+            //Form JSON as {"tempIR":1,"temps":[1,...,10],"latitude":37,"longitude":51,"altitude":21,"validGeo":true}
+             //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);
+             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);
+  //  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
+           // device_id
+
+           
+            index = strlen(bufferString);
+            pc.printf("index = %d \r\n", index);
+
+            for(i=0;i<SENSORS_COUNT;i++){
+                //index += snprintf(&bufferString[index], 2048-index, i==0?"%f":",%f", stickTemperatures[i]);
+                if (stickTemperatures[i] > 100)
+                {
+                    stickTemperatures[i] = 100;
+                }
+                else if (stickTemperatures[i] < -280)
+                {
+                    stickTemperatures[i] = -280;
+                }
+                index += snprintf(&bufferString[index], 2048-index, i==0?"%4.4f":",%4.4f", stickTemperatures[i]);
+            }
+            strcat (bufferString,"]}");
+
+            index = strlen(bufferString);
+            pc.printf("\r\n index = %d", index);
+            
+            for (size_t i = 0; i < strlen(bufferString); i++) 
+            {
+             pc.printf("%c",bufferString[i]);
+            }
+        
+            wd.Service();
+            state=STATE_SENDING_TELEMETRY;
+        } 
+        else if(state==STATE_SENDING_TELEMETRY)
+        {
+            #if DEBUG_PC
+            pc.printf("\r\nSTATE=SENDING TELEMETRY\r\n");
+            pc.printf(bufferString);
+            #endif
+         
+          
          
             fixTries=0;
             
-            while(fixTries<FIX_MAX_TRIES && !sendTelemetry())
+            if(!sendTelemetry())
             {
-                wait(2);
                 terminateCOAPSession();
+                wd.Service();
                 wait(2);
-                wd.Service();
-                fixTries++;
+                 wait(2);
+                  wait(2);
+                  wd.Service();
+                   wait(2);
+                sendTelemetry();
             }
-
+            wait(1);
+            terminateCOAPSession();
+ 
             //disconnectNetwork();
             //initSIM();
             //enableGPS(0); 
@@ -879,7 +922,7 @@
             //fixTries++;
             wd.Service();
             enableRF(0);
-
+ 
             off = 0;
             while(off<COMD_EXE_TRIES && !setPowerSavingMode())
             {
@@ -941,15 +984,7 @@
                 #if DEBUG_PC
                 pc.printf("boot\r\n");
                 #endif
-                for(i=0;i<7;i++)
-                {
-                    myled = 1;
-                    wait(1);
-                    myled = 0;
-                    wait(1);
-                    wd.Service();
-                    myled = 1;
-                }
+                blink_fast();
     wd.Service();
     wait(2);
     wd.Service();
@@ -990,8 +1025,8 @@
                    
                 }
  
-                //state=STATE_STARTING_GPS;
-                state = STATE_COLLECTING_TELEMETRY;
+                state=STATE_STARTING_GPS;
+                //state = STATE_COLLECTING_TELEMETRY;
                 sleepTimer=0;
             } else {
                wait(2);  
@@ -1011,4 +1046,5 @@
         wd.Service();
         wait(0.5);
     }
-}
\ No newline at end of file
+}
+