Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include "WiFi.h"
- #include "SPIFFS.h"
- #include "HTTPClient.h"
- HTTPClient http;
- uint8_t buffer[8192];
- constexpr static const char* url = "http://192.168.54.12/test.bin";
- void setup() {
- Serial.begin(115200);
- char ssid[65];
- ssid[0] = 0;
- char pass[129];
- pass[0] = 0;
- SPIFFS.begin();
- // check SPIFFS for /wifi.txt.
- // first line is ssid, second line is password
- bool loaded = false;
- puts("Looking for wifi.txt creds on internal flash");
- if (SPIFFS.exists("/wifi.txt")) {
- File file = SPIFFS.open("/wifi.txt", "r");
- // parse the file
- String str = file.readStringUntil('\n');
- if (str.endsWith("\r")) {
- str = str.substring(0, str.length() - 1);
- }
- strncpy(ssid, str.c_str(), sizeof(ssid));
- str = file.readStringUntil('\n');
- file.close();
- if (str.endsWith("\r")) {
- str = str.substring(0, str.length() - 1);
- }
- strncpy(pass, str.c_str(), sizeof(pass));
- loaded = true;
- }
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- // if we found wifi.txt, start the connection process:
- if (loaded) {
- printf("Read wifi.txt. Connecting to %s\n", ssid);
- WiFi.begin(ssid, pass);
- } else {
- puts("wifi.txt not found - using remembered creds");
- WiFi.begin();
- }
- // there are more steps to using https, not covered here
- if(http.begin(url)) {
- File dst = SPIFFS.open("/test.bin","wb",true);
- NetworkClient src = http.getStream();
- while(1) {
- size_t bytes_read = src.readBytes(buffer,sizeof(buffer));
- if(bytes_read==0) {
- break;
- }
- dst.write(buffer,bytes_read);
- }
- dst.close();
- }
- http.end();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement