Hello I'm new to Arduino and esp2866. I'm building a wireless controlled relay through nodemcu v3 with esp2866, I'm using fire base as my database. I wanted to add a schedule functionality where I can set time shutdown or power on. Unfortunately when I add this date ( DateTimeCheck() ) code it just keeps tripping.
Here my code:
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <WiFiManager.h>
#include <NTPClient.h>
#include <WiFiUdp.h> //wifimanager by tzapu ver 2.0
#include <time.h>
// esp8266 ver 2.7.4
// ntpclient ver 3.2.1
// ArduinoJSON ver 5.13.4
// Firebase Arduino Client Library - mobiz - 4.2.6 - https://github.com/mobizt/Firebase-ESP-Client
// CHECK https://www.youtube.com/watch?v=8ZSjrvJL3dY&t=159s TO CONFIGURE CONNECTION
// https://www.instructables.com/NodeMCU-ProjectButton-Control-LED/ LED SWITCH
// https://dronebotworkshop.com/wifimanager/ WIFI MANAGER
#define FIREBASE_HOST ""
#define FIREBASE_AUTH ""
#define RELAY_PIN_1 D2
#define SWITCH_1 D1
#define LED_GREEN D6
int buttonState = 0;
int monthDay = 0;
int currentYear = 0;
int currentMonth = 0;
int timeout = 120;
unsigned long lastMillis;
String IOTSPPDVName = "IOTSPPDV001";
String FBrelaystatus = "";
String FBCurrentDate = "";
String FBCurrentTime = "";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
//Week Days
//String weekDays[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
void setup() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
//wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("IOTSPPDV001"); // password protected ap
if (!res) {
Serial.println("Failed to connect");
// ESP.restart();
} else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
}
pinMode(SWITCH_1, INPUT);
timeClient.begin();
timeClient.setTimeOffset(28800);
timeClient.update(); // utc +8
// DateTimeCheck();
//rescheck
int resStat = Firebase.getInt(IOTSPPDVName + "/rescheck");
resStat = resStat + 1;
Firebase.setInt(IOTSPPDVName + "/rescheck", resStat);
}
int n = 0;
void loop() {
buttonState = digitalRead(SWITCH_1); // put your main code here, to run repeatedly:
if (buttonState == 1) {
Serial.println("1:)");
delay(200);
Serial.println("EXECUTE AP RESET");
WiFiManager wm;
//reset settings - for testing
wm.resetSettings();
ESP.restart();
}
FBrelaystatus = Firebase.getString(IOTSPPDVName + "/Relay_status"); // get ld status input from firebase
if (FBrelaystatus == "ON") { // compare the input of led status received from firebase
//Serial.println("Led Turned ON");
RelaySwitch(1);
} else if (FBrelaystatus == "OFF") { // compare the input of led status received from firebase
//Serial.println("Led Turned OFF");
RelaySwitch(0);
// make external led OFF
} else {
Serial.println("Command Error! Please send ON/OFF");
Serial.println(FBrelaystatus);
}
if (millis() - lastMillis >= 1 * 60 * 1000UL) {
lastMillis = millis(); //get ready for the next iteration
// DateTimeCheck();
}
}
void RelaySwitch(int SwitchMode) {
if (SwitchMode == 1) {
digitalWrite(RELAY_PIN_1, LOW);
digitalWrite(LED_GREEN, HIGH);
Firebase.setString(IOTSPPDVName + "/Relay_status", "ON");
} else {
digitalWrite(RELAY_PIN_1, HIGH);
digitalWrite(LED_GREEN, LOW);
Firebase.setString(IOTSPPDVName + "/Relay_status", "OFF");
}
}
/*void DateTimeCheck()
{
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime((time_t *)&epochTime);
monthDay = ptm->tm_mday;
currentMonth = ptm->tm_mon + 1;
currentYear = ptm->tm_year + 1900;
String formattedDate = String(currentYear) + String(currentMonth) + String(monthDay);
String FBCurrentDate = Firebase.getString(IOTSPPDVName + "/Schedule/0/Date");
String FBCurrentTime = Firebase.getString(IOTSPPDVName + "/Schedule/0/Time");
String FBSchedSwitch = Firebase.getString(IOTSPPDVName + "/Schedule/0/Switch");
String isFBExecuted = Firebase.getString(IOTSPPDVName + "/Schedule/0/Executed");
if (formattedDate == FBCurrentDate && isFBExecuted == "0") {
String formattedTime = String(timeClient.getHours());
formattedTime = formattedTime + ":" + String(timeClient.getMinutes()) + ":00";
Serial.println(formattedTime);
if (formattedTime == FBCurrentTime) {
Serial.print("time and date the samee");
if (FBSchedSwitch == "true") {
RelaySwitch(1);
Firebase.setString(IOTSPPDVName + "/Schedule/0/Executed", "1");
} else {
RelaySwitch(0);
Firebase.setString(IOTSPPDVName + "/Schedule/0/Executed", "1");
}
}
Serial.println("samedate");
} else {
Serial.print("Not the same date");
}
}
With ESP, you have to feed the output to the exception decoder.
What part of your code is the "this date code" that you say introduces the error? Why is there a huge commented out section? What are we supposed to think about that?
sorry about that, I pasted this from my sketch, I added a comment to isolate that specific function.
I tried installing the decoder from in the ide, it wont show in the tools menu.
I tried troubleshooting the program to isolate the problematic code. I notice that when I add DateTimeCheck() function which I added a comment, the board restarts.
I don't know if this is useful information, but when I downgrade to esp 2.3.0 without the DateTimeCheck() it doesn't trip. The only reason why I'm not using it because is outdated.
a way to know
if it's staying in a function or waiting for something that keeps it from getting back to the top of the loop (approx 2.5 secs ??) then it will watchdog.
It will show up in the Crash Log as "wdt reset".
Update it seems that the problem is not just on DateTimeCheck() function, I tried running the program overnight on esp8266 ver 2.7.4, it restarts every 30-40 mins. Now I'm confused. Maybe its the firebase that is the problem? Downloading older ide to open decoder.
Hi everyone I manage to get decodes the error, I honestly don't understand what this means. My hunch is the wifi manager /ap manager. any idea how to implement a better ap/wifi functionality? thank you.
Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
Decoding 24 results
0x40211a14: EspClass::getCpuFreqMHz() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Esp.cpp line 499
0x4020afe0: WiFiManager::wifiConnectDefault() at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x4020be9c: WiFiManager::waitForConnectResult(unsigned int) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x4020c2c8: WiFiManager::processConfigPortal() at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x40204278: WiFiClientSecure::_connectSSL(char const*) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 517
0x4020cf0c: WiFiManager::handleWifi(unsigned char) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x40205bb3: std::string::assign(char const*, unsigned int) at c:\users\marla\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/basic_string.tcc line 732
0x402038b4: WiFiClient::~WiFiClient() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClient.cpp line 149
0x4020423c: SSLContext::connect(ClientContext*, char const*, unsigned int) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 517
: (inlined by) WiFiClientSecure::_connectSSL(char const*) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 279
0x40204b2d: UdpContext::UdpContext() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiUdp.cpp line 283
0x4020cec3: WiFiManager::handleWifi(unsigned char) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x40204bbe: WiFiUDP::beginPacketMulticast(IPAddress, unsigned short, IPAddress, int) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiUdp.cpp line 283
0x4020cf0c: WiFiManager::handleWifi(unsigned char) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x4020d53a: DEBUG_WM at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
: (inlined by) WiFiManager::debugSoftAPConfig() at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3348
0x402012dd: delay at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/core_esp8266_wiring.c line 45
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x40212120: Print::println(String const&) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 64
0x401001b1: umm_assimilate_down at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266\umm_malloc/umm_malloc.c line 1187
0x4020e1a4: WiFiManager::handleUpdate() at C:\Users\marla\OneDrive\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp line 3244
0x40100d71: __adddf3 at d:\ivan\projects\arduinoesp\toolchain\dl\gcc-xtensa\build-2\xtensa-lx106-elf\libgcc/../../../libgcc/config/xtensa/ieee754-df.S line 283
Another decoded exception. This time I added the DateTimeCheck() function, updated the espboard to 2.7.4, and removed <time.h>. It seems to have a problem with the wifi manager and data regarding firebase. Do you have any clue how to fix it?
Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
Decoding 40 results
0x402172dc: HTTPClient::connected() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266HTTPClient\src/ESP8266HTTPClient.cpp line 475
0x4020fc18: HTTPClient::disconnect(bool) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266HTTPClient\src/ESP8266HTTPClient.cpp line 434
0x40210b38: HTTPClient::end() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266HTTPClient\src/ESP8266HTTPClient.cpp line 425
0x40210f64: HTTPClient::begin(String, unsigned short, String, String) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266HTTPClient\src/ESP8266HTTPClient.cpp line 379
0x40204700: std::string::_Rep::_M_destroy(std::allocator const&) at c:\users\marla\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2\bits/basic_string.tcc line 452
0x40212574: String::copy(char const*, unsigned int) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.cpp line 214
0x4020603b: FirebaseHttpClientEsp8266::begin(std::string const&, std::string const&) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseHttpClient_Esp8266.cpp line 51
0x40203d3c: FirebaseRequest::sendRequest(std::string const&, std::string const&, char*, std::string const&, std::string const&) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\firebase-arduino-master\src/Firebase.cpp line 73
0x402046c4: std::basic_string , std::allocator >::basic_string(char const*, std::allocator const&) at c:\users\marla\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2\bits/basic_string.tcc line 218 (discriminator 2)
0x40204fb5: FirebaseArduino::getRequest(String const&) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseArduino.cpp line 101
0x4021252b: String::reserve(unsigned int) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.cpp line 146
0x40205046: FirebaseArduino::getString(String const&) at C:\Users\marla\OneDrive\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseArduino.cpp line 132
0x40212574: String::copy(char const*, unsigned int) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.cpp line 214
0x40212ba6: operator+(StringSumHelper const&, char const*) at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.cpp line 417 (discriminator 2)
0x402123b0: String::~String() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.cpp line 125
0x40201217: DateTimeCheck() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/WString.h line 324
: (inlined by) DateTimeCheck() at C:\Users\marla\OneDrive\Documents\Arduino\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2-20221204T102749Z-001\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2\old ide\sketch_dec05a/sketch_dec05a.ino line 135
0x40201072: _Z11RelaySwitchi$part$1 at C:\Users\marla\OneDrive\Documents\Arduino\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2-20221204T102749Z-001\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2\old ide\sketch_dec05a/sketch_dec05a.ino line 122
0x402015d6: loop at C:\Users\marla\OneDrive\Documents\Arduino\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2-20221204T102749Z-001\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2\old ide\sketch_dec05a/sketch_dec05a.ino line 111
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x402179f8: WiFiServer::_s_discard(void*, ClientContext*) at ?? line ?
0x40104825: lmacTxFrame at ?? line ?
0x4010440a: lmacMSDUAged at ?? line ?
0x40103c75: lmacRecycleMPDU at ?? line ?
0x4010430d: lmacMSDUAged at ?? line ?
0x40104586: lmacProcessCtsTimeout at ?? line ?
0x401028cb: wDev_ProcessFiq at ?? line ?
0x40100198: ets_post at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/core_esp8266_main.cpp line 177
0x40102584: wDev_ProcessFiq at ?? line ?
0x402015e4: loop at C:\Users\marla\OneDrive\Documents\Arduino\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2-20221204T102749Z-001\FirebaseDemo_ESP8266 - FIREBASE CONECTION_v2\old ide\sketch_dec05a/sketch_dec05a.ino line 111
0x40213898: loop_wrapper() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/core_esp8266_main.cpp line 197
0x40100198: ets_post at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/core_esp8266_main.cpp line 177
0x40213898: loop_wrapper() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/core_esp8266_main.cpp line 197
0x40100d55: esp8266::polledTimeout::timeoutTemplate >::expiredRetrigger() at C:\Users\marla\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/PolledTimeout.h line 237
I never use wifimanager, because it doesn't provide me with a server on both AP & STA (AP is used only to set the STA credentials)
You can serve a page to enter and load credentials if that is a requirement for your project, and store them in EEPROM.
As for a ready made setup. WiFiManager is the only one that i know of, but you can of course attempt to contact the Author on github and see if there is information there (or open an issue)
Possibly, but i don't want to disable my ad-blocker to read that.
you can post the code here and then i'll have a look.
It is not hugely complicated to do.
Well yeah that is not quite what i meant, but i guess it could work.
I start the unit in AP mode using.
WiFi.softAP(apname, appass);
and serve a page using the ESP8266webserver, in which the user can enter credentials, picking a network from the scan. connect to that network, and if connected, store that network in EEPROM, for retrieval when restarted. I guess you would have to check if the last save network is actually available etc.
Unfortunately (for you) i don't have a simple example of that for you.
Once you launch the webserver, you aren't going to want to have any delays. Those are going to trip your watchdog timer. The wifi radio can't be blocked from performing whatever function it wants to when it wants to. You have two 1000 ms delays in your loop. Those are going to cause issues I think.
I think the server is not at all accessible from within loop() this is more or less the part of the code that becomes available once STA has started. Also it seems to drop AP-mode (or at least it doesn't reconnect whatever device is connected to it) But server.handleClient() is not called from loop at all. This snippet is more as an example i think.