Skip to content

Instantly share code, notes, and snippets.

@DarylWM
Created April 11, 2013 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarylWM/5367721 to your computer and use it in GitHub Desktop.
Save DarylWM/5367721 to your computer and use it in GitHub Desktop.
// This program takes a snapshot and sends it to the XBee module
#include <Adafruit_VC0706.h>
#include <SoftwareSerial.h>
#include <XBee.h>
#include <PString.h>
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3); // Arduino RX, TX
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
XBee xbee = XBee();
void sendImagePayload(uint16_t bytesRemaining, uint8_t bufferLength, uint8_t *buffer) {
uint8_t payload[bufferLength+4];
payload[0] = 0xf4; // payload type
payload[1] = highByte(bytesRemaining);
payload[2] = lowByte(bytesRemaining);
payload[3] = bufferLength;
for (uint8_t i=0; i<bufferLength; i++) {
payload[i+4] = buffer[i];
}
// Specify the address of the remote XBee (this is the SH + SL)
XBeeAddress64 addr64 = XBeeAddress64(0x0, 0x0); // Coordinator address
// Create a TX Request
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
// Send your request
xbee.send(zbTx);
}
void sendInfoPayload(String info) {
info.replace('\n', ',');
info.replace('\r', ' ');
char payload[2+info.length()];
payload[0] = 0xf3; // payload type
PString infoString(&payload[1], sizeof(payload)-1);
infoString.print(info);
// Specify the address of the remote XBee (this is the SH + SL)
XBeeAddress64 addr64 = XBeeAddress64(0x0, 0x0); // Coordinator address
// Create a TX Request
ZBTxRequest zbTx = ZBTxRequest(addr64, (uint8_t *)payload, sizeof(payload));
// Send your request
xbee.send(zbTx);
}
void setup() {
Serial.begin(57600);
sendInfoPayload("VC0706 Camera snapshot test");
// Try to locate the camera
if (cam.begin()) {
sendInfoPayload("Camera found.");
}
else {
sendInfoPayload("No camera found?");
return;
}
// Get the camera version
char *reply = cam.getVersion();
if (reply == 0) {
sendInfoPayload("Failed to get version");
}
else {
sendInfoPayload(reply);
}
// Set the picture size
cam.setImageSize(VC0706_640x480); // biggest
// Turn on motion detection
boolean flag = cam.setMotionDetect(true); // turn it on
if (cam.getMotionDetect())
sendInfoPayload("Motion detection is ON.");
else
sendInfoPayload("Motion detection is OFF.");
}
void loop() {
if (cam.motionDetected()) {
cam.setMotionDetect(false);
// Take the picture
sendInfoPayload("Motion detected - taking snap now...");
if (! cam.takePicture())
sendInfoPayload("Failed to snap!");
else
sendInfoPayload("Picture taken!");
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
sendInfoPayload("Sending " + String(jpglen, DEC) + " byte image.");
int32_t time = millis();
// Read all the data up to # bytes!
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);
jpglen -= bytesToRead;
sendImagePayload(jpglen, bytesToRead, buffer);
}
time = millis() - time;
sendInfoPayload(String(time)+" ms elapsed");
cam.resumeVideo();
cam.setMotionDetect(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment