teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

プログラムコードを追加しました。

2019/03/20 11:34

投稿

raspypy
raspypy

スコア247

title CHANGED
File without changes
body CHANGED
@@ -165,4 +165,186 @@
165
165
  client.stop();
166
166
  }
167
167
  }
168
+ ```
169
+
170
+ ①のコードは、
171
+
172
+ ```html
173
+ #include <WiFiClientSecure.h>
174
+ #include "esp_deep_sleep.h"
175
+ #include "wifi_config.h"
176
+ #include "ifttt_config.h"
177
+
178
+ // hostname of Maker of IFTTT Platform
179
+ const char* HOSTNAME = "maker.ifttt.com";
180
+
181
+ WiFiClientSecure client;
182
+
183
+ void setup() {
184
+ Serial.begin(115200);
185
+ delay(100);
186
+
187
+ }
188
+
189
+ void loop() {
190
+ //IFTTT Event Trig
191
+ if(digitalRead(GPIO_NUM_34) == 0){
192
+ trigger_event1();
193
+ }
194
+ }
195
+
196
+ // connect to the WiFi network of home
197
+ bool connect_to_wifi() {
198
+ Serial.print("Attempting to connect to SSID: ");
199
+ Serial.println(ssid);
200
+
201
+ WiFi.begin(ssid, password);
202
+ // attempt to connect to Wifi network:
203
+ int retry_count = 0;
204
+ while (WiFi.status() != WL_CONNECTED) {
205
+ Serial.print(".");
206
+ // wait 1 second for re-trying
207
+ delay(1000);
208
+ retry_count++;
209
+ if (retry_count > 30) {
210
+ Serial.println("WiFi connection failed!");
211
+ return false;
212
+ }
213
+ }
214
+
215
+ Serial.print("Connected to ");
216
+ Serial.println(ssid);
217
+ Serial.print("IP address: ");
218
+ Serial.println(WiFi.localIP());
219
+
220
+ return true;
221
+ }
222
+
223
+ // trigger an event of Maker
224
+ void trigger_event1() {
225
+ client.setCACert(ROOT_CA_CERT);
226
+
227
+ Serial.println("\nStarting connection to server...");
228
+ if (!client.connect(HOSTNAME, 443)) {
229
+ Serial.println("Server connection failed!");
230
+ } else {
231
+ Serial.println("Connected to server!");
232
+
233
+ // Make a HTTP request:
234
+ client.print("GET ");
235
+ client.print("/trigger/");
236
+ client.print(EVENT1);
237
+ client.print("/with/key/");
238
+ client.print(KEY);
239
+ client.println(" HTTP/1.0");
240
+ client.print("Host: ");
241
+ client.println(HOSTNAME);
242
+ client.println("Connection: close");
243
+ client.println();
244
+
245
+ while (client.connected()) {
246
+ String line = client.readStringUntil('\n');
247
+ if (line == "\r") {
248
+ Serial.println("headers received");
249
+ break;
250
+ }
251
+ }
252
+ while (client.available()) {
253
+ char c = client.read();
254
+ Serial.write(c);
255
+ }
256
+ Serial.println();
257
+
258
+ client.stop();
259
+ }
260
+ }
261
+
262
+
263
+ ```
264
+
265
+ ②のコードは、
266
+
267
+ ```html
268
+ #include <WiFiClientSecure.h>
269
+ #include <PubSubClient.h>
270
+ #include <IRremote.h>
271
+ #include "config.h"
272
+ #include "certificate.h"
273
+
274
+
275
+ WiFiClientSecure wifiClient;
276
+ PubSubClient client(wifiClient);
277
+
278
+ void setup() {
279
+ Serial.begin(115200);
280
+ delay(100);
281
+ Serial.println();
282
+ WiFi.mode(WIFI_STA);
283
+ client.setCallback(callback);
284
+ client.setServer(host, port);
285
+ }
286
+
287
+ void loop() {
288
+ //Wifi接続
289
+ if (WiFi.status() != WL_CONNECTED) {
290
+ Serial.print("connecting to ");
291
+ Serial.print(ssid);
292
+ Serial.println("...");
293
+ WiFi.begin(ssid, password);
294
+
295
+ if (WiFi.waitForConnectResult() != WL_CONNECTED) {
296
+ //アクセスポイントへの接続に失敗したら5秒間待ってリトライ
297
+ Serial.println("failed to connect");
298
+ delay(5000);
299
+ return;
300
+ } else {
301
+ Serial.print("WiFi connected: ");
302
+ Serial.println(WiFi.localIP());
303
+ }
304
+
305
+ wifiClient.setCACert(ca_cert);
306
+ }
307
+
308
+ //CloudMQTTに接続
309
+ if (!client.connected()) {
310
+ client.connect(clientID, CloudMQTT_user, CloudMQTT_pass);
311
+
312
+ if (client.connected()) {
313
+ Serial.println("MQTT connected");
314
+
315
+ // トピック名を指定してsubscribe
316
+ client.subscribe(topic);
317
+ } else {
318
+ Serial.print("MQTT connection failed: ");
319
+ Serial.println(client.state());
320
+ delay(5000);
321
+ }
322
+ } else {
323
+ // 既にサーバに接続されていれば通常処理を行う
324
+ client.loop();
325
+ }
326
+ }
327
+
328
+ //Cloud MQTT 受信メッセージをシリアルにプリント
329
+ void callback(char* topic, byte* payload, unsigned int length) {
330
+
331
+ Serial.print("Message arrived in topic: ");
332
+ Serial.println(topic);
333
+
334
+ Serial.print("Message:");
335
+ String message;
336
+
337
+ for (int i = 0; i < length; i++) {
338
+ message += (char)payload[i];
339
+ }
340
+ Serial.println(message);
341
+ if (message == "On") {
342
+ digitalWrite(LED,HIGH);
343
+ delay(100);
344
+ digitalWrite(LED,LOW);
345
+ delay(100);
346
+ }
347
+ Serial.println();
348
+ Serial.println("-----------------------");
349
+ }
168
350
  ```