/*
* IRrecv + Wake On LAN Example
*/
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <IRremoteESP8266.h>
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
WiFiUDP udp;
// The Broadcast IP address to send the magic packet to.
IPAddress broad_ip(192, 168, 10, 255);
//int RECV_PIN = 2; //an IR detector/demodulatord is
connected to GPIO2
int RECV_PIN = 5; //an IR detector/demodulatord is
connected to GPIO5
int LED_PIN = 2; //LED Pin
IRrecv irrecv(RECV_PIN);
decode_results results;
Ticker ticker1;
// The targets MAC address to send the packet to
byte mac[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
unsigned int localPort = 8;
// local port to listen on
void sendWOL(IPAddress addr, byte * mac, size_t
size_of_mac) {
byte preamble[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF};
byte i;
udp.beginPacket(addr, localPort);
udp.write(preamble, sizeof(preamble));
for (i = 0; i < 16; i++) {
udp.write(mac, size_of_mac);
}
udp.endPacket();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Wait for WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void flush_led() {
static bool flag = true;
if (flag) digitalWrite(LED_PIN,LOW);
if (!flag) digitalWrite(LED_PIN,HIGH);
flag=!flag;
}
void setup() {
delay(1000);
Serial.begin(9600);
setup_wifi();
udp.begin(localPort); //start udp client, not sure
if really necessary.
irrecv.enableIRIn(); // Start the receiver
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,HIGH);
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.print("results.decode_type=");
Serial.print(results.decode_type, HEX);
Serial.print(" result.value=");
Serial.println(results.value, HEX);
if (results.decode_type == 3) { // RC5
format
if (results.value == 0xC ||
results.value == 0x80C) {
ticker1.attach_ms(50, flush_led);
Serial.println("Sending WOL Packet...");
sendWOL(broad_ip, mac, sizeof(mac));
delay(1000);
ticker1.detach();
digitalWrite(LED_PIN,HIGH);
}
}
irrecv.resume(); // Receive the next
value
}
}
|