//#define FAN
#define ONECHANNELS
//#define TWOCHANNELS
//#define FOURCHANNELS
#if defined(ONECHANNELS) || defined(TWOCHANNELS) || defined(FAN)
#define CH1_PIN 0
#define CH2_PIN 2
#endif
#if defined(FOURCHANNELS)
#define CH1_PIN 12
#define CH2_PIN 13
#define CH3_PIN 14
#define CH4_PIN 15
#endif
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Ticker.h> //Ticker Library
#define _ssids 3
Ticker setTim1;
//Ticker setTim2;
//uint16_t N = 0;
//uint16_t M = 100;
//uint16_t X = 200;
//uint32_t cnt = 0;
IPAddress broadcastIp(192,168,1,255);
unsigned int localPort = 8002;
//Hex command to send to serial for close relay
const byte rel1ON[] = {0xA0, 0x01, 0x01, 0xA2};
//Hex command to send to serial for open relay
const byte rel1OFF[] = {0xA0, 0x01, 0x00, 0xA1};
//Hex command to send to serial for close relay
const byte rel2ON[] = {0xA0, 0x02, 0x01, 0xA3};
//Hex command to send to serial for open relay
const byte rel2OFF[] = {0xA0, 0x02, 0x00, 0xA2};
String _ssid[] = {"******", "******", "******"};
String _password[] = {"******", "******", "******"};
//char Buff[255]="";
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
char replyPacket[] = "Command OK";
uint32_t udp_tim = 0;
ESP8266WebServer server(80);
WiFiUDP UdpClt;
//WiFiUDP UdpSrv;
void setup() {
// put your setup code here, to run once:
Serial.begin(74880);
delay(100);
Serial.println("");
Serial.println("Hello!");
Serial.println(ESP.getFlashChipSize());
Serial.println(ESP.getFlashChipRealSize());
pinMode(CH1_PIN, OUTPUT);
digitalWrite(CH1_PIN, LOW);
pinMode(CH2_PIN, OUTPUT);
digitalWrite(CH2_PIN, LOW);
WIFIinit();
delay(1000);
myOTASetup();
delay(1000);
WEBroutine();
delay(1000);
// UdpSrv.begin(localPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localPort);
// setTim1.attach(5, udpSendState);
delay(5000);
Serial.begin(115200);
delay(1000);
Serial.write (rel1OFF, sizeof(rel1OFF));
delay(250);
Serial.write (rel2OFF, sizeof(rel2OFF));
for(int i = 0; i < 5; i++){
udpSendState();
delay(500);
}
setTim1.attach(5, udpSendState);
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
ArduinoOTA.handle();
//handleUDPServer();
delay(10);
}