SD formatting, deleting and reading

Everything about programming the platform and using the Waspmote API
Post Reply
cyril_dewez_hzs
Posts: 3
Joined: Fri Nov 29, 2019 5:42 pm
Company: HZS Antwerpen

SD formatting, deleting and reading

Post by cyril_dewez_hzs » Tue Dec 17, 2019 11:56 pm

Dear Libelium Users and Developers,

I have encountered an issue today while working on my Libelium Plug and Sense Smart Environment PRO.

I had made a code which was measuring and sending the data to the SD card. The code has run a week with working code, collecting around 8,700 measures on a file named "test_1.TXT".
Before putting the Libelium on board a ship to measure the air quality, I wanted to clear the SD card to be sure to have enough place on it. I executed this code :

Code: Select all


boolean answer;

void setup()
{

USB.ON();
SD.ON();

}
void loop()
{
answer = SD.format();  

  if( answer == true ) 
  {
    USB.println(F("SD format OK"));
  }
  else
  {
    USB.println(F("SD format failed"));
  }
  delay(2000);
  }
  
and I always get the answer "SD format failed".

Afterwards, I tried this :

Code: Select all

#define filename "test_1.TXT"     //Must be short
uint8_t sd_answer=1;

void setup()
{
USB.ON();
SD.ON();

}


void loop()
{

sd_answer = SD.del(filename);
  
  if( sd_answer == 1 )
  {
    USB.println(F("file deleted"));
  }
  else 
  {
    USB.println(F("file NOT deleted"));  
  }

    delay(2000);
}
having once again the message "file NOT deleted"...

I then executed the SD_card_menu code, with choice number 9, which is "format SD". I didn't get any answer but when putting back working code, I got all my data on the screen... So even with 3 attends of deleting my file, it remains on the SD card.

Laters, I was working on my working code (on the end of the code). Each time I uploaded the code, at each setup of the Libelium, as SD.showfile is into it, Libelium should show my data, but it once did it, then not...

Then, I tried again SD.delete (same code as in the bottom), I get "file NOT deleted" but when executing working code, I didn't saw my old data but the newest well...

It seems there is a problem with whether the SD card, whether the code. Il all the case, I would like to be sure once onboard, my SD Card will work and store the data.

Does someone has an idea of the origin of this mystery?

Thankfully,

Cyril

Working code:

Code: Select all

#include <WaspSensorGas_Pro.h>      //Libraries pour tous les sensors
//#include <WaspSD.h>                 //SD
int n=1;                            //measure number

//Période entre deux mesures (exprimé en minutes)
//int periode = 2;                   //en minutes
int attend_gros = 13300;

//Période de fonctionnement du lazer et du ventilateur
//#define dust_acquire_time 5000      //en millisecondes

//Nom du fichier
#define filename "test_1.TXT"     //Doit être court

//Socket des sensors
  Gas CO_PRO_sensor(SOCKET_A);
  Gas SO2_PRO_sensor(SOCKET_F);
  Gas O3_PRO_sensor(SOCKET_B);
  Gas NO2_PRO_sensor(SOCKET_C);
  bmeGasesSensor  bme;              //Température sur SOCKET_E


//Variables de fonctionnement des sensors
  float CO_concentration;           //Probes gaz
  float O3_concentration;
  float NO2_concentration;
  float SO2_concentration;
  float temperature;                //Probe température
  float humidity;
  float pressure;
  float PM_1;                       //Probe Particule matter
  float PM_2_5;
  float PM_10;

//Variables pour convertir les valeurs
  char data[200];
  char format_str[] = "%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s";
  char CO_concentration_str[10];           //Probes gaz
  char O3_concentration_str[10];
  char NO2_concentration_str[10];
  char SO2_concentration_str[10];
  char temperature_str[10];                //Probe température
  char humidity_str[10];
  char pressure_str[15];               
  char PM_1_str[15];                       //Probe Particule matter
  char PM_2_5_str[15];
  char PM_10_str[15];
  char battery_level_str[10];


//Dust sensor
  #include <WaspPM.h>               //Librries PM
  #include <WaspFrame.h>
  char dust_info_string[61];
  char dust_serial_number[61];
  int dust_status;
  int dust_measure;
  int dust_acquire_time =1000;            //Temps de fonctionnement du lazer et du ventilateur


//Accelerometer
  uint8_t ACC_status;
  float x_acc;
  float y_acc;
  float z_acc;


//battery level
  int battery_level;
  bool chargeState;
  

//SD write
  int data_number;
  uint8_t sd_answer;
  //float filename = "TEST1.TXT";
  uint8_t data_write_state = 1;

//SD read
  char output[101];
  //uint8_t sd_answer;

//Conversion des valeurs
  char number[20];

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////SETUP///////////////////////////////SETUP////////////////////////////////////SETUP///////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void setup()
{

  USB.ON();                           //Allumer l'USB
/*
//Power state
  chargeState = PWR.getChargingState();


  if (chargeState == true)
  {
    Utils.setLED(LED1, LED_ON);
    delay(500);
    Utils.setLED(LED1, LED_OFF);
    delay(500);
    Utils.setLED(LED1, LED_ON);
    delay(500);
    Utils.setLED(LED1, LED_OFF);
    delay(500);
    Utils.setLED(LED1, LED_ON);
    delay(500);
    Utils.setLED(LED1, LED_OFF);
  }
  else
  {
     Utils.blinkRedLED(5000, 5);
  }
*/
//Allumer les sensors
  CO_PRO_sensor.ON();                 //allumer le sensor CO
  O3_PRO_sensor.ON();                 //allumer le sensor O3
  NO2_PRO_sensor.ON();                //allumer le sensor NO2
  bme.ON();                           //allumer le sensor de température
  SO2_PRO_sensor.ON();                //allumer le sensor O3


//Particule matter
  //dust_status = PM.ON();// switch on sensor
  
//
  

//SD card
  

  SD.ON();
  delay(2000);
SD.showFile(filename);
USB.print(F("Creation of the file...      "));
   sd_answer = SD.create(filename);
   if( sd_answer == 1 )
      {
        USB.println(F("file created"));
      }
      else 
      {
        USB.println(F("file NOT created")); 
      } 

//delay(120000);                                       //Attendre 120 secondes sensors allumés pour avoir de meilleurs résultats

  Utils.setLED(LED1, LED_ON);
  delay(500);
  Utils.setLED(LED1, LED_OFF);
  delay(500);
  Utils.setLED(LED1, LED_ON);
  delay(500);
  Utils.setLED(LED1, LED_OFF);
  delay(500);
  Utils.setLED(LED1, LED_ON);
  delay(500);
  Utils.setLED(LED1, LED_OFF);
  delay(500);
  Utils.setLED(LED1, LED_ON);
  delay(3000);
  Utils.setLED(LED1, LED_OFF);

//CAlculer le délais nécessaire entre les mesures
//attend_gros = (120 - 46,7)*1000;
 
}
 
 

   
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////LOOP///////////////////////////////LOOP////////////////////////////////////LOOP//////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()
{

USB.print(F("Sensoring no "));
USB.println(n);

//RTC
  //RTC.setAlarm1( 0,0,periode,0, RTC_OFFSET, RTC_ALM1_MODE4 );   //periode défini plus haut

//Battery Level
  battery_level = PWR.getBatteryLevel();
  

//Led
if(battery_level > 50)
  { 
  Utils.setLED(LED1, LED_ON);
  delay(1000);
  Utils.setLED(LED1, LED_OFF);  
  }
if(battery_level < 40)
  {
  Utils.setLED(LED1, LED_OFF);
  delay(1000);
  Utils.setLED(LED1, LED_ON);  
  }


//Lire les concentrations des probes
    CO_concentration = CO_PRO_sensor.getConc();
    O3_concentration = O3_PRO_sensor.getConc();
    NO2_concentration = NO2_PRO_sensor.getConc();
    SO2_concentration = SO2_PRO_sensor.getConc();
    temperature = bme.getTemperature();
    humidity = bme.getHumidity();
    pressure = bme.getPressure();

   
//Lecture du dust sensor
    if (battery_level > 60)
    {
      dust_acquire_time = 10000;             //Période d'analyse plus longue et plus précise si batterie pleine
    }
    if (battery_level < 40)
    {
      dust_acquire_time = 5000;              //Période plus courte sur batterie plutôt faible
    }
    dust_status = PM.ON();                  //Allumer dust sensor
      if (dust_status == 1)
      {
      dust_measure = PM.getPM(dust_acquire_time, dust_acquire_time);// Power the fan and the laser and perform a measure of 5 seconds
            
   PM_1 = PM._PM1;
   PM_2_5 = PM._PM2_5;
   PM_10 = PM._PM10;
      }

/*
//Accelerometer
  x_acc = ACC.getX();
  y_acc = ACC.getY();
  z_acc = ACC.getZ();

  USB.print(F("Acceleration x: ")); USB.println(x_acc);
  USB.print(F("Acceleration y: ")); USB.println(y_acc);
  USB.print(F("Acceleration z: ")); USB.println(z_acc);

*/

//Convertir de float à string
  dtostrf( CO_concentration, 1, 3, CO_concentration_str);
  dtostrf( O3_concentration, 1, 3, O3_concentration_str);
  dtostrf( NO2_concentration, 1, 3, NO2_concentration_str);
  dtostrf( SO2_concentration, 1, 3, SO2_concentration_str);
  dtostrf( temperature, 1, 3, temperature_str);
  dtostrf( humidity, 1, 3, humidity_str);
  dtostrf( pressure, 1, 3, pressure_str);
  dtostrf( PM_1, 1, 3, PM_1_str);
  dtostrf( PM_2_5, 1, 3, PM_2_5_str);
  dtostrf( PM_10, 1, 3, PM_10_str);
  //dtostrf( battery_level, 1, 0, battery_level_str);
  
  snprintf( data, sizeof(data), format_str, RTC.getTime(), CO_concentration_str, O3_concentration_str, 
            NO2_concentration_str, SO2_concentration_str, temperature_str, humidity_str, pressure_str,
            PM_1_str, PM_2_5_str, PM_10_str);

//Mémoriser les données dans la carte SD
  sd_answer = SD.append(filename,RTC.getTime());
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,CO_concentration_str); // Convert 134.54342 to string (3 decimals) 
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,O3_concentration_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,NO2_concentration_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,SO2_concentration_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,PM_1_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,PM_2_5_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,PM_10_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,temperature_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,humidity_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.append(filename,";");
  
  sd_answer = SD.append(filename,pressure_str);
  if (sd_answer == 0) {data_write_state = 0;}
  SD.appendln(filename,";");

//  sd_answer = SD.appendln(filename,battery_level_str);
//  if (sd_answer == 0) {data_write_state = 0;}
  

  if (data_write_state == 0)
    {
      Utils.blinkRedLED(3000, 5);
      
    }
  else
    {
        if(battery_level > 50)
            { 
            Utils.setLED(LED1, LED_ON);
            delay(10);
            Utils.setLED(LED1, LED_OFF);  
            }
         if(battery_level < 40)
            {
            Utils.setLED(LED1, LED_OFF);
            delay(10);
            Utils.setLED(LED1, LED_ON);  
            }
    }

data_write_state = 1;


n=n+1;

//delay(attend_gros);
delay(540000);

}

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: SD formatting, deleting and reading

Post by libelium-dev » Thu Dec 19, 2019 11:17 am

Hi,

The SD card could be corrupter. What is the API version used? Did you perform any modification in the SD library or another core library?

COuld you please test the SD example codes and share with the output?
http://www.libelium.com/development/was ... &subcat=sd

What is the serial ID of the node?

Regards

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests