SD 07: Datalogger

This example shows how to store formatted frames in SD file using the Data Frame class. Besides, it shows how to get these frames from the file and get them ready to be sent.

Required Materials

1 x Waspmote 1 x Battery 1 x SD card 1 x MiniUSB wire


- Remember that only 8.3 SHORT FILE NAME are valid for files and directories - Make sure Waspmote is switched off before inserting or removing the SD card. Otherwise, the SD card could be damaged. - Waspmote can not be switched off or reseted while there are ongoing read or write operations in the SD card. Otherwise, the SD card could be damaged and data could be lost. If you suspect that there may be some ongoing SD operations, wait a while until they are completed. - This example can be executed in Waspmote v12 and Waspmote v15


 *  ------ [SD_07] - Datalogger to store info in SD file -------- 
 *  Explanation: This example shows how to store formatted frames in 
 *  SD file using the Data Frame class. Besides, it shows how to get
 *  these frames from the file and get them ready to be sent.
 *  Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L. 
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  GNU General Public License for more details. 
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see . 
 *  Version:           3.0
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona

// define folder and file to store data
char path[]="/data";
char filename[]="/data/log";

// buffer to write into Sd File
char toWrite[200];

// define variables to read stored frames 
uint8_t frameSD[MAX_FRAME+1];
uint16_t lengthSD;
int32_t numLines;

// variables to define the file lines to be read
int startLine;
int endLine;

// define variable
uint8_t sd_answer;

void setup()
  // open USB port
  USB.println(F("SD_07 example"));

  // Set SD ON

  // create path
  sd_answer = SD.mkdir(path);
  if( sd_answer == 1 )
    USB.println(F("path created"));
    USB.println(F("mkdir failed"));

  // Create file for Waspmote Frames
  sd_answer = SD.create(filename);
  if( sd_answer == 1 )
    USB.println(F("/data/log created"));
    USB.println(F("/data/log not created"));

  // init Accelerometer


void loop()
  // 1. Create Data frame using the Waspmote Frame class

  // Create new frame (ASCII)

  // add frame field (String message)
  frame.addSensor(SENSOR_STR, (char*) "data frame");
  // add frame field (Battery level)
  frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
  // add frame field (Accelerometer axis)
  frame.addSensor(SENSOR_ACC, ACC.getX(), ACC.getY(), ACC.getZ() );
  //show actual Frame 

  memset(toWrite, 0x00, sizeof(toWrite) ); 

  // Conversion from Binary to ASCII
  Utils.hex2str( frame.buffer, toWrite, frame.length);

  USB.print(F("Frame to be stored:"));

  // 2. Append data into file
  sd_answer = SD.appendln(filename, toWrite);
  if( sd_answer == 1 )
    USB.println(F("Frame appended to file"));
    USB.println(F("Append failed"));

  // 3. Get information from File

  // get number of lines in file
  numLines = SD.numln(filename);
  // get specified lines from file
  // get only the last file line
  startLine = numLines-1; 
  endLine = numLines;
  // iterate to get the File lines specified
  for( int i=startLine; i SD.buffer
    SD.catln( filename, i, 1); 
    // initialize frameSD
    memset(frameSD, 0x00, sizeof(frameSD) ); 
    // conversion from ASCII to Binary 
    lengthSD = Utils.str2hex(SD.buffer, frameSD );

    // Conversion from ASCII to Binary 
    USB.print(F("Get previously stored frame:"));
    for(int j=0; j


 SD_07 example
 path created
 /data/log created
 Current ASCII Frame:
 Length: 78
 Frame Type: 134
 frame (HEX): 3C3D3E8603233444334337303633443933373432463423576173706D6F74655F50726F2330235354523A64617461206672616D65234241543A313030234143433A2D32393B2D33373B3130303123
 frame (STR): <=>�#4D3C7063D93742F4#Waspmote_Pro#0#STR:data frame#BAT:100#ACC:-29;-37;1001#
 Frame to be stored:3C3D3E8603233444334337303633443933373432463423576173706D6F74655F50726F2330235354523A64617461206672616D65234241543A313030234143433A2D32393B2D33373B3130303123
 Frame appended to file
 Get previously stored frame:<=>�#4D3C7063D93742F4#Waspmote_Pro#0#STR:data frame#BAT:100#ACC:-29;-37;1001#

Last updated