-->
Page 1 of 1

can't reconnect to ESP8266 SoftAP

PostPosted: Sun Jul 09, 2017 5:24 am
by Aleech Nedim
Hi guys,
I have an issue with ESP8266 boards (nodeMCU, wemos D1, standalone ESP):
after one connection with androids phones (they works great) there is no possible to reconnect until ESP reset.
I have try latest Arduino IDE versions 1.8.2 a older, but same issue. ESP boards are also the latest (2.3.0 ).
Here 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;
}


Re: can't reconnect to ESP8266 SoftAP

PostPosted: Mon Jul 10, 2017 10:55 am
by johnnyfrx
I've had luck with ONLY having the server.handleClient in the loop.
Try moving the digitalWrite(relej,termostat()); code elsewhere.

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


}

Re: can't reconnect to ESP8266 SoftAP

PostPosted: Mon Jul 10, 2017 2:41 pm
by Aleech Nedim
I have moved digitalWrite command into handleclient() method but same issue. Also I have reduced dhcp lease time to 3 minutes. Didn't help :cry:

Re: can't reconnect to ESP8266 SoftAP

PostPosted: Fri Jul 14, 2017 10:44 am
by johnnyfrx
Try adding this to your code:
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}