witnessmenow / Universal-Arduino-Telegram-Bot

Use Telegram on your Arduino (ESP8266 or Wifi-101 boards)
MIT License
1.12k stars 307 forks source link

How to send photo from local ip (ip cam) #241

Open Roman-Andr opened 3 years ago

Roman-Andr commented 3 years ago

I want to send photo from my IP camera, but when I trying to send it gives me error. (Example up: 192.168.1.90/shot.jpg)

devinasander commented 3 years ago

Save file with this mehtod to SPIFFS. Then you can attach the image file to the Telegram message

usage: downloadAndSaveFile("/image.jpg","http://192.168.1.40/jpg/image.jpg");


void downloadAndSaveFile(String fileName, String url){

HTTPClient http;

Serial.println("[HTTP] begin...\n"); Serial.println(fileName); Serial.println(url); http.begin(url); Serial.printf("[HTTP] GET...\n", url.c_str()); // start connection and send HTTP header int httpCode = http.GET(); if(httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); Serial.printf("[FILE] open file for writing %d\n", fileName.c_str());

  File file = SPIFFS.open(fileName, "a+");

  // file found at server
  if(httpCode == HTTP_CODE_OK) {

      // get lenght of document (is -1 when Server sends no Content-Length header)
      int len = http.getSize();
      // create buffer for read
      uint8_t buff[128] = { 0 };

      // get tcp stream
      WiFiClient * stream = http.getStreamPtr();

      // read all data from server
      while(http.connected() && (len > 0 || len == -1)) {
          // get available data size
          size_t size = stream->available();
          if(size) {
              // read up to 128 byte
              int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
              // write it to Serial
              //Serial.write(buff, c);
              file.write(buff, c);
              if(len > 0) {
                  len -= c;
              }
          }
          delay(1);
      }

      Serial.println();
      Serial.println("[HTTP] connection closed or file end.\n");
      Serial.println("[FILE] closing file\n");
      file.close();

  }

} http.end();

}