Hi All,
I am using Arduino MKR1400 board with 2 GB SD card. I need to perform OTA update on the board, so far I have used <SDU.h> library provided by arduino that updates the code by uploading .Bin file on SD card when rebooted.
Now, I have a http server containing the .bin file ready to download. I just want your guidance in how to download the file from http server and save it directly to SD card.
My attempts to download the file from http server was a success, but I am stuck in saving this file to SD card. I am attaching the code, please review it and let me know the changes......Waiting for your earliest responses....
I am using Arduino MKR1400 board with 2 GB SD card. I need to perform OTA update on the board, so far I have used <SDU.h> library provided by arduino that updates the code by uploading .Bin file on SD card when rebooted.
Now, I have a http server containing the .bin file ready to download. I just want your guidance in how to download the file from http server and save it directly to SD card.
My attempts to download the file from http server was a success, but I am stuck in saving this file to SD card. I am attaching the code, please review it and let me know the changes......Waiting for your earliest responses....
C:
Arduino Code:
#include <MKRGSM.h>
#include <SD.h>
#include <SPI.h>
#include <ArduinoHttpClient.h>
const char PINNUMBER[] = "";
// APN data
const char GPRS_APN[] = "";
const char GPRS_LOGIN[] = "";
const char GPRS_PASSWORD[] = "";
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
File myFile;
// URL, path and port (for example: example.org)
char server[] = "103.9.13.150";
int port = 80; // port 80 is the default for HTTP
HttpClient http(client, server, port);
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(F("Initializing SD card..."));
if (!SD.begin(4)) {
Serial.println(F(" initialization failed!"));
return;
}
Serial.println(F(" initialization done."));
Serial.println("Starting Arduino web client.");
// connection state
bool connected = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
http.beginRequest();
Serial.println("connected");
// Make a HTTP request:
// Check to see if the file exists
if (SD.exists("UPDATE.bin")) {
Serial.println("UPDATE.bin exists.");
Serial.println("Removing UPDATE.bin...");
SD.remove("UPDATE.bin");
} else {
Serial.println("UPDATE.bin doesn't exist.");
}
Serial.println("Creating UPDATE.bin...");
myFile = SD.open("UPDATE.bin", FILE_WRITE);
if (myFile)
{
Serial.print("File Created...");
http.get("/UPDATE.bin");
http.sendHeader("Content-Type", "application/octet-stream");
http.sendHeader("Content-Disposition", "attachment; filename=UPDATE.bin");
http.sendHeader("Connection", "close");
http.endRequest();
// close the file:
myFile.close();
Serial.println("");
Serial.println("done.");
}// if you get a connection, report back via serial:
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
}