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();
}