Send captured image from ESP32cam to AWS with code
The ESP32-CAM provides an inexpensive way to build more advanced home automation projects that feature video, taking photos, and face recognition.
Send data captured from esp32 cam to AWS .
According to AWS Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics. Amazon S3 provides easy-to-use management features so you can organize your data and configure finely-tuned access controls to meet your specific business, organizational, and compliance requirements. Amazon S3 is designed for 99.999999999% (11 9's) of durability, and stores data for millions of applications for companies all around the world.
Send data captured from esp32 cam to AWS .
According to AWS Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics. Amazon S3 provides easy-to-use management features so you can organize your data and configure finely-tuned access controls to meet your specific business, organizational, and compliance requirements. Amazon S3 is designed for 99.999999999% (11 9's) of durability, and stores data for millions of applications for companies all around the world.
Step 1:
- Make AWS account and then make S3 bucket and keep the bucket public and the bucket url
- Code to send images to AWS which will be in base64 format
const char* ssid = "";
const char* password = "";
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "Base64.h"
#include "esp_camera.h"
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
// or another board which has PSRAM enabled
//CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
Serial.println("ssid: " + (String)ssid);
Serial.println("password: " + (String)password);
WiFi.begin(ssid, password);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if ((StartTime+10000) < millis()) break;
}
if (WiFi.status() == WL_CONNECTED) {
char* apssid = "";
char* appassword = ""; //AP password require at least 8 characters.
Serial.println("");
Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());
Serial.println("' to connect");
WiFi.softAP((WiFi.localIP().toString()+"_"+(String)apssid).c_str(), appassword);
}
else {
Serial.println("Connection failed");
return;
}
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QQVGA; // VGA|CIF|QVGA|HQVGA|QQVGA (UXGA|SXGA|XGA|SVGA|???)
config.jpeg_quality = 10;
config.fb_count = 1;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
delay(1000);
ESP.restart();
}
SaveCapturedImageToS3();
}
void loop() {
}
void SaveCapturedImageToS3() {
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
return;
}
String imageFile = "data:image/jpeg;base64,";
char *input = (char *)fb->buf;
char output[base64_enc_len(3)];
for (int i=0;i<fb->len;i++) {
base64_encode(output, (input++), 3);
if (i%3==0) imageFile += urlencode(String(output));
}
Serial.println("size = "+String(imageFile.length()));
String domain = "esp32cam.s3.amazonaws.com";
String request ="/test.txt";
//String Authorization = "AWS xxxxx:yyyyy";
Serial.println("Connect to "+domain);
WiFiClientSecure client;
if(client.connect(domain.c_str(), 443)) {
Serial.println("Connection Successful");
client.println("PUT "+request+" HTTP/1.1");
client.println("HOST: "+domain);
client.println("Content-Length:"+imageFile.length());
//client.println("Authorization: "+Authorization);
//client.println("x-amz-date: 20191124T015000Z");
client.println();
int Index;
for (Index = 0; Index < imageFile.length(); Index = Index+1000) {
client.print(imageFile.substring(Index, Index+1000));
}
Serial.println("Waiting for response.");
String getResponse="",Feedback="";
boolean state = false;
int wait = 1;
int waitTime = 3000; // timeout 3 seconds
long int StartTime=millis();
while (!client.available())
{
Serial.print(".");
delay(100);
if ((StartTime+10000) < millis()) {
Serial.println();
Serial.println("No response.");
break;
}
}
Serial.println();
while (client.available()) {
Serial.print(char(client.read()));
//client.read();
}
Serial.println("Finished");
}
else {
ledcAttachPin(4, 3);
ledcSetup(3, 5000, 8);
ledcWrite(3,10);
delay(200);
ledcWrite(3,0);
delay(200);
ledcDetachPin(3);
Serial.println("Connected to " + String(domain) + " failed.");
}
client.stop();
esp_camera_fb_return(fb);
}
//https://github.com/zenmanenergy/ESP8266-Arduino-Examples/
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
- Save this file as base.cpp
* https://github.com/adamvr/arduino-base64*/#if (defined(__AVR__))#include <avr\pgmspace.h>#else#include <pgmspace.h>#endifconst char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz""0123456789+/";/* 'Private' declarations */inline void a3_to_a4(unsigned char * a4, unsigned char * a3);inline void a4_to_a3(unsigned char * a3, unsigned char * a4);inline unsigned char b64_lookup(char c);int base64_encode(char *output, char *input, int inputLen) {int i = 0, j = 0;int encLen = 0;unsigned char a3[3];unsigned char a4[4];while(inputLen--) {a3[i++] = *(input++);if(i == 3) {a3_to_a4(a4, a3);for(i = 0; i < 4; i++) {output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);}i = 0;}}if(i) {for(j = i; j < 3; j++) {a3[j] = '\0';}a3_to_a4(a4, a3);for(j = 0; j < i + 1; j++) {output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);}while((i++ < 3)) {output[encLen++] = '=';}}output[encLen] = '\0';return encLen;}int base64_decode(char * output, char * input, int inputLen) {int i = 0, j = 0;int decLen = 0;unsigned char a3[3];unsigned char a4[4];while (inputLen--) {if(*input == '=') {break;}a4[i++] = *(input++);if (i == 4) {for (i = 0; i <4; i++) {a4[i] = b64_lookup(a4[i]);}a4_to_a3(a3,a4);for (i = 0; i < 3; i++) {output[decLen++] = a3[i];}i = 0;}}if (i) {for (j = i; j < 4; j++) {a4[j] = '\0';}for (j = 0; j <4; j++) {a4[j] = b64_lookup(a4[j]);}a4_to_a3(a3,a4);for (j = 0; j < i - 1; j++) {output[decLen++] = a3[j];}}output[decLen] = '\0';return decLen;}int base64_enc_len(int plainLen) {int n = plainLen;return (n + 2 - ((n + 2) % 3)) / 3 * 4;}int base64_dec_len(char * input, int inputLen) {int i = 0;int numEq = 0;for(i = inputLen - 1; input[i] == '='; i--) {numEq++;}return ((6 * inputLen) / 8) - numEq;}inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {a4[0] = (a3[0] & 0xfc) >> 2;a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);a4[3] = (a3[2] & 0x3f);}inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);a3[2] = ((a4[2] & 0x3) << 6) + a4[3];}inline unsigned char b64_lookup(char c) {if(c >='A' && c <='Z') return c - 'A';if(c >='a' && c <='z') return c - 71;if(c >='0' && c <='9') return c + 4;if(c == '+') return 62;if(c == '/') return 63;return -1;}
- Save file as Base.h in the same folder of aws
*/#ifndef _BASE64_H#define _BASE64_H/* b64_alphabet:* Description: Base64 alphabet table, a mapping between integers* and base64 digits* Notes: This is an extern here but is defined in Base64.c*/extern const char b64_alphabet[];/* base64_encode:* Description:* Encode a string of characters as base64* Parameters:* output: the output buffer for the encoding, stores the encoded string* input: the input buffer for the encoding, stores the binary to be encoded* inputLen: the length of the input buffer, in bytes* Return value:* Returns the length of the encoded string* Requirements:* 1. output must not be null or empty* 2. input must not be null* 3. inputLen must be greater than or equal to 0*/int base64_encode(char *output, char *input, int inputLen);/* base64_decode:* Description:* Decode a base64 encoded string into bytes* Parameters:* output: the output buffer for the decoding,* stores the decoded binary* input: the input buffer for the decoding,* stores the base64 string to be decoded* inputLen: the length of the input buffer, in bytes* Return value:* Returns the length of the decoded string* Requirements:* 1. output must not be null or empty* 2. input must not be null* 3. inputLen must be greater than or equal to 0*/int base64_decode(char *output, char *input, int inputLen);/* base64_enc_len:* Description:* Returns the length of a base64 encoded string whose decoded* form is inputLen bytes long* Parameters:* inputLen: the length of the decoded string* Return value:* The length of a base64 encoded string whose decoded form* is inputLen bytes long* Requirements:* None*/int base64_enc_len(int inputLen);/* base64_dec_len:* Description:* Returns the length of the decoded form of a* base64 encoded string* Parameters:* input: the base64 encoded string to be measured* inputLen: the length of the base64 encoded string* Return value:* Returns the length of the decoded form of a* base64 encoded string* Requirements:* 1. input must not be null* 2. input must be greater than or equal to zero*/int base64_dec_len(char *input, int inputLen);#endif // _BASE64_H