Save file on the sd card from html client

Hi,

I am trying now for days to store a config file from a web page to the sd card.

The REST command i use is "/arduino/saveconfig/data"
Where data is a json string.

On the arduino site i collect the data with the code where myfile is on the sd card
myfile FileSystem.open("/mnt/sd/arduino/www/project/myfile.cfg", FILE_WRITE);

while(client.available()>0) {
c=client.read();
myfile.print(c);

This works ok till my configuration size is bigger than 500 bytes.
If the size is bigger than 500 bytes then i do get a timeout.

Are there any possibilities to write to a sd card in an other way?

Regards

Sahin

for big size file please bypass FileIO at ATmega32u4 , let AR9331 take care it only.

nano /mnt/sda1/download.py
#!/usr/bin/python
import urllib2
zipfile = urllib2.urlopen("http://download.thinkbroadband.com/10MB.zip")
output = open('/mnt/sda1/10MB.zip','wb')
output.write(zipfile.read())
output.close()
chmod 755 /mnt/sda1/download.py

Profile the code:

time /mnt/sda1/download.py
real    0m 7.64s
user    0m 2.55s
sys     0m 1.49s

It take 7.64s to download 10 MB file and save it at microsd.

by use sys.argv and Process then AR9331 could be control by ATmega32u4.

void insertdb() {
  Process p;            
  p.begin("/mnt/sda1/download.py");      
  p.addParameter("http://download.thinkbroadband.com/10MB.zip"); 
  p.addParameter("/mnt/sda1/10MB.zip"); 
  p.run();

Thanks,

This is very nice. Will keep it for future use. But it is not usable for my project.

I am configuring an online led schedule and want to save this on the arduino sd card. The schedule will be less then 1 kbyte.

Today i had one sollution which took about 15 sec for 0.41 kbyte. The 15 seconds is long because i had to chunk the data before sending in 300 bytes part.

Data is my JSON data created on the fly.

Data2=JSON.stringify(Data);
Data2=Data2.match(/.{1,300}/g);
error=false;
for (index = 0; index < Data2.length; ++index){
cnt=0;
do{
$.ajax({
async: false,
url:"/arduino/saveschedule/"+index.toString()+"/"+encodeURIComponent(Data2[index]),
type:'GET',
success: function(data) {
if (data='OK'){error=false;} else {error=true;}
},
error: function(data) {error=true;}

});
++cnt;
} while ((error) && (cnt<5));
}
if (error) alert('Error saving schedular'); else {alert('Schedule saved successfuly');}
}

ARDUINO CODE:

if (command == "saveschedule") {
String command = client.readStringUntil('/');
command.trim();
int mode;
FileSystem.begin();
if (command=="0")
{mode=FILE_WRITE;}
else { mode=FILE_APPEND;}
File schedule = FileSystem.open("/mnt/sd/arduino/www/oac/oac.cfg", mode);
if (schedule)
{
char c;
while(client.available()>0) {
c=client.read();
schedule.print(c);
}
client.println("OK");
schedule.close(); // close the file
}
else client.println("ERROR");

}

Suprisingly this works.
But i really hope that there is a better way.
I also have a problem reading the file again. Same problem timeout while reading the file from the SD.

Thanks

Sahin RH

Hi,
I have a similar problem, I try to resolve in this way.... but arduino doesn't write anything...
(via ssh on terminal the logger.sh seems to be correct: it writes to file)

Function

String writeLog() {
  String result = " LOG_TEST";
  
  Process pLog;
  pLog.begin("/mnt/sda1/arduino/www/logger.sh");
  pLog.addParameter(result);
  pLog.runAsynchronously();
  
}

Shell code

#!/bin/sh

echo $(date +%Y.%m.%d_%T)$1 >> datalog.txt

UPDATE

Try also with

String writeLog() {
  Process pLog;
  pLog.runShellCommand("/mnt/sda1/arduino/www/logger.sh TEST_ARDUINO");
}

...without results.

sd.png

keebOo:
Hi,
I have a similar problem, I try to resolve in this way.... but arduino doesn't write anything...
(via ssh on terminal the logger.sh seems to be correct: it writes to file)
...

Your complete Arduino Code?

Here the code
I deleted almost everything, keeping only the code (and libraries) for write on SD.
Also this version doesn't write on SD.

could be something about the access permissions to file?
-rwxr-xr-x 1 root root 58 Feb 24 18:01 logger.sh

the code

#include <SoftwareSerial.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <Process.h>

SoftwareSerial mySerial(10, 11); // RX, TX
YunServer server;

int BTN = 12;
int LED = 13;
int ENB = 8;
int MAX_LEN = 14; //  CMD
int SYNCIN = 7;
int SYNCOUT = 6;


int logcounter = 0;

void setup() {
  Serial.begin(38400);
  mySerial.begin(38400);
  Bridge.begin();



  server.listenOnLocalhost();
  server.begin();

  pinMode(BTN, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(SYNCIN, INPUT);
  pinMode(SYNCOUT, OUTPUT);

  digitalWrite(ENB, LOW);
  digitalWrite(LED, LOW);
  digitalWrite(SYNCIN, LOW);
  digitalWrite(SYNCOUT, LOW);

  // attachInterrupt(4, changeleds, RISING);

  for (int i = 0; i < 7; i++) {
    // I'm alive
    digitalWrite(LED, HIGH);
    delay(150);
    digitalWrite(LED, LOW);
    delay(150);
  }

  delay(100);


}

void loop() {
  YunClient client = server.accept();
  if (logcounter < 3) {
    writeLog();
    logcounter ++;
  }

  delay(50);
}



String writeLog() {

  Process pLog;
  pLog.runShellCommand("/mnt/sda1/arduino/www/logger.sh TEST_ARDUINO");
  
  // DEBUG flash
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  
}
#include <Process.h>

void setup() {
  // Initialize Bridge
  Bridge.begin();
}

void loop() {
  Process p;              
  p.begin("/mnt/sda1/log.sh");      
  p.addParameter("test123"); 
  p.run();
  delay(10000);
}

Above code is working, then add your code by small group until broken.

I try to copy your code, but.... nothing happen.
(change also the position of logger.sh to root of SD)

really don't know .... now I try with a friend's YUN the same code.

k.

Hi,

Is there any suggestion for my original problem .
I can write to the sd card but after about few 100 bytes there is a timeout. I send the data in the REST GET request which one is own is not very nice approach. But i do not know any other way.

I though it would be very easy to send 1 kbytes of configuration data to the yun with HTTP request and read back again.
I also tried to increase the timeout of ajax request but this also did not help.

I suspect that the yun drops the connection after few milliseconds. Can this be the case???

Thanks

@keebOo;-

Test "/mnt/sda1/log.sh 'test123' " at console ( without double quotation). also make it has proper permission.
chmod 755 /mnt/sda1/log.sh

@hilsah

YESS! Resolved updating the firmware. :slight_smile:

Thanks.

sonnyyu:
@hilsah

[quote author=Federico Fissore link=topic=196091.msg1517378#msg1517378 date=1387789081]
I'm happy to say we have solved this. Details here

Until we release an updated package, here is what you need to do.

  • Using SSH or YunSerialTerminal, connect to the linux side of the yun.
  • Edit file /usr/bin/run-bridge, changing "python bridge.py" with "python -u bridge.py" (ie: add a "-u" to the command line)
  • Edit file /usr/bin/kill-bridge, again changing "python bridge.py" with "python -u bridge.py" (ie: add a "-u" to the command line)
  • Type kill-bridge and re-run your sketch

[/quote]