Skip to content

Instantly share code, notes, and snippets.

@DarylWM
Created March 27, 2013 07:43
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/5252463 to your computer and use it in GitHub Desktop.
Save DarylWM/5252463 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 <Base64.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);
void sendMsg(String msgType, String msg) {
Serial.println("{\"type\": \"" + msgType + "\", \"msg\": " + msg + "}");
}
String formatImagePayload(uint16_t bytesRemaining, String encodedImageSegment) {
String result = "{\"seg\": {\"rem\":" + String(bytesRemaining) + ", \"enc\":\"" + encodedImageSegment + "\"}}";
return result;
}
String formatInfoPayload(String msg) {
String result = "\"" + msg + "\"";
return result;
}
void setup() {
Serial.begin(115200);
sendMsg("inf", formatInfoPayload("VC0706 Camera snapshot test"));
// Try to locate the camera
if (cam.begin()) {
sendMsg("inf", formatInfoPayload("Camera found."));
}
else {
sendMsg("inf", formatInfoPayload("No camera found?"));
return;
}
// Set the picture size
cam.setImageSize(VC0706_640x480); // biggest
}
void loop() {
// Take the picture
sendMsg("inf", formatInfoPayload("Snap in 3 secs..."));
delay(3000);
if (! cam.takePicture())
sendMsg("inf", formatInfoPayload("Failed to snap!"));
else
sendMsg("inf", formatInfoPayload("Picture taken!"));
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
sendMsg("inf", formatInfoPayload("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);
int encodedStringLength = base64_enc_len(bytesToRead);
char encodedStringBuffer[encodedStringLength];
encodedStringLength = base64_encode(encodedStringBuffer, (char *)buffer, bytesToRead);
jpglen -= bytesToRead;
sendMsg("img", formatImagePayload(jpglen, encodedStringBuffer));
}
time = millis() - time;
sendMsg("inf", formatInfoPayload(String(time)+" ms elapsed"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment