Closed MnemonicFish closed 1 year ago
modifying the bool initWifi() function by assigning the primary DNS in WIFI.config prior to calling WIFI.begin the local time is now correct using static IP address, this also fixed Telegram notifications failure as the time stamp used cannot be before current time.
Adapter settings:
Code:
`
bool init_wifi()
{
int connAttempts = 0;
// *** NEW CODE - Assign Network Adapter Settings **
// Set static IP IPAddress local_IP(192, 168, 1, 142); // Set your Gateway IP address IPAddress gateway(192, 168, 1, 1); // Set your Gateway IP address IPAddress subnet(255, 255, 0, 0); // Set your primary DNS, required to get the correct local time IPAddress primaryDNS(192, 168, 1, 1);
//**
Serial.println(" Disable brownout");
uint32_t brown_reg_temp = READ_PERI_REG(RTC_CNTL_BROWN_OUT_REG); //save WatchDog register
Serial.print("\nBrownOut Regsiter was (in hex) "); Serial.println(brown_reg_temp, HEX);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
if (strlen(ssid) == 1) {
//if (1) {
WiFiManager wm;
bool res;
//wm.resetSettings(); // for debugging
wm.setConnectTimeout(20); // how long to try to connect for before continuing
wm.setConfigPortalTimeout(30); // auto close configportal after n seconds
// res = wm.autoConnect(); // auto generated AP name from chipid
res = wm.autoConnect(devname); // use the devname defined above, with no password
//res = wm.setSTAStaticIPConfig(IPAddress(192,168,1,140), IPAddress(192,168,1,123), IPAddress(255,255,255,0));
//res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
if (res) {
Serial.println("Succesful Connection using WiFiManager");
} else {
InternetFailed = true;
Serial.println("Internet failed using WiFiManager - not starting Web services");
}
} else {
WiFi.disconnect(true, true);
WiFi.setHostname(devname);
WiFi.mode(WIFI_STA);
WiFi.printDiag(Serial);
// **** NEW CODE **
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) { Serial.println("STA Failed to configure"); }
//***
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED ) {
delay(1000);
Serial.print(".");
if (connAttempts == 20 ) {
Serial.println("Cannot connect - try again");
WiFi.begin(ssid, password);
}
if (connAttempts == 30) {
Serial.println("Cannot connect - fail");
WiFi.printDiag(Serial);
return false;
}
connAttempts++;
}
Serial.println("\nInternet connected");
}
if (!InternetFailed) {
if (!MDNS.begin(devname)) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.printf("mDNS responder started '%s'\n", devname);
}
configTime(0, 0, "pool.ntp.org");
setenv("TZ", TIMEZONE, 1); // mountain time zone from #define at top
tzset();
time_t now ;
timeinfo = { 0 };
int retry = 0;
const int retry_count = 15;
delay(1000);
time(&now);
localtime_r(&now, &timeinfo);
while (timeinfo.tm_year < (2016 - 1900) && ++retry < retry_count) {
Serial.printf("Waiting for system time to be set... (%d/%d) -- %d\n", retry, retry_count, timeinfo.tm_year);
delay(1000);
time(&now);
localtime_r(&now, &timeinfo);
}
Serial.print("Local time: "); Serial.println(ctime(&now));
sprintf(localip, "%s", WiFi.localIP().toString().c_str());
}
//typedef enum {
// WIFI_PS_NONE, /**< No power save */
// WIFI_PS_MIN_MODEM, /**< Minimum modem power saving. In this mode, station wakes up to receive beacon every DTIM period */
// WIFI_PS_MAX_MODEM, /**< Maximum modem power saving. In this mode, interval to receive beacons is determined by the listen_interval
// parameter in wifi_sta_config_t.
// Attention: Using this option may cause ping failures. Not recommended */
//} wifi_ps_type_t;
wifi_ps_type_t the_type;
esp_err_t get_ps = esp_wifi_get_ps(&the_type);
Serial.printf("The power save was: %d\n", the_type);
Serial.printf("Set power save to %d\n", WIFI_PS_NONE);
esp_err_t set_ps = esp_wifi_set_ps(WIFI_PS_NONE);
esp_err_t new_ps = esp_wifi_get_ps(&the_type);
Serial.printf("The power save is : %d\n", the_type);
Serial.println(" Enable brownout");
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, brown_reg_temp); //enable brownout detector
// v99 for esp32-1.06 https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/issues/235
client.setInsecure();
return true;
}`
I added multiple ESP32 cameras to a webpage but found that certain ESP32 Cam IP addresses will not load in browser i.e., (192.168.1.108) so I assigned each ESPcam with a static IP address using the following code: `
now all the cameras load in HTML Iframes however this has affected other functionality.
All cameras live stream simultaneously, saving of images and video to SD works and is accessible in file manager.
However Telegram notifications stopped working and time stamp is incorrect. its showing out of sync time and year is 1970.
I'm trying to get a better understanding how the libraries work together mainly Telegram and assignment of local time and why having a static IP would stop notifications, any help would be appreciated.