Read Arduino Bluetooth Data in Csv File

When you create a datalogging It's important to structure your data, quondam a good solution tin can be JSON format.

ArduinoJson

JavaScript Object Note (JSON) is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and assortment information types (or whatever other serializable value). It is a very common data format used for asynchronous browser–server communication, including as a replacement for XML in some AJAX-fashion systems.

JSON is a language-independent data format. Information technology was derived from JavaScript, but equally of 2017 many programming languages include code to generate and parse JSON-format data. The official Internet media blazon for JSON isapplication/json. JSON filenames use the extension.json. (cit. wiki)

For example, you need to archive and update the value of some totals many times, you can create a structure like this:

{     "lastUpdate": "05/06/2019 06:l:57",     "energyLifetime": 21698620,     "energyYearly": 1363005,     "energyMonthly": 58660,     "energyWeekly": 41858,     "energyDaily": 158 }          

Here an instance of bombardment voltage log:

{     "lastUpdate": "thirty/01/2019 21:24:34",     "data": {         "1004": iii.914468,         "1024": three.931694,         "1044": 3.90479,         "1104": 3.973645,         "1124": three.969726,         "1144": 3.954823,         "1204": three.957871,         "1224": 3.930581,         "1244": 3.954048,         "1304": three.947516,         "1324": 3.945629,         "1344": 3.863081,         "1404": 3.919597,         "1424": three.927387,         "1444": 3.912968,         "1504": three.856597,         "1524": 3.846629,         "1544": 3.903871,         "1604": three.857226,         "1624": 3.889839,         "1644": 3.865693,         "1704": 3.846145,         "1724": three.780726,         "1744": 3.846677,         "1804": 3.770323,         "1824": 3.778887,         "1844": 3.769597,         "1904": 3.778693,         "1924": 3.806177,         "1944": three.801145,         "2004": 3.744049,         "2024": 3.707661,         "2044": iii.780871,         "2104": 3.708484,         "2124": 3.729726,         "0003": 4.138742,         "0023": 4.147887,         "0043": iv.143387,         "0103": 4.139806,         "0123": 4.078258,         "0143": 4.128,         "0203": 4.107871,         "0223": 4.066645,         "0243": four.103419,         "0303": iv.082081,         "0323": 4.126839,         "0343": 4.118032,         "0403": four.096113,         "0423": 4.110532,         "0443": iv.099307,         "0503": iv.013565,         "0523": 4.089581,         "0544": 4.075549,         "0604": 4.025274,         "0624": 4.067129,         "0644": three.997742,         "0704": three.987677,         "0724": 3.981823,         "0744": 4.006113,         "0804": iv.0035,         "0824": 3.966968,         "0844": 4.016418,         "0904": 3.969049,         "0924": 4.002532,         "0944": three.907742     } }          

As you tin can see Information technology's more than readable than CSV or other format, and It'south more versatile.

Library

For Arduino like organisation exist a library that can be considered a standard, you can download It from github or Arduino IDE library management.

Select library ArduinoJson on Arduino IDE

For this library exist a site also very informative.

How to

The usage is quite simply, the difference from previous version is that DynamicJsonDocument is no more dynamic manage of retentivity, so now we can use that document for all (static and dynamic).

const size_t chapters = 1024; DynamicJsonDocument doctor(capacity);          

Information technology'south importanto to calculate the chapters is the max size of your file, to have an thought of the size you need you can check here, Information technology'due south a simple calculator that from file give you the relative size.

To ready the SD you can refer to my article "How to use SD carte with esp8266 and Arduino".

In this example nosotros write a file like:

{   "energyLifetime": 21698620,   "energyYearly": 1363005 }          

A archetype configuration file construction.

I add a annotate on all relevant lawmaking.

/*  Write JSON file to SD  {    "energyLifetime": 21698620,    "energyYearly": 1363005  }  past Mischianti Renzo <https://world wide web.mischianti.org>   https://www.mischianti.org/ */  #include <ArduinoJson.h> #include <SD.h> #include <SPI.h>  const int chipSelect = 4;  const char *filename = "/test.txt";  // <- SD library uses viii.iii filenames  // Prints the content of a file to the Serial void printFile(const char *filename) {   // Open file for reading   File file = SD.open(filename);   if (!file) {     Series.println(F("Failed to read file"));     return;   }    // Extract each characters by i past one   while (file.bachelor()) {     Series.impress((char)file.read());   }   Serial.println();    // Close the file   file.close(); }  void setup() {   // Initialize serial port   Serial.brainstorm(9600);   while (!Series) proceed;    delay(500);    // Initialize SD library   while (!SD.brainstorm(chipSelect)) {     Serial.println(F("Failed to initialize SD library"));     delay(1000);   }    SD.remove(filename);    // Open up file for writing   File file = SD.open up(filename, FILE_WRITE);   if (!file) {     Serial.println(F("Failed to create file"));     return;   }    // Allocate a temporary JsonDocument   // Don't forget to change the chapters to match your requirements.   // Use arduinojson.org/v6/assistant to compute the chapters. //  StaticJsonDocument<512> doc;   // You can use DynamicJsonDocument equally well   DynamicJsonDocument doc(512);    // Set up the values in the document   medico["energyLifetime"] = 21698620;   dr.["energyYearly"] = 1363005;     // Serialize JSON to file   if (serializeJson(doc, file) == 0) {     Serial.println(F("Failed to write to file"));   }    // Close the file   file.shut();    // Impress test file   Series.println(F("Print test file..."));   printFile(filename); }  void loop() {   // not used in this example }          

Generate an array of data, add an element every 5 seconds and update the original file.

The structure generated is similar this:

{   "millis": 10735,   "data": [     {       "prevNumOfElem": 1,       "newNumOfElem": 2     },     {       "prevNumOfElem": two,       "newNumOfElem": 3     },     {       "prevNumOfElem": 3,       "newNumOfElem": 4     }   ] }          

Where millis is overrided and a new value announced on array every time.

/*  Write JSON file to SD  past Mischianti Renzo <https://www.mischianti.org>   https://www.mischianti.org/  */  #include <ArduinoJson.h> #include <SD.h> #include <SPI.h>  const int chipSelect = 4;  const char *filename = "/test.jso";  // <- SD library uses 8.three filenames  // Prints the content of a file to the Series void printFile(const char *filename) { 	// Open file for reading 	File file = SD.open(filename); 	if (!file) { 		Series.println(F("Failed to read file")); 		return; 	}  	// Extract each characters by one by i 	while (file.bachelor()) { 		Series.print((char) file.read()); 	} 	Serial.println();  	// Close the file 	file.shut(); }  void setup() { 	// Initialize serial port 	Serial.begin(9600); 	while (!Serial) 		proceed;  	delay(500);  	// Initialize SD library 	while (!SD.begin(chipSelect)) { 		Serial.println(F("Failed to initialize SD library")); 		filibuster(1000); 	}  	Serial.println(F("SD library initialized"));  	Series.println(F("Delete original file if exists!")); 	SD.remove(filename);  }  void loop() { 	// Allocate a temporary JsonDocument 	// Don't forget to change the capacity to lucifer your requirements. 	// Utilize arduinojson.org/v6/assistant to compute the chapters. 	//  StaticJsonDocument<512> doc; 	// You lot can use DynamicJsonDocument as well 	DynamicJsonDocument doctor(1024);  	JsonObject obj; 	// Open file 	File file = SD.open(filename); 	if (!file) { 		Serial.println(F("Failed to create file, probably not exists")); 		Serial.println(F("Create an empty one!")); 		obj = physician.to<JsonObject>(); 	} else {  		DeserializationError error = deserializeJson(dr., file); 		if (error) { 			// if the file didn't open, impress an mistake: 			Serial.println(F("Error parsing JSON ")); 			Serial.println(error.c_str());  			// create an empty JSON object 			obj = doc.to<JsonObject>(); 		} else { 			// Get THE ROOT OBJECT TO Dispense 			obj = md.as<JsonObject>(); 		}  	}  	// close the file already loaded: 	file.shut();  	obj[F("millis")] = millis();  	JsonArray data; 	// Check if exist the array 	if (!obj.containsKey(F("data"))) { 		Series.println(F("Non find information array! Crete one!")); 		data = obj.createNestedArray(F("data")); 	} else { 		Serial.println(F("Find data array!")); 		information = obj[F("information")]; 	}  	// create an object to add to the array 	JsonObject objArrayData = data.createNestedObject();  	objArrayData["prevNumOfElem"] = data.size(); 	objArrayData["newNumOfElem"] = information.size() + 1;  	SD.remove(filename);  	// Open up file for writing 	file = SD.open up(filename, FILE_WRITE);  	// Serialize JSON to file 	if (serializeJson(dr., file) == 0) { 		Serial.println(F("Failed to write to file")); 	}  	// Close the file 	file.close();  	// Print test file 	Serial.println(F("Print test file...")); 	printFile(filename);  	delay(5000); }          

At present let'due south organize the code a bit. The code in this format is unusable, only with 2 simple functions it should amend.

/*  Write JSON file to SD  by Renzo Mischianti <https://world wide web.mischianti.org>   https://www.mischianti.org/  */  #include <ArduinoJson.h> #include <SD.h> #include <SPI.h>  const int chipSelect = iv;  const char *filename = "/test.jso";  // <- SD library uses 8.three filenames  File myFileSDCart;  /**  * Function to deserialize file from SD  * by Renzo Mischianti <https://www.mischianti.org>  * example:  *  DynamicJsonDocument doctor(1024); 	JsonObject obj; 	obj = getJSonFromFile(&physician, filename);  */ JsonObject getJSonFromFile(DynamicJsonDocument *doc, String filename, bool forceCleanONJsonError = true ) { 	// open the file for reading: 	myFileSDCart = SD.open(filename); 	if (myFileSDCart) { 		// read from the file until there'due south nada else in it: //			if (myFileSDCart.available()) { //				firstWrite = false; //			}  		DeserializationError error = deserializeJson(*doc, myFileSDCart); 		if (mistake) { 			// if the file didn't open, print an error: 			Series.print(F("Error parsing JSON ")); 			Serial.println(error.c_str());  			if (forceCleanONJsonError){ 				return physician->to<JsonObject>(); 			} 		}  		// shut the file: 		myFileSDCart.close();  		return doc->as<JsonObject>(); 	} else { 		// if the file didn't open, print an error: 		Series.print(F("Error opening (or file non exists) ")); 		Serial.println(filename);  		Serial.println(F("Empty json created")); 		return doctor->to<JsonObject>(); 	}  }  /**  * Part to serialize file to SD  * by Renzo Mischianti <https://www.mischianti.org>  * instance:  * boolean isSaved = saveJSonToAFile(&doc, filename);  */ bool saveJSonToAFile(DynamicJsonDocument *doc, String filename) { 	SD.remove(filename);  	// open up the file. note that only one file tin exist open at a time, 	// and then you take to close this one earlier opening another. 	Serial.println(F("Open file in write mode")); 	myFileSDCart = SD.open(filename, FILE_WRITE); 	if (myFileSDCart) { 		Serial.print(F("Filename --> ")); 		Serial.println(filename);  		Series.print(F("Showtime write..."));  		serializeJson(*doc, myFileSDCart);  		Serial.print(F("...")); 		// shut the file: 		myFileSDCart.close(); 		Serial.println(F("washed."));  		render true; 	} else { 		// if the file didn't open, print an error: 		Series.impress(F("Error opening ")); 		Series.println(filename);  		return fake; 	} }  // Prints the content of a file to the Series void printFile(const char *filename) { 	// Open file for reading 	File file = SD.open up(filename); 	if (!file) { 		Serial.println(F("Failed to read file")); 		return; 	}  	// Extract each characters past one by i 	while (file.available()) { 		Serial.print((char) file.read()); 	} 	Series.println();  	// Shut the file 	file.close(); }  void setup() { 	// Initialize series port 	Series.begin(9600); 	while (!Serial) 		keep;  	delay(500);  	// Initialize SD library 	while (!SD.begin(chipSelect)) { 		Series.println(F("Failed to initialize SD library")); 		filibuster(chiliad); 	}  	Series.println(F("SD library initialized"));  	Serial.println(F("Delete original file if exists!")); 	SD.remove(filename);  }  void loop() { 	// Allocate a temporary JsonDocument 	// Don't forget to change the capacity to lucifer your requirements. 	// Use arduinojson.org/v6/banana to compute the capacity. 	//  StaticJsonDocument<512> doc; 	// You can use DynamicJsonDocument as well 	DynamicJsonDocument doc(1024);  	JsonObject obj; 	obj = getJSonFromFile(&doc, filename);  	obj[F("millis")] = millis();  	JsonArray data; 	// Check if exist the array 	if (!obj.containsKey(F("information"))) { 		Serial.println(F("Non find data array! Crete ane!")); 		data = obj.createNestedArray(F("data")); 	} else { 		Serial.println(F("Observe data array!")); 		data = obj[F("information")]; 	}  	// create an object to add to the array 	JsonObject objArrayData = data.createNestedObject();  	objArrayData["prevNumOfElem"] = data.size(); 	objArrayData["newNumOfElem"] = data.size() + 1;   	boolean isSaved = saveJSonToAFile(&doc, filename);  	if (isSaved){ 		Serial.println("File saved!"); 	}else{ 		Serial.println("Error on save File!"); 	}  	// Impress exam file 	Series.println(F("Print examination file...")); 	printFile(filename);  	delay(5000); }          

At present I think it's improved and it's pretty clear.

Thanks


solisrourt1999.blogspot.com

Source: https://www.mischianti.org/2020/01/26/manage-json-file-with-arduino-and-esp8266/

0 Response to "Read Arduino Bluetooth Data in Csv File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel