質問編集履歴

3

一部修正

2022/10/13 21:48

投稿

MaeharaKenji
MaeharaKenji

スコア86

test CHANGED
File without changes
test CHANGED
@@ -314,7 +314,7 @@
314
314
 
315
315
 
316
316
  #if 1
317
- while(0) {
317
+ while(1) {
318
318
  Serial.printf("%s - run\r\n",__func__);
319
319
  delay(5000);
320
320
  }

2

メインコードのinoファイル”OnDemandAP.ino” コード内容を追加

2022/10/13 21:47

投稿

MaeharaKenji
MaeharaKenji

スコア86

test CHANGED
File without changes
test CHANGED
@@ -70,6 +70,367 @@
70
70
 
71
71
  ```
72
72
 
73
+
74
+
75
+ ```c
76
+ //メインのinoファイル "OnDemandAP.ino" 
77
+
78
+ //#include <WiFi.h> //https://github.com/esp8266/Arduino
79
+ #include <WebServer.h>
80
+ #include <DNSServer.h>
81
+ #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
82
+
83
+ #include "FS.h"
84
+ #include "SPIFFS.h" // ①ライブラリを読み込み
85
+
86
+ #include <DS3232RTC.h>
87
+ #include "AWSIotSecrets.h"
88
+ #include <WiFiClientSecure.h>
89
+ #include <MQTTClient.h>
90
+ #include <ArduinoJson.h>
91
+
92
+
93
+
94
+ #define FORMAT_SPIFFS_IF_FAILED true
95
+
96
+
97
+ #define Button_D0 D0
98
+ #define Button_D1 D1
99
+ #define Button_D2 D2
100
+ #define Button_D3 D3
101
+
102
+ #define OPEN 0
103
+ #define CLOSE 1
104
+
105
+ #define INPUT_CHAT_TIME 300
106
+
107
+ DS3232RTC myRTC(false);
108
+
109
+ char s2[80];//文字格納用
110
+ char time_message[20];//文字格納用
111
+
112
+ //struct tm timeInfo;//時刻を格納するオブジェクト
113
+
114
+ int gButton_D0_State = CLOSE;
115
+ int gButton_D1_State = CLOSE;
116
+ int gButton_D2_State = CLOSE;
117
+ int gButton_D3_State = CLOSE;
118
+
119
+ int gRecieveCount = 0;
120
+
121
+
122
+ #define FORMAT_SPIFFS_IF_FAILED true
123
+
124
+
125
+ const char* AWS_IOT_PUBLISH_TOPIC = "node/airsensor";
126
+
127
+ const int WIFI_TIMEOUT_MS = 20000;
128
+ const int MQTT_TIMEOUT_MS = 5000;
129
+
130
+ WiFiClientSecure net = WiFiClientSecure();
131
+ MQTTClient client = MQTTClient(256);
132
+ //CRC8 crc;
133
+ //ST7032_asukiaaa lcd;
134
+
135
+ double temperature;
136
+ double humidity;
137
+ uint32_t particle;
138
+
139
+
140
+ void delay_with_client_loop(unsigned long ms) {
141
+ unsigned long start = millis();
142
+ while((millis() - start) < ms) {
143
+ client.loop();
144
+ //Serial.println("AWS looping");
145
+ }
146
+ //Serial.println("AWS loop end");
147
+ }
148
+
149
+ void connectWiFi() {
150
+ if(WiFi.status() == WL_CONNECTED) {
151
+ return;
152
+ }
153
+
154
+ WiFi.begin();
155
+ Serial.println("Connecting to Wi-Fi");
156
+
157
+
158
+ int wifi_connecting = 0;
159
+ while ((WiFi.status() != WL_CONNECTED) && (wifi_connecting <= WIFI_TIMEOUT_MS)){
160
+ delay(500);
161
+ wifi_connecting += 500;
162
+ Serial.print(".");
163
+ }
164
+ if(WiFi.status() != WL_CONNECTED) {
165
+ Serial.println("Wi-Fi Timeout");
166
+ WiFi.disconnect();
167
+
168
+ delay(30000);
169
+ ESP.restart();
170
+ return;
171
+ }
172
+
173
+ // Configure WiFiClientSecure to use the AWS IoT device credentials
174
+ net.setCACert(AWS_CERT_CA);
175
+ net.setCertificate(AWS_CERT_CRT);
176
+ net.setPrivateKey(AWS_CERT_PRIVATE);
177
+ return;
178
+ }
179
+
180
+ void connectAWS() {
181
+ if(client.connected()){
182
+ return;
183
+ }
184
+
185
+ // Connect to the MQTT broker on the AWS endpoint we defined earlier
186
+ client.begin(AWS_IOT_ENDPOINT, 8883, net);
187
+ client.setTimeout(1000);
188
+
189
+ Serial.println("Connecting to AWS IOT");
190
+
191
+ int client_connecting = 0;
192
+ //while ((!client.connect(THINGNAME)) && (client_connecting <= MQTT_TIMEOUT_MS)) {
193
+ while ((!client.connect(gSerialName.c_str())) && (client_connecting <= MQTT_TIMEOUT_MS)) {
194
+ Serial.print(".");
195
+ delay(100);
196
+ client_connecting += 100;
197
+ }
198
+ if(!client.connected()){
199
+ WiFi.disconnect(true, true);
200
+
201
+ Serial.println("AWS IoT Timeout!");
202
+
203
+ delay(30000);
204
+ ESP.restart();
205
+ return;
206
+ }
207
+
208
+ Serial.println("AWS IoT Connected!");
209
+
210
+ return;
211
+ }
212
+
213
+ void publishMessage()
214
+ {
215
+ ///文字制限のため省略
216
+ }
217
+
218
+
219
+ void setup_AWSIotMqtt() {
220
+
221
+ //WiFi.mode(WIFI_STA);
222
+ //WiFi.setAutoConnect(false);
223
+
224
+ // Configure WiFiClientSecure to use the AWS IoT device credentials
225
+ net.setCACert(AWS_CERT_CA);
226
+ net.setCertificate(AWS_CERT_CRT);
227
+ net.setPrivateKey(AWS_CERT_PRIVATE);
228
+ }
229
+
230
+ void TASK_AWSIotMqtt(void *args) {
231
+
232
+ //delay(5000);
233
+
234
+
235
+ #if 1
236
+ while(1)
237
+ {
238
+ Serial.printf("%s - run\r\n",__func__);
239
+ delay(9000);
240
+ }
241
+ #endif
242
+
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+
250
+ String gSerialName;
251
+
252
+
253
+ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
254
+ ///文字制限のため省略
255
+ }
256
+
257
+ void readFile(fs::FS &fs, const char * path){
258
+ ///文字制限のため省略
259
+ }
260
+
261
+
262
+
263
+
264
+ void SerialNameReadFile(fs::FS &fs, const char * path){
265
+ ///文字制限のため省略
266
+ }
267
+
268
+
269
+
270
+ void writeFile(fs::FS &fs, const char * path, const char * message){
271
+ ///文字制限のため省略
272
+ }
273
+
274
+
275
+ void appendFile(fs::FS &fs, const char * path, const char * message){
276
+ ///文字制限のため省略
277
+ }
278
+
279
+
280
+
281
+ void setup_DoorChecker() {
282
+
283
+ pinMode(Button_D0, INPUT_PULLUP);
284
+ pinMode(Button_D1, INPUT_PULLUP);
285
+ pinMode(Button_D2, INPUT_PULLUP);
286
+ pinMode(Button_D3, INPUT_PULLUP);
287
+
288
+ pinMode(D6, INPUT);
289
+ pinMode(D7, INPUT);
290
+ pinMode(D8, INPUT);
291
+
292
+
293
+ }
294
+
295
+ void currentTime() {
296
+ tmElements_t tm;
297
+ myRTC.read(tm);
298
+
299
+ sprintf(time_message, " %04d/%02d/%02d %02d:%02d:%02d",
300
+ tm.Year + 1970, tm.Month, tm.Day,
301
+ tm.Hour, tm.Minute, tm.Second);//人間が読める形式に変換
302
+ }
303
+
304
+ void TASK_ButtonChecker(void *args) {
305
+ // put your main code here, to run repeatedly:
306
+ //delay(5000);
307
+
308
+ String wrfile = "/log_test.txt";
309
+
310
+ //File fw = SPIFFS.open(wrfile.c_str(), "w");// ⑥ファイルを書き込みモードで開く
311
+ //const char * message;
312
+
313
+ setup_DoorChecker();
314
+
315
+
316
+ #if 1
317
+ while(0) {
318
+ Serial.printf("%s - run\r\n",__func__);
319
+ delay(5000);
320
+ }
321
+ #endif
322
+ }
323
+
324
+
325
+
326
+ struct tm timeInfo;//時刻を格納するオブジェクト
327
+ char s[20];//文字格納用
328
+
329
+ TaskHandle_t thp[2];//マルチスレッドのタスクハンドル格納用
330
+
331
+ void multiTaskSetup() {
332
+
333
+ xTaskCreatePinnedToCore(TASK_ButtonChecker, "TASK_ButtonChecker", 8192, NULL, 4, NULL, 0);
334
+ xTaskCreatePinnedToCore(TASK_AWSIotMqtt, "TASK_AWSIotMqtt", 4096, NULL, 3, NULL, 0);
335
+
336
+ }
337
+
338
+
339
+
340
+ void setup() {
341
+
342
+ if (!SPIFFS.begin(true)) {
343
+ Serial.println("An Error has occurred while mounting SPIFFS");
344
+ return;
345
+ }
346
+
347
+ // put your setup code here, to run once:
348
+ Serial.begin(115200);
349
+ Serial.println("\n Starting");
350
+ Serial.println("Ver_106");
351
+ Serial.println("\n----DIR: /");
352
+ listDir(SPIFFS,"/",0);
353
+ SerialNameReadFile(SPIFFS,"/ID.txt");
354
+ readFile(SPIFFS,"/log_test.txt");
355
+
356
+
357
+ //multiTaskSetup();
358
+
359
+ pinMode(D7, INPUT_PULLUP);
360
+ delay(100);
361
+
362
+ int longPushCount = 0;
363
+ while(digitalRead(D7)==HIGH)
364
+ {
365
+ longPushCount++;
366
+ delay(1000);
367
+ if (longPushCount > 5)
368
+ {
369
+ WiFiManager wifiManager;
370
+
371
+ //if (!wifiManager.startConfigPortal((char *)gSerialName.c_str())) {
372
+ if (!wifiManager.startConfigPortal(gSerialName.c_str())) {
373
+ Serial.println("failed to connect and hit timeout");
374
+ delay(3000);
375
+ //reset and try again, or maybe put it to deep sleep
376
+ ESP.restart();
377
+ delay(5000);
378
+ }
379
+ }
380
+ }
381
+
382
+
383
+ myRTC.begin();
384
+
385
+ WiFi.begin();
386
+ Serial.println("connecting");
387
+
388
+ while (WiFi.status() != WL_CONNECTED) {
389
+ delay(1000);
390
+ Serial.print(".");
391
+ }
392
+
393
+ if(WiFi.status() == WL_CONNECTED) {
394
+
395
+ //if you get here you have connected to the WiFi
396
+ IPAddress ipadr = WiFi.localIP();
397
+ Serial.println("connected(^^)");
398
+ Serial.println("local ip");
399
+ Serial.println(ipadr);
400
+ Serial.println(WiFi.SSID());
401
+ Serial.print("Serial name:");
402
+ Serial.println(gSerialName);
403
+ Serial.println("");
404
+
405
+ Serial.println("Connected to the WiFi network!");
406
+ configTime(9 * 3600L, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");//NTPの設定
407
+
408
+ getLocalTime(&timeInfo);//tmオブジェクトのtimeInfoに現在時刻を入れ込む
409
+ sprintf(s, " %04d/%02d/%02d %02d:%02d:%02d",
410
+ timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
411
+ timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);//人間が読める形式に変換
412
+ Serial.println(s);//時間をシリアルモニタへ出力
413
+
414
+ setTime(timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec, timeInfo.tm_mday, timeInfo.tm_mon + 1, timeInfo.tm_year + 1900);
415
+ myRTC.set(now());
416
+ }
417
+
418
+ multiTaskSetup();
419
+
420
+ }
421
+
422
+ void loop() {
423
+ // put your main code here, to run repeatedly:
424
+
425
+ delay(100);
426
+ }
427
+
428
+
429
+
430
+ ```
431
+
432
+
433
+
73
434
  ### 試したこと
74
435
 
75
436
  ここに問題に対して試したことを記載してください。

1

ファイル名を追加。

2022/10/13 21:26

投稿

MaeharaKenji
MaeharaKenji

スコア86

test CHANGED
File without changes
test CHANGED
@@ -37,6 +37,8 @@
37
37
  なお、このヘッダファイルは下記のコードです。こちらの内容に何か問題などありますでしょうか?
38
38
 
39
39
  ```c
40
+ //ヘッダーファイル名”AWSIotSecrets.h”
41
+
40
42
  #define SECRET
41
43
  #define THINGNAME "MAIN_DEV_0010"
42
44