Sine wave predictor with IoT
Documentation for Consentium TinyML Library Integration
This guide explains the usage and functionality of the Consentium TinyML library with ESP32 or Raspberry Pi Pico W compatible Edge boards. The code demonstrates how to perform sine wave prediction using a TensorFlow Lite model, which runs on a microcontroller (ESP32 or Raspberry Pi Pico W) and interacts with IoT boards for data transmission.
Key Components
Libraries Used
EdgeNeuron.h: This library includes the necessary functionality to work with TensorFlow Lite on microcontroller platforms.
ConsentiumThings.h: Provides support for IoT-related operations, such as WiFi initialization and data transmission.
Core Variables
ConsentiumThingsDalton board;
: Creates an object to manage IoT operations (WiFi, data transmission).WiFi Credentials:
ssid
: Network SSID (WiFi name).pass
: WiFi password.
API Keys:
SendApiKey
: The key to authenticate sending data to a remote server.BoardApiKey
: API key for identifying the board.
TensorFlow Lite Variables:
tensor_arena
: Memory arena for TensorFlow Lite model execution.kTensorArenaSize
: Defines the maximum size of the tensor arena.
Code Breakdown
1. Setup Function
This function initializes both WiFi connectivity and the TensorFlow Lite model.
board.initWiFi(ssid, pass);
: Connects to the specified WiFi network.board.beginSend(SendApiKey, BoardApiKey);
: Authenticates the board with its unique API keys.initializeModel(...)
: Loads the TensorFlow Lite model into memory using the defined tensor arena. If this fails, the execution stops.
2. Main Loop
The loop
function continuously predicts the sine of x
using the model and compares it with the actual sine value.
setModelInput(x, 0);
: Provides the input (x
value) to the TensorFlow Lite model.runModelInference();
: Runs the inference process to predict the sine value.getModelOutput(0);
: Retrieves the model's predicted output.sin(x);
: Calculates the actual sine ofx
for comparison.board.sendData(...)
: Sends the input, predicted, and actual sine values to a remote server with low precision (to reduce data size).
Additional Notes
WiFi Configuration: The code connects to a WiFi network using
initWiFi()
. If there are connectivity issues, ensure the SSID and password are correct.API Keys: The API keys (
SendApiKey
andBoardApiKey
) are essential for interacting with the server. If the keys are invalid, the data will not be sent successfully.TensorFlow Lite Model: This code assumes that a pre-trained TensorFlow Lite model is already present, loaded via
model.h
. Ensure that the model is compiled correctly and fits within the definedtensor_arena
size.
Potential Modifications
Step Size (
x
): You can adjust thestep
size forx
to change how quickly it increments. A smaller step will result in more detailed predictions.Delay: The
delay(interval);
statement controls how frequently data is sent. In this case, the delay is set to 7 seconds. Adjust it as per your use case.Model Initialization: If the model initialization frequently fails due to insufficient memory, consider increasing the
kTensorArenaSize
value or reducing the model complexity.
Example Use Case
This code is designed to predict the sine of values between 0 and 2π using a TinyML model on an edge device. It's a typical example for IoT projects where data is transmitted wirelessly to a remote server.
License: This project is licensed under the MIT License. The above information, including the text from Consentium IoT., must be included in any redistribution or modification of this code.
Full Code:
Last updated