Hello everyone, I have been banging my head against the wall all day trying to figure this out and I'm at a lost. I have an ESP32 wired to a MAX7219 dot matrix with the following code. It's just a clock, I've made one of these by hard coding my wifi creds and it works with no problem. Now I'm trying to make another using wifi manager. The following code works perfectly after compiling but when I unplug the ESP32 from my computer and plug it back into the same port, or into a wall outlet, nothing gets displayed on the dot matrix. I can see that it is going out and receiving the time via the serial monitor. Anyone have any ideas? Any help would be much appreciated!!
#include <WiFi.h>
#include "time.h"
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 19
int timeout = 120;
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_H
#define MAX_DEVICES 4
#define CLK_PIN 18
#define DATA_PIN 23
#define CS_PIN 5
MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -18000;
String minute ="";
int thr;
String hr = "";
const int daylightOffset_sec = 3600;
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Display.setTextAlignment(PA_CENTER);
if(timeinfo.tm_min<10) minute = "0";
else minute = "";
if(timeinfo.tm_hour>12) {
thr = timeinfo.tm_hour-12;
hr = String(thr);
}
else hr = String(timeinfo.tm_hour);
Display.print(hr + ":" + minute + String(timeinfo.tm_min));
}
WiFiManager wm;
bool res;
void setup()
{
Serial.begin(115200);
Display.begin();
Display.setIntensity(0);
//Display.displayClear();
pinMode(TRIGGER_PIN,INPUT_PULLUP);
res = wm.autoConnect("Cams clock","password");
Display.print("trying to auto connect");
if(res){
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Display.print("good wifi");
delay(4000);
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
}
void getTime(){
Serial.println("got time");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
bool btn = false;
bool wifiConnected = true;
void loop()
{
Serial.println(digitalRead(TRIGGER_PIN));
if ( digitalRead(TRIGGER_PIN) == LOW && !btn) {
Serial.println("mm");
btn=true;
}
else if(btn) {
btn=false;
//reset settings - for testing
//wm.resetSettings();
// set configportal timeout
wm.setConfigPortalTimeout(timeout);
if (!wm.startConfigPortal("Cams clock")) {
wifiConnected = true;
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
if(wifiConnected) {
wifiConnected=false;
getTime();
}
delay(4000);
printLocalTime();
}