ASimon
Published © GPL3+

Email 3G/GPRS Camera

Photographing an object (VC0706 camera), saving a photo from a camera on a micro SD card, sending a photo using 3G/GPRS shield to an email.

IntermediateShowcase (no instructions)18,414
Email 3G/GPRS Camera

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Adafruit MicroSD card breakout board+
×1
Commercial MLC microSD
Delkin Commercial MLC microSD
×1
Adafruit TTL Serial JPEG Camera with NTSC Video
Alternative option: https://www.itead.cc/vc0706-uart-camera-supports-jpeg.html
×1
3G/GPRS/GSM Shield for Arduino with GPS - European version SIM5320E
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Wires, etc.

Story

Read more

Schematics

VC0706 scheme

VC0706 scheme

Code

EmailCamera.ino

Arduino
Make photo (VC0706). Write photo to microSD card. Send photo to Email.
#include <VC0706_UART.h>
#include <SPI.h>
#include <SD.h>
#include <XModem.h>
// comment out this line if using Arduino V23 or earlier
#include <SoftwareSerial.h>         
#define chipSelect 4
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
XModem xmodem(&Serial, ModeXModem);
VC0706 cam = VC0706(&cameraconnection);
const char smtp_server[ ]    = "*****";   // SMTP server
const char smtp_user_name[ ] = "*****";   // SMTP user name
const char smtp_password[ ]  = "*****";   // SMTP password
const char smtp_port[ ]      = "*****";   // SMTP server port
//Write here you SIM card data
const char apn[]             = "*****";
const char user_name[]       = "*****";
const char password[]        = "*****";
//Write here your information about sender, direcctions and names
const char sender_address[ ] = "*****";   // Sender address
const char sender_name[ ]    = "*****";   // Sender name
const char to_address[ ]     = "*****";   // Recipient address
const char to_name[ ]        = "*****";   // Recipient name
char subject[ ] = "My First Email";
const char body[ ] = "Picture:";
#define chipSelect  4 // enable uSD card
int8_t answer;
int    onModulePin = 8;
char   aux_str[64];
char   filename[14];
void  setup(void)
{
   pinMode(onModulePin, OUTPUT);
   Serial.begin(115200);
   delay(5000);
   power_on();
   while ((sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
       sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
   // see if the card is present and can be initialized:
   if (!SD.begin(chipSelect)) {
       // don't do anything more:
       return;
   }  
   if (true == cameraInit()) {
       snapShot();
       sendEmail();
   } else {
   }
}
void  loop(void)
{
}
bool  cameraInit(void)
{
   cam.begin(BaudRate_38400);
   char *reply = cam.getVersion();
   if (reply == 0) {
       return false;
   } else {
       return true;
   }
}
void  snapShot(void)
{
   if (!cam.takePicture()) { 
   } else { 
   }
   // Create an image with the name IMAGExx.JPG
   strcpy(filename, "IMAGE000.JPG");
   for (int i = 0; i < 1000; i++) {
       filename[5] = '0' + i/100;
       filename[6] = '0' + (i/10)%10;
       filename[7] = '0' + i%10;
       // create if does not exist, do not open existing, write, sync after write
       if (! SD.exists(filename)) {
           break;
       }
   }
   // Open the file for writing
   File imgFile = SD.open(filename, FILE_WRITE);
   uint16_t jpglen = cam.getFrameLength();
   int32_t time = millis();
   cam.getPicture(jpglen);
   uint8_t *buffer;
   while(jpglen != 0) {
        uint8_t bytesToRead = min(32, jpglen);
        buffer = cam.readPicture(bytesToRead);     
        imgFile.write(buffer, bytesToRead);
        //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
        jpglen -= bytesToRead;   
   } 
   imgFile.close();
   time = millis() - time;
   cam.resumeVideo();    
}
void  sendEmail(void)
{
   sprintf(aux_str, "AT+CRXFILE=\"%s\"", filename);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   File dataFile = SD.open(filename);
   // if the file is available, write to it:
   if (dataFile) {
       xmodem.sendFile(dataFile, filename);
   }
   // if the file isn't open, pop up an error:
   else {
       return;
   } 
   dataFile.close();
   // sets the SMTP server and port
   sprintf(aux_str, "AT+SMTPSRV=\"%s\",%s", smtp_server, smtp_port);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // sets user name and password
   sprintf(aux_str, "AT+SMTPAUTH=1,\"%s\",\"%s\"", smtp_user_name, smtp_password);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // sets sender adress and name
   sprintf(aux_str, "AT+SMTPFROM=\"%s\",\"%s\"", sender_address, sender_name);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // sets sender adress and name
   sprintf(aux_str, "AT+SMTPRCPT=1,0,\"%s\",\"%s\"", to_address, to_name);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // subjet of the email
   sprintf(aux_str, "AT+SMTPSUB=\"%s\"", filename);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // body of the email
   sprintf(aux_str, "AT+SMTPBODY=\"%s %s\" ", body, filename);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   sprintf(aux_str, "AT+SMTPFILE=1,\"%s\"", filename);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   // sets APN, user name and password
   sprintf(aux_str, "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   sprintf(aux_str, "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
   if (!sendATcommand(aux_str, "OK", 2000)) {
   }
   delay(2000);
   // sends the email and waits the answer of the module
   answer = sendATcommand("AT+SMTPSEND", "+SMTP: SUCCESS", 60000);
   if (answer == 1) {
   } else {
   }
}
void  power_on(void)
{
   uint8_t answer=0;
   // checks if the module is started
   answer = sendATcommand("AT", "OK", 2000);
   if (answer == 0) {
       // power on pulse
       digitalWrite(onModulePin,HIGH);
       delay(3000);
       digitalWrite(onModulePin,LOW);
       // waits for an answer from the module
       while(answer == 0) {     // Send AT every two seconds and wait for the answer
           answer = sendATcommand("AT", "OK", 10000);    
       }
   }
}
int8_t  sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout)
{
   uint8_t x=0,  answer=0;
   char response[100];
   unsigned long previous;
   memset(response, '\0', 100);    // Initialize the string
   delay(100);
   while( Serial.available() > 0) Serial.read();  // Clean the input buffer
   Serial.println(ATcommand);    // Send the AT command
   x = 0;
   previous = millis();
   // this loop waits for the answer
   do {
       // if there are data in the UART input buffer, reads it and checks for the answer
       if (Serial.available() != 0) {
           response[x] = Serial.read();
           x++;
           // check if the desired answer is in the response of the module
           if (strstr(response, expected_answer1) != NULL) {
               answer = 1;
           }
       }
       // Waits for the answer with time out
   }
   while((answer == 0) && ((millis() - previous) < timeout));
   return answer;
}

Credits

ASimon

ASimon

0 projects • 5 followers

Comments