void Server_ini()
{
if (HTTP_Server == false)
{
server.on("/", []() {
// если был запущен сервер настроек и идет обращение к нему,
// то запускаем страницу настроек WiFi
Send_Module = Module_ID;
if (EEPROM_ini == true) Show_htm("/setting.htm");
else {
Read_State();
Show_htm("/status.htm");
}
});
server.on("/save", []() {
InputHTTP();
if (server.hasArg("first"))
{
// Serial.println("\nFirst SSID="+ server.arg("ssid")+" PASS="+server.arg("pswd"));
EEPROM_Save("first");
}
else {
EEPROM_Save("");
}
});
server.on("/clear", EEPROM_Clear);
server.on("/rescanWiFi", []() {
Show_htm("/setting.htm");
});
server.on("/memory", []() {
EEPROM_Read(true);
Show_htm("/setting.htm");
});
server.on("/spiffs", []() {
InputHTTP();
Spiffs_Files();
});
server.on("/sms", Sms);
server.on("/mailsetting", []() {
InputHTTP();
String S=String(server.arg("mailfrom"));
S.trim();
if(S=="" || (StringSymbol(S,"@")==1 && StringSymbol(S,".")>0))
{
String_to_Char(S,&smtpUser);
if(smtpUser!=NULL)
{
byte i=String(smtpUser).indexOf("@");
if(i<255) String_to_Char("smtp."+String(smtpUser).substring(i+1, String(smtpUser).length()),&smtpHost);
}
}
if (server.hasArg("mailpass"))
{
if (server.arg("mailpass").indexOf("**") != 1)
String_to_Char(server.arg("mailpass"),&smtpPass);
}
S=server.arg("mailto");
S.trim();
if(S=="" || (StringSymbol(S,"@")==1 && StringSymbol(S,".")>0))
String_to_Char(S,&mailTo);
String_to_Char(server.arg("mailsubj"),&Mail_Subj);
EEPROM_Mail_Save();
Show_htm("/setting.htm");
});
server.on("/entertime", []() {
Now_Time.GMT = server.arg("gmt").toInt();
if (Now_Time.year + 1900 < 2000) AskNTPTime = false;
else {
unsigned long Tm=Date_to_Unix(server.arg("mydate"), server.arg("mytime"));
secsUnix_delta = Tm - secsUnix;
secsUnix = Tm;
Unix_to_GMT(secsUnix, &Now_Time);
Serial.println(Time_to_String("dwt"));
AskNTPTime = true;
}
EEPROM_Time_Save();
Show_htm("/setting.htm");
});
server.on("/boot", []() {
// перезагрузка модуля
InputHTTP();
ESP_reset = true;
});
server.on("/onlyota", []() {
// ввод в режим Arduino OTA
InputHTTP();
Only_OTA = true;
if (Arduino_OTA) ArduinoOTA.begin();
Time_to_Time = millis();
});
server.on("/reset", []() {
// перезагрузка модуля
InputHTTP();
ESP.reset();
delay(1000);
});
server.on("/root", []() {
InputHTTP();
Show_htm("/setting.htm");
});
server.on("/json", []() {
InputHTTP();
Make_json();
});
server.on("/status", []() {
// Serial.println("\n**************/status Send Module="+Send_Module);
InputHTTP();
Show_htm("/status.htm");
});
server.on("/status1", []() {
InputHTTP();
Show_htm("/message.htm");
});
server.on("/diagram", []() {
InputHTTP();
Show_htm("/temp_hum.htm");
TF_file_All(0, 15);
});
server.on("/timer", []() {
if (Send_Module != Module_ID || Module_GPIO != Module_ID)
{
Send_Module = Module_ID;
Read_State();
}
Show_htm("/timer.htm");
});
server.on("/send_message", []() {
byte i,j;
InputHTTP();
String_to_Char(server.arg("modul"), &Send_Message);
Message_send = server.arg("message");
// Serial.println("/send_message Send_Module="+Send_Module+" server.arg(modul)="+server.arg("modul")+" Module_ID="+Module_ID+" Message="+Message_send);
for (i = 0; i < 4; i++)
ip_send[i] = server.arg("ip" + String(i)).toInt();
Period_send = server.arg("periodsend").toInt();
i = Message_send.indexOf("&from_ip");
if (i >= 0) Message_send = Message_send.substring(0, i);
if (Message_send.charAt(0)!='/') Message_send = "/" + Message_send;
if (Message_send.indexOf("?") == -1) Message_send = Message_send + "?";
Message_send.replace("?&", "?");
if (ip_send[0] != 0)
{
Message_send.replace("^", "&");
Write_Answer("Sended to IP: "+String(ip_send[0])+"." +String(ip_send[1])+"."+String(ip_send[2])+"."+String(ip_send[3])+" Message: "+ Message_send+" with period: "+String(Period_send));
}
else if (String(Send_Message) != "" && !Eq_module(String(Send_Message), Module_ID))
Write_Answer("Sended to module: ["+String(Send_Message)+"] Message: " + Message_send);
if(SendtoIP()==false) Write_Answer("Bad send. Message=" + Message_send);
myDelete(&Send_Message);
});
server.on("/transit", Send_Transit);
server.on("/OK", []() {
InputHTTP();
if(server.arg("func") == "transit")
for(byte i=0;i<5;i++)
if(For_Send[i]!=NULL)
if(String(For_Send[i]->Subj).indexOf("/transit") >= 0)
if(For_Send[i]->id == server.arg("from_id").toInt())
{
myDelete(&(For_Send[i]->Subj));
delete For_Send[i];
For_Send[i]=NULL;
}
if(server.arg("func") == "sendmail")
{
myDelete(&Mail_Message);
Write_Answer(" e-Mail was sended from "+server.arg("module_begin"));
}
});
server.on ("/gpio", []() {
InputHTTP();
byte i, On;
int tm;
String S = "", S1;
if (Module_GPIO != Module_ID) Read_State();
else Read_GPIO_State();
for (i = 0; i < All_GPIO_OUT; i++)
if (server.hasArg("g" + String(GPIO_OUT[i])))
{
S1=server.arg("g" + String(GPIO_OUT[i]));
S += "GPIO" + String(GPIO_OUT[i]) + " label " + String(GPIO_Name[GPIO_OUT[i]]);
if (S1 == "1")
{
Write_State(GPIO_OUT[i], 2, true);
S += " set HIGH";
}else if (S1 == "0")
{
Write_State(GPIO_OUT[i], 1, true);
S += " set LOW";
} else {
On=(S1.substring(0,1)).toInt()+1;
tm=(S1.substring(2,S1.length()-2)).toInt();
if(tm<10 || HTA[i]!=NULL)
{
Write_State(GPIO_OUT[i], On, true);
myPause(tm+1);
Write_State(GPIO_OUT[i], GPIO_OUT_S[GPIO_OUT[i]]%2+1, true);
} else {
Init_HTA(i);
HTA[i]->Timer_ON=On;
HTA[i]->GPIO_NEXT=secsUnix;
if(S1!="") HTA[i]->GPIO_LONG=tm;
else HTA[i]->GPIO_LONG=1;
S=" set in "+String(HTA[i]->Timer_ON==2?"HIGH":"LOW")+" for time "+String(HTA[i]->GPIO_LONG);
}
}
}
Serial.println(S);
if (server.hasArg("module_begin"))
SendModule(server.arg("module_end"), server.arg("module_begin"), "/sms?func=gpio_ans&mod_set=" + Module_ID + "&gpio_data=" + S + "&from_id="+server.arg("id"), 0);
else if (server.hasArg("from_module"))
SendModule(Module_ID, server.arg("from_module"), "/sms?func=gpio_ans&mod_set=" + Module_ID + "&gpio_data=" + S + "&from_ip=" + IP_to_String(isSTA(String_to_IP(server.arg("from_ip"))) ? AP_ip : ip) + "&from_chip=" + Chip_id + "&from_module=" + Module_ID +"&from_id="+server.arg("id"), 0);
else server.send ( 200, "text/html", S);
} );
server.begin();
Serial.println("HTTP server started");
HTTP_Server = true;
}
}