-->
Page 1 of 1

ESP8266 AP mode won't let reconnect

PostPosted: Sat Jul 08, 2017 1:52 pm
by Aleech Nedim
Hi guys,
I have a problem with androids phones: after one connection they wont reconnect (forbidden acces). I use many of solutions as: another channel, make STA then AP, newer version of IDE 1.8.2 etc, same problem on various boards like nodeMCU, wemosD1, standalone ESP8266.
ESP boards version 2.3.0 in IDE.
This is my code:
Code: Select all#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define ONE_WIRE_BUS_1 4
#define ONE_WIRE_BUS_2 5
#define relej 13

OneWire oneWire_topli(ONE_WIRE_BUS_1);
OneWire oneWire_hladni(ONE_WIRE_BUS_2);
DallasTemperature sensor_topli(&oneWire_topli);
DallasTemperature sensor_hladni(&oneWire_hladni);
ESP8266WebServer server(80);
int dif_temp,gasenje;
bool prvo=false;
void setup() {
 
  pinMode(relej,OUTPUT);
  digitalWrite(relej,0);
    EEPROM.begin(4);
  dif_temp=EEPROM.read(0);
  gasenje=EEPROM.read(1);
  EEPROM.end();
 
  sensor_topli.begin();
  sensor_hladni.begin();
  WiFi.mode(WIFI_AP);
  WiFi.softAP("termostat", "12345678",10);
  delay(2000);
  server.on("/submit",handleSubmit);
  server.on("/", handleRoot);
  server.begin();

}

void loop() {
  server.handleClient();
  digitalWrite(relej,termostat());


}

void handleRoot() {
  EEPROM.begin(4);
  dif_temp=EEPROM.read(0);
  gasenje=EEPROM.read(1);
  EEPROM.end();
  // put your main code here, to run repeatedly:
   char hladna[8];
   char topla[8];
     float f=daj_topli();
     ftoa(topla,  f, 1);
     f=daj_hladni();
     ftoa(hladna,f,1);
        char temp[1090];
   snprintf ( temp, 1089,

"<html>\
  <head>\
  <meta http-equiv='refresh' content='7'>\
    <title>wifi termostat</title>\
    <style>\
    .button {\
    background-color: #4CAF50;\
    border: none;\
    color: white;\
    padding: 15px 32px;\
    text-align: center;\
    text-decoration: none;\
    display: inline-block;\
    font-size: 26px;\
    margin: 4px 2px;\
    cursor: pointer;\
}   body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; font-size: 30px; }\
    </style>\
  </head>\
  <body>\
    <h1>Wifi termostat</h1>\
    <p>Topla: %s C</p>\
    <p>Hladna: %s C</p>\
    <form action='http://192.168.4.1/submit' method='POST'>\
    Diferencijalna temperatura(*0.5C):<input type='text' name='fname' value='%d' maxlength='1' size='3' ><br>\
    Temperatura izjednacenja(*0.5C):<input type='text' name='temp' value='%d' maxlength='1' size='3'><br>\
    <input type='submit' class='button' value='Podesi'>\
     <a href='..' class='button'>Osvjezi</a>\
</form>\
  </body>\
</html>",
      topla,hladna,dif_temp,gasenje
      ); 
   
   server.send(200, "text/html", temp);

 
     
 
}
float daj_topli(void){
  sensor_topli.requestTemperatures();
  delay(100);
return sensor_topli.getTempCByIndex(0);
  }
float daj_hladni(void){
  sensor_hladni.requestTemperatures();
  delay(100);
return sensor_hladni.getTempCByIndex(0);
  }

  void handleSubmit()
{
 if (server.args() > 0 ) {
   for ( uint8_t i = 0; i < server.args(); i++ ){
      if (server.argName(i) == "fname")   { uint8_t temp=server.arg(i).toInt();
        EEPROM.begin(4);   
        EEPROM.write(0, temp);
        EEPROM.commit();
      }
     if (server.argName(i) == "temp")   { uint8_t temp=server.arg(i).toInt();
       EEPROM.begin(4);   
        EEPROM.write(1, temp);
        EEPROM.commit();
     } 
   }   
 }


    char temp[500];
   snprintf ( temp, 500,

"<html>\
<body>\
  <head>\
    <title>WiFi termostat</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body><br><br>\
  <p>Postavke spremljene</p>\
  <a href='..'><button>NAZAD</button></a>\
  </body>\
</html>");
 server.send(200, "text/html", temp);
}




char *ftoa(char *a, double f, int precision)
{
 long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
 
 char *ret = a;
 long heiltal = (long)f;
 itoa(heiltal, a, 10);
 while (*a != '\0') a++;
 *a++ = '.';
 long desimal = abs((long)((f - heiltal) * p[precision]));
 itoa(desimal, a, 10);
 return ret;
}
bool termostat(void){
  float g,dif;
  dif=dif_temp*0.5;
  g=gasenje*0.5;
  float a=daj_topli();
  float b=daj_hladni();
  if (a-b>dif) {prvo=true; return true;}
  if (a-b>g&&prvo==true) return true;
  prvo=false;
  return false;
}