API Documentation

Device Integration Guide

NodeMCU ESP8266 Integration
REST API endpoints for device communication with the Smart School Bell System

These APIs allow your ESP8266 device to authenticate, download bell schedules, and report status to the server. All endpoints accept and return JSON.

Base URL

POST /api/functions/deviceApi

All device API calls are made to this single endpoint with different actions specified in the request body.

Device Authentication
Validate device credentials and get initial connection info

Request

POST /api/functions/deviceApi
Content-Type: application/json

{
  "action": "auth",
  "device_code": "1234567",
  "device_secret": "your_device_secret"
}

Response (Success)

{
  "success": true,
  "device_id": "device_uuid",
  "school_id": "school_uuid",
  "school_name": "Delhi Public School",
  "timezone": "Asia/Kolkata",
  "status": "active"
}

Response (Error)

{
  "error": "Invalid device code"
}
// HTTP 401 Unauthorized

Usage Notes

  • • Call this on device startup to validate credentials
  • • The first successful auth marks the device as "first_seen"
  • • Store the timezone for local time calculations
ESP8266 Arduino Example
Sample code for making API calls from ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* API_URL = "https://your-app-url/api/functions/deviceApi";
const char* DEVICE_CODE = "1234567";
const char* DEVICE_SECRET = "your_device_secret";

void getConfig() {
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClientSecure client;
    client.setInsecure(); // For testing only
    
    HTTPClient http;
    http.begin(client, API_URL);
    http.addHeader("Content-Type", "application/json");
    
    String payload = "{\"action\":\"config\",";
    payload += "\"device_code\":\"" + String(DEVICE_CODE) + "\",";
    payload += "\"device_secret\":\"" + String(DEVICE_SECRET) + "\"}";
    
    int httpCode = http.POST(payload);
    
    if (httpCode > 0) {
      String response = http.getString();
      
      DynamicJsonDocument doc(2048);
      deserializeJson(doc, response);
      
      if (doc["success"]) {
        JsonArray bellTimes = doc["bell_times"];
        for (JsonObject bell : bellTimes) {
          const char* time = bell["time"];
          const char* label = bell["label"];
          int duration = bell["duration"];
          // Store bell times...
        }
      }
    }
    
    http.end();
  }
}

void sendHeartbeat() {
  // Similar structure with action: "heartbeat"
  // Include wifi_signal: WiFi.RSSI()
}