實現控制多個發射遙控器訊號的初版
以WiFiManualWebServer範例當基底,融合紅外線發射
因為找不到比網頁更方便的範例來開發
畢竟UI部分,也很難找到比網頁更方便的方式
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message.
#ifndef STASSID
#define STASSID "你的WIFI名稱"
#define STAPSK "你的WIFI密碼"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
irsend.begin();
// prepare LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
// Start the server
server.begin();
Serial.println(F("Server started"));
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.accept();
if (!client) { return; }
Serial.println(F("new client"));
client.setTimeout(5000); // default is 1000
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(F("request: "));
Serial.println(req);
// Match the request
int val,IR;
if (req.indexOf(F("/gpio/0")) != -1) {
val = 0;
} else if (req.indexOf(F("/gpio/1")) != -1) {
val = 1;}
else if (req.indexOf(F("/IRon")) != -1)
{IR = 0;
Serial.println("NEC");
irsend.sendNEC(0xCF20D);
}
else if (req.indexOf(F("/IRclose")) != -1)
{IR = 1;}
else {
Serial.println(F("invalid request"));
val = digitalRead(LED_BUILTIN);
}
// Set LED according to the request
digitalWrite(LED_BUILTIN, val);
// read/ignore the rest of the request
// do not client.flush(): it is for output only, see below
while (client.available()) {
// byte by byte is not very efficient
client.read();
}
// Send the response to the client
// it is OK for multiple small client.print/write,
// because nagle algorithm will group them into one single packet
client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"));
client.print(WiFi.localIP());
client.print(F("This hostname:\n"));//读取当前的hostname并输出
client.print(WiFi.hostname());
client.print(F(" "));
client.print(F("GPIO is now "));
client.print((val) ? F("high") : F("low"));
client.print(F(" "));
client.print(F("Click <a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/0'>here</a> to switch LED GPIO on, or <a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/1'>here</a> to switch LED GPIO off. Or <a href='http://"));
client.print(WiFi.localIP());
client.print(F("/IRon'>here</a> to send IR on."));
client.print(F("<br><br>"));
client.print(F("192.168.179.146"));
client.print(F(" "));
client.print(F("Click <a href='http://"));
client.print(F("192.168.179.146"));
client.print(F("/gpio/0'>here</a> to switch LED GPIO on, or <a href='http://"));
client.print(F("192.168.179.146"));
client.print(F("/gpio/1'>here</a> to switch LED GPIO off. Or <a href='http://"));
client.print(F("192.168.179.146"));
client.print(F("/IRon'>here</a> to send IR on."));
client.print(F("<br><br>"));
client.print(F("192.168.179.246"));
client.print(F(" "));
client.print(F("Click <a href='http://"));
client.print(F("192.168.179.246"));
client.print(F("/gpio/0'>here</a> to switch LED GPIO on, or <a href='http://"));
client.print(F("192.168.179.246"));
client.print(F("/gpio/1'>here</a> to switch LED GPIO off. Or <a href='http://"));
client.print(F("192.168.179.246"));
client.print(F("/IRon'>here</a> to send IR on.</html>"));
// The client will actually be *flushed* then disconnected
// when the function returns and 'client' object is destroyed (out-of-scope)
// flush = ensure written data are received by the other side
Serial.println(F("Disconnecting from client"));
}
沒有留言:
張貼留言