/* BUTTON EXTENDER
? SERVER:
ESP32
Скетч:
% ESP-32 може працювати як сервер-клієнт, підключені до однієї WiFi мережі.
Extender приймає команди від client і тупо транслює їх в Serial
V.0.1
- Статичний IP
- TCP зєднання.
- Відключення клієнта при відсутності повідомлень від клієнта протягм 15сек.
- Ping між сервер клієнтом. Розрив зєднання при відсутності клієнта довше 10 сек
!Косяки:
TODO:
*/
int firmware = 0.1; //Версия прошивки
#include <Arduino.h>
#include <SPI.h>
#include <Streaming.h>
#include <WiFi.h>
// Set your access point network credentials
// const char *ssid = "ControlRoom";
// const char *password = "5425-Gro$Ven0r";
const char *ssid = "weplay";
const char *password = "5425grosvenor";
IPAddress local_IP(192, 168, 1, 100); // IP ESP-32
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional
const uint ServerPort = 4080;
WiFiServer Server(ServerPort);
WiFiClient RemoteClient;
#define sendQt 5 //кількіть раз відправки команди TCP клієнту
#define RSTBTN 26 //пін кнопки RESET
int CMNDmas[4] = {10, 20, 30, 40}; //команди по WIFI
int LEDmas [4] = {5, 18, 19, 21};
int BTNmas[4] = {13, 12, 14, 27};
bool BTN = false;
unsigned long myTime; //Для таймера часу
unsigned long prevWatchDog; //доп таймер клієнта
unsigned long prevWifiTmr; //доп таймер wifi
const int dataSendInterval = 1000; // період відправки інфо клієнту
const int watchDog = 15000; //таймер скидання кліжнта і WiFi
void WiFibgn() {
// Configure static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial << "Connected to WiFi: " << ssid << endl;
Serial << "IP Address: " << WiFi.localIP() << endl;
Server.begin();
Serial << "WiFi Server started" << endl;
delay(100);
}
//Перевірка підключення Wifi
void wifiReconect() {
// if WiFi is down, try reconnecting
if ((WiFi.status() != WL_CONNECTED) && (myTime - prevWifiTmr >= watchDog)) {
Serial << "Reconnecting to WiFi..." << endl;
WiFi.disconnect();
WiFi.reconnect();
prevWifiTmr = myTime;
}
}
//Контроль підключення клієнтів
void connectionStatus() {
if (Server.hasClient()) { //вирубає другий конект.
if (RemoteClient.connected()) {
Serial.println("Connection rejected");
Server.available().stop();
} else {
Serial.println("Connection accepted");
RemoteClient = Server.available();
prevWatchDog = myTime;
}
}
if (RemoteClient.connected()) { //обмін повідомленнями між клієнт-сервер
if (RemoteClient.available()) {
char c = RemoteClient.read();
Serial.print(c);
prevWatchDog = myTime;
}
}
if (myTime - prevWatchDog > watchDog) { // Watchdog на підключення.
if (RemoteClient.connected()) {
Serial.println("Connection refused");
RemoteClient.stop();
prevWatchDog = myTime;
}
}
}
void setup() {
Serial.begin(115200);
Serial << "Setup start" << endl;
Serial << "Firmware version: " << firmware << endl;
// WiFibgn();
for (int i = 0; i < 4; i++)
{
pinMode(BTNmas[i], INPUT_PULLDOWN);
pinMode(LEDmas[i], OUTPUT);
Serial << i << endl;
}
delay(100);
pinMode(RSTBTN, INPUT_PULLDOWN);
Serial << "Setup done" << endl;
}
void loop() {
myTime = millis();
// wifiReconect();
// connectionStatus();
//Нажаття кнопки
if (BTN == false){
for (int i = 0; i < 4; i++){
if (digitalRead(BTNmas[i])){
Serial.println(CMNDmas[i]);
digitalWrite(LEDmas[i], HIGH);
if(RemoteClient.connected()){
for (int k = 0; k < sendQt; k++){
RemoteClient.write(CMNDmas[i]);
}
}
BTN = true;
}
}
}
//Кнопка RESET
if(BTN){
if (digitalRead(RSTBTN)){
BTN = false;
for (size_t i = 0; i < 4; i++){
digitalWrite(LEDmas[i], LOW);
}
}
}
//Кнопка RESET через WIFI
if(BTN){
if(RemoteClient.connected()){
if (RemoteClient.available()) {
if (RemoteClient.read() == 300){
BTN = false;
for (size_t i = 0; i < 4; i++){
digitalWrite(LEDmas[i], LOW);
}
Serial.print("Button Reset");
}
}
}
}
}