Saving to Arduino Yun 'www' folder

Hello,
I am a beginner when dealing with Arduino and coding so bear with me. I am attempting to use the Arduino Yun, a usb webcam, PIR sensor, and sd card to create a security cam of sorts (as seen in many other projects). I want the image captured to be uploaded onto the Yun’s server for viewing at any time. I am able to capture the image and it saves onto the sd card, but I am not able to determine how to save the image from the Yun into its own server ‘www’ folder. I have attached my arduino sketch and also pasted it to the end of this forum question. Any advice would be appreciated. Thank You

#include <Bridge.h>

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 1000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 8;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;

Process p;
String imageName;
String path= "/mnt/sd/prac";

/////////////////////////////
//SETUP
void setup(){
 Bridge.begin();
 Serial.begin(9600);
 pinMode(pirPin, INPUT);
 pinMode(ledPin, OUTPUT);
 digitalWrite(pirPin, LOW);

 //give the sensor some time to calibrate
 Serial.print("calibrating sensor ");
 for(int i = 0; i < calibrationTime; i++){
   Serial.print(".");
   delay(1000);
 }
 Serial.println(" done");
 Serial.println("SENSOR ACTIVE");
 delay(50);
}

////////////////////////////
//LOOP
void loop(){

 if(digitalRead(pirPin) == HIGH){
   digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
   if(lockLow){  
     //makes sure we wait for a transition to LOW before any further output is made:
     lockLow = false;            
     Serial.println("---");
     Serial.print("motion detected at ");
     Serial.print(millis()/1000);
     Serial.println(" sec"); 

     imageName = uniqueFileName("png"); //generate a new, uniqe file name

     p.runShellCommand("fswebcam" + path + imageName); //takes the picture
     while(p.running()); //wait till the process ends
     //p.runShellCommand("python /mnt/sda1/sendemail.py /mnt/sda1/" + imageName); //sends the picture via email
     //while(p.running()); //wait till the process ends

     delay(50);
   }         
   takeLowTime = true;
 }

 if(digitalRead(pirPin) == LOW){       
   digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

   if(takeLowTime){
     lowIn = millis();          //save the time of the transition from high to LOW
     takeLowTime = false;       //make sure this is only done at the start of a LOW phase
   }
   //if the sensor is low for more than the given pause, 
   //we assume that no more motion is going to happen
   if(!lockLow && millis() - lowIn > pause){  
     //makes sure this block of code is only executed again after 
     //a new motion sequence has been detected
     lockLow = true;                        
     Serial.print("motion ended at ");      //output
     Serial.print((millis() - pause)/1000);
     Serial.println(" sec");
     delay(50);
   }
 }
}


/* A simple function to generate unique timestamp based filenames */

String uniqueFileName(String ext){
 String filename = "";
 p.runShellCommand("date +%s");
 while(p.running()); 

 while (p.available()>0) {
   char c = p.read();
   filename += c;
 } 

 filename.trim();
 filename += "." + ext;

 return filename;
}  [code]

[/code]

prac.ino (2.97 KB)

ArduinoNewbie91:
::::SNIP::::
and also pasted it to the end of this forum question. Any advice would be appreciated. Thank You

@ArduinoNewbie91,
could you start by editing your message. You can after you have posted, there are button on the bottom right of your message. The add the
[code] markups around your code. This will make it easier for people to copy your code and the see it in their favorite editor. See the attached image.

Test what you have done by using the [preview] button.

TIA
Jesse

arduino_markup.png

@jessemonroy650 thanks for your help

ArduinoNewbie91: but I am not able to determine how to save the image from the Yun into its own server 'www' folder.

Anything saved to the /mnt/sd/arduino/www folder is accessible to the web server. For example:

  • /mnt/sd/arduino/www/index.html can be viewed by HTTP://arduino.local/sd/index.html
  • /mnt/sd/arduino/www/test/picture.jpg can be viewed by HTTP://arduino.local/sd/test/picture.jpg

if you leave off the file name in the web request, it will look for a default file of index.html, so the first example could also be accessed as HTTP://arduino.local/sd/

Try changing your initial path to:

String path= "/mnt/sd/arduino/www/prac";

Doesn't work for me. not in /mnt/sd/arduino/www. (I had to create the map) not in /mnt/sda1/arduino/www. not in /www.

how does Yun webserver realy works? and is there a way to swap this webserver for my favorite webserver:

here is the sample from : http://basic-converter.proboards.com/thread/298/bacon-on-rosetta-code

OPEN "localhost:8080" FOR SERVER AS handle RECEIVE dat$ FROM handle SEND "Hello from BACON World to Arduino YUN" TO handle CLOSE SERVER handle

Cant get easier than this..

rikky: Doesn't work for me.

What is not working? Are you unable to save a file in any of those folders? Or is the file getting there but it's not being served up?

Can you manually put a file there and access it from the web server? If not, what error is your web browser giving?

not in /mnt/sd/arduino/www. (I had to create the map)

Do you have an SD card inserted in the Yun? If so, does the SD card have an "arduino" folder at the root level of it? If so, the Yun should automatically detect the card, mount it as /mnt/sda1 or /mnt/sda2, and then create the symbolic link at /mnt/sd

Does the folder /mnt/sda1/arduino/www exist? Can you create files in it?

OPEN "localhost:8080" FOR SERVER AS handle
RECEIVE dat$ FROM handle
SEND "Hello from BACON World to Arduino YUN" TO handle
CLOSE SERVER handle

You can do something rather similar on the Yun using YunServer and YunClient. But you are then losing out on a lot of the power that the Linux based web server can offer.

I have a SD card inserted.
It is called /mnt/sda1
I can make any map or file in it that I want.
but it doesn’t serve up with (in my case) 192.168.240.1/mnt/sda1/arduino/www/anything.html
nor does it in a SD map I created.
Nor does it in /www

Even when I change the index.html page in /www, it still goes to the Welcome to the arduino yun, type in your password, page.

YunServer and Yunclient are for the arduino scetch part of the Yun.
I can work with that, sort of.

for the Linux part, I speak a sort of Bash, and Bacon.
The Bacon Server will do perfectly for for me.

I do not understand the OpenWrt Server, but I beleve this thing speaks Lua?
If it would speak Bash or Sh, that would be fine.
Sort of that the webserver points to my script, witch does:
#echo Hallo world.

I can then invent my own way in life. Without Java and Lua and other sophisticated ~~rubbish~~ goods.

But if I have to learn yet another boring language,
I 'd better spent my time learning how to cross compile a Bacon in OpenWrt.

Install bash:

opkg update
opkg install bash
nano /www/cgi-bin/test.sh
#!/bin/bash
echo "Content-type: text/html"
echo 
echo "<html><head><title>hello world</title></head><body><p>It works!</p></body>"
exit 0
chmod 755 /www/cgi-bin/test.sh

View result at “http://192.168.0.102/cgi-bin/test.sh

Yessss, with the exeption of that I have another ip nummer,,

It works !!!!!!!!

Ok, Now a new life is just beginning, Thanks.

rikky:
but it doesn’t serve up with (in my case) 192.168.240.1/mnt/sda1/arduino/www/anything.html

That’s the wrong URL. To serve up the file /mnt/sda1/arduino/www/anything.html you need to use the URL 192.168.240.1/sd/anything.html

If the first token after the address is /sd/, the web server will look in /mnt/sd/arduino/www/ for the file.

Even when I change the index.html page in /www, it still goes to the Welcome to the arduino yun, type in your password, page.

When you changed /www/index.html, did you change the refresh statement in the third line of the original file? If not, as soon as you load the page, it will jump to the configuration login web page.

YunServer and Yunclient are for the arduino scetch part of the Yun. I can work with that, sort of.

It can be done, but it’s far from ideal.

for the Linux part, I speak a sort of Bash, and Bacon.

My apologies, I didn’t realize Bacon runs on the Linux side, I assumed it was a BASIC variant that ran on an embedded microprocessor, so I was assuming you wanted something for the sketch.

I do not understand the OpenWrt Server, but I beleve this thing speaks Lua?

It can run Lua scripts, but it has many other capabilities.

If it would speak Bash or Sh, that would be fine.
Sort of that the webserver points to my script, witch does:
#echo Hallo world.

It can a wide variety of CGI scripts, and also serve up static web pages. PHP, Python, and other CGI scripts are all possible.

The web server is rather powerful for a small board like the Yun, it’s called uhttpd (don’t worry about the installation instructions on that page, it’s already installed.) A description of the configuration options are here.

There are MANY ways to generate and serve up web pages.

I did have a look at uhttpd before, but could not figure out any sense of it. If you google about it on the internet it will give you about 34.100 hits. Where to start? anyway, out of all this information, I could not even have guessed, that you could serve a webpage out of a 'bash' or any script.

And sinse I do not have to crosscompile Bacon anymore, (wich is above my capabilaties also), I will have some spare time to look into uhttpd again.

So, thanks again.

Im sorry, I have been out of town this past weekend. But now that I am back working on the project, I am able to physically put a file in the wee folder and hpload the sketch in The IDE. This file can be viewed on my Yun's server. I believe I am running into problems with my code. I feel like i have tried every variation of the path name in which to save the captured image, but no luck

ArduinoNewbie91:
Im sorry, I have been out of town this past weekend. But now that I am back working on the project, I am able to physically put a file in the wee folder and hpload the sketch in
The IDE. This file can be viewed on my Yun’s server. I believe I am running into problems with my code. I feel like i have tried every variation of the path name in which to save the captured image, but no luck

@ArduinoNewbie91,
while you were gone, your post was hijacked by another person. Please start over.

But before you do, you have a typo to fix. On line 61, you have

    p.runShellCommand("fswebcam" + path + imageName); //takes the picture

The command fswebcam needs a blank space " " between the command (fswebcam) and the path. To be clear, that line should look like this:

    p.runShellCommand("fswebcam " + path + imageName); //takes the picture

Jesse

I am sorry for the hijack, I did not realize, untill it was too late. It won't happen again.

jessemonroy650: The command fswebcam needs a blank space " " between the command (fswebcam) and the path.

Good catch! I was so focused on the path name, I didn't catch that detail. I'm sure that will make a BIG difference in the OP's code.

I sorry I contributed to detailing the thread, I hadn't realized I was suddenly answering questions from another poster.

ShapeShifter: ::::SNIP:::: I sorry I contributed to detailing the thread, I hadn't realized I was suddenly answering questions from another poster.

Yeah, I didn't realize it until the OP responded. I'm sure we'll get him back online.

Jesse