Skip to content

Instantly share code, notes, and snippets.

@brianrho
Last active August 6, 2023 03:19
Show Gist options
  • Save brianrho/e95c893de4fd980ec610ca60b5de013f to your computer and use it in GitHub Desktop.
Save brianrho/e95c893de4fd980ec610ca60b5de013f to your computer and use it in GitHub Desktop.
#define HEADER_BUF_SZ 256
char headerBuf[HEADER_BUF_SZ];
char controlName[] = "imgupload";
char fileName[] = "finger.bmpp";
uint16_t connectAndAssembleMultipartHeaders(void)
{
uint16_t bodyLen = 0;
if (client.connect(server, port))
{
Serial.println("Connected to server");
client.println("POST /post HTTP/1.1");
client.println("Host: 192.168.1.106");
client.println("Content-Type: multipart/form-data; boundary=X-ARDUINO_MULTIPART");
client.println("Connection: close");
/* Copy boundary and content disposition for the part */
uint16_t availSpace = HEADER_BUF_SZ;
int wouldCopy = 0;
wouldCopy = snprintf(headerBuf + wouldCopy, availSpace, "--X-ARDUINO_MULTIPART\r\nContent-Disposition: form-data;"
" name=\"%s\"; filename=\"%s\"\r\n", controlName, fileName);
if (wouldCopy < 0 || wouldCopy >= availSpace)
{
Serial.println("Header buffer too small. Stopping.");
return 0;
}
bodyLen += wouldCopy;
availSpace -= wouldCopy;
/* Copy content type for the part */
wouldCopy = snprintf(headerBuf + wouldCopy, availSpace, "Content-Type: application/octet-stream\r\n\r\n");
if (wouldCopy < 0 || wouldCopy >= availSpace)
{
Serial.println("Header buffer too small (2). Stopping.");
return 0;
}
bodyLen += wouldCopy;
availSpace -= wouldCopy;
/* Add the image size itself */
uint16_t IMAGE_SZ = 36864;
bodyLen += IMAGE_SZ;
/* Add the length of the final boundary */
bodyLen += strlen("\r\n--X-ARDUINO_MULTIPART--\r\n\r\n");
/* Send content length finally -- a sum of all bytes sent from the beginning of first boundary
* till the last byte of the last boundary */
client.print("Content-Length: "); client.print(bodyLen); client.print("\r\n\r\n");
/* Then send the header for the first (and only) part of this multipart request */
client.print(headerBuf);
return bodyLen;
}
return 0;
}
void GetFingerprint(){
if (!set_packet_len_128()) {
Serial.println("Could not set packet length");
return;
}
delay(100);
int16_t p = -1;
Serial.println("Waiting for a finger...");
while (p != FPM_OK) {
p = finger.getImage();
switch (p) {
case FPM_OK:
Serial.println("Image taken");
break;
case FPM_NOFINGER:
break;
case FPM_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FPM_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;
}
yield();
}
if (connectAndAssembleMultipartHeaders())
{
/* The client is ready to send the data. Now request the image from the sensor */
p = finger.downImage();
switch (p) {
case FPM_OK:
Serial.println("Starting image stream...");
break;
case FPM_PACKETRECIEVEERR:
Serial.println("Communication error");
return;
case FPM_UPLOADFAIL:
Serial.println("Cannot transfer the image");
return;
}
bool read_finished;
uint16_t readlen = TRANSFER_SZ;
uint16_t count = 0;
while(true)
{
bool ret = finger.readRaw(FPM_OUTPUT_TO_BUFFER, buffer, &read_finished, &readlen);
if (ret) {
count++;
client.write(buffer, readlen);
/* indicate the length to be read next time like before */
readlen = TRANSFER_SZ;
if (read_finished)
{
client.print("\r\n--X-ARDUINO_MULTIPART--\r\n\r\n");
break;
}
}
}
}
while(client.connected()) {
while(client.available()){
char c = client.read();
Serial.print(c);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment