jameszah / ESP32-CAM-Video-Recorder

Video Recorder for ESP32-CAM with http server for config and ftp (or http) server to download video
GNU General Public License v3.0
443 stars 101 forks source link

Multiple cameras, static IP and Telegram Notifications #78

Closed MnemonicFish closed 1 year ago

MnemonicFish commented 1 year ago

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: `

  IPAddress local_IP(192, 168, 1, 143);  

  // Set your Gateway IP address
  IPAddress gateway(192, 168, 1, 123);
  IPAddress subnet(255, 255, 255, 0);

     if (!WiFi.config(local_IP, gateway, subnet)) {
    Serial.println("STA Failed to configure");`

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.


----------------------------------------------------------------------
Serial  Monitor  
----------------------------------------------------------------------
`---
setup, core 1, priority = 1

-------------------------------------
ESP-CAM Video Recorder v.01
 http://Living Room camera 01.local - to access the camera
`-------------------------------------
Get took 2
Good settings in the EPROM 
Internet_Enabled 1
DeepSleepPir 0
record_on_reboot 1
PIRpin 13
PIRenabled 1
framesize 8
repeat_config 300
xspeed 15
gray 0
quality 10
capture_interval 500
total_frames_config 3600
xlength 1800
EnableBOT 1
Starting wifi ...
 Disable brownout

BrownOut Regsiter was (in hex) 43FFC000
.
Internet connected
mDNS responder started 'Living Room camera 01'
Waiting for system time to be set... (1/15) -- 70
Waiting for system time to be set... (2/15) -- 70
Waiting for system time to be set... (3/15) -- 70
Waiting for system time to be set... (4/15) -- 70
Waiting for system time to be set... (5/15) -- 70
Waiting for system time to be set... (6/15) -- 70
Waiting for system time to be set... (7/15) -- 70
Waiting for system time to be set... (8/15) -- 70
Waiting for system time to be set... (9/15) -- 70
Waiting for system time to be set... (10/15) -- 70
Waiting for system time to be set... (11/15) -- 70
Waiting for system time to be set... (12/15) -- 70
Waiting for system time to be set... (13/15) -- 70
Waiting for system time to be set... (14/15) -- 70
Local time: Thu Jan  1 00:00:16 1970

The power save was: 1
Set power save to 0
The power save is : 0
 Enable brownout
Starting sd card ...
SD card mount successfully!
Name: SD
Type: SDHC/SDXC
Speed: 40 MHz
Size: 7619MB
SD_MMC Begin: E (16818) vfs_fat_sdmmc: host init failed (0x103).
1
Starting server ...
http task prio: 5
Camera http started
Starting ftp ...
Starting the esp32 sd file manager
Total space: 7611MB
Used space: 6MB
Starting tasks ...
camera, core 1, priority = 2
aviwriter, core 1, priority = 3
ftp, core 1, priority = 4
Starting camera ...
Camera Ready! Use 'http://192.168.1.142' to connect
zcam / avi / ftp / ftp2 / loop 
0 / 0 / 4 / 0 / 0  
Internal Total heap 286596, internal Free Heap 149476
SPIRam Total heap   4191979, SPIRam Free Heap   1464355
ChipRevision 1, Cpu Freq 240, SDK Version v4.4.2
Avi Writer / Camera / Ftp 
2404 / 316 / 3324
----
Total space: 7611MB
Used space: 6MB
Nothing deleted, 0.1% disk full
the loop, core 1, priority = 1
Taking a picture for file ...
File saved: /sdcard/19700101/Living Room camera 01_1970-01-01_00.00.18_vga_Q10_I500_L1800_S15.jpg
Taking a picture for telegram...
Sending Photo Telegram, bytes: 23891

Send_photo heap before: 149212
[BOT Client]Connecting to server
[BOT Client]Conection error
zzzz[BOT Client]Another Conection error
zz
Send_photo heap after : 149112

Photo telegram failed ><
z 
Total space: 7611MB
Used space: 6MB
Nothing deleted, 0.1% disk full
Starting an avi 

File name will be >/sdcard/19700101/Living Room camera 01_1970-01-01_00.00.25_vga_Q10_I500_L1800_S15.avi<

Recording 3600 video frames ...
MnemonicFish commented 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: image

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;
}`