Hi all, I am working on a home automation project, in which we are trying to make a system like ESP12E will update the ON/OFF status of each appliances of a room on Firebase Realtime Database.
And its working absolutely fine till we haven't attached the code of LittleFS(File System). Whenever we attached the LittleFS Code, it start misbehaving. After fresh code uploading it is updating the database only once, after that is it stops working automatically without any connection lost. I don't know what is happing with it, and some times the same code automatically starts working fluently. Is there any Networking issues?? or any Code Error??
Please look at below code in which i'm simply sending/receiving the data from esp12e chip to firebase real-time database.
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// Set these to run example.
char webpage[] PROGMEM = R"=====(
<html>
<head>
<title>Reset Password</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
background-color: gray;
color: white;
background-size: 5%;
}
.maincontainer {
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
}
.resetsection {
width: 350px;
height: 550px;
box-shadow: 0 10px 25px rgba(255, 255, 255, 0.2);
display: flex;
align-items: center;
flex-direction: column;
background: linear-gradient(#18191a, #3a3b3c);
border-radius: 40px;
}
.animcon {
background-color: white;
overflow: hidden;
margin-top: 20px;
height: 170px;
width: 170px;
border-radius: 50%;
background-image: url(https://raw.githubusercontent.com/naaficodes/Monkey-Login/master/images/monkey.gif);
background-size: 90% 85%;
background-repeat: no-repeat;
background-position: center;
box-shadow: 0 10px 25px rgba(255, 255, 255, 0.2);
display: flex;
align-items: center;
flex-direction: column;
}
.animcon img {
margin-top: 110%;
height: 170px;
width: 170px;
transition: 1s;
}
.formcon {
margin-top: 38px;
}
input {
height: 44px;
width: 300px;
background-color: #3a3b3c;
border-radius: 20px;
border: none;
color: #d1d1d1;
text-indent: 15px;
font-size: 100%;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
outline: none;
}
input::placeholder {
color: #c0bebe;
font-size: 100%;
font-weight: lighter;
text-indent: 15px;
}
.sbutton {
text-indent: 0px;
height: 40px;
width: 300px;
margin-top: 10px;
background-color: teal;
transition: "2s";
border: none;
color: white;
font-weight: bolder;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
outline: none;
}
.sbutton:hover {
color: #5a5449;
cursor: pointer;
}
@media screen and (prefers-color-scheme: light) {
body {
background-color: gray;
color: black;
background-size: 5%;
}
.resetsection {
background: white;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
.animcon {
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
input {
background-color: #ffffff;
color: #353434;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
}
</style>
</head>
<body>
<div class="maincontainer">
<div class="resetsection">
<div class="animcon" id="animcon">
</div>
<div class="formcon">
<form>
<input
value=""
type="text"
id="ssid"
placeholder="SSID"
onclick="openeye();"
/>
<br />
<br />
<input
type="password"
value=""
id="password"
placeholder="PASSWORD"
onclick="closeye();"
/>
<br />
<br />
<input
type="text"
value=""
id="userId"
placeholder="USER ID"
onclick="openeye();"
/>
<br />
<br />
<input
type="text"
value=""
id="deviceId"
placeholder="DEVICE ID"
onclick="openeye();"
/>
<br />
<br />
<input
type="submit"
class="sbutton"
id="savebtn"
type="submit"
onclick="myFunction()"
/>
<div id="dialog" style="color: teal;text-align:center"></div>
</form>
</div>
</div>
</div>
</body>
<script>
function myFunction() {
console.log("button was clicked!");
document.getElementById("dialog").innerHTML = "Submited Succesfully";
var ssid = document.getElementById("ssid").value;
var password = document.getElementById("password").value;
var userId = document.getElementById("userId").value;
var deviceId = document.getElementById("deviceId").value;
var dataa = {
ssid: ssid,
password: password,
user_id: userId,
device_id: deviceId,
};
var xhr = new XMLHttpRequest();
var url = "/settings";
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
if (xhr.responseText != null) {
console.log(xhr.responseText);
}
}
};
xhr.open("POST", url, true);
xhr.send(JSON.stringify(dataa));
}
</script>
</html>
)=====";
#define FIREBASE_HOST "HOST URL"
#define FIREBASE_AUTH "AUTH ID"
ESP8266WebServer server;
char* password = "AP PASSWORD";
char* mySsid = "AP ID";
String _status = "bdfhjlnpr";
boolean StringReady=false;
bool _active=true;
String path="";
String active_path = "";
unsigned long myPrevMillis = millis();
void setup() {
Serial.begin(9600);
LittleFS.begin();
getData();
LittleFS.end();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Serial.write("Rectd");
}
void loop() {
SerialSense();
InternetSense();
if((millis()-myPrevMillis )>20000){
if(WiFi.status() == WL_CONNECTED){
Serial.write("Rectd");
}else{
Serial.write("Disctd");
}
myPrevMillis = millis();
}
}
void SerialSense(){
///////// Very IMPORTANT BLOCK/////////
String touchResponse="";
if((Serial.available()) && (!StringReady)){
touchResponse=Serial.readString();
StringReady=true;
}
if(StringReady){
int len = touchResponse.length();
if(len==9){
String _path=path;
_path+="/devices";
Firebase.setString(_path,touchResponse);
_status=touchResponse;
}
StringReady=false;
}
//////////////////////////////////////
}
void InternetSense(){
String _path= path;
_path+= "/devices";
String temp=Firebase.getString(_path);
if(temp!=_status){
_status=temp;
int len = temp.length();
for(int i=0;i<len;i++){
Serial.write(temp[i]);
}
}
}
void ApMode(){
Serial.write("Reset");
server.on("/",[](){server.send_P(200,"text/html", webpage);});
server.on("/settings", HTTP_POST, handleSettingsUpdate);
server.begin();
WiFi.mode(WIFI_AP);
WiFi.softAP(mySsid, password);
while(2){
server.handleClient();
}
}
void handleSettingsUpdate(){
String data = server.arg("plain");
DynamicJsonBuffer jBuffer;
JsonObject& jObject = jBuffer.parseObject(data);
File configFile = LittleFS.open("/config.json", "w");
jObject.printTo(configFile);
configFile.close();
server.send(200, "application/json", "{\"status\" : \"ok\"}");
Serial.write("Done");
WiFi.softAPdisconnect(true);
WiFi.disconnect();
}
void getData(){
if(LittleFS.exists("/config.json")){
const char * _ssid = "", *_pass = "";
const char *_user_id="", *_device_id="";
File configFile = LittleFS.open("/config.json", "r");
if(configFile){
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
configFile.close();
DynamicJsonBuffer jsonBuffer;
JsonObject& jObject = jsonBuffer.parseObject(buf.get());
if(jObject.success())
{
_ssid = jObject["ssid"];
_pass = jObject["password"];
_user_id = jObject["user_id"];
_device_id = jObject["device_id"];
path+="/";
path+=_user_id;
path+="/";
path+=_device_id;
WiFi.mode(WIFI_STA);
WiFi.disconnect();
WiFi.begin(_ssid, _pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
}
}else{
ApMode();
}
}