hygain-ir-controller/hygain-ir-controller.ino

232 lines
7.6 KiB
Arduino
Raw Normal View History

2024-02-02 00:39:25 +01:00
// Libraries
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <IRremote.h>
#include <ESPAsyncWebServer.h>
byte SEND_PIN = 4;
IRsend irsend(SEND_PIN);
// WiFi network
const char* ssid = "xxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxx";
AsyncWebServer server(80);
const char* input_heading = "heading";
char headingmap[360];
const uint16_t IRCodes[] = {
0x44, // A
0x7, // B
0x9, // C
0x40, // D
0x19, // E
0x16, // F
0xC, // G
0x18, // H
0x5E, // I
0x8, // J
0x1C, // K -> U
0x5A // L
};
// rotate CCW: 0x43
// rotate CW: 0x45
// initial: 0x42
// memory: 0x4A
/*
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>Hy-Gain AR-500X</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {font-family: Times New Roman; display: inline-block; text-align: center;}
h2 {font-size: 3.0rem; color: #FF0000;}
</style>
</head><body>
<h2>HyGain AR-500X</h2>
<form action="/get">
Enter new Heading: <input type="text" name="input_integer">
<input type="submit" value="Submit">
</form><br>
</body></html>)rawliteral";
*/
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hy-Gain AR-500X</title>
<style>
html {font-family: Times New Roman; display: inline-block; text-align: center;}
h2 {font-size: 3.0rem; color: #FF0000;}
</style>
</head>
<body>
<h2>HyGain AR-500X</h2>
<form action="/get">
<table cellspacing="0" cellpadding="0" align="center">
<tbody>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="A">000</button></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="L">345</button></td><td></td><td><button type="submit" name="heading" value="B">015</button></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="K">330</button></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="C">030</button></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td><button type="submit" name="heading" value="J">315</button></td><td></td><td></td><td>N</td><td></td><td></td><td><button type="submit" name="heading" value="D">045</button></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td><button type="submit" name="heading" value="I">300</button></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="E">060</button></td><td></td><td></td>
</tr>
<tr>
<td></td><td><button type="submit" name="heading" value="H">285</button></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="F">075</button></td><td></td>
</tr>
<tr>
<td><button type="submit" name="heading" value="G">270</button></td><td></td><td></td><td>W</td><td></td><td></td><td></td><td></td><td></td><td>E</td><td></td><td></td><td><button type="submit" name="heading" value="G">090</button></td>
</tr>
<tr>
<td></td><td><button type="submit" name="heading" value="F">255</button></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="H">105</button></td><td></td>
</tr>
<tr>
<td></td><td></td><td><button type="submit" name="heading" value="E">240</button></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="I">120</button></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td><button type="submit" name="heading" value="D">225</button></td><td></td><td></td><td>S</td><td></td><td></td><td><button type="submit" name="heading" value="J">135</button></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="C">210</button></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="K">150</button></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="B">195</button></td><td></td><td><button type="submit" name="heading" value="L">164</button></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td><button type="submit" name="heading" value="A">180</button></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
</table>
</form><br>
</body>
</html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
// Function to get value by key
const uint16_t getValueByKey(char key) {
// Convert key to uppercase for consistency
key = toupper(key);
// Calculate index
int index = key - 'A';
// Return value at index
return IRCodes[index];
}
int extractValidHeading(String inputString) {
int foundNumber = -1; // Default value if no valid number is found
// Loop through each character in the string
for (int i = 0; i < inputString.length(); i++) {
// Check if the current character is a digit
if (isdigit(inputString.charAt(i))) {
// Parse the number starting from this position
foundNumber = inputString.substring(i).toInt();
// Check if the parsed number is within the valid range (0-360)
if (foundNumber >= 0 && foundNumber <= 360) {
return foundNumber; // Return the valid number
} else {
return -1; // Return -1 if the number is outside the valid range
}
}
}
return foundNumber; // Return -1 if no number is found in the string
}
void setup() {
// Start serial
Serial.begin(9600);
delay(10);
// 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());
// Fill the array
char currentChar = 'A';
for (int i = 0; i < 360; i++) {
headingmap[i] = currentChar;
if ((i + 1) % 15 == 0) {
currentChar++;
if (currentChar > 'L')
currentChar = 'A';
}
}
Serial.println("OK");
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String input_message;
String input_parameter;
if (request->hasParam(input_heading)) {
input_message = request->getParam(input_heading)->value();
input_parameter = input_heading;
}
else {
input_message = "No message sent";
input_parameter = "none";
}
Serial.println(input_message);
uint16_t val = getValueByKey(input_message[0]);
Serial.println("IR Code: " + String(val));
irsend.sendNEC(0x0, val, 2);
delay(500);
request->send_P(200, "text/html", index_html);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
}