#include <WiFi.h>
const char* ssid = "******"; //ご自分のルーターのSSIDに書き換えてください。
const char* password = "******"; //ご自分のルーターのパスワードに書き換えてください。
//先ほど学習した信号情報をコピーする
int data_on[] = {337,143,58,21,58,102,58,21,58,21,58,102,57,21,58,102,57,21,58,21,58,102,54,102,55,102,54,21,58,102,54,21,58,102,54,102,54,102,54,22,58,22,58,22,57,22,57,104,55,104,55,22,58,104,55,22,57,104,55,104,55,22,57,22,57,22,57,104,55,22,57,104,55,22,58,22,58,104,55,102,54,102,54,21,58,102,54,102,54,21,58,102,54,102,54,102,54,102,54,102,57,21,58,21,58,102,54,21,58,21,58,21,58,21,58,102,57,102,57,102,57,21,58,102,57,102,58,102,58,101,58,21,58,21,58,21,58,102,58,21,58,21,58,21,58,21,58,102,57,21,58,102,58,102,58,101,58,101,58,101,58,101,58,19,57,102,58,19,57,19,57,19,57,21,58,21,58,19,57,101,58,101,58,101,58,101,58,101,58,101,58,101,58,101,58,19,60,19,60,19,60,19,60,19,60,19,60,19,60,19,61,101,58,101,58,101,59,18,61,101,59,100,59,100,59,101,58,18,61,18,61,18,62,100,59,18,61,18,61,18,61,18,61,100,59,100,59,100,59,98,58,98,58,98,61,18,61,98,58,19,61,18,61,18,61,18,61,18,61,18,61,98,58,18,61,98,61,98,61,18,61,98,61,98,61,98,61,98,61,18,61,18,61,18,61,98,61,18,61,18,61,18,61,18,61,98,58};
int data_off[] = {314, 166, 34, 45, 34, 125, 34, 45, 34, 46, 34, 125, 34, 45, 34, 125, 34, 45, 34, 46, 34, 125, 34, 125, 34, 126, 34, 45, 34, 125, 34, 45, 34, 125, 34, 126, 34, 125, 34, 45, 34, 45, 34, 45, 34, 45, 34, 125, 34, 125, 34, 45, 34, 125, 34, 45, 34, 125, 34, 126, 34, 45, 34, 46, 33, 45, 34, 126, 34, 45, 34, 125, 34, 45, 34, 45, 34, 125, 34, 126, 34, 126, 34, 125, 34, 126, 34, 45, 34, 125, 34, 125, 34, 126, 34, 126, 34, 125, 34, 45, 34, 46, 33, 126, 34, 45, 34, 46, 34, 45, 34, 46, 33, 45, 34, 126, 34, 126, 34, 126, 34, 45, 33, 125, 35, 124, 34, 125, 34, 125, 34, 46, 33, 46, 33, 45, 34, 125, 34, 45, 34, 45, 34, 45, 34, 45, 34, 125, 34, 45, 34, 126, 34, 126, 34, 125, 34, 125, 34, 125, 34, 128, 31, 45, 34, 126, 34, 45, 34, 46, 33, 46, 33, 46, 33, 46, 33, 46, 34, 125, 34, 125, 34, 125, 34, 124, 34, 125, 34, 125, 34, 125, 34, 125, 34, 45, 34, 45, 34, 45, 34, 45, 34, 46, 34, 46, 34, 45, 34, 45, 34, 125, 34, 125, 34, 125, 34, 46, 34, 125, 34, 125, 34, 125, 34, 125, 34, 45, 34, 45, 34, 46, 33, 125, 34, 45, 34, 46, 34, 45, 34, 45, 34, 125, 34, 125, 34, 127, 32, 125, 34, 126, 34, 124, 34, 46, 41, 118, 34, 46, 34, 46, 33, 46, 34, 45, 34, 45, 34, 46, 34, 125, 34, 46, 34, 125, 34, 125, 34, 45, 34, 125, 34, 126, 34, 126, 34, 125, 34, 45, 34, 46, 34, 45, 34, 125, 34, 45, 34, 45, 34, 46, 33, 45, 34, 126, 34};
int IR_SEND_PIN = 27; // 赤外線LEDのピン
const char* host = "******";//作成したサイトのURLを入れる。http://は不要
int on_off_flag = 2;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// 出力に設定
pinMode(IR_SEND_PIN, OUTPUT);
}
void loop() {
delay(100);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URL for the request
String url = "/stateGet.php";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
int i = 0;
int flag_temp = on_off_flag;
while (client.available()) {
String line = client.readStringUntil('\r');
if (i == 8) {
if (line.lastIndexOf("OFF") == -1) {
Serial.println("on");
on_off_flag = 0;
} else {
Serial.println("off");
on_off_flag = 1;
}
break;
}
i++;
}
if (flag_temp != on_off_flag) {
if (on_off_flag == 0) {
sendSignal(data_on, sizeof(data_on) / sizeof(data_on[0]));
} else {
sendSignal(data_off, sizeof(data_off) / sizeof(data_off[0]));
}
}
Serial.println();
Serial.println("closing connection");
}
void sendSignal(int data1[], int num) {
int dataSize = num;
int j = 0;
for (int cnt = 0; cnt < dataSize; cnt++) {
unsigned long len = data1[cnt] * 10;
unsigned long us = micros();
//Serial.println(len);
do {
digitalWrite(IR_SEND_PIN, 1 - (cnt & 1));
delayMicroseconds(8.8);
digitalWrite(IR_SEND_PIN, 0);
delayMicroseconds(17.5);
//Serial.println(j);
} while (long(us + len - micros()) > 0); // 送信時間に達するまでループ
//digitalWrite(IR_SEND_PIN, 0);
}
}