camera with gsm

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
DigitalMAS
 
Posts: 13
Joined: Sun Mar 01, 2015 4:49 pm

camera with gsm

Post by DigitalMAS »

i make this code
but it doesn't work
it is get hang or restart

Code: Select all

#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#include <LiquidCrystal.h>


#define chipSelect 10
#if ARDUINO >= 100
SoftwareSerial cameraconnection = SoftwareSerial(8, 9);
#else
NewSoftSerial cameraconnection = NewSoftSerial(8, 9);
#endif
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

LiquidCrystal lcd(15, 14, 7, 6, 5, 4);


#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 1

uint16_t x ;
char m[]="0123456789";
char replybuffer[255];
int k=1;
int c=15;


String l="I am avilable in office";
SoftwareSerial fonaSSone = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

// initialize the library with the numbers of the interface pins


int counter=40;
void setup() {
 
  
  
  #if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
  if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

  Serial.begin(9600);
  Serial.println(F("VC0706 Camera test"));

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    return;
  }  

   //initialize camera
  cameraconnection.listen();
  // Try to locate the camera
  if (cam.begin()) {
    Serial.println(F("Camera Found:"));
  } 
  else {
    Serial.println(F("No camera found?"));
    return;
  }

  // Print out the camera version information (optional)
  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print(F("Failed to get version"));
  } 
  else {
    Serial.println(F("-----------------"));
    Serial.print(reply);
    Serial.println(F("-----------------"));
  }

  cam.setImageSize(VC0706_320x240);        // medium
  uint8_t imgsize = cam.getImageSize();
  Serial.print(F("Image size: "));
  if (imgsize == VC0706_320x240) 
    Serial.println("320x240");
  cam.setMotionDetect(true);           // turn it on
  Serial.print(F("Motion detection is "));
  if (cam.getMotionDetect()) 
    Serial.println(F("ON"));
  else 
    Serial.println(F("OFF"));

 //initialize FONA
  fonaSSone.listen();  
  fonaSSone.begin(4800);
  if (! fona.begin(fonaSSone)) 
  { 
    while (1);
  }

Serial.println(F("FONA found"));

  cameraconnection.listen();
  
  Serial.print(F("Free RAM ")); Serial.println(freeRam());

 
  da();
   lcd.begin(16, 2);
  // Print a message to the LCD.
  pinMode (13,OUTPUT);//pin 13 is the led pin
  pinMode (19, INPUT);//pin 9 reads the high signal from the PIR  

}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  
  lcd.clear();
  turn(l);
  /////////////////avilable
 if (sensor()==1 && k==1){
   counter=20;
   l="I am avilable in the office";
 }
  
  
  delay(300);
  //////////////// not avilable
  if (counter==0&&k==1){
     fonaSSone.listen();
     l="I am not in the office";
     // send message to doctor GSM
     fona.sendSMS("0507002765", "Where are you Dr.Mansour?"); //0505238426
     k=0;
  } 
  else {
    counter--;
  }
  
  
  
  /////////////// reciving sms
  if (k==0){
     
    x=r();  
  
   if(x!=0 && fona.readSMS(1, replybuffer, 250, &x)) {
     Serial.println(replybuffer);
      ///////////// 0 ///////////////   
     if(replybuffer[0]==m[0]){
      
      l="I will be back in a minute";
      Serial.println("I will be back in a minute");   
     }
     else if(replybuffer[7]==m[0]){
     
      l="I will be back in a minute";
      Serial.println("I will be back in a minute");   
     }
     
     ///////////// 1 //////////////// 
     else if(replybuffer[0]==m[1]){
      
      l="I am in the meeting";
      Serial.println("I am in the meeting");   
     }
     else if(replybuffer[7]==m[1]){
      
      l="I am in the meeting";
      Serial.println("I am in the meeting");   
     }
     
     /////////////// 2 /////////////////
      else if(replybuffer[0]==m[2]){
      
      l="I am outside. I will not come today";
      Serial.println("I am outside. I will not come today");   
     }
     else if(replybuffer[7]==m[2]){
     
      l="I am outside. I will not come today";
      Serial.println("I am outside. I will not come today");   
     }
   
    ///////////// another message ///////////////
     else {
       
       l="faild faild faild";
       Serial.println("faild"); 
     }
     
     d();
    
   }
   
   ///////////// catch moving
  

   else if (sensor()==1 ){
   camera(); 
  fona.sendSMS("0507002765", "some one in the office, is that you?");
  k=1;
 }
  
  
  
  
  
  
  
  
  
  
  
  ///////////////////////////////// 
    
   flushSerial();
   while (fona.available()) {
      Serial.write(fona.read());
  }
 }
    
 

}

//////////////////////////////////////////////////////////////////////////////


int sensor () {
int val = digitalRead(10);//Pin 9 is initiallly 0
digitalWrite(13,val);//pin thirteen is equal to whatever pin 9 is

  return val;  

delay(1000);
}



void turn(String s) {
  
  lcd.clear();
  lcd.setCursor(c, 0);
 
  lcd.print(s);
  c--;
  if (c==0)c=-24;
  if (c==-60) {
    lcd.clear();
    c=15;
  }
  
  // print the number of seconds since reset:
  lcd.setCursor(0, 1);
  lcd.print("   0559991700   ");
  
  delay(300);

}




char r(){
      // read an SMS
      uint8_t smsn = 1;
      //Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);

      // Retrieve SMS sender address/phone number.
      if (! fona.getSMSSender(smsn, replybuffer, 250)) {
      //  Serial.println("Failed!");
      }
     //Serial.print(F("FROM: ")); Serial.println(replybuffer);

      // Retrieve SMS value.
      uint16_t smslen;
      if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
    //    Serial.println("Failed!");
      }
    //  Serial.print(F("***** SMS #")); Serial.print(smsn); 
    //  Serial.print(" ("); 
      return smslen; 
      /*Serial.println(F(") bytes *****"));
      Serial.println(replybuffer);
      Serial.println(F("*****"));*/
      
      
    }
   void R(){
      // read all SMS
      int8_t smsnum = fona.getNumSMS();
      uint16_t smslen;
      for (int8_t smsn=1; smsn<=smsnum; smsn++) {
        //Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);
        if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) {  // pass in buffer and max len!
           //Serial.println(F("Failed!"));
           //break;
        }
        // if the length is zero, its a special case where the index number is higher
        // so increase the max we'll look at!
        if (smslen == 0) {
         // Serial.println(F("[empty slot]"));
          smsnum++;
         // continue;
        }
        
      /*  Serial.print(F("***** SMS #")); Serial.print(smsn); 
        Serial.print(" ("); Serial.print(smslen); Serial.println(F(") bytes *****"));
        Serial.println(replybuffer);
        Serial.println(F("*****"));
      */}
      
    }

    void d(){
      // delete an SMS
     // Serial.print(F("Delete #"));
      uint8_t smsn = 1;
      
     // Serial.print(F("\n\rDeleting SMS #")); Serial.println(smsn);
      if (fona.deleteSMS(smsn)) {
      //  Serial.println(F("OK!"));
      } else {
       // Serial.println(F("Couldn't delete"));
      }
    }
    
    
    void da(){
      // delete all SMS
      String x="";
   int8_t y=1;
   while(x!="Couldn't delete" ){
      uint8_t smsn = y;
    
      if (fona.deleteSMS(smsn)) {
       x ="OK!";
      } else {
        x="Couldn't delete";
      }
      y++;
   }
 }
 
 
 
 void flushSerial() {
    while (Serial.available()) 
    Serial.read();
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}
uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}
  
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
  uint16_t buffidx = 0;
  boolean timeoutvalid = true;
  if (timeout == 0) timeoutvalid = false;
  
  while (true) {
    if (buffidx > maxbuff) {
      //Serial.println(F("SPACE"));
      break;
    }

    while(Serial.available()) {
      char c =  Serial.read();

      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0)   // the first 0x0A is ignored
          continue;
        
        timeout = 0;         // the second 0x0A is the end of the line
        timeoutvalid = true;
        break;
      }
      buff[buffidx] = c;
      buffidx++;
    }
    
    if (timeoutvalid && timeout == 0) {
      //Serial.println(F("TIMEOUT"));
      break;
    }
    delay(1);
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}

///////////////////////////////////////////////////////////////////

int freeRam () 
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

/////////////////////////////////////////////////////////////

void camera()
{
  cameraconnection.listen();
  
  if (cam.motionDetected()) {
    Serial.println(F("Motion!")); 
    cam.setMotionDetect(false);
    if (! cam.takePicture()) 
      Serial.println(F("Failed to snap!"));
    else 
      Serial.println(F("Picture taken!"));

  Serial.print(F("Free RAM ")); Serial.println(freeRam());

delay(1000);
    char filename[13];
    strcpy(filename, "IMAGE00.JPG");
    for (int i = 0; i < 100; i++) {
      filename[5] = '0' + i/10;
      filename[6] = '0' + i%10;
      // create if does not exist, do not open existing, write, sync after write
      if (! SD.exists(filename)) {
        break;
      }
    }

    File imgFile = SD.open(filename, FILE_WRITE);

    uint16_t jpglen = cam.frameLength();
    Serial.print(jpglen, DEC);
    Serial.println(F(" byte image"));

    Serial.print(F("Writing image to ")); 
    Serial.print(filename);

    while (jpglen > 0) {
      // read 32 bytes at a time;
      uint8_t *buffer;
      uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
      buffer = cam.readPicture(bytesToRead);
      imgFile.write(buffer, bytesToRead);

      //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");

      jpglen -= bytesToRead;
    }
    imgFile.close();
    Serial.println(F("...Done!"));
    cam.resumeVideo();
    cam.setMotionDetect(true);
    
  }
 
  fonaSSone.listen();
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: camera with gsm

Post by adafruit_support_rick »

Please post the output from Serial Monitor

User avatar
DigitalMAS
 
Posts: 13
Joined: Sun Mar 01, 2015 4:49 pm

Re: camera with gsm

Post by DigitalMAS »

if i make this code

i get this serial
Attachments
final_1new.png
final_1new.png (114.28 KiB) Viewed 334 times
final_1new.txt
(10.94 KiB) Downloaded 132 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: camera with gsm

Post by adafruit_support_rick »

I think you are running out of SRAM. You an do some thing to reduce your SRAM usage, but I'm not sure it will be enough. You will probably have to go to an Arduino Mega to make this work.

Please see this tutorial on Arduino memory.
https://learn.adafruit.com/memories-of-an-arduino

You should at least try using the reserve() function for your String objects, and you should use the F() macro on your literal strings in Serial.print statements.

User avatar
DigitalMAS
 
Posts: 13
Joined: Sun Mar 01, 2015 4:49 pm

Re: camera with gsm

Post by DigitalMAS »

first rick thanks for every thing

second, well we had this problem before within the SRAM and you helped us to make it work by freeing the SRAM before any operation in the following link

viewtopic.php?f=22&t=69409&p=354189#p354189

now when we completed the project and uploaded all code we had again the same problem that you fixed it

we are trying to make this project work i hope you can find a way like before

thanks

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: camera with gsm

Post by adafruit_support_rick »

You've still got a few literal strings that you haven't used the F() macro on.

But I think you've hit the wall this time.

User avatar
DigitalMAS
 
Posts: 13
Joined: Sun Mar 01, 2015 4:49 pm

Re: camera with gsm

Post by DigitalMAS »

Ok ,,, I have read lots of information about any solution to my problem and there's 3 solution almost two will not be easy and there is 90% will not work but the third solution will be possible to do and fix the problem by using the flash memory instead of the SRAM

http://www.arduino.cc/en/Reference/PROGMEM
This is what I have found please give a review or any idea how I can do that

Thanks

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: camera with gsm

Post by adafruit_support_mike »

The F() macro stores strings in PROGMEM, and you can store other constants there too.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: camera with gsm

Post by adafruit_support_rick »

Other than your literal strings, you don't have any static data in SRAM that you can move to PROGMEM.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”