#include <WiFi.h>
#include <ESPAsyncWebSrv.h>
// WiFi credentials
const char* ssid = "NETGEAR38";
const char* password = "rapidteapot020";
// Multiplexer control pins
const int s0 = 18;
const int s1 = 19;
const int s2 = 22;
const int s3 = 23;
//generate json file for wokwi replace to this #define E 21
//#define S0 18
//#define S1 19
//#define S2 22
//#define S3 23
//#define COM 34
//#define NP_PIN 4
//#define NUMPIXELS 16
// Web server
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"html(
<html>
<head>
<script>
function switchInput() {
var input = document.getElementById("input").value;
var output = document.getElementById("output").value;
var xhr = new XMLHttpRequest();
xhr.open("GET", "/switch?input=" + input + "&output=" + output, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
document.getElementById("result").innerHTML = xhr.responseText;
}
}
xhr.send();
}
</script>
</head>
<body>
Input: <input type="number" id="input" min="0" max="7"><br>
Output: <input type="number" id="output" min="0" max="7"><br>
<button onclick="switchInput()">Switch Input</button><br>
<div id="result"></div>
</body>
</html>
)html";
void setup() {
// Initialize WiFi
Serial.begin(115200);
Serial.println("-=0=- -=0=- -=0=- ");
Serial.println(__FILE__);
Serial.println(__DATE__);
Serial.println(__TIME__);
Serial.println("-_-_-_-_-_-_-");
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());
// Initialize control pins
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
// Set up the web server
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
server.on("/switch", HTTP_GET, [](AsyncWebServerRequest *request){
int input = request->arg("input").toInt();
int output = request->arg("output").toInt();
// Error checking for input and output values
if (input < 0 || input > 7 || output < 0 || output > 7) {
request->send(400, "text/plain", "Invalid input or output");
return;
}
// Control the multiplexer
digitalWrite(s0, (input & 1) == 1);
digitalWrite(s1, (input & 2) == 2);
digitalWrite(s2, (input & 4) == 4);
digitalWrite(s3, (input & 8) == 8);
// Send response to the AJAX request
String response = "Input " + String(input) + " connected to output " + String(output);
request->send(200, "text/plain", response);
});
server.begin();
}
void loop() {
// Put your main code here, if required
}