質問編集履歴

3

情報追記の為

2020/08/08 08:56

投稿

rokurou
rokurou

スコア2

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,375 @@
9
9
  一つのファイルに一定以上の容量が溜まったら新しいファイルを作成するなどで対応できるでしょうか?
10
10
 
11
11
  一般的な対策があれば教えていただきたいと思います。
12
+
13
+
14
+
15
+ ***************************************
16
+
17
+
18
+
19
+ 8月8日追記
20
+
21
+ 多くの方からご意見を頂きましてありがとうございます。
22
+
23
+ 情報不足との指摘をいただき、追記をさせていただきます。
24
+
25
+ また前回質問した問題点を「問題1」とし、「問題2」を新たに追加しました。
26
+
27
+ 文末にプログラムを記載します。
28
+
29
+
30
+
31
+ 使用部材
32
+
33
+ ・ESP32-DevKitC http://akizukidenshi.com/catalog/g/gM-11819/
34
+
35
+ ・BME280 http://akizukidenshi.com/catalog/g/gK-09421/
36
+
37
+ ・MicroSDcard Adapter https://www.amazon.co.jp/HiLetgo-3%E5%80%8B%E3%82%BB%E3%83%83%E3%83%88-TF%E3%82%AB%E3%83%BC%E3%83%89%E3%83%A1%E3%83%A2%E3%83%AA%E3%82%B7%E3%83%BC%E3%83%AB%E3%83%89%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB-Arduino%E3%81%AB%E5%AF%BE%E5%BF%9C-SPI%E3%83%9E%E3%82%A4%E3%82%AF%E3%83%ADSD%E3%82%A2%E3%83%80%E3%83%97%E3%82%BF/dp/B010GXAFFU
38
+
39
+ ・MicroSDcard 4GB https://www.amazon.co.jp/ADATA-microSDHC%E3%82%AB%E3%83%BC%E3%83%89-4GB-Class4-AUSDH4GCL4-RA1/dp/B002XVX6LY
40
+
41
+
42
+
43
+ 目的
44
+
45
+ 1秒ごとにBME280で温度湿度気圧を取得し、NTPで取得した時刻と取得データをSDカードへCSVファイルとして保存したい。
46
+
47
+
48
+
49
+ 問題1
50
+
51
+ 2時間起動したが、1時間半分のデータしか保存されていない(14:55から17:00まで起動したが、14:55から16:32までしか保存されていない。また最後のみ17:00のレコードが保存されている)。なおCSVファイルは65kBであった。
52
+
53
+
54
+
55
+ 問題2
56
+
57
+ 1秒間隔でデータを取得しているので理屈の上では1分間に60レコードであるはず(プログラム起動のため実際にはその限りではないはずであるが)。
58
+
59
+ しかし14:56は55レコードであるが、16:31では8レコードであった。
60
+
61
+
62
+
63
+ 期待する解決
64
+
65
+ ・起動中は常にデータを保存したい
66
+
67
+ ・時間経過によるレコード数の低下を防ぎたい
68
+
69
+
70
+
71
+ 以上です。
72
+
73
+ 可能な限り情報を追加したつもりですが、不足があった場合ご教示頂けますと幸いです。
74
+
75
+ よろしくお願いします。
76
+
77
+
78
+
79
+ ![イメージ説明](f997e9cc0214e22ee0e6916b52fd1e4c.png)
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+ 以下プログラム
88
+
89
+
90
+
91
+ ```
92
+
93
+ #include "secrets.h"
94
+
95
+ #include <WiFi.h>
96
+
97
+ #include <Wire.h>
98
+
99
+ #include "SparkFunBME280.h"
100
+
101
+ #include <SD.h>
102
+
103
+ #include <SPI.h>
104
+
105
+
106
+
107
+ #define FILE_NAME "/log.csv"
108
+
109
+
110
+
111
+ //SPI_setting
112
+
113
+ #define SCK 5
114
+
115
+ #define MISO 16
116
+
117
+ #define MOSI 17
118
+
119
+ #define SS 18
120
+
121
+
122
+
123
+ //データ取得間隔
124
+
125
+ int delay_time = 1000 * 1; //1秒間隔
126
+
127
+
128
+
129
+ //SD_setting
130
+
131
+ const uint8_t SD_CS = SS; // GPIO5=CS
132
+
133
+ static File myFile;
134
+
135
+
136
+
137
+ //WIFI_setting
138
+
139
+ char ssid[] = SECRET_SSID; // your network SSID (name)
140
+
141
+ char pass[] = SECRET_PASS; // your network password
142
+
143
+ int keyIndex = 0; // your network key Index number (needed only for WEP)
144
+
145
+ WiFiClient client;
146
+
147
+
148
+
149
+ // BME280_setting
150
+
151
+ BME280 mySensorA; //Uses default I2C address 0x77
152
+
153
+ BME280 mySensorB; //Uses I2C address 0x76 (jumper closed)
154
+
155
+ float Temp;
156
+
157
+ float Hum;
158
+
159
+ float hPa;
160
+
161
+
162
+
163
+ //SD
164
+
165
+ void SD_init() {
166
+
167
+ Serial.print("Initializing SD card...");
168
+
169
+ if (!SD.begin(SD_CS)) {
170
+
171
+ Serial.println("initialization failed!");
172
+
173
+ return;
174
+
175
+ }
176
+
177
+ Serial.println("initialization done.");
178
+
179
+ }
180
+
181
+
182
+
183
+ //SD_read
184
+
185
+ String SD_read() {
186
+
187
+ String str;
188
+
189
+ File file = SD.open(FILE_NAME, FILE_READ);
190
+
191
+ if (file) {
192
+
193
+ //---1byteずつ読み込んだ文字を結合
194
+
195
+ while (file.available()) {
196
+
197
+ str += char(file.read());
198
+
199
+ }
200
+
201
+ } else {
202
+
203
+ Serial.println(" error...");
204
+
205
+ }
206
+
207
+ //---ファイルを閉じる
208
+
209
+ file.close();
210
+
211
+ return str;
212
+
213
+ }
214
+
215
+
216
+
217
+ void setup() {
218
+
219
+ delay(1000);
220
+
221
+ SPI.begin(SCK, MISO, MOSI, SS);
222
+
223
+ Serial.begin(115200); //Initialize serial
224
+
225
+
226
+
227
+ //NTP_setup
228
+
229
+ if (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_DISCONNECTED) {
230
+
231
+ ESP.restart();
232
+
233
+ }
234
+
235
+ while (WiFi.status() != WL_CONNECTED) {
236
+
237
+ delay(1000);
238
+
239
+ Serial.println("wifi_disconnected...");
240
+
241
+ }
242
+
243
+ configTime(9 * 3600L, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");
244
+
245
+
246
+
247
+ //wifi_setup
248
+
249
+ Wire.begin();
250
+
251
+ WiFi.mode(WIFI_STA);
252
+
253
+
254
+
255
+ while (!Serial);
256
+
257
+ Serial.println();
258
+
259
+
260
+
261
+ //SD_setup
262
+
263
+ SD_init();
264
+
265
+ Serial.println(SD_read());
266
+
267
+
268
+
269
+ //BME280_setup
270
+
271
+ mySensorA.setI2CAddress(0x76); //The default for the SparkFun Environmental Combo board is 0x77 (jumper open).
272
+
273
+ if (mySensorA.beginI2C() == false) Serial.println("Sensor A connect failed");
274
+
275
+ mySensorB.setI2CAddress(0x76); //Connect to a second sensor
276
+
277
+ if (mySensorB.beginI2C() == false) Serial.println("Sensor B connect failed");
278
+
279
+ }
280
+
281
+
282
+
283
+ struct tm timeInfo;//時刻を格納するオブジェクト
284
+
285
+ char s[20];//文字格納用
286
+
287
+
288
+
289
+ void loop() {
290
+
291
+ //BME280
292
+
293
+ Temp = mySensorA.readTempC();
294
+
295
+ Hum = mySensorA.readFloatHumidity();
296
+
297
+ hPa = mySensorA.readFloatPressure() / 100;
298
+
299
+
300
+
301
+ //NTP
302
+
303
+ getLocalTime(&timeInfo);//tmオブジェクトのtimeInfoに現在時刻を入れ込む
304
+
305
+ sprintf(s, "%04d/%02d/%02d %02d:%02d:%02d",
306
+
307
+ timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
308
+
309
+ timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);//人間が読める形式に変換
310
+
311
+
312
+
313
+ //Serial.print
314
+
315
+ Serial.println(s);
316
+
317
+ Serial.print("Temp: ");
318
+
319
+ Serial.print(Temp);
320
+
321
+ Serial.println(" ℃");
322
+
323
+ Serial.print("Hum: ");
324
+
325
+ Serial.print(Hum);
326
+
327
+ Serial.println(" %");
328
+
329
+ Serial.print("Press: ");
330
+
331
+ Serial.print(hPa);
332
+
333
+ Serial.println(" hPa");
334
+
335
+
336
+
337
+ //SD_writting
338
+
339
+ String backLog = SD_read();
340
+
341
+ myFile = SD.open(FILE_NAME, FILE_WRITE);
342
+
343
+ if (myFile) {
344
+
345
+ Serial.print("Writing to ");
346
+
347
+ Serial.println(FILE_NAME);
348
+
349
+ myFile.print(backLog);
350
+
351
+ myFile.print(s);
352
+
353
+ myFile.print(",");
354
+
355
+ myFile.print(Temp);
356
+
357
+ myFile.print(",");
358
+
359
+ myFile.print(Hum);
360
+
361
+ myFile.print(",");
362
+
363
+ myFile.print(hPa);
364
+
365
+ myFile.println();
366
+
367
+ myFile.close();
368
+
369
+ Serial.println("done.");
370
+
371
+ } else {
372
+
373
+ Serial.print("error opening ");
374
+
375
+ Serial.println(FILE_NAME);
376
+
377
+ }
378
+
379
+ delay(delay_time); // Wait time
380
+
381
+ }
382
+
383
+ ```

2

初心者タグをつけました

2020/08/08 08:56

投稿

rokurou
rokurou

スコア2

test CHANGED
File without changes
test CHANGED
@@ -6,4 +6,6 @@
6
6
 
7
7
  上記の問題は「一つのファイルに保存する上限容量が決まっている」ということなのでしょうか?
8
8
 
9
- 一つのファイルに一定以上の容量が溜まったら新しいファイルを作成するなどで対応できるでしょうか? 一般的な対策があれば教えていただきたいと思います。
9
+ 一つのファイルに一定以上の容量が溜まったら新しいファイルを作成するなどで対応できるでしょうか?
10
+
11
+ 一般的な対策があれば教えていただきたいと思います。

1

タイトル変更

2020/08/02 15:00

投稿

rokurou
rokurou

スコア2

test CHANGED
@@ -1 +1 @@
1
- SDカードへの上限?
1
+ SDカードへの保存において、一定以のデータが保存されない
test CHANGED
File without changes