Skip to content

Advanced Usage

GALIH RIDHO UTOMO edited this page Feb 24, 2025 · 1 revision

Advanced-Usage

Advanced Usage

This page covers advanced techniques and customizations for the GitHub IoT Arduino Module.

Custom HTTP Methods

The standard library implements GET and PUT requests. You can extend it with additional methods:

// Add to githubiot.h
public:
    String downloadFileContent();
    bool deleteFile(String& sha);

// Implement in githubiot.cpp
String githubiot::downloadFileContent() {
    HTTPClient http;
    String content = "";
    
    http.begin(_repo_url);
    http.addHeader("Authorization", _token);
    int httpCode = http.GET();
    
    if (httpCode == HTTP_CODE_OK) {
        DynamicJsonDocument doc(1024);
        deserializeJson(doc, http.getString());
        String b64content = doc["content"].as<String>();
        content = base64::decode(b64content);
    }
    
    http.end();
    return content;
}

bool githubiot::deleteFile(String& sha) {
    HTTPClient http;
    bool success = false;
    
    http.begin(_repo_url);
    http.addHeader("Content-Type", "application/json");
    http.addHeader("Authorization", _token);
    
    String payload = "{\"message\":\"Delete file\",\"sha\":\"" + sha + "\"}";
    int httpCode = http.sendRequest("DELETE", payload);
    
    success = (httpCode == HTTP_CODE_OK);
    http.end();
    return success;
}

Custom Commit Messages

Modify the upload_to_github method to accept custom commit messages:

// Update method signature in githubiot.h
void upload_to_github(DynamicJsonDocument doc, String& last_sha, String commitMessage = "Update data");

// Update implementation in githubiot.cpp
void githubiot::upload_to_github(DynamicJsonDocument doc, String& last_sha, String commitMessage) {
    // ... existing code ...
    
    String payload = "{\"message\":\"" + commitMessage + "\",\"content\":\"" + encodedData + "\",\"sha\":\"" + last_sha + "\"}";
    
    // ... rest of method ...
}

Time Series Data Collection

This advanced example maintains a growing time series by downloading, parsing, and appending to existing data:

void appendTimeSeriesData(float newValue) {
  // Get current SHA
  String sha = iot_module.get_current_sha();
  if (sha == "") {
    Serial.println("Failed to get SHA");
    return;
  }
  
  // Download current content
  String content = iot_module.downloadFileContent(); // Custom method
  
  // Parse existing data or create new document
  DynamicJsonDocument doc(4096); // Larger document for time series
  
  if (content.length() > 0) {
    DeserializationError error = deserializeJson(doc, content);
    if (error) {
      Serial.println("Failed to parse content");
      return;
    }
  } else {
    // Initialize new document
    doc.createNestedArray("data");
  }
  
  // Add new data point
  JsonObject newPoint = doc["data"].createNestedObject();
  newPoint["timestamp"] = getTimeStamp(); // Your time function
  newPoint["value"] = newValue;
  
  // Limit array size if needed
  const size_t MAX_POINTS = 100;
  while (doc["data"].size() > MAX_POINTS) {
    // Remove oldest entry (first element)
    JsonArray data = doc["data"];
    for (size_t i = 0; i < data.size() - 1; i++) {
      data[i] = data[i + 1];
    }
    data.remove(data.size() - 1);
  }
  
  // Upload updated document
  iot_module.upload_to_github(doc, sha, "Add data point");
}

Clone this wiki locally