I want to send a file from an Arduino to the server or another Arduino.
I wrote a code that's open the file then write the content of the text on an array and then send it.
That would work if the data is small, but when i try to send, .wav it will takes lots of time if i do it that why (storing each byte in array). I want to send the whole file at on time. Is there a way or a library helps ?
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>
const int push1 = 2;
File myFile;
char a[400];
int c= 0;
// network parameters
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x02 }; // ethernet interface MAC address
IPAddress localIp(10,131,41,72); // local ip address
IPAddress destIp(10,131,41,74); // destination ip address
unsigned int port = 9631; // destination port
// EthernetUDP to send and receive messages.
EthernetUDP Udp;
// timing
// setup the arduino and shields
void setup() {
pinMode(push1, INPUT);
//Initialize serial and wait for port to open:
Serial.begin(9600); // 19200
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// start ethernet and udp
Ethernet.begin(mac, localIp); // static ip version
//if(Ethernet.begin(mac) == 0) { // dhcp version
// report failure to obtain network parameters
// Serial.println("DHCP error");
// no point in carrying on, loop indefinitely:
// while(true)
// ;
//}
// open UDP port
Udp.begin(port);
// show the local ip address (useful for dhcp)
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
// initialize previous millis variables
}
// do tasks
void loop() {
if(digitalRead(push1) == LOW){
myFile = SD.open("R.wav" , FILE_READ);
if (myFile) {
// read from the file until there's nothing else in it:
Serial.println("i will send now");
while (myFile.available()) {
a[i] = myFile.read();
i++;
}
Udp.beginPacket(destIp, port);
Udp.write(a);
Udp.endPacket();
Serial.println("Sending UDP message");
Serial.println();
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
} } }