Skip to content

Usage Examples

GALIH RIDHO UTOMO edited this page Feb 24, 2025 · 2 revisions

Usage-Examples

Usage Examples

This page provides practical examples of using the GitHub IoT Arduino Module for various IoT scenarios.

Basic Data Logging

This example demonstrates how to log sensor data to GitHub at regular intervals.

#include <githubiot.h>

// Configuration
const char* ssid = "WiFi_SSID";
const char* password = "WiFi_Password";
const char* token = "Bearer ghp_xxxxxxxxxxxxxxxxxxxx";
const char* repo_url = "https://api.github.com/repos/username/repo/contents/sensor_log.json";

// Initialize the module
githubiot iot_module(token, repo_url);

void setup() {
  Serial.begin(115200);
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Get sensor reading
  float temperature = 23.5; // Replace with actual sensor reading
  
  // Get current SHA
  String sha = iot_module.get_current_sha();
  
  // Create data document
  DynamicJsonDocument doc(1024);
  doc["device_id"] = "esp32_01";
  doc["sensor"] = "temperature";
  doc["value"] = temperature;
  doc["timestamp"] = millis();
  
  // Upload to GitHub
  iot_module.upload_to_github(doc, sha);
  Serial.println("Data uploaded");
  
  // Wait before next reading
  delay(60000); // Upload once per minute
}

Multiple Sensor Data Collection

This example shows how to collect and upload data from multiple sensors.

#include <githubiot.h>

// Configuration
const char* ssid = "WiFi_SSID";
const char* password = "WiFi_Password";
const char* token = "Bearer ghp_xxxxxxxxxxxxxxxxxxxx";
const char* repo_url = "https://api.github.com/repos/username/repo/contents/multi_sensor.json";

// Initialize the module
githubiot iot_module(token, repo_url);

void setup() {
  Serial.begin(115200);
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Read sensor values (replace with actual sensor readings)
  float temperature = 23.5;
  float humidity = 65.2;
  float pressure = 1013.25;
  
  // Get current SHA
  String sha = iot_module.get_current_sha();
  
  // Create data document
  DynamicJsonDocument doc(1024);
  JsonObject data = doc.createNestedObject("data");
  data["temperature"] = temperature;
  data["humidity"] = humidity;
  data["pressure"] = pressure;
  doc["timestamp"] = millis();
  
  // Upload to GitHub
  iot_module.upload_to_github(doc, sha);
  Serial.println("Data uploaded");
  
  // Wait before next reading
  delay(300000); // 5 minutes
}

Time Series Data with Timestamp

This example uses NTP to get accurate timestamps for data logging.

#include <githubiot.h>
#include <time.h>

// Configuration
const char* ssid = "WiFi_SSID";
const char* password = "WiFi_Password";
const char* token = "Bearer ghp_xxxxxxxxxxxxxxxxxxxx";
const char* repo_url = "https://api.github.com/repos/username/repo/contents/timeseries.json";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;

// Initialize the module
githubiot iot_module(token, repo_url);

void setup() {
  Serial.begin(115200);
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
  
  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}

void loop() {
  // Get current time
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  
  char timestamp[20];
  strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", &timeinfo);
  
  // Get sensor reading
  float temperature = 23.5; // Replace with actual sensor reading
  
  // Get current SHA
  String sha = iot_module.get_current_sha();
  
  // Create data document
  DynamicJsonDocument doc(1024);
  doc["timestamp"] = timestamp;
  doc["temperature"] = temperature;
  
  // Upload to GitHub
  iot_module.upload_to_github(doc, sha);
  Serial.println("Data uploaded at: " + String(timestamp));
  
  // Wait before next reading
  delay(3600000); // 1 hour
}

For more advanced usage patterns, see the Advanced Usage page.


Clone this wiki locally