├── README.md ├── WiFiClientSecure-IFTTT.ino ├── certificate.h ├── config.h └── convcert.sh /README.md: -------------------------------------------------------------------------------- 1 | # WiFiClientSecure-IFTTT 2 | ESP32からIFTTTをHTTPS経由で使って、LINE Notifyで通知を送るサンプルです。 3 | LEDとPIRモーションセンサーを想定して作っていますが、少々の変更でこれらに依存しないものとして使えると思います。 4 | http://qiita.com/mascii/items/4c366ad4709469d5fda9 5 | -------------------------------------------------------------------------------- /WiFiClientSecure-IFTTT.ino: -------------------------------------------------------------------------------- 1 | #include "config.h" // Wifi and IFTTT 2 | #include "certificate.h" // IFTTT 3 | #include 4 | #include 5 | 6 | const int inputPir = 5; // IO5 7 | const int outputLed = 2; // IO2 8 | bool outputState = true; 9 | unsigned long t = 0; // Time 10 | 11 | const char* server = "maker.ifttt.com"; // Server URL 12 | 13 | WiFiClientSecure client; 14 | 15 | void errorBlink() { 16 | for (int i = 0; i < 10; i++) { 17 | digitalWrite(outputLed, HIGH); 18 | delay(300); 19 | digitalWrite(outputLed, LOW); 20 | delay(300); 21 | } 22 | } 23 | 24 | void wifiBegin() { 25 | Serial.print("Attempting to connect to SSID: "); 26 | Serial.println(ssid); 27 | WiFi.begin(ssid, password); 28 | } 29 | 30 | bool checkWifiConnected() { 31 | // attempt to connect to Wifi network: 32 | int count = 0; 33 | while (WiFi.status() != WL_CONNECTED) { 34 | Serial.print("."); 35 | // wait 1 second for re-trying 36 | delay(1000); 37 | 38 | count++; 39 | if (count > 15) { // about 15s 40 | Serial.println("(wifiConnect) failed!"); 41 | errorBlink(); 42 | return false; 43 | } 44 | } 45 | 46 | Serial.print("Connected to "); 47 | Serial.println(ssid); 48 | 49 | return true; 50 | } 51 | 52 | void send() { 53 | while (!checkWifiConnected()) { 54 | wifiBegin(); 55 | } 56 | 57 | client.setCACert(test_ca_cert); 58 | 59 | Serial.println("\nStarting connection to server..."); 60 | if (!client.connect(server, 443)) { 61 | Serial.println("Connection failed!"); 62 | } else { 63 | Serial.println("Connected to server!"); 64 | // Make a HTTP request: 65 | String url = "/trigger/" + makerEvent + "/with/key/" + makerKey; 66 | // url += "?value1=VALUE1"; 67 | client.println("GET " + url + " HTTP/1.1"); 68 | client.print("Host: "); 69 | client.println(server); 70 | client.println("Connection: close"); 71 | client.println(); 72 | 73 | Serial.print("Waiting for response "); //WiFiClientSecure uses a non blocking implementation 74 | 75 | int count = 0; 76 | while (!client.available()) { 77 | delay(50); // 78 | Serial.print("."); 79 | 80 | count++; 81 | if (count > 20 * 20) { // about 20s 82 | Serial.println("(send) failed!"); 83 | errorBlink(); 84 | return; 85 | } 86 | } 87 | // if there are incoming bytes available 88 | // from the server, read them and print them: 89 | while (client.available()) { 90 | char c = client.read(); 91 | Serial.write(c); 92 | } 93 | 94 | // if the server's disconnected, stop the client: 95 | if (!client.connected()) { 96 | Serial.println(); 97 | Serial.println("disconnecting from server."); 98 | client.stop(); 99 | } 100 | } 101 | } 102 | 103 | void setup() { 104 | pinMode(outputLed, OUTPUT); 105 | pinMode(inputPir, INPUT); 106 | 107 | //Initialize serial and wait for port to open: 108 | Serial.begin(115200); 109 | delay(100); 110 | 111 | wifiBegin(); 112 | while (!checkWifiConnected()) { 113 | wifiBegin(); 114 | } 115 | } 116 | 117 | void loop() { 118 | if (digitalRead(inputPir) == HIGH) { 119 | if (!outputState && millis() - t >= 10000) { 120 | digitalWrite(outputLed, HIGH); 121 | outputState = true; 122 | send(); 123 | t = millis(); 124 | } 125 | } else { 126 | digitalWrite(outputLed, LOW); 127 | outputState = false; 128 | } 129 | } 130 | 131 | -------------------------------------------------------------------------------- /certificate.h: -------------------------------------------------------------------------------- 1 | const char* test_ca_cert = \ 2 | "-----BEGIN CERTIFICATE-----\n" \ 3 | "MIIDxTCCAq2gAwIBAgIBADANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMCVVMx\n" \ 4 | "EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxGjAYBgNVBAoT\n" \ 5 | "EUdvRG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N9ss4UXnT3ZJE95kTXWXwTr\n" \ 20 | "gIOrmgIttRD02JDHBHNA7XIloKmf7J6raBKZV8aPEjoJpL1E/QYVN8Gb5DKj7Tjo\n" \ 21 | "2GTzLH4U/ALqn83/B2gX2yKQOC16jdFU8WnjXzPKej17CuPKf1855eJ1usV2GDPO\n" \ 22 | "LPAvTK33sefOT6jEm0pUBsV/fdUID+Ic/n4XuKxe9tQWskMJDE32p2u0mYRlynqI\n" \ 23 | "4uJEvlz36hz1\n" \ 24 | "-----END CERTIFICATE-----\n" \ 25 | ; 26 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = ""; 2 | const char* password = ""; 3 | 4 | String makerEvent = ""; // Maker Webhooks 5 | String makerKey = ""; // Maker Webhooks 6 | -------------------------------------------------------------------------------- /convcert.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat <(echo "const char* test_ca_cert = \\") <(cat $1 | tr -d "\r" | sed -e "s/^/\"/" -e "s/$/\\\n\" \\\/") <(echo ";") > certificate.h 4 | 5 | exit 0 6 | --------------------------------------------------------------------------------