Skip to content

Chip Crashing when changing from SoftAP Mode to Station Mode #5737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
Aggeloz opened this issue Feb 8, 2019 · 7 comments
Closed
6 tasks done

Chip Crashing when changing from SoftAP Mode to Station Mode #5737

Aggeloz opened this issue Feb 8, 2019 · 7 comments

Comments

@Aggeloz
Copy link

Aggeloz commented Feb 8, 2019

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [ESP-12]
  • Development Env: [Arduino IDE]
  • Operating System: [Windows

Settings in IDE

  • Module: [Nodemcu]
  • Flash Mode: [qio|dio|other]
  • Flash Size: [4MB]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: [nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [115200] (serial upload only)

Problem Description

So i want to make a captive portal on my esp8266 so i can put my WiFi info and then use them to connect to my home wifi....I start with SoftAP mode i serve a page that i just put my WiFi info into then i press a button it sends the info to the chip as a JSON file i parse it then put the SSID and PASS on vars then i close the SoftAP and start a Station Mode with the into that i got but every time i try to send the data the chip crashes immediately after it receives the data and gives me this :

Exception (9):
epc1=0x40208d46 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4f575357 depc=0x00000000

ctx: cont
sp: 3ffffd50 end: 3fffffd0 offset: 01a0

stack>>>
3ffffef0: 3ffef7d4 3ffef418 3ffef450 402081cd
3fffff00: 00000000 3fffdad0 3ffef9f8 00000030
3fffff10: 00000000 3fffdad0 3ffe85f8 3ffef418
3fffff20: 3ffef7d4 000000b4 4020a36c 3fffefb0
3fffff30: 3ffef7d4 3ffef418 00000000 402017af
3fffff40: 00000000 3ffef418 3ffef504 40208da8
3fffff50: 00000000 00000000 4bc6a7f0 00000000
3fffff60: 4020208d 00000000 40106644 3ffef9f8
3fffff70: 3fffdad0 00000000 3ffef418 40208dd9
3fffff80: 00d5f721 00000000 3ffef418 40203bed
3fffff90: 00000000 00000000 00000000 3ffef9f8
3fffffa0: 00000000 00000000 3ffef9f2 40203c31
3fffffb0: 3fffdad0 00000000 3ffef9f2 4020a3f8
3fffffc0: feefeffe feefeffe 3ffe85f8 40100739
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(1,6)

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <ArduinoJson.h>
#include <WebSocketClient.h>
#include <string>
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
//#include <DNSServer.h>

//char* ssid = "AGGELOS SSID";
//char* password = "12345678900";


ESP8266WebServer server;
WebSocketsServer webSocket = WebSocketsServer(81);

//DNSServer dnsServer;
//IPAddress ip(192,168,1,5);
//IPAddress gateway(192,168,1,254);   
//IPAddress subnet(255,255,255,0);   



char jsontest[20];
String jsontest2;
int SoftAPFLAG=0;

const int capacity = JSON_OBJECT_SIZE(10);

StaticJsonBuffer<capacity> WIFI_JSON_BUFFER;

char ssid[20];
char password[20];

const char* SoftAPSSID = "BlackBox Access Point";
const char* SoftAPPASS = "";




uint8_t pin_lamp1 = 2;
uint8_t pin_lamp2 = 0;




char CaptivePortal[] PROGMEM = R"=====(
<!DOCTYPE html>
  <html>
  <title>Captive Portal</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="theme-color" content="#333333">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
  <link rel="icon" href="https://instagram.fath5-1.fna.fbcdn.net/vp/f0985bf5d3ac0d431168476364440fc7/5C843E49/t51.2885-19/s150x150/44349720_579346052485576_2520493195356274688_n.jpg" type="image/png" >
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
  <style>
    body { background-color: #333333; top: 0; left: 0; margin: 0; font-family: 'Open Sans', sans-serif; }
    #Header { padding: 0 30px; margin: 0; font-weight: 400; text-align: center; color: white; font-size: 30px; text-decoration: none; }
    #Copyright { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; color: white; font-size: 15px; }


*{
    box-sizing: border-box;

}


.form-group {
    height : 40px;
    width: 300px;
    position: relative;
    margin-bottom: 35px;
}

.form-group input{
    background : none;
    border: none;
    width: 100%;
    height: 100%;
    padding: 15px 8px 5px;
    border-bottom: 2px solid #ddd;
    color: white; /* makes the cursor and text invisible */
    font-size: 16px;
    outline: none; /* remove browser field outlines on focus */
    -webkit-appearance: none; /* prevent browser field styling */
    -moz-appearance: none;
}

.form-group label{
    position : absolute;
    bottom: 4px;
    left: 8px;
    font-size: 16px;
    opacity: .5;
    height: 20px;
    font-family: sans-serif;
    color: #666;
    cursor: text;
    /* this makes it so the label moves up and down smoothly */
    transition:
            font-size .3s ease,
            bottom .3s ease,
            opacity .3s ease,
            left .3s ease;
}


/* makes field color visible on focus and blur if there is typed text */
.form-group input:focus, .form-group input.blurred{
    color : white;
    border-color: black;
}

.form-group input:focus {
    transition:
            color 0s 0.2s, /* the text color of text and cursor shows up mid label transition */
            border-color .3s;
}

/* keeps label up when on focus and blur if there is typed text */
.form-group input:focus + label, .form-group input.blurred + label{
    font-size : 12px;
    bottom: 25px;
    left: 0;
    opacity: .3;
}
a:link{
  color : white;
  text-decoration: none;
  -webkit-transition-duration: 0.4s; /* For Safari */
  transition-duration: 0.4s;
}
a:visited{
  color : white;
  text-decoration: none;
}

a:hover{
  color : red;
  text-decoration: underline;
}

a:active{
  color : grey;
  text-decoration: underline;
}


.active{
background-color : black;
}

ul{
list-style-type : none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #282828;
}

li{
float : left;
}

li a {
display : block;
color: white;
padding: 14px 16px;
text-align: center;
text-decoration: none;
}

li a:hover{
background-color : #111;
  }


.grid-container{
display : grid;
grid-row-gap: 20px;
padding: 10px;
      }

.grid-item{
text-align : center;
  }
.copy{
padding : 20px;
text-decoration: none
color: #444;
}

.group:before,
.group:after {
    content: "";
    display: table;
}
.group:after {
    clear: both;
}
.group {
    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}


.row {
  position: relative;
  text-align: center;
}

.column_center {
  display: inline-block;
  padding: 25px;
}

.Backing{
  height: auto;
}

label {
  float: left;
  margin: 0 5px;
  text-align:left;
}

input {
  width: 240px;
  height: 50px;
  display: block;
}

@media only screen
and (max-width : 600px) {
label {
float: none;
margin: 0 5px;
text-align:center;
}}

.Button{
  background-color: #55555;
  border: none;
  color: black;
  padding: 10px 24px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
  -webkit-transition-duration: 0.4s; /* For Safari */
  transition-duration: 0.4s;
  }
  .Button:hover{
    background-color: #474747;
    color: white;
  }
  .Button1{
    background-color: #55555;
    color: black;
    border: 2px solid #303030;
  }
  ::-moz-selection { /* Code for Firefox */
    color: black;
    background: white;
  }

  ::selection {
    color: black;
    background: white;
  }

</style>
<body onload="javascript:Init();">
  <h1 id="Header">WiFi Credentials</h1>
<div class="Backing">
<div class="row" >
  <form class="column_center" action="" >
    <div class="form-group" >
      <input name="SSID" type="text" id="SSID">
      <label for="SSID">WIFI SSID</label>
    </div>
    <div class="form-group" >
      <input name="PASSWORD" type="password" id="PASSWORD">
      <label for="PASSWORD">WIFI PASSWORD</label>
    </div>
        <button class="Button Button1" id="lamp1on" type="button" onclick="WiFiPar();">Set WiFi Credentials</button>
  </form>
</div>
</div>

  <script>

    const textFields = document.querySelectorAll('input[type="text"]');
        textFields.forEach(field => {
          // handle field on blur
          field.addEventListener('blur', () => {
            if (field.value.trim().length) {
              field.classList.add('blurred');
            } else {
              field.classList.remove('blurred');
            }
          });
        })
        const textFields1 = document.querySelectorAll('input[type="password"]');
            textFields1.forEach(field => {
              // handle field on blur
              field.addEventListener('blur', () => {
                if (field.value.trim().length) {
                  field.classList.add('blurred');
                } else {
                  field.classList.remove('blurred');
                }
              });
            })

        var SSID;
        var PASSWORD;
        var Socket;


        function Init(){
          Socket = new WebSocket('ws://' + window.location.hostname + ':81/');
          Socket.onmessage = function(event){
          console.log(event.value);

          };
        }
        function WiFiPar(){
          SSID = document.getElementById('SSID').value;
          PASSWORD = document.getElementById('PASSWORD').value;
          var JSON1 = { "SSID": SSID, "PASSWORD" : PASSWORD };
          var JSON2 = JSON.stringify(JSON1)
          console.log(JSON2);
          Socket.send(JSON2);
        }


  </script>
  <hr>
  <footer class="copy" id="Copyright" >&copy; Copyright 2018 BlackBox</footer>

</body>

</html>

)=====";

//(WEBUI file)
char webpage[] PROGMEM = R"=====(
<!DOCTYPE html>
  <html>
  <head>
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
  <title>Black-Box</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="theme-color" content="#333333">
  <link rel="icon" href="https://instagram.fath5-1.fna.fbcdn.net/vp/f0985bf5d3ac0d431168476364440fc7/5C843E49/t51.2885-19/s150x150/44349720_579346052485576_2520493195356274688_n.jpg" type="image/png" >
  <style>
  body { background-color: #333333; top: 0; left: 0; margin: 0; font-family: 'Open Sans', sans-serif; }
  #RGBColor { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 30px; }
  #RangeR { padding: 10 30px; margin: 0; font-weight: 300; text-align: center; }
  #RangeG { padding: 10 30px; margin: 0; font-weight: 300; text-align: center; }
  #RangeB { padding: 10 30px; margin: 0; font-weight: 300; text-align: center; }
  #Temp1 { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; color: white; font-size: 35px; }
  #Temp2{ padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 25px; }
  #Humi1 { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 25px; }
  #Humi2 { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; color: white; font-size: 35px;}
  #lamp1textstat { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 25px; color: white;}
  #lamp2textstat { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 25px; color: white;}
  #Lamp1Buttons { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; }
  #Lamp2Buttons { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; }
  #Header { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; font-size: 40px; color: white;}
  #Copyright { padding: 0 30px; margin: 0; font-weight: 300; text-align: center; color: white; font-size: 15px; }

  a:link{
    color: white;
    text-decoration: none;
    -webkit-transition-duration: 0.4s; /* For Safari */
    transition-duration: 0.4s;
  }
  a:visited{
    color: white;
    text-decoration: none;
  }

  a:hover{
    color: red;
    text-decoration: underline;
  }

  a:active{
    color: grey;
    text-decoration: underline;
  }

.Button{
  background-color: #55555;
  border: none;
  color: black;
  padding: 10px 24px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
  -webkit-transition-duration: 0.4s; /* For Safari */
  transition-duration: 0.4s;
  }
  .Button:hover{
    background-color: #474747;
    color: white;
  }
  .Button1{
    background-color: #55555;
    color: black;
    border: 2px solid #303030;
  }


.slidecontainer {
      width: 100%;
  }


.slider{
      -webkit-appearance: none;
      width: 200px;
      height: 15px;
      border-radius: 5px;
      background: #d3d3d3;
      outline: none;
      opacity: 0.7;
      -webkit-transition: .2s;
      transition: opacity .2s;
    }

.slider:hover {
      opacity: 1;
  }


.slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      border-radius: 50%;
      background: #101110;
      cursor: pointer;
  }


.slider::-moz-range-thumb {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background: #4CAF50;
      cursor: pointer;
  }


.grid-container {
    display: grid;
    grid-row-gap: 20px;

    padding: 10px;
  }


.grid-item{
    text-align: center;



    }
.active{
                background-color: black;
            }

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #282828;
    }

    li {
        float: left;
    }

    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }

    li a:hover {
        background-color: #111;

        }
.copy {
  padding: 20px;
      }
      ::-moz-selection { /* Code for Firefox */
        color: black;
        background: white;
      }

      ::selection {
        color: black;
        background: white;
      }

  </style>
  <script>
    var Socket;
    function init() {
      Socket = new WebSocket('ws://' + window.location.hostname + ':81/');
      Socket.onmessage = function(event){
        console.log(event);
        var STATUStxt = event.data;
        var STATUSobj = JSON.parse(STATUStxt);
        var e1 = document.getElementById('lamp1status');
        var e2 = document.getElementById('lamp2status');
        if(STATUSobj.lamp1 === "lamp1on" || event.data === 'lamp1on'){
          e1.innerHTML="Livingroom Lamp ON";
          }
        else if(STATUSobj.lamp1 === "lamp1off" || event.data === "lamp1off" ) {
          e1.innerHTML="Livingroom Lamp OFF";
          }


        if(STATUSobj.lamp2 === "lamp2on" || event.data === "lamp2on"){
          e2.innerHTML="Bedroom Lamp ON";
          }
        else if(STATUSobj.lamp2 === "lamp2off" || event.data === "lamp2off" ) {
          e2.innerHTML="Bedroom Lamp OFF";
          }


        var Rvalpin = parseInt(STATUSobj.Rpin);
        var Gvalpin = parseInt(STATUSobj.Gpin);
        var Bvalpin = parseInt(STATUSobj.Bpin);
        var rgbstat2 = document.getElementById('RGBstatus');
        rgbstat2.style.color = "rgb(" + Rvalpin  + "," + Gvalpin + "," + Bvalpin + ")";
        if(STATUSobj.eval === "START"){
        document.getElementById('RGBbrightnessR').value = Rvalpin*4;
        document.getElementById('RGBbrightnessG').value = Gvalpin*4;
        document.getElementById('RGBbrightnessB').value = Bvalpin*4;
        }
        var DHTtxt = event.data;
        var DHTobj = JSON.parse(DHTtxt);
        if(DHTobj.eval === "DHT"){
        document.getElementById("HUMIDITY").innerHTML = DHTobj.humidity + ".0%";
        document.getElementById("TEMP").innerHTML = DHTobj.temperature + ".0C";
        }



      };
    }
  function buttonclick(e){
    Socket.send(e.id);
  }
  function sendRGBvalR(){
    Socket.send("R"+document.getElementById("RGBbrightnessR").value);
    var r = parseInt(document.getElementById('RGBbrightnessR').value);
    var g = parseInt(document.getElementById('RGBbrightnessG').value);
    var b = parseInt(document.getElementById('RGBbrightnessB').value);
      RGBsend(r, g , b);

    }
  function sendRGBvalG(){
    Socket.send("G"+document.getElementById("RGBbrightnessG").value);
    var r = parseInt(document.getElementById('RGBbrightnessR').value);
    var g = parseInt(document.getElementById('RGBbrightnessG').value);
    var b = parseInt(document.getElementById('RGBbrightnessB').value);
      RGBsend(r, g , b);
    }

  function sendRGBvalB(){
    Socket.send("B"+document.getElementById("RGBbrightnessB").value);
    var r = parseInt(document.getElementById('RGBbrightnessR').value);
    var g = parseInt(document.getElementById('RGBbrightnessG').value);
    var b = parseInt(document.getElementById('RGBbrightnessB').value);
    var rgbstat = document.getElementById('RGBstatus');
      RGBsend(r, g , b);

    }

  function RGBsend(valR, valG, valB){
    var rgbstat = document.getElementById('RGBstatus');

    rgbstat.style.color = "rgb(" + valR/4  + "," + valG/4 + "," + valB/4 + ")";
    Socket.send("{" +'\"' + "Rpin" + '\"' +":" + valR/4 + "," + '\"' + "Gpin" + '\"' + ":" + valG/4 + "," + '\"' + "Bpin" + '\"' + ":"
    + valB/4 + "," + '\"' + "eval" + '\"' + ":" + '\"' + "START" + '\"' + "}");
  }

  </script>

  <body onload="javascript:init();">


<nav>
    <ul>
      <li><a class="active" href="home.html">Home</a></li>
      <li><a href="Second_ESP.html">Second</a></li>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="alarm.html" >Alarm</a></li>
      <!--<li><a href="about.html">About</a></li>-->
    </ul>
</nav>
  <div id="Header"><font>HOME SETTINGS</font></div>
    <div id="lamp1textstat">
      <div id="lamp1status"><font>Livingroom Lamp</font></div>
    </div>

    <div id="Lamp1Buttons">
      <button class="Button Button1" id="lamp1on" type="button" onclick="buttonclick(this);">On</button>
      <button class="Button Button1" id="lamp1off" type="button" onclick="buttonclick(this);">Off</button>
    </div>
    <div id="lamp2textstat">
      <div  id="lamp2status"><font>Bedroom Lamp</font></div>
    </div>
    <div id="Lamp2Buttons">
      <button class="Button Button1" id="lamp2on" type="button" onclick="buttonclick(this);">On</button>
      <button class="Button Button1" id="lamp2off" type="button" onclick="buttonclick(this);">Off</button>
    </div>
    <div id="RGBColor">
      <div id ="RGBstatus"><font>RGB Colour</font></div>
    </div>
    <div class="grid-container">
    <div id="RangeR">
      <input class="slider grid-item" type="range" min="0" max="1023" value="0" id="RGBbrightnessR" oninput="sendRGBvalR()"/>
    </div>

    <div id="RangeG">
      <input class="slider grid-item" type="range" min="0" max="1023" value="0" id="RGBbrightnessG" oninput="sendRGBvalG()"/>
    </div>

    <div id="RangeB">
      <input class="slider grid-item" type="range" min="0" max="1023" value="0" id="RGBbrightnessB" oninput="sendRGBvalB()"/>
    </div>
  </div>

    <div>
      <h1 id="Temp1">Temperature</h1>
    </div>
    <div id="Temp2">
      <font id="TEMP" color="white">TEMP</font>
    </div>
    <div>
      <h1  id="Humi2">Humidity</h1>
    </div>
    <div id="Humi1" >
      <font id="HUMIDITY" color="white">HUMIDITY</font>
    </div>
    <hr>
    <footer class="copy" id="Copyright" >&copy; Copyright 2018 BlackBox</footer>
      </body>
    </head>
  </html>

)=====";

const int RGBPIN_R = 14;
const int RGBPIN_G = 13;
const int RGBPIN_B = 12;

int isconnected = 0;

bool LAMP1status;
bool LAMP2status;



const char LAMP1ON[] = "lamp1on";
const char LAMP1OFF[] = "lamp1off";

const char LAMP2ON[] = "lamp2on";
const char LAMP2OFF[] = "lamp2off";



//(Pin statuses)
int lamp1stat;
int lamp2stat;
int Rpinstat;
int Gpinstat;
int Bpinstat;


DHT dht(DHTPIN, DHTTYPE); //(Initialization for the DHT11 sensor)
  float humi = dht.readHumidity();
  float temp = dht.readTemperature();

  char humichar[10];
  char tempchar[10];
  char testbuf[10];


char* humid;
char* temper;


unsigned long previousMillis = 0; //(Millis instead of delays)
const long interval = 20000;
unsigned long currentMillis = millis();





static void writeLAMP1(bool LAMP1on){ //(Controls the Pin for the LED, it can be used as an example to control other pins)
  LAMP1status = LAMP1on;
  if (LAMP1on){
    digitalWrite(pin_lamp1, 0);
    }
  else {
    digitalWrite(pin_lamp1, 1);
    }
  }

static void writeLAMP2(bool LAMP2on){ //(Controls the Pin for the LED, it can be used as an example to control other pins)
  LAMP2status = LAMP2on;
  if (LAMP2on){
    digitalWrite(pin_lamp2, 0);
    }
  else {
    digitalWrite(pin_lamp2, 1);
    }
  }



int PIN = 1234;
char testbuffer[25];

char jsonSETPIN[25]; /*= "{\"SETPIN\":1234,\"eval\":\"PIN\"}"*/


char jsonONCONNECT[100];
char jsonONPRESS[100];
char lamp1statc[8];
char lamp2statc[8];
char Rpinstatc[10];
char Gpinstatc[10];
char Bpinstatc[10];





char jsonDHT[50];

void TEMPsense(){ //(Reads Temperature and Humidity and then broadcasts a JSON file to the WEBUI)
  humi = dht.readHumidity();
  temp = dht.readTemperature();

  if (isnan(humi) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!"); //(In case that the sensos can't read )
    return;
  }
   humid = dtostrf(humi, 2, 0, humichar); //(Takes the  data(data type : Float) from the DHT11 sensor and converts it to char)

   temper = dtostrf(temp, 2, 0, tempchar);


strcpy(jsonDHT,"{\"temperature\":");  //(Creates the JSON file)
strcat(jsonDHT,temper);
strcat(jsonDHT,",\"humidity\":");
strcat(jsonDHT,humid);
strcat(jsonDHT,",\"eval\":\"DHT\"}");
   webSocket.broadcastTXT(jsonDHT); //(broadcasts the JSON file to the WEBUI)
   Serial.print(jsonDHT); //(print JSON string on serial for debbuging)

  }



void webSocketEventAP(uint8_t num, WStype_t type, uint8_t * payload, size_t length){
    if (type == WStype_CONNECTED){
        
      }
    if (type == WStype_DISCONNECTED){
        
      }    
    if (type == WStype_TEXT){
      if (WiFi.status()!=WL_CONNECTED && payload[0] == '{' ){
        strcpy(jsontest,(const char *)payload);    
        Serial.println(jsontest);
        JsonObject& obj = WIFI_JSON_BUFFER.parseObject(jsontest);
        const char* ssidjson = obj["SSID"];
        const char* passjson = obj["PASSWORD"];
        strcpy(ssid,ssidjson);
        strcpy(password,passjson);
        Serial.println(ssid);
        Serial.println(password);  
        WiFi.softAPdisconnect();
        WiFi.disconnect();
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid, password);
        delay(500);
        //dnsServer.stop();
        SoftAPFLAG=0;
        }
    }
}


void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){
char* LAMP1STATUS;
char* LAMP2STATUS;
  if (type == WStype_CONNECTED){
    isconnected = 1; //(Constant for knowing if a client is connected to the WEBUI)
    if(lamp1stat == 1){
      LAMP1STATUS = "lamp1on";
      }
    else if (lamp1stat == 0){
      LAMP1STATUS = "lamp1off";
      }

    if(lamp2stat == 1){
      LAMP2STATUS = "lamp2on";
      }
    else if (lamp2stat == 0){
      LAMP2STATUS = "lamp2off";
      }

   strcpy(jsonONCONNECT, "{\"lamp1\":");
   //itoa(lamp1stat,lamp1statc,10);
   strcat(jsonONCONNECT, "\"");
   strcat(jsonONCONNECT, LAMP1STATUS);
   strcat(jsonONCONNECT, "\",");
   strcat(jsonONCONNECT, "\"lamp2\":");
   strcat(jsonONCONNECT, "\"");
   strcat(jsonONCONNECT, LAMP2STATUS);
   strcat(jsonONCONNECT, "\"");


   strcat(jsonONCONNECT, ",\"Rpin\":");
   itoa(Rpinstat,Rpinstatc,10);

   strcat(jsonONCONNECT, Rpinstatc);
   strcat(jsonONCONNECT, ",\"Gpin\":");
   itoa(Gpinstat,Gpinstatc,10);

   strcat(jsonONCONNECT, Gpinstatc);
   strcat(jsonONCONNECT, ",\"Bpin\":");
   itoa(Bpinstat,Bpinstatc,10);
   strcat(jsonONCONNECT, Bpinstatc);
   
   itoa(PIN,testbuffer,10);
   strcat(jsonONCONNECT,",\"SETPIN\":");
   strcat(jsonONCONNECT,testbuffer);



   strcat(jsonONCONNECT, ",\"eval\":\"START\"}");
   

  TEMPsense();
   
   webSocket.broadcastTXT(jsonONCONNECT);

   Serial.println(jsonONCONNECT); //(Printing in Serial COM for debbuging)
}











    if (type == WStype_DISCONNECTED){
      isconnected = 0;
      }





  if (type == WStype_TEXT){
      uint8_t lamp1status = (uint8_t) strtol((const char *) &payload, NULL, 10);


      if (strcmp (LAMP1ON,(const char *)payload) == 0){


        lamp1stat = 1;
        //Serial.print(lamp1stat); //(Printing inside the COM for debbuging)
        Serial.println("Lamp 1 is on");

        writeLAMP1(true);
        strcpy(jsonONPRESS,"{\"lamp1\":\"lamp1on\"}");
        webSocket.broadcastTXT(jsonONPRESS);
      }
      else if (strcmp (LAMP1OFF,(const char *)payload) == 0){


        lamp1stat = 0;

        //Serial.print(lamp1stat); //(Printing inside the COM for debbuging)
        Serial.println("Lamp 1 is off");

        writeLAMP1(false);
        strcpy(jsonONPRESS,"{\"lamp1\":\"lamp1off\"}");
        webSocket.broadcastTXT(jsonONPRESS);
      }



      if (strcmp (LAMP2ON,(const char *)payload) == 0){


        lamp2stat = 1;
        //Serial.print(lamp2stat); //(Printing inside the COM for debbuging)
        Serial.println("Lamp 2 is on");

        writeLAMP2(true);
        strcpy(jsonONPRESS,"{\"lamp2\":\"lamp2on\"}");
        webSocket.broadcastTXT(jsonONPRESS);
      }
      else if (strcmp (LAMP2OFF,(const char *)payload) == 0){


        lamp2stat = 0;

        //Serial.print(lamp2stat); //(Printing inside the COM for debbuging)
        Serial.println("Lamp 2 is off");

        writeLAMP2(false);
        strcpy(jsonONPRESS,"{\"lamp2\":\"lamp2off\"}");
        webSocket.broadcastTXT(jsonONPRESS);
      }


    if(payload[0] == 'R'){
      uint16_t RGBbrightnessR = (uint16_t) strtol((const char *)&payload[1], NULL, 10);


      Rpinstat = RGBbrightnessR / 4;

      Serial.println(" Rpin_value= ");
      Serial.println(Rpinstat); //(Printing inside the COM for debbuging)


      analogWrite(RGBPIN_R, RGBbrightnessR);
      //Serial.print("RED value= "); //(Prints the RGB values for debbuging)
      //Serial.println(RGBbrightnessR);
      }
    if (payload[0] == 'G'){
      uint16_t RGBbrightnessG = (uint16_t) strtol((const char *)&payload[1], NULL, 10);


      Gpinstat = RGBbrightnessG / 4;

      Serial.println(" Gpin_value= ");
      Serial.println(Gpinstat); //(Printing inside the COM for debbuging)


      analogWrite(RGBPIN_G, RGBbrightnessG);
      //Serial.print("GREEN value= "); //(Prints the RGB values for debbuging)
      //Serial.println(RGBbrightnessG);
      }
    if (payload[0] == 'B'){
      uint16_t RGBbrightnessB = (uint16_t) strtol((const char *)&payload[1], NULL, 10);


      Bpinstat = RGBbrightnessB / 4;

      Serial.println(" Bpin_value= ");
      Serial.println(Bpinstat); //(Printing inside the COM for debbuging)


      analogWrite(RGBPIN_B, RGBbrightnessB);
      //Serial.print("BLUE value= "); //(Prints the RGB values for debbuging)
      //Serial.println(RGBbrightnessB);
      }
      webSocket.broadcastTXT(payload, length);


      if (payload[0] == 'P'){
        PIN = strtol((const char *)&payload[1], NULL, 10);
        itoa(PIN,testbuffer,10);
        strcpy(jsonSETPIN,"{\"SETPIN\":");
        strcat(jsonSETPIN,testbuffer);
        strcat(jsonSETPIN,",\"eval\":\"PIN\"}");
        Serial.println(jsonSETPIN);
      }
  }






}

void handleRoot()
{
  server.send_P(200, "text/html", webpage);
}

void handleNotFound()
{
  server.send(404, "text/plain", "404: Not found");
}







void setup()
{
  pinMode(pin_lamp1, OUTPUT);
  pinMode(pin_lamp2, OUTPUT);
  pinMode(RGBPIN_R, OUTPUT);
  pinMode(RGBPIN_G, OUTPUT);
  pinMode(RGBPIN_B, OUTPUT);
  writeLAMP1(false);
  writeLAMP2(false);
  lamp1stat = 0;
  lamp2stat = 0;
  dht.begin();
  Serial.begin(115200);

  WiFi.mode(WIFI_AP); 
  WiFi.softAP(SoftAPSSID, SoftAPPASS);
  Serial.print("Booting In Acces Point Mode With The Name:\t");
  Serial.println(SoftAPSSID);
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());
  SoftAPFLAG = 1;
  Serial.print("SoftAPFlag:\t");
  Serial.println(SoftAPFLAG);





  server.on("/",[](){
    server.send_P(200, "text/html", webpage);
  });

  
  server.on("/home.html",[](){
    server.send_P(200, "text/html", webpage);
  });

    server.on("/EditConnection.html",[](){
   server.send_P(200, "text/html", CaptivePortal);
  });
  
server.onNotFound(handleNotFound);

  
  server.begin();
  webSocket.begin();
  if(SoftAPFLAG == 0){
      webSocket.onEvent(webSocketEvent);
    }
  else if(SoftAPFLAG == 1){
      webSocket.onEvent(webSocketEventAP);
    }
  


}






void webthings(){
  webSocket.loop();
  server.handleClient();
  if(Serial.available() > 0){
    char c[] = {(char)Serial.read()};
    webSocket.broadcastTXT(c, sizeof(c));
  }


  }




  void loop()
{
  webthings();
  unsigned long currentMillis = millis(); //(Millis instead of delays so it's not interupting other parts of the code)
  if(currentMillis - previousMillis >= interval && isconnected == 1){
    previousMillis = currentMillis;
    TEMPsense();

    }
}

@Aggeloz Aggeloz closed this as completed Feb 8, 2019
@ToyboxLabs
Copy link

I'm having the same problem, what was the solution?

@Aggeloz
Copy link
Author

Aggeloz commented Feb 19, 2019

I'm having the same problem, what was the solution?

i didn't find one i just closed the issue cause no one replied to it....should i re-open it?

@Aggeloz Aggeloz reopened this Feb 19, 2019
@ToyboxLabs
Copy link

Yeah I am trying to narrow down my code to see exactly what is causing it, but it seems to be some corruption when I switch between AP mode and STA mode.

@Aggeloz
Copy link
Author

Aggeloz commented Feb 19, 2019

Yeah I am trying to narrow down my code to see exactly what is causing it, but it seems to be some corruption when I switch between AP mode and STA mode.

i think it has to do something with the way im storing the values for the SSID and the PASSWORD for the new wifi but i could be wrong

@ToyboxLabs
Copy link

Mine ended up being a completely different issues, so sorry to confuse you. I would suggest keep removing code until it doesn't repro and it should give you a better idea of what is actually happening.

@Aggeloz
Copy link
Author

Aggeloz commented Feb 20, 2019

Mine ended up being a completely different issues, so sorry to confuse you. I would suggest keep removing code until it doesn't repro and it should give you a better idea of what is actually happening.

What was your issue i might give your solution a try

@devyte
Copy link
Collaborator

devyte commented Oct 17, 2019

No feedback in 8 months, and the provided sketch is far from minimal. Closing.
Off the top of my head, I suggest a delay(100) after WiFi.(mode) as a potential workaround, see #6630 for discussion.

@devyte devyte closed this as completed Oct 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants