-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathFileUtilsJsonFile.ino
121 lines (88 loc) · 2.5 KB
/
FileUtilsJsonFile.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Manage JSON configurations via GSM module filesystem.
This sketch demonstrates how to use the internal filesystem
of the GSM module to store and retrieve data and configurations
both as JSON objects or C++ structs.
Circuit:
* MKR GSM 1400 board
created 19 June 2020
by Giampaolo Mancini
*/
#include <Arduino_JSON.h>
#include <MKRGSM.h>
#include "Config.h"
GSMFileUtils fileUtils;
void setup()
{
Serial.begin(9600);
while (!Serial)
;
Serial.println("Store and Retrieve JSON data to GSM module storage.");
Serial.println();
fileUtils.begin();
simpleDemo();
structDemo();
while (true)
;
}
void loop()
{
}
void simpleDemo()
{
Serial.println();
Serial.println("========================");
Serial.println("Running simple JSON demo");
Serial.println();
JSONVar myObject;
myObject["hello"] = "world";
myObject["true"] = true;
myObject["x"] = 42;
String jsonString = JSON.stringify(myObject);
Serial.println("Saving JSON file (test.json): ");
Serial.println(jsonString);
Serial.println();
fileUtils.downloadFile("test.json", jsonString);
printFiles(fileUtils);
Serial.println();
String jsonData;
Serial.println("Reading JSON file (test.json): ");
fileUtils.readFile("test.json", &jsonData);
Serial.println("File contents:");
Serial.println(jsonData);
Serial.println();
Serial.println("Parsing JSON contents:");
JSONVar myConf = JSON.parse(jsonData);
Serial.print("myConf[\"hello\"]: ");
Serial.println(myConf["hello"]);
Serial.print("myConf[\"true\"]: ");
Serial.println(myConf["true"]);
Serial.print("myConf[\"x\"]: ");
Serial.println(myConf["x"]);
}
void structDemo()
{
GSMModem modem;
Serial.println();
Serial.println("========================");
Serial.println("Running Configuration via struct and JSON demo");
Serial.println();
Serial.println("Creating configuration struct:");
Config conf;
conf.deviceId = modem.getICCID();
conf.timestamp = millis();
Serial.print(conf);
fileUtils.downloadFile("conf.json", conf.toJson());
Serial.println();
printFiles(fileUtils);
Serial.println();
Serial.println("Reading configuration file:");
String jsonConf;
fileUtils.readFile("conf.json", &jsonConf);
Serial.println(jsonConf);
Serial.println();
Serial.println("Reading configuration struct:");
Config newConf;
newConf.fromJSON(jsonConf);
Serial.print(newConf);
}