Skip to content

API Reference

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

API-Reference

API Reference

This page documents all classes, methods, and properties available in the GitHub IoT Arduino Module.

Class: githubiot

The main class for interacting with GitHub repositories for IoT data storage.

Constructor

githubiot(const char* token, const char* repo_url)

Creates a new instance of the githubiot class.

Parameters:

  • token: GitHub authentication token with 'Bearer ' prefix
  • repo_url: GitHub API URL for the target file

Example:

const char* token = "Bearer ghp_xxxxxxxxxxxxxxxxxxxx";
const char* repo_url = "https://api.github.com/repos/username/repo/contents/data.json";
githubiot iot_module(token, repo_url);

Method: get_current_sha()

String get_current_sha()

Retrieves the current SHA hash of the file in the GitHub repository.

Returns:

  • String containing the SHA hash, or empty string if failed

Example:

String sha = iot_module.get_current_sha();
if (sha != "") {
  Serial.println("Current SHA: " + sha);
} else {
  Serial.println("Failed to get SHA");
}

Method: upload_to_github()

void upload_to_github(DynamicJsonDocument doc, String& last_sha)

Uploads JSON data to GitHub by updating the specified file.

Parameters:

  • doc: ArduinoJson DynamicJsonDocument containing the data to upload
  • last_sha: Reference to the current SHA string (will be updated with new SHA)

Example:

DynamicJsonDocument doc(1024);
doc["sensor"] = "temperature";
doc["value"] = 25.5;
doc["timestamp"] = 1645729845;

String sha = iot_module.get_current_sha();
iot_module.upload_to_github(doc, sha);

Private Class Members

These members are used internally by the class:

private:
    const char* _token;     // GitHub authentication token
    const char* _repo_url;  // GitHub repository API URL
    String _last_sha;       // Last known SHA hash

Header File Structure

#ifndef GITHUBIOT_H
#define GITHUBIOT_H

#include <Arduino.h>
#include <ArduinoJson.h>
#include <base64.h>

// Platform-specific includes
#ifdef ESP32
  #include <WiFi.h>
  #include <HTTPClient.h>
#elif defined(ESP8266)
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
#endif

class githubiot {
public:
    githubiot(const char* token, const char* repoUrl);
    String get_current_sha();
    void upload_to_github(DynamicJsonDocument doc, String& last_sha);
private:
    const char* _token;
    const char* _repo_url;
    String _last_sha;
};

#endif

For usage examples, see the Usage Examples page.


Clone this wiki locally