Development

Sensors / Video Camera Board:

» VC 07: PIR interrupt with picture and FTP

This example shows how use the PIR interrupt taking a picture and uploading it when the interrupt occurs.

Required Materials

1 x Waspmote
1 x battery
1 x Video Camera Sensor Board
1 x 3G + GPS module
1 x GPRS/3G antenna
1 x SIM card
1 x SD card in 3G + GPS board

Notes

Waspmote must be powered with a battery
Video Camera Sensor Board needs 3G + GPS Board to work
FTP actions need some time to complete it
If the microSD card is not present on the 3G module, the code won't work.

Code

/*  
 *  ------ VC_07 - PIR interrupt with picture and FTP -------- 
 *  
 *  Explanation: This example shows how use the PIR interrupt taking a 
 *  picture and uploading it when the interrupt occurs. To use this example,
 *  a 3G + GPS module is required.
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego 
 */


#include "Wasp3G.h"

int count = 0;
int answer = 0;
unsigned long time;

char picture_name[30];

void setup(){

    USB.ON();
    USB.println(F("Starting"));

    answer = _3G.ON();
    if ((answer == 1) || (answer == -3))
    {
        USB.println(F("3G module ready..."));

        _3G.setTime();

        _3G.setMode(_3G_RF_OFF);
        USB.println(F("RF circuits disabled"));

        // if there isn't a name the 3G module use the date and time for the picture's name
        _3G.pictureName("");

        USB.println(F("Waiting for SD..."));

        time = millis();
        while ((answer == 0) && ((millis() - time) < 60000))
        {
            answer = _3G.isSD();
            // Condition to avoid an overflow (DO NOT REMOVE)
            if (millis() < time)
            {
                time = millis();	
            }
        }

        if (answer == 1)
        {
            // Selects microSD to store the pictures
            _3G.selectStorage(1);
        }
        else
        {
            // Selects microSD to store the pictures
            _3G.selectStorage(0);   
            USB.println("SD card not available. Check that the SD card is inserted and reboot the Waspmote"); 
        }
    }
    else
    {
        USB.println(F("3G module NOT ready..."));
    }
}


void loop(){ 
    USB.print(F("Enabling PIR interrupt..."));
    _3G.enablePIRInterrupt();
    USB.println(F("ready"));

    // waits the interrupt
    do{
        PWR.sleep(0);  
    }while ( intFlag & PIR_3G_INT == 0);
    USB.println(F("PIR interrupt detected"));

    // disables the interrupt and start communications with the 3G module
    _3G.disablePIRInterrupt(1);

    USB.println(F("Changes power mode to full"));
    _3G.setMode(_3G_FULL);

    // starts the camera and configures it
    count = 3;
    do{
        answer = _3G.startCamera();
        count--;
    }
    while ((answer != 1) && (count > 0));


    if (answer == 1)
    {
        _3G.autoLevel();
        _3G.cameraResolution(5);
        _3G.cameraBrightness(3);

        count = 3;
        do{
            // power on the LED light 
            _3G.autoLight();
            answer = _3G.takePicture();

            // power off the LEDs
            _3G.powerIRLED(0);
            count--;
        }while ((answer != 1) && (count > 0));
        // saves the name of the picture
        strcpy(picture_name, _3G.buffer_3G);

        // stops the camera
        _3G.stopCamera();

        if (answer == 1){
            USB.println(F("Picture captured!"));    
            USB.print(F("Connecting to the network..."));

            answer = _3G.check(60);
            if (answer == 1){
                USB.println(F("connected"));    

                // configures 3G onnection for FTP
                answer = _3G.configureFTP("ftp_server", "ftp_port", "username", "password", 1, "I");
                if (answer == 1)
                {

                    USB.print(F("Uploading the picture..."));
                    USB.println(picture_name);

                    // uploads file from SD card to the FTP server:
                    answer = _3G.uploadFile(4, picture_name);
                    if (answer == 1)
                    {
                        USB.println(F("Upload done"));
                    }
                    else if(answer == -2)
                    {
                        USB.print(F("Upload failed. Error code: "));
                        USB.println(answer, DEC);
                        USB.print(F("CME error code: "));
                        USB.println(_3G.CME_CMS_code, DEC);
                    }
                    else 
                    {
                        USB.print(F("Upload failed. Error code: "));
                        USB.println(answer, DEC);
                    }
                }
                else
                {
                    USB.print(F("Configuration failed. Error code:"));
                    USB.println(answer, DEC);
                }
            }
            else
            {
                USB.println(F("Error connecting to the network"));
            }
        }
        else
        {
            USB.print(F("Error taking the picture. Error code:"));
            USB.println(answer, DEC);
        }
    }
    else
    {
        USB.print("Error starting the camera. Error code: ");
        USB.println(answer, DEC);
    }

    _3G.setMode(_3G_RF_OFF);    
    USB.println(F("RF circuits disabled"));
}


Output

D#
Starting
3G module ready...
RF circuits disabled
Enabling PIR interrupt...ready
PIR interrupt detected
Changes power mode to full
Setting PIN code...
PIN code accepted
Picture captured!
Connecting to the network...connected
Uploading the picture...20121112_160754.jpg
Upload done
RF circuits disabled
Enabling PIR interrupt...ready
PIR interrupt detected
Changes power mode to full
Setting PIN code...
PIN code accepted
Picture captured!
Connecting to the network...connected
Uploading the picture...20121112_160841.jpg
Upload done
RF circuits disabled
Enabling PIR interrupt...ready

Quick Publish: