質問編集履歴
1
プログラムコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -333,3 +333,367 @@
|
|
333
333
|
}
|
334
334
|
|
335
335
|
```
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
①のコードは、
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
```html
|
344
|
+
|
345
|
+
#include <WiFiClientSecure.h>
|
346
|
+
|
347
|
+
#include "esp_deep_sleep.h"
|
348
|
+
|
349
|
+
#include "wifi_config.h"
|
350
|
+
|
351
|
+
#include "ifttt_config.h"
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
// hostname of Maker of IFTTT Platform
|
356
|
+
|
357
|
+
const char* HOSTNAME = "maker.ifttt.com";
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
WiFiClientSecure client;
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
void setup() {
|
366
|
+
|
367
|
+
Serial.begin(115200);
|
368
|
+
|
369
|
+
delay(100);
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
void loop() {
|
378
|
+
|
379
|
+
//IFTTT Event Trig
|
380
|
+
|
381
|
+
if(digitalRead(GPIO_NUM_34) == 0){
|
382
|
+
|
383
|
+
trigger_event1();
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
// connect to the WiFi network of home
|
392
|
+
|
393
|
+
bool connect_to_wifi() {
|
394
|
+
|
395
|
+
Serial.print("Attempting to connect to SSID: ");
|
396
|
+
|
397
|
+
Serial.println(ssid);
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
WiFi.begin(ssid, password);
|
402
|
+
|
403
|
+
// attempt to connect to Wifi network:
|
404
|
+
|
405
|
+
int retry_count = 0;
|
406
|
+
|
407
|
+
while (WiFi.status() != WL_CONNECTED) {
|
408
|
+
|
409
|
+
Serial.print(".");
|
410
|
+
|
411
|
+
// wait 1 second for re-trying
|
412
|
+
|
413
|
+
delay(1000);
|
414
|
+
|
415
|
+
retry_count++;
|
416
|
+
|
417
|
+
if (retry_count > 30) {
|
418
|
+
|
419
|
+
Serial.println("WiFi connection failed!");
|
420
|
+
|
421
|
+
return false;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
Serial.print("Connected to ");
|
430
|
+
|
431
|
+
Serial.println(ssid);
|
432
|
+
|
433
|
+
Serial.print("IP address: ");
|
434
|
+
|
435
|
+
Serial.println(WiFi.localIP());
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
return true;
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
// trigger an event of Maker
|
446
|
+
|
447
|
+
void trigger_event1() {
|
448
|
+
|
449
|
+
client.setCACert(ROOT_CA_CERT);
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
Serial.println("\nStarting connection to server...");
|
454
|
+
|
455
|
+
if (!client.connect(HOSTNAME, 443)) {
|
456
|
+
|
457
|
+
Serial.println("Server connection failed!");
|
458
|
+
|
459
|
+
} else {
|
460
|
+
|
461
|
+
Serial.println("Connected to server!");
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
// Make a HTTP request:
|
466
|
+
|
467
|
+
client.print("GET ");
|
468
|
+
|
469
|
+
client.print("/trigger/");
|
470
|
+
|
471
|
+
client.print(EVENT1);
|
472
|
+
|
473
|
+
client.print("/with/key/");
|
474
|
+
|
475
|
+
client.print(KEY);
|
476
|
+
|
477
|
+
client.println(" HTTP/1.0");
|
478
|
+
|
479
|
+
client.print("Host: ");
|
480
|
+
|
481
|
+
client.println(HOSTNAME);
|
482
|
+
|
483
|
+
client.println("Connection: close");
|
484
|
+
|
485
|
+
client.println();
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
while (client.connected()) {
|
490
|
+
|
491
|
+
String line = client.readStringUntil('\n');
|
492
|
+
|
493
|
+
if (line == "\r") {
|
494
|
+
|
495
|
+
Serial.println("headers received");
|
496
|
+
|
497
|
+
break;
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
while (client.available()) {
|
504
|
+
|
505
|
+
char c = client.read();
|
506
|
+
|
507
|
+
Serial.write(c);
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
Serial.println();
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
client.stop();
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
```
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
②のコードは、
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
```html
|
534
|
+
|
535
|
+
#include <WiFiClientSecure.h>
|
536
|
+
|
537
|
+
#include <PubSubClient.h>
|
538
|
+
|
539
|
+
#include <IRremote.h>
|
540
|
+
|
541
|
+
#include "config.h"
|
542
|
+
|
543
|
+
#include "certificate.h"
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
WiFiClientSecure wifiClient;
|
550
|
+
|
551
|
+
PubSubClient client(wifiClient);
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
void setup() {
|
556
|
+
|
557
|
+
Serial.begin(115200);
|
558
|
+
|
559
|
+
delay(100);
|
560
|
+
|
561
|
+
Serial.println();
|
562
|
+
|
563
|
+
WiFi.mode(WIFI_STA);
|
564
|
+
|
565
|
+
client.setCallback(callback);
|
566
|
+
|
567
|
+
client.setServer(host, port);
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
void loop() {
|
574
|
+
|
575
|
+
//Wifi接続
|
576
|
+
|
577
|
+
if (WiFi.status() != WL_CONNECTED) {
|
578
|
+
|
579
|
+
Serial.print("connecting to ");
|
580
|
+
|
581
|
+
Serial.print(ssid);
|
582
|
+
|
583
|
+
Serial.println("...");
|
584
|
+
|
585
|
+
WiFi.begin(ssid, password);
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
590
|
+
|
591
|
+
//アクセスポイントへの接続に失敗したら5秒間待ってリトライ
|
592
|
+
|
593
|
+
Serial.println("failed to connect");
|
594
|
+
|
595
|
+
delay(5000);
|
596
|
+
|
597
|
+
return;
|
598
|
+
|
599
|
+
} else {
|
600
|
+
|
601
|
+
Serial.print("WiFi connected: ");
|
602
|
+
|
603
|
+
Serial.println(WiFi.localIP());
|
604
|
+
|
605
|
+
}
|
606
|
+
|
607
|
+
|
608
|
+
|
609
|
+
wifiClient.setCACert(ca_cert);
|
610
|
+
|
611
|
+
}
|
612
|
+
|
613
|
+
|
614
|
+
|
615
|
+
//CloudMQTTに接続
|
616
|
+
|
617
|
+
if (!client.connected()) {
|
618
|
+
|
619
|
+
client.connect(clientID, CloudMQTT_user, CloudMQTT_pass);
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
if (client.connected()) {
|
624
|
+
|
625
|
+
Serial.println("MQTT connected");
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
// トピック名を指定してsubscribe
|
630
|
+
|
631
|
+
client.subscribe(topic);
|
632
|
+
|
633
|
+
} else {
|
634
|
+
|
635
|
+
Serial.print("MQTT connection failed: ");
|
636
|
+
|
637
|
+
Serial.println(client.state());
|
638
|
+
|
639
|
+
delay(5000);
|
640
|
+
|
641
|
+
}
|
642
|
+
|
643
|
+
} else {
|
644
|
+
|
645
|
+
// 既にサーバに接続されていれば通常処理を行う
|
646
|
+
|
647
|
+
client.loop();
|
648
|
+
|
649
|
+
}
|
650
|
+
|
651
|
+
}
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
//Cloud MQTT 受信メッセージをシリアルにプリント
|
656
|
+
|
657
|
+
void callback(char* topic, byte* payload, unsigned int length) {
|
658
|
+
|
659
|
+
|
660
|
+
|
661
|
+
Serial.print("Message arrived in topic: ");
|
662
|
+
|
663
|
+
Serial.println(topic);
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
Serial.print("Message:");
|
668
|
+
|
669
|
+
String message;
|
670
|
+
|
671
|
+
|
672
|
+
|
673
|
+
for (int i = 0; i < length; i++) {
|
674
|
+
|
675
|
+
message += (char)payload[i];
|
676
|
+
|
677
|
+
}
|
678
|
+
|
679
|
+
Serial.println(message);
|
680
|
+
|
681
|
+
if (message == "On") {
|
682
|
+
|
683
|
+
digitalWrite(LED,HIGH);
|
684
|
+
|
685
|
+
delay(100);
|
686
|
+
|
687
|
+
digitalWrite(LED,LOW);
|
688
|
+
|
689
|
+
delay(100);
|
690
|
+
|
691
|
+
}
|
692
|
+
|
693
|
+
Serial.println();
|
694
|
+
|
695
|
+
Serial.println("-----------------------");
|
696
|
+
|
697
|
+
}
|
698
|
+
|
699
|
+
```
|