Created
November 22, 2024 12:45
-
-
Save mattgaidica/2448e9af8823e7fa4405b0e3aa27fb64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <SD.h> | |
// WiFi credentials | |
const char* ssid = "Your_SSID"; | |
const char* password = "Your_PASSWORD"; | |
// S3 credentials and bucket details | |
const char* bucketName = "your-bucket-name"; | |
const char* region = "your-region"; // e.g., "us-east-1" | |
const char* accessKey = "your-access-key"; | |
const char* secretKey = "your-secret-key"; | |
const char* fileName = "/data.txt"; // File on SD card to upload | |
// AWS S3 endpoint | |
const char* host = "s3.amazonaws.com"; | |
const int httpsPort = 443; | |
// SD card details | |
#define SD_CS_PIN 5 // Adjust based on your SD card module wiring | |
void setup() { | |
Serial.begin(115200); | |
// Initialize WiFi | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi..."); | |
} | |
Serial.println("WiFi connected!"); | |
// Initialize SD card | |
if (!SD.begin(SD_CS_PIN)) { | |
Serial.println("SD card initialization failed!"); | |
while (1); | |
} | |
Serial.println("SD card initialized."); | |
// Upload file to S3 | |
if (uploadFileToS3(fileName)) { | |
Serial.println("File uploaded successfully!"); | |
} else { | |
Serial.println("File upload failed."); | |
} | |
} | |
bool uploadFileToS3(const char* filePath) { | |
File file = SD.open(filePath); | |
if (!file) { | |
Serial.println("Failed to open file for reading."); | |
return false; | |
} | |
// Calculate file size | |
size_t fileSize = file.size(); | |
// Read file contents | |
String fileContent = ""; | |
while (file.available()) { | |
fileContent += (char)file.read(); | |
} | |
file.close(); | |
// Prepare S3 signature (use your preferred method for generating the signature) | |
String signature = generateS3Signature(filePath, fileSize); | |
// Create HTTPS client | |
WiFiClientSecure client; | |
client.setInsecure(); | |
if (!client.connect(host, httpsPort)) { | |
Serial.println("Connection to S3 failed!"); | |
return false; | |
} | |
// HTTP PUT request to upload the file | |
String url = String("/") + bucketName + filePath; | |
client.println("PUT " + url + " HTTP/1.1"); | |
client.println("Host: " + String(host)); | |
client.println("Content-Type: text/plain"); | |
client.println("Content-Length: " + String(fileSize)); | |
client.println("Authorization: " + signature); | |
client.println("x-amz-date: 20241122T000000Z"); | |
client.println(); | |
client.print(fileContent); | |
// Wait for response | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") break; | |
} | |
// Check HTTP response | |
String response = client.readString(); | |
Serial.println("S3 Response:"); | |
Serial.println(response); | |
return response.indexOf("200 OK") != -1; | |
} | |
String generateS3Signature(const char* filePath, size_t fileSize) { | |
// Add logic to generate S3 signature (HMAC-SHA256) | |
// This is a placeholder. Use an external library or service to create the signature. | |
return "AWS4-HMAC-SHA256 Placeholder_Signature"; | |
} | |
void loop() { | |
// Do nothing | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment