print("Hello, ESP32!")
import utime
import math
from tfli import TFLite # Import the library we just added
# This is the pre-trained TensorFlow Lite model.
# It's been converted into a C byte array and pasted here as a Python bytearray.
# This model was trained to predict the sine of a single input number.
sine_model_data = bytearray(b'\x1c\x00\x00\x00TFL3\x14\x00\x18\x00\x04\x00\x08\x00\x0c\x00\x10\x00\x14\x00\x00\x00\x18\x00\x03\x00\x00\x00\x14\x00\x00\x00\x94\x02\x00\x00\x84\x02\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\xac\x02\x00\x00\xfc\xff\xff\xff\x14\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x08\x00\x0c\x00\x04\x00\x08\x00\x08\x00\x00\x00\x08\x00\x00\x00\x0f\x00\x00\x00\x4c\x02\x00\x00\x40\x02\x00\x00\x34\x02\x00\x00\x28\x02\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x01\x00\x00\x00\x10\x00\x00\x00\x0c\x00\x00\x00\x06\x00\x00\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x04\x00\x00\x00\x84\x00\x00\x00\x04\x00\x00\x00\x18\x01\x00\x00\x01\x00\x00\x00\x10\x00\x00\x00\x80\x00\x00\x00\x04\x00\x00\x00\x84\x01\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x80\x00\x00\x00\x04\x00\x00\x00\x04\x02\x00\x00\x02\x00\x00\x00\x10\x00\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00\x80\xbf\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00\x00\xfc\xff\xff\xff\x04\x00\x00\x00\x10\x00\x00\x00serving_default_x:0\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x08\x00\x0c\x00\x04\x00\x08\x00\x04\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x0c\x00\x10\x00\x0c\x00\x0c\x00\x08\x00\x04\x00\x0c\x00\x00\x00\x10\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x18\x00\x00\x00StatefulPartitionedCall:0\x00\xfc\xff\xff\xff\x04\x00\x00\x00\x08\x00\x00\x00dense_1\x00\xfc\xff\xff\xff\x0c\x00\x00\x00\x04\x00\x00\x00\x0f\x00\x00\x00\xfc\xff\xff\xff\x18\x00\x00\x00\x04\x00\x00\x00\x13\x00\x00\x00\xfc\xff\xff\xff\x04\x00\x00\x00\x08\x00\x00\x00dense_2\x00\x00\xd9\x1e\x96=\xfc\t\x81\xbd\xfb(R\xbe\xe3\xd8\x12>\xf2\x94\xf7\xbd\xecY\x06>\xccQ\xfc\xbd7\x0b\xb2>\xfd\xf1\x0c\xbe \xc6/>\xfd\x12\xb9\xbd]\xde\xcc<\x8fA(=\xfc\xff\xff\xff\x7f\x8e\x9c\xbc\x0e\x84\x13\xbc\x0c\x8e\xc5\xbc\xcd\xd3\x91\xbc\xd1o\x1a\xbc\xf7[\x18<\xc6!\xa6\xbc\x86/\x15\xbc\xfc\xff\xff\xff3]\xd5=\xe1\xb6N=\xd5\x9f\xa4\xbd\xef\x03*>\xde!\xdc=\xe1\x88(>\x13P\xf6\xbd\x10\xc6\x0c>\xd0;\xba\xbd\xdf\xd8\xa8=\xe4\xf8?>\xe8k\xd2\xbd\xde\xc2\xc1=\xd7\xee\x96=\xd2\xb5\xdd=\xda\xea\xab=')
# The amount of memory to give the model.
# This value is found through trial and error.
TENSOR_ARENA_SIZE = 2 * 1024
print("--- Sine Wave Prediction with TFLite ---")
# 1. Initialize the model
try:
sine_model = TFLite(sine_model_data, TENSOR_ARENA_SIZE)
print("Model initialized successfully.")
except Exception as e:
print("Failed to initialize model!")
print(e)
# Stop the script if model fails
raise SystemExit
# 2. Set up a loop to run predictions
position = 0.0
while True:
# Calculate the input angle 'x'. It will loop from 0 to 2*PI.
x_val = position * math.pi
# 3. Provide the input to the model
# The 'f' means we are passing a float
sine_model.input(x_val, "f")
# 4. Run the model to get a prediction
sine_model.invoke()
# 5. Get the output from the model
predicted_y = sine_model.output("f")
# Calculate the actual sine value for comparison
actual_y = math.sin(x_val)
# Print the results
print("Input: {:.2f}, Predicted: {:.4f}, Actual: {:.4f}".format(x_val, predicted_y, actual_y))
# Increment position for the next loop
position += 0.1
if position > 2.0:
position = 0.0
utime.sleep_ms(500)