Created
September 18, 2022 21:43
-
-
Save JA1TYE/4a90e3fd6e78c91062b732cc6f7017a3 to your computer and use it in GitHub Desktop.
Arduino Sourcecode for HDMI Switcher Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <WebServer.h> | |
#include <ESPmDNS.h> | |
//WiFi Settings | |
const char* ssid = "YOUR_SSID"; | |
const char* password = "YOUR_PASSWORD"; | |
//MDNS Host Name | |
const char* mDNSName = "HDMISwitcher"; | |
//If you use static IP address, set true | |
const bool isStaticIP = true; | |
const IPAddress hostIP(192, 168, xxx, xxx); | |
const IPAddress gwIP(192, 168, xxx, xxx); | |
const IPAddress subnetMask(255, 255, 255, 0); | |
//Reconnection threshold | |
const int RECONNECT_THRESH = 10000; | |
//Constructor of Web Server | |
WebServer server(80); | |
//Pin Definition | |
const int SW_0 = 4; | |
const int SW_1 = 5; | |
const int LED = 2; | |
const int BTN = 9; | |
//LED Definition | |
const int NUMPIXELS = 1; | |
//State variables | |
int switchStatus = 0; | |
int buttonPrev = HIGH; | |
int reconnectCount = 0; | |
//Change Switch Status | |
void setSwitch(int pos) { | |
if (pos == 0) { | |
digitalWrite(SW_0, 0); | |
digitalWrite(SW_1, 1); | |
switchStatus = 0; | |
} else { | |
digitalWrite(SW_0, 1); | |
digitalWrite(SW_1, 0); | |
switchStatus = 1; | |
} | |
} | |
//Handler for /sw/0 | |
void setSW_0() { | |
setSwitch(0); | |
server.send(200, "text/plain", "OK"); | |
} | |
//Handler for /sw/1 | |
void setSW_1() { | |
setSwitch(1); | |
server.send(200, "text/plain", "OK"); | |
} | |
//Handler for /status | |
void getStatus() { | |
server.send(200, "text/plain", String(switchStatus)); | |
} | |
void handleNotFound() { | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i = 0; i < server.args(); i++) { | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
} | |
void setup(void) { | |
//Init GPIO | |
pinMode(SW_0, OUTPUT); | |
pinMode(SW_1, OUTPUT); | |
digitalWrite(SW_0, 0); | |
digitalWrite(SW_1, 1); | |
pinMode(BTN, INPUT); | |
Serial.begin(115200); | |
if (isStaticIP) { | |
WiFi.config(hostIP, gwIP, subnetMask); | |
} | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin(mDNSName)) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/sw/0", setSW_0); | |
server.on("/sw/1", setSW_1); | |
server.on("/status", getStatus); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void loop(void) { | |
server.handleClient(); | |
//Read Button Status | |
int buttonNow = digitalRead(BTN); | |
//Toggling HDMI port select | |
if (buttonNow != buttonPrev && buttonNow == LOW) { | |
if (switchStatus == 0) { | |
setSwitch(1); | |
} else { | |
setSwitch(0); | |
} | |
} | |
buttonPrev = buttonNow; | |
//If Wifi is disconnected, reset the system to reconnect | |
if (WiFi.status() != WL_CONNECTED) { | |
reconnectCount++; | |
if (reconnectCount == RECONNECT_THRESH) { | |
ESP.restart(); | |
} | |
} else { | |
reconnectCount = 0; | |
} | |
delay(2); //allow the cpu to switch to other tasks | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment