I am currently working on a project in which I would like to send text files from the sd card to another sd card
using xbees. My current setup has 2 arduini unos, 2 xbees, 2 microSDcard shields and 2 sd cards.
I have soldered all three of them together and have the xbees communicating with each other. I am new to this
and cannot figure out how to send files from the sd card to the other arduino using xbees. Any help would be really appreciated.
If the XBees are communicating with each other, then all you are looking at doing is having one Arduino read an SD card, and send that data, and having the other Arduino receive some data, and write it to an SD card.
The SD library provided with the Arduino IDE has an example that writes data to an SD card, and an example that reads data from an SD card. These two examples provided the heart of your project.
Yes I understand that. The problem is that I am unable or dont know how to read the text file from the sd card into an array.
I can send the array wirelessly. But I dont know how to read the file into an array.
I did look at the examples! I can read a file.
what I don't get is how to store the value that I just read and send.it to the other arduino. sending should be easy but I dont know how to store.
/*
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
int var;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("hell.txt");
Serial.println("hello.txt:");
// read from the file until there's nothing else in it:
while (myFile.available())
{
var = Serial.write(myFile.read());
Serial.print(var);
}
// close the file:
myFile.close();
}
void loop()
{
// nothing happens after setup
}
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
int incomingByte;
int serial_available;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("hello.txt", FILE_WRITE);
if ((serial_available = Serial.available()) > 0)
{
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
myFile.println(incomingByte);
}
myFile.close();
}
void loop()
{
// nothing happens after setup
}
Thats the receiver. I just tried that out. It compiles and creates a file hello.txt on the other arduino. But none of the data was transferred.
Do i have to have serial monitor on for both of them? or am i doing something wrong?
while (myFile.available())
{
var = Serial.write(myFile.read());
Serial.print(var);
}
What do you suppose Serial.write() returns? Why are you then sending that value?
Try:
while (myFile.available())
{
char aChar = myFile.read();
Serial.print(aChar);
}
On the receiver:
if ((serial_available = Serial.available()) > 0)
{
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
myFile.println(incomingByte);
}
If there is a byte to read (which there may not be), read that one byte, and store it on the SD card. Then close the file and stop.
I don’t think that that is even close to what you want to do.
You need to have the file receiver tell the file sender that it is read to receive. The sender should not start sending until it knows that the receiver is read. (The sender should keep saying ready until it gets a response.
Oh ic. Thank you so much for replying. Umm my ultimate goal is to create a network of zigbees.
But currently I am just trying to get the basics down. Do you know any links or books that I could
read to help me get starting with file,sdcard programmin?
I really appreciate your help! I will try that and let you know if it works
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
int incomingByte;
int serial_available;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("hello.txt", FILE_WRITE);
Serial.println(incomingByte);
}
void loop()
{
// nothing happens after setup
if ((serial_available = Serial.available()) > 0)
{
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
myFile.write(incomingByte);
}
}
sender:
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
int var;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("hello.txt");
Serial.println("hello.txt");
// read from the file until there's nothing else in it:
while (myFile.available())
{
var = Serial.write(myFile.read());
Serial.print(var);
}
// close the file:
myFile.close();
}
void loop()
{
// nothing happens after setup
}
So I edited the code as you suggested, but it does not seem to be working. While I was playing around with the code, I got it to write 255 in the outputfile even though I had ‘1’ as the only thing in the input file. I am quite confused as to why this is not working. Can anyone help?
As always any help is highly appreciated
var = Serial.write(myFile.read());
Serial.print(var);
We are not going to get anywhere until you explain what the heck you are doing here. You read a character from the file, and then use the Serial.write() function to send that data to the serial port, causing the XBee to broadcast it. The Serial.write() function then returns a value - always 1. Then, you send that value to the serial port, causing the XBee to broadcast it. WHY???
The sender sends the data as soon as possible after it resets. Is the receiver already running?
There is nothing on the receiver that closes the file. That's a really good way to corrupt the file. I'm not surprised that you are not seeing anything in the file.
On the receiver, you don't need serial_available. Get rid of it. You are checking that there is serial data to read, so the variable to hold the return value form Serial.read() should be char or byte, NOT int.
You are sending characters. You should be using Serial.print() to do that.
oooooh I understand now. I had a quick question. In my receiver, I open the file and then receive in loop. If I close the file before the loop
wouldn't it fail to write anything into the file? thats the last thing I am confused about .
ps I made all the changes you asked me to make. It still dint transfer anything. So I tried moving the fileopen,receive and fileClose in the loop(), that did
work and transferred the data. But it also transferred "File initializing..." and other garbage characters.
I am guessing thats prolly because I have the open and close in the loop().
is there a way to just cout or printf in the serial monitor and not write it in the file??
On the receiver, forget about writing to the SD card, for now. Simply open the Serial Monitor for the port that the receiver Arduino is attached to. Verify, using Serial.print() that the Arduino/XBee received what the other one sent.
What you eventually need to do is send a message with the name of the file to open. The receiver needs to receive that message, open the appropriate file on the SD card, and wait for data to write to the file. The sender should send each record within some delimiters that are not used in the file, like < and >. When the receiver receives one of the characters, it stores the subsequent serial data in an array, until it receives the other one. Then, it writes the array to the file. You would send some other start and end markers around a command to the Arduino to close the file.
So, you would send something like this:
[Open HelloThere.txt]
<Save this text, too.>
[Close]
to create a file called HelloThere.txt, containing two records, and closed properly.
Oh I see. Thats a great idea. PS: I had one small question. with the current code, I am getting a lot of random ^^^^^^^ along with the data transferred from the file. As in if the send file = “123456789”, the received file is “^^^^^^^^^^^^^^^123456789^^^^^^^^^^^^^^^”.
Do you by any chance know why would that happen?
I will post the code once again.
Sender
/*This example code is in the public domain.
*/
#include <SD.h>
File myFile;
int var;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("hello.txt");
// read from the file until there's nothing else in it:
while (myFile.available())
{
char aChar = myFile.read();
Serial.print(aChar);
}
// close the file:
myFile.close();
}
void loop()
{
// nothing happens after setup
}
Receiver:
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
char incomingByte;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
}
void loop()
{
// nothing happens after setup
myFile = SD.open("hello.txt", FILE_WRITE);
incomingByte = Serial.read();
myFile.write(incomingByte);
myFile.close();
// read the oldest byte in the serial buffer:
}