Skip to main content

Overview

Kinematrix is designed with platform abstraction in mind, allowing you to write code once and deploy it across multiple microcontroller platforms. This guide covers platform-specific considerations and best practices.

Supported Platforms

ESP32

Primary Target
  • 240MHz dual-core
  • 520KB SRAM
  • WiFi + Bluetooth
  • Full feature set

ESP8266

Secondary
  • 80MHz single-core
  • 80KB SRAM
  • WiFi only
  • Optimized features

Arduino AVR

Limited Support
  • 16MHz
  • 2KB SRAM (Uno)
  • Basic I/O
  • Core features only

Platform Detection

Kinematrix automatically detects the target platform at compile time:
#if defined(ESP32)
    // ESP32-specific code
    #define BUFFER_SIZE 256
    #define ENABLE_WIFI
#elif defined(ESP8266)
    // ESP8266-specific code
    #define BUFFER_SIZE 128
    #define ENABLE_WIFI
#else
    // Arduino AVR code
    #define BUFFER_SIZE 64
    #undef ENABLE_WIFI
#endif

Writing Portable Code

Platform-Agnostic Sensor Code

This example works on all platforms:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_ANALOG_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

SensorModuleV2 sensors;

void setup() {
    Serial.begin(115200);
    
    // Works on all platforms
    sensors.addSensor("temp", new DHTSensV2(2, DHT22));
    sensors.addSensor("light", new AnalogSensV2(A0, 5.0, 1023));
    
    sensors.init();
}

void loop() {
    sensors.update();
    
    if (sensors.isUpdated("temp")) {
        float temp = sensors.getValue<float>("temp", "temperature");
        Serial.print("Temperature: ");
        Serial.println(temp);
    }
    
    delay(1000);
}

Platform-Specific Features

Use conditional compilation for platform-specific features:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2

#if defined(ESP32) || defined(ESP8266)
    #define ENABLE_MODULE_WIFI_HANDLER_V2
    #define ENABLE_MODULE_MQTT_MANAGER
#endif

#include "Kinematrix.h"

SensorModuleV2 sensors;

#if defined(ESP32) || defined(ESP8266)
    WiFiHandlerV2 wifi;
    MQTTManager mqtt;
#endif

void setup() {
    Serial.begin(115200);
    
    // Core functionality (all platforms)
    sensors.addSensor("temp", new DHTSensV2(2, DHT22));
    sensors.init();
    
    #if defined(ESP32) || defined(ESP8266)
        // WiFi-enabled platforms only
        wifi.begin("YourSSID", "YourPassword");
        mqtt.begin("mqtt.example.com", 1883);
    #endif
}

void loop() {
    sensors.update();
    
    #if defined(ESP32) || defined(ESP8266)
        wifi.handle();
        mqtt.loop();
        
        if (sensors.isUpdated("temp")) {
            float temp = sensors.getValue<float>("temp", "temperature");
            mqtt.publish("sensor/temperature", String(temp));
        }
    #endif
    
    delay(1000);
}

Memory Optimization

ESP32 (Generous Memory)

Full feature enablement is fine:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_BME680_V2
#define ENABLE_SENSOR_INA219_V2
#define ENABLE_SENSOR_FILTER_V2
#define ENABLE_SENSOR_ALERT_SYSTEM_V2
#define ENABLE_MODULE_WIFI_HANDLER_V2
#define ENABLE_MODULE_MQTT_MANAGER
#define ENABLE_MODULE_PID_CONTROLLER
#include "Kinematrix.h"

ESP8266 (Moderate Memory)

Enable only required features:
// Core sensors only
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_ANALOG_V2
#define ENABLE_SENSOR_UTILITY_V2

// Essential WiFi features
#define ENABLE_MODULE_WIFI_HANDLER_V2
#define ENABLE_MODULE_MQTT_MANAGER

#include "Kinematrix.h"

Arduino AVR (Limited Memory)

Minimal feature set:
// Basic sensors only
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_UTILITY_V2

#include "Kinematrix.h"

Platform-Specific Pin Mappings

ESP32

// Recommended pins for ESP32
#define I2C_SDA 21
#define I2C_SCL 22
#define DHT_PIN 4
#define ANALOG_PIN 34  // ADC1 channel
#define LED_PIN 2

void setup() {
    Wire.begin(I2C_SDA, I2C_SCL);
    analogSetAttenuation(ADC_11db);  // For 0-3.3V range
}

ESP8266

// Recommended pins for ESP8266 (NodeMCU)
#define I2C_SDA D2  // GPIO4
#define I2C_SCL D1  // GPIO5
#define DHT_PIN D4  // GPIO2
#define ANALOG_PIN A0
#define LED_PIN D0  // GPIO16

void setup() {
    Wire.begin(I2C_SDA, I2C_SCL);
}

Arduino Uno/Nano

// Standard Arduino pins
#define I2C_SDA A4
#define I2C_SCL A5
#define DHT_PIN 2
#define ANALOG_PIN A0
#define LED_PIN 13

void setup() {
    Wire.begin();  // Uses default pins
}

Complete Multi-Platform Example

Here’s a complete example that adapts to all platforms:
1

Platform Configuration

// Multi-platform pin definitions
#if defined(ESP32)
    #define DHT_PIN 4
    #define ANALOG_PIN 34
    #define I2C_SDA 21
    #define I2C_SCL 22
    #define ADC_RESOLUTION 4095
    #define ADC_VOLTAGE 3.3
#elif defined(ESP8266)
    #define DHT_PIN D4
    #define ANALOG_PIN A0
    #define I2C_SDA D2
    #define I2C_SCL D1
    #define ADC_RESOLUTION 1023
    #define ADC_VOLTAGE 3.3
#else  // Arduino AVR
    #define DHT_PIN 2
    #define ANALOG_PIN A0
    #define I2C_SDA A4
    #define I2C_SCL A5
    #define ADC_RESOLUTION 1023
    #define ADC_VOLTAGE 5.0
#endif
2

Module Configuration

#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_ANALOG_V2
#define ENABLE_SENSOR_UTILITY_V2

#if defined(ESP32) || defined(ESP8266)
    #define ENABLE_MODULE_WIFI_HANDLER_V2
#endif

#include "Kinematrix.h"
3

Implementation

SensorModuleV2 sensors;

#if defined(ESP32) || defined(ESP8266)
    WiFiHandlerV2 wifi;
#endif

void setup() {
    Serial.begin(115200);
    
    // Print platform info
    #if defined(ESP32)
        Serial.println("Platform: ESP32");
    #elif defined(ESP8266)
        Serial.println("Platform: ESP8266");
    #else
        Serial.println("Platform: Arduino AVR");
    #endif
    
    // Initialize sensors (works on all platforms)
    sensors.addSensor("temp", new DHTSensV2(DHT_PIN, DHT22));
    sensors.addSensor("analog", new AnalogSensV2(ANALOG_PIN, ADC_VOLTAGE, ADC_RESOLUTION));
    sensors.init();
    
    // Initialize WiFi (ESP only)
    #if defined(ESP32) || defined(ESP8266)
        wifi.begin("YourSSID", "YourPassword");
    #endif
}

void loop() {
    sensors.update();
    
    #if defined(ESP32) || defined(ESP8266)
        wifi.handle();
    #endif
    
    if (sensors.isUpdated("temp")) {
        float temp = sensors.getValue<float>("temp", "temperature");
        float humidity = sensors.getValue<float>("temp", "humidity");
        
        Serial.print("Temp: ");
        Serial.print(temp);
        Serial.print("°C, Humidity: ");
        Serial.print(humidity);
        Serial.println("%");
        
        #if defined(ESP32) || defined(ESP8266)
            if (wifi.isConnected()) {
                Serial.println("WiFi: Connected");
            }
        #endif
    }
    
    delay(2000);
}

PlatformIO Configuration

Use PlatformIO for easy multi-platform development:
[platformio]
default_envs = esp32

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200

[env:esp8266]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200

[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200
Build for specific platform:
pio run -e esp32        # Build for ESP32
pio run -e esp8266      # Build for ESP8266
pio run -e uno          # Build for Arduino Uno

Feature Availability Matrix

FeatureESP32ESP8266Arduino AVR
Basic Sensors
SensorModuleV2
Data Filters⚠️ Limited
Alert System⚠️ Limited
WiFi Modules
MQTT
Firebase
PID Controller
Fuzzy Logic⚠️ Limited
Machine Learning⚠️ Limited
SD Card⚠️ SPI only
Display Menus

Best Practices

1. Use Platform Detection Macros

#if defined(ESP32)
    // ESP32 code
#elif defined(ESP8266)
    // ESP8266 code
#else
    // Arduino code
#endif

2. Abstract Hardware Differences

// Create platform-agnostic constants
const int DHT_PIN = 
    #if defined(ESP32)
        4
    #elif defined(ESP8266)
        D4
    #else
        2
    #endif
;

3. Test on Multiple Platforms

Regularly test your code on all target platforms to catch platform-specific issues early.

4. Document Platform Requirements

/**
 * Multi-Platform Temperature Logger
 * 
 * Supported Platforms:
 * - ESP32: Full features including WiFi
 * - ESP8266: WiFi support with reduced memory
 * - Arduino Uno: Basic logging only
 * 
 * Memory Requirements:
 * - ESP32: ~50KB
 * - ESP8266: ~30KB
 * - Arduino Uno: ~15KB
 */

Troubleshooting Platform Issues

  • Check if all required modules are enabled
  • Verify pin definitions are correct for platform
  • Ensure feature is supported on target platform
  • Check PlatformIO configuration for correct board
  • Reduce number of enabled modules
  • Use simpler data structures
  • Decrease buffer sizes
  • Consider upgrading to platform with more memory
  • Check ADC resolution differences (10-bit vs 12-bit)
  • Verify voltage reference (3.3V vs 5V)
  • Review timing-sensitive code
  • Test I2C clock speeds

Next Steps

Debugging

Platform-specific debugging techniques

Basic Sensor Reading

Review sensor fundamentals

IoT Data Logger

Build cross-platform data loggers

LED Control

Platform-agnostic LED control

Build docs developers (and LLMs) love