JVC Comp Link atualização
#include
#include
#define D1 26
const char* ssid = "ID da rede";
const char* password = "Senha da rede";
// Configuração de IP fixo
IPAddress local_IP(192, 168, 1, 6);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
WiFiServer server(80);
const unsigned long reconnectInterval = 10000;
const unsigned long clientTimeout = 500; // Reduzido para resposta mais rápida
unsigned long previousReconnectMillis = 0;
bool wifiInitialized = false;
// HTML armazenado estritamente na Flash (PROGMEM)
const char HTML_PAGE[] PROGMEM = R"rawliteral(
Controle JVC
)rawliteral";
void initWiFi() {
if (!wifiInitialized) {
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
WiFi.setSleep(false);
WiFi.begin(ssid, password);
wifiInitialized = true;
}
}
void checkWiFiReconnect() {
unsigned long currentMillis = millis();
if (WiFi.status() == WL_CONNECTED) return;
if (currentMillis - previousReconnectMillis >= reconnectInterval) {
previousReconnectMillis = currentMillis;
if (wifiInitialized) {
WiFi.disconnect();
WiFi.reconnect();
} else {
initWiFi();
}
}
}
void sendBit(bool bitValue) {
digitalWrite(D1, HIGH);
delayMicroseconds(5000);
digitalWrite(D1, LOW);
if (bitValue) {
delayMicroseconds(15000);
} else {
delayMicroseconds(5000);
}
}
void sendByte(uint8_t data) {
for (int i = 7; i >= 0; i--) {
sendBit(data & (1 << i));
}
digitalWrite(D1, HIGH);
delayMicroseconds(5000);
digitalWrite(D1, LOW);
}
// Analisa a requisição em um buffer estático sem alocação dinâmica de memória
const char* processRequest(const char* req) {
if (strstr(req, "/D1/on")) { sendByte(0x20); return "PLAY B"; }
if (strstr(req, "/D2/on")) { sendByte(0x25); return "PAUSE B"; }
if (strstr(req, "/D3/on")) { sendByte(0x29); return "REC B"; }
if (strstr(req, "/D4/on")) { sendByte(0x27); return "SIDE B"; }
if (strstr(req, "/D5/on")) { sendByte(0x23); return "FF B"; }
if (strstr(req, "/D6/on")) { sendByte(0x22); return "STOP B"; }
if (strstr(req, "/D7/on")) { sendByte(0x24); return "REW B"; }
if (strstr(req, "/D8/on")) { sendByte(0x60); return "PLAY A"; }
if (strstr(req, "/D9/on")) { sendByte(0x67); return "SIDE A"; }
if (strstr(req, "/D10/on")) { sendByte(0x63); return "FF A"; }
if (strstr(req, "/D11/on")) { sendByte(0x64); return "REW A"; }
if (strstr(req, "/D12/on")) { sendByte(0x62); return "STOP A"; }
if (strstr(req, "/D13/on")) { sendByte(0xBF); return "POWER OFF"; }
if (strstr(req, "/D14/on")) { sendByte(0xBE); return "POWER ON"; }
return NULL;
}
void handleClient(WiFiClient& client) {
char requestBuf[80];
size_t bytesRead = 0;
unsigned long startMillis = millis();
// Lê apenas a primeira linha do cabeçalho HTTP
while (client.connected() && (millis() - startMillis <= clientTimeout)) {
if (client.available()) {
char c = client.read();
if (c == '\r' || c == '\n') break;
if (bytesRead < sizeof(requestBuf) - 1) {
requestBuf[bytesRead++] = c;
}
}
}
requestBuf[bytesRead] = '\0';
const char* status = processRequest(requestBuf);
if (status != NULL) {
client.print(F("HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Cache-Control: no-cache\r\n"
"Connection: close\r\n\r\n"));
client.print(status);
} else {
client.print(F("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Cache-Control: no-cache\r\n"
"Connection: close\r\n\r\n"));
client.print((const __FlashStringHelper*)HTML_PAGE);
client.print(F("Aguardando comando"));
client.print((const __FlashStringHelper*)HTML_FOOTER);
}
client.flush();
}
void setup() {
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
initWiFi();
server.begin();
}
void loop() {
checkWiFiReconnect();
WiFiClient client = server.available();
if (client) {
handleClient(client);
client.stop();
}
yield();
}
Comentários
Postar um comentário