Advertisement
honey_the_codewitch

arduino download to SPIFFS

Apr 4th, 2025 (edited)
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "WiFi.h"
  3. #include "SPIFFS.h"
  4. #include "HTTPClient.h"
  5.  
  6. HTTPClient http;
  7. uint8_t buffer[8192];
  8. constexpr static const char* url = "http://192.168.54.12/test.bin";
  9. void setup() {
  10.     Serial.begin(115200);
  11.     char ssid[65];
  12.     ssid[0] = 0;
  13.     char pass[129];
  14.     pass[0] = 0;
  15.     SPIFFS.begin();
  16.     // check SPIFFS for /wifi.txt.
  17.     // first line is ssid, second line is password
  18.     bool loaded = false;
  19.     puts("Looking for wifi.txt creds on internal flash");
  20.     if (SPIFFS.exists("/wifi.txt")) {
  21.         File file = SPIFFS.open("/wifi.txt", "r");
  22.         // parse the file
  23.         String str = file.readStringUntil('\n');
  24.         if (str.endsWith("\r")) {
  25.             str = str.substring(0, str.length() - 1);
  26.         }
  27.         strncpy(ssid, str.c_str(), sizeof(ssid));
  28.         str = file.readStringUntil('\n');
  29.         file.close();
  30.         if (str.endsWith("\r")) {
  31.             str = str.substring(0, str.length() - 1);
  32.         }
  33.         strncpy(pass, str.c_str(), sizeof(pass));
  34.         loaded = true;
  35.     }
  36.     WiFi.mode(WIFI_STA);
  37.     WiFi.disconnect();
  38.     // if we found wifi.txt, start the connection process:
  39.     if (loaded) {
  40.         printf("Read wifi.txt. Connecting to %s\n", ssid);
  41.         WiFi.begin(ssid, pass);
  42.     } else {
  43.         puts("wifi.txt not found - using remembered creds");
  44.         WiFi.begin();
  45.     }
  46.     // there are more steps to using https, not covered here
  47.     if(http.begin(url)) {
  48.         File dst = SPIFFS.open("/test.bin","wb",true);
  49.         NetworkClient src = http.getStream();
  50.         while(1) {
  51.             size_t bytes_read = src.readBytes(buffer,sizeof(buffer));
  52.             if(bytes_read==0) {
  53.                 break;
  54.             }
  55.             dst.write(buffer,bytes_read);
  56.         }
  57.         dst.close();
  58.     }
  59.     http.end();
  60.    
  61. }
  62. void loop() {
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement