{"id":3686,"date":"2025-06-18T15:23:47","date_gmt":"2025-06-18T07:23:47","guid":{"rendered":"https:\/\/saludpcb.com\/zh\/?p=3686"},"modified":"2025-09-26T06:15:35","modified_gmt":"2025-09-25T22:15:35","slug":"aiot-esp32-and-edge-ai","status":"publish","type":"post","link":"https:\/\/saludpcb.com\/zh\/aiot-esp32-and-edge-ai\/","title":{"rendered":"\u6253\u9020 AIoT \u8d85\u7d1a\u5927\u8166 | \u7528 ESP32 \u548c TensorFlow Lite Micro \u5be6\u73fe\u908a\u7de3 AI"},"content":{"rendered":"\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p><strong>AIoT<\/strong>\uff08\u4eba\u5de5\u667a\u6167\u7269\u806f\u7db2\uff09\u6b63\u6539\u8b8a\u6211\u5011\u7684\u751f\u6d3b\uff0c\u800c\u8981\u8b93\u667a\u6167\u88dd\u7f6e\u5177\u5099\u5373\u6642\u5224\u65b7\u80fd\u529b\uff0c\u300c\u908a\u7de3 AI\u300d\u6210\u70ba\u95dc\u9375\u6280\u8853\u3002\u908a\u7de3 AI \u8b93\u8cc7\u6599\u4e0d\u9700\u56de\u50b3\u96f2\u7aef\uff0c\u80fd\u5728\u88dd\u7f6e\u7aef\u5373\u6642\u5206\u6790\uff0c\u964d\u4f4e\u5ef6\u9072\u4e26\u63d0\u5347\u96b1\u79c1\u5b89\u5168\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u6211\u5011\u5c31\u4f86\u5be6\u6230\u6559\u5b78\uff0c\u5982\u4f55\u4f7f\u7528 <strong>ESP32<\/strong> \u642d\u914d <strong>TensorFlow Lite Micro\uff08TFLM\uff09<\/strong>\uff0c\u6253\u9020\u5c6c\u65bc\u4f60\u7684 AIoT \u8d85\u7d1a\u5927\u8166\uff0c\u5be6\u73fe\u771f\u6b63\u7684\u908a\u7de3 AI \u61c9\u7528\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2025\/06\/Supercharge-Your-AIoT-Projects-with-ESP32-and-TensorFlow-Lite-Micro-Unlock-the-Power-of-Edge-AI.jpg\" alt=\"AIoT\" class=\"wp-image-3708\" title=\"\" srcset=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2025\/06\/Supercharge-Your-AIoT-Projects-with-ESP32-and-TensorFlow-Lite-Micro-Unlock-the-Power-of-Edge-AI.jpg 1024w, https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2025\/06\/Supercharge-Your-AIoT-Projects-with-ESP32-and-TensorFlow-Lite-Micro-Unlock-the-Power-of-Edge-AI-768x768.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>\u5167\u5bb9<\/h2><nav><ul><li class=\"\"><a href=\"#\u4ec0\u9ebc\u662f-a-io-t\uff1f\">\u4ec0\u9ebc\u662f AIoT\uff1f<\/a><\/li><li class=\"\"><a href=\"#\u4ec0\u9ebc\u662f-lvgl-render\uff1f\">\u4ec0\u9ebc\u662f ESP32 \u8207 TFLM\uff1f<\/a><\/li><li class=\"\"><a href=\"#esp-32-\u7684-spi-dma-\u539f\u7406\u7c21\u4ecb\">\u70ba\u4ec0\u9ebc\u9078\u64c7 ESP32 \u548c TFLM\uff1f<\/a><\/li><li class=\"\"><a href=\"#\u74b0\">\u958b\u767c\u74b0\u5883<\/a><\/li><li class=\"\"><a href=\"#\u5c08\">AIoT \u5c08\u6848\u7d50\u69cb<\/a><\/li><li class=\"\"><a href=\"#\u5c08\u6848\u7d50\u69cb\">\u74b0\u5883\u6e96\u5099<\/a><\/li><li class=\"\"><a href=\"#c\">Code<\/a><\/li><li class=\"\"><a href=\"#\u4f7f\u7528-python-tensor-flow-\u5efa\u7acb\u7c21\u55ae\u6a21\u578b\uff08\u5206\u985e\u4efb\u52d9\uff09\">\u4f7f\u7528 Python + TensorFlow \u5efa\u7acb\u7c21\u55ae\u6a21\u578b<\/a><\/li><li class=\"\"><a href=\"#\u7de8\u8b6f\u548c\u71d2\u9304-1\">\u7de8\u8b6f\u548c\u71d2\u9304<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8ad6\">\u7d50\u8ad6<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4ec0\u9ebc\u662f-a-io-t\uff1f\">\u4ec0\u9ebc\u662f AIoT\uff1f<\/h2>\n\n\n\n<p>AIoT \u662f\u300cArtificial Intelligence of Things\uff08\u4eba\u5de5\u667a\u6167\u7269\u806f\u7db2\uff09\u300d\u7684\u7e2e\u5beb\uff0c\u7d50\u5408\u4e86 <strong>AI\uff08\u4eba\u5de5\u667a\u6167\uff09<\/strong> \u548c <strong>IoT\uff08\u7269\u806f\u7db2\uff09<\/strong> \u5169\u5927\u6280\u8853\uff0c\u8b93\u88dd\u7f6e\u4e0d\u53ea\u662f\u9023\u7db2\u50b3\u8cc7\u6599\uff0c\u66f4\u80fd\u300c\u81ea\u4e3b\u5224\u65b7\u300d\u8207\u300c\u5373\u6642\u53cd\u61c9\u300d\u3002<\/p>\n\n\n\n<p>\u8209\u4f8b\u4f86\u8aaa\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u50b3\u7d71 IoT\uff1a\u611f\u6e2c\u5668\u6536\u96c6\u8cc7\u6599 \u2192 \u50b3\u9001\u5230\u96f2\u7aef \u2192 \u7b49\u5f85\u4f3a\u670d\u5668\u5206\u6790 \u2192 \u56de\u50b3\u7d50\u679c<\/li>\n\n\n\n<li>AIoT\uff1a\u611f\u6e2c\u5668\u6536\u96c6\u8cc7\u6599 \u2192 <strong>\u5728\u88dd\u7f6e\u7aef\u5373\u6642\u63a8\u8ad6\u8207\u6c7a\u7b56<\/strong> \u2192 \u7576\u4e0b\u56de\u61c9<\/li>\n<\/ul>\n\n\n\n<p><strong>AIoT<\/strong> \u7684\u61c9\u7528\u9818\u57df\u975e\u5e38\u5ee3\uff0c\u5305\u62ec\u667a\u6167\u5bb6\u5c45\u3001\u5de5\u696d\u81ea\u52d5\u5316\u3001\u91ab\u7642\u76e3\u63a7\u3001\u667a\u6167\u4ea4\u901a\u7b49\u3002\u800c\u300c\u908a\u7de3 AI\u300d\u6280\u8853\u6b63\u662f\u5be6\u73fe AIoT \u7684\u6838\u5fc3\uff0c\u56e0\u70ba\u5b83\u80fd\u8b93 AI \u6f14\u7b97\u6cd5\u76f4\u63a5\u5728\u88dd\u7f6e\u7aef\u904b\u884c\uff0c\u4e0d\u518d\u4f9d\u8cf4\u96f2\u7aef\uff0c\u5927\u5e45\u964d\u4f4e\u5ef6\u9072\u4e26\u63d0\u5347\u6548\u7387\u8207\u96b1\u79c1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4ec0\u9ebc\u662f-lvgl-render\uff1f\">\u4ec0\u9ebc\u662f ESP32 \u8207 TFLM\uff1f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ESP32<\/strong>\uff1a<strong>\u8d85\u5f37 AIoT \u6676\u7247<\/strong><br>ESP32 \u662f Espressif \u63a8\u51fa\u7684\u96d9\u6838 Wi-Fi + \u85cd\u7259 SoC\uff08\u7cfb\u7d71\u55ae\u6676\u7247\uff09\uff0c\u529f\u80fd\u5f37\u5927\u4e14\u4f4e\u529f\u8017\uff0c\u662f\u73fe\u4eca\u7269\u806f\u7db2\u8a2d\u5099\u7684\u71b1\u9580\u9078\u64c7\u3002ESP32-S3 \u7248\u672c\u66f4\u52a0\u5165\u4e86 AI \u63a8\u8ad6\u52a0\u901f\u7684\u5411\u91cf\u6307\u4ee4\u96c6\uff0c\u8b93 TinyML \u7684\u61c9\u7528\u6210\u70ba\u53ef\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TensorFlow Lite Micro<\/strong>\uff1a<strong>\u70ba MCU \u8a2d\u8a08\u7684\u8f15\u91cf AI \u6846\u67b6<\/strong><br>TensorFlow Lite Micro \u662f Google TensorFlow \u5718\u968a\u63a8\u51fa\u7684\u8f15\u91cf\u7d1a AI \u63a8\u8ad6\u6846\u67b6\uff0c\u5c08\u9580\u91dd\u5c0d\u8cc7\u6e90\u6709\u9650\u7684\u5fae\u63a7\u5236\u5668\uff08MCU\uff09\u8a2d\u8a08\u3002\u5b83\u53ef\u4ee5\u8b93\u4f60\u5c07\u8a13\u7df4\u597d\u7684\u6a5f\u5668\u5b78\u7fd2\u6a21\u578b\uff0c\u8f49\u63db\u6210\u9069\u5408 MCU \u904b\u884c\u7684\u683c\u5f0f\uff0c\u76f4\u63a5\u5728 ESP32 \u4e0a\u904b\u884c\uff0c\u5be6\u73fe\u7121\u9700\u96f2\u7aef\u7684 AI \u63a8\u8ad6\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp-32-\u7684-spi-dma-\u539f\u7406\u7c21\u4ecb\">\u70ba\u4ec0\u9ebc\u9078\u64c7 ESP32 \u548c TFLM\uff1f <\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5373\u6642\u904b\u7b97<\/strong>\uff1a\u6e1b\u5c11\u96f2\u7aef\u4f9d\u8cf4\uff0c\u964d\u4f4e\u7db2\u8def\u5ef6\u9072<\/li>\n\n\n\n<li><strong>\u7bc0\u7701\u6210\u672c<\/strong>\uff1a\u7701\u53bb\u983b\u5bec\u8207\u96f2\u7aef\u8cc7\u6e90\u8cbb\u7528<\/li>\n\n\n\n<li><strong>\u5f37\u5927\u751f\u614b<\/strong>\uff1aESP-IDF \u8207 esp-tflite-micro \u5b98\u65b9\u7d44\u4ef6\u5b8c\u7f8e\u6574\u5408<\/li>\n\n\n\n<li><strong>\u4f4e\u529f\u8017<\/strong>\uff1a\u9069\u5408\u96fb\u6c60\u4f9b\u96fb\u7684\u667a\u6167\u88dd\u7f6e<\/li>\n\n\n\n<li><strong>\u64f4\u5c55\u6027\u5f37<\/strong>\uff1a\u652f\u63f4\u5404\u7a2e\u611f\u6e2c\u5668\u8207\u901a\u8a0a\u5354\u5b9a<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u74b0\">\u958b\u767c\u74b0\u5883<\/h2>\n\n\n\n<p>\u5728\u958b\u59cb\u7de8\u7a0b\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u5df2\u5b8c\u6210\u4ee5\u4e0b\u6e96\u5099\u5de5\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/saludpcb.com\/zh\/esp32-tutorial-how-to-create-an-esp32-project-with-vscode\/\">\u5b89\u88dd ESP-IDF \u958b\u767c\u74b0\u5883<\/a> (\u81f3\u5c11\u7248\u672c v5.x \u6216\u66f4\u9ad8)\u3002<\/li>\n\n\n\n<li><a href=\"https:\/\/saludpcb.com\/zh\/esp32-devkit-module-pinout\/\">ESP32 \u958b\u767c\u677f<\/a>\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5c08\">AIoT \u5c08\u6848\u7d50\u69cb<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u793a\u7bc4\u5982\u4f55\u5efa\u7acb\u4e00\u500b\u4e7e\u6de8\u7684 esp32_tflm_demo AIoT \u5c08\u6848\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">esp32_tflm_demo\/\n\u251c\u2500\u2500 CMakeLists.txt               \u2190 \u9802\u5c64 CMake \u6a94\uff0c\u544a\u8a34 IDF \u5c08\u6848\u5728\u54ea\u88e1\n\u251c\u2500\u2500 Makefile                     \u2190 \u53ef\u7701\u7565\uff0c\u50c5\u70ba\u76f8\u5bb9\u6027\u5b58\u5728\n\u251c\u2500\u2500 sdkconfig                    \u2190 build \u6642\u81ea\u52d5\u751f\u6210\n\u251c\u2500\u2500 sdkconfig.defaults           \u2190 \u53ef\u9078\uff1a\u9810\u8a2d config \u8a2d\u5b9a\n\u251c\u2500\u2500 idf_component.yml            \u2190 \u5ba3\u544a\u5143\u4ef6\u4f9d\u8cf4\uff0c\u5982 esp-tflite-micro\n\u2502\n\u251c\u2500\u2500 main\/                        \u2190 \u4e3b\u61c9\u7528\u7a0b\u5f0f\n\u2502   \u251c\u2500\u2500 esp32_tflm_demo.cpp.     \u2190 \u7a0b\u5f0f\u4e3b\u9ad4\uff0c\u521d\u59cb\u5316\u8207\u63a8\u8ad6\u908f\u8f2f\n\u2502   \u251c\u2500\u2500 model_data.h             \u2190 \u7528 xxd -i \u7522\u751f\u7684\u6a21\u578b C \u9663\u5217\n\u2502   \u2514\u2500\u2500 CMakeLists.txt           \u2190 \u8a3b\u518a\u5143\u4ef6\u8207\u4f9d\u8cf4\u7684\u8a2d\u5b9a\n\u2502\n\u251c\u2500\u2500 managed_components\/          \u2190 v5.x \u5f9e\u9019\u88e1\u4e0b\u8f09\u5143\u4ef6\uff08\u81ea\u52d5\u7522\u751f\uff09\n\u2502   \u2514\u2500\u2500 espressif__esp-tflite-micro\/\n\u2502       \u2514\u2500\u2500 tensorflow\/\n\u2502           \u2514\u2500\u2500 lite\/\n\u2502               \u251c\u2500\u2500 micro\/\n\u2502               \u251c\u2500\u2500 schema\/\n\u2502               \u2514\u2500\u2500 ...\n\u2502\n\u251c\u2500\u2500 build\/                       \u2190 \u57f7\u884c build \u5f8c\u7522\u751f\u7684\u5efa\u69cb\u8f38\u51fa\n\u2514\u2500\u2500 components\/                  \u2190 \u53ef\u9078\uff1a\u4f60\u81ea\u5b9a\u7fa9\u7684\u672c\u5730\u7d44\u4ef6\u6703\u653e\u9019\u88e1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5c08\u6848\u7d50\u69cb\">\u74b0\u5883\u6e96\u5099<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u88dd ESP-IDF<\/strong><br>ESP-IDF \u662f ESP32 \u7684\u5b98\u65b9\u958b\u767c\u6846\u67b6\uff0c\u8acb\u53c3\u8003 <a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/v5.4.1\/esp32\/get-started\/index.html\" target=\"_blank\" rel=\"noopener\">ESP-IDF \u5b98\u65b9\u6559\u5b78<\/a> \u5b8c\u6210\u5b89\u88dd\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">git clone --recursive https:\/\/github.com\/espressif\/esp-idf.git\ncd esp-idf\n.\/install.sh\n. .\/export.sh<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u65b0\u5efa\u5c08\u6848\u4e26\u52a0\u5165 esp-tflite-micro<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">idf.py create-project esp32_tflm_demo\ncd esp32_tflm_demo\nidf.py add-dependency \"espressif\/esp-tflite-micro=*\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4fee\u6539 <code>main\/CMakeLists.txt<\/code> \u52a0\u5165\u7d44\u4ef6\u4f9d\u8cf4<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">idf_component_register(\n    SRCS \"main.c\"\n    INCLUDE_DIRS \".\"\n    REQUIRES esp-tflite-micro\n)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c\">Code<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 TFLM \u63a8\u8ad6\u7bc4\u4f8b\uff0c\u6a21\u64ec\u5169\u500b\u6d6e\u9ede\u6578\u8f38\u5165\u4e26\u8f38\u51fa\u5224\u65b7\u7d50\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#include \"esp_log.h\"\n#include \"tensorflow\/lite\/micro\/micro_interpreter.h\"\n#include \"tensorflow\/lite\/micro\/micro_mutable_op_resolver.h\"\n#include \"tensorflow\/lite\/schema\/schema_generated.h\"\n#include \"tensorflow\/lite\/version.h\"\n#include \"model_data.h\"  \/\/ Model data in C array format (generated from .tflite using xxd)\n\nstatic const char *TAG = \"TFLM_DEMO\";\n\n\/\/ Allocate memory for tensors (working memory for TFLM)\nconstexpr int tensor_arena_size = 8 * 1024;\nstatic uint8_t tensor_arena[tensor_arena_size];\n\nvoid app_main(void) {\n    ESP_LOGI(TAG, \"Starting TensorFlow Lite Micro\");\n\n    \/\/ Load the model from the compiled array\n    const tflite::Model* model = tflite::GetModel(g_model_tflite);\n    if (model-&gt;version() != TFLITE_SCHEMA_VERSION) {\n        ESP_LOGE(TAG, \"Model schema version mismatch\");\n        return;\n    }\n\n    \/\/ Register the necessary operators used in the model\n    tflite::MicroMutableOpResolver&lt;4&gt; resolver;\n    resolver.AddFullyConnected();\n    resolver.AddRelu();\n    resolver.AddLogistic();\n\n    \/\/ Create the interpreter\n    tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, tensor_arena_size);\n    if (interpreter.AllocateTensors() != kTfLiteOk) {\n        ESP_LOGE(TAG, \"Tensor allocation failed\");\n        return;\n    }\n\n    \/\/ Get pointers to the model's input and output tensors\n    TfLiteTensor* input = interpreter.input(0);\n    TfLiteTensor* output = interpreter.output(0);\n\n    \/\/ Run inference in a loop\n    while (1) {\n        \/\/ Generate two random input values between 0.0 and 1.0\n        float x1 = (float)(rand() % 100) \/ 100.0f;\n        float x2 = (float)(rand() % 100) \/ 100.0f;\n\n        \/\/ Assign inputs to the model\n        input-&gt;data.f[0] = x1;\n        input-&gt;data.f[1] = x2;\n\n        \/\/ Perform inference\n        if (interpreter.Invoke() != kTfLiteOk) {\n            ESP_LOGE(TAG, \"Inference failed\");\n            continue;\n        }\n\n        \/\/ Read the prediction result\n        float pred = output-&gt;data.f[0];\n        ESP_LOGI(TAG, \"Input: %.2f, %.2f, Prediction: %.3f\", x1, x2, pred);\n\n        \/\/ Delay for readability\n        vTaskDelay(pdMS_TO_TICKS(2000));\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4f7f\u7528-python-tensor-flow-\u5efa\u7acb\u7c21\u55ae\u6a21\u578b\uff08\u5206\u985e\u4efb\u52d9\uff09\">\u4f7f\u7528 Python + TensorFlow \u5efa\u7acb\u7c21\u55ae\u6a21\u578b<\/h2>\n\n\n\n<p>\u7522\u751f\u4e00\u500b\u300c\u8a13\u7df4\u7c21\u55ae\u6a21\u578b \u2192 \u8f49\u6210 <code>.tflite<\/code> \u2192 \u6e96\u5099\u7d66 ESP32\u300d\u7684\u793a\u7bc4\u3002<\/p>\n\n\n\n<p>\u6253\u958b\u7d42\u7aef\u6a5f\u6216\u547d\u4ee4\u63d0\u793a\u5b57\u5143\uff0c\u57f7\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">pip install tensorflow numpy<\/code><\/pre>\n\n\n\n<p>\u64b0\u5beb\u4e26\u57f7\u884c Python \u7a0b\u5f0f\uff1a<code>train_and_convert.py<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">import tensorflow as tf\nimport numpy as np\n\n# 1. Prepare data (XOR problem, ideal for microcontrollers)\nX = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32)\ny = np.array([[0], [1], [1], [0]], dtype=np.float32)\n\n# 2. Build a simple neural network model\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Input(shape=(2,)),            # Input layer with 2 features\n    tf.keras.layers.Dense(4, activation='relu'),  # Hidden layer with 4 neurons and ReLU\n    tf.keras.layers.Dense(1, activation='sigmoid') # Output layer with 1 neuron and sigmoid\n])\n\n# 3. Compile the model with optimizer, loss function, and metrics\nmodel.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n\n# 4. Train the model on XOR data\nmodel.fit(X, y, epochs=100, verbose=0)\n\n# 5. Convert the trained Keras model to TensorFlow Lite format\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# 6. Save the converted model to a .tflite file\nwith open(\"model.tflite\", \"wb\") as f:\n    f.write(tflite_model)\n\nprint(\"Model successfully saved as model.tflite\")<\/code><\/pre>\n\n\n\n<p>\u57f7\u884c\u5f8c\u6703\u7522\u751f <code>model.tflite<\/code> \u6a94\u6848\u3002<\/p>\n\n\n\n<p>\u76f4\u63a5\u5728 macOS \u7684 Terminal\uff08\u7d42\u7aef\u6a5f\uff09\u4e2d\u57f7\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">xxd -i model.tflite &gt; model_data.h<\/code><\/pre>\n\n\n\n<p>\u9019\u6703\u628a\u4f60\u7684 TensorFlow Lite \u6a21\u578b <code>.tflite<\/code> \u6a94\u6848\uff0c\u8f49\u63db\u6210\u53ef\u5728 C\/C++ \u4e2d\u4f7f\u7528\u7684\u9663\u5217\u683c\u5f0f\uff0c\u4e26\u8f38\u51fa\u6210 <code>model_data.h<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f TensorFlow \u7248\u672c<\/strong><\/p>\n\n\n\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u4f60\u7684 TensorFlow \u7248\u672c\u8207\u7bc4\u4f8b\u76f8\u5bb9\u3002\u5efa\u8b70\u4f7f\u7528 TensorFlow 2.15.0 \u7248\u672c\uff0c\u907f\u514d\u56e0\u7248\u672c\u5dee\u7570\u5c0e\u81f4\u6a21\u578b\u8f49\u63db\u6216\u57f7\u884c\u932f\u8aa4\u3002\u4f60\u53ef\u4ee5\u900f\u904e\u4ee5\u4e0b\u6307\u4ee4\u5b89\u88dd\u6307\u5b9a\u7248\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"><code>pip uninstall tensorflow -y\npip install tensorflow==2.15.0<\/code><\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u6b63\u78ba\u7248\u672c\u7684 TensorFlow\uff0c\u6709\u52a9\u65bc\u9806\u5229\u5b8c\u6210\u6a21\u578b\u8a13\u7df4\u8207\u8f49\u63db\uff0c\u4e26\u78ba\u4fdd\u5728 ESP32 TensorFlow Lite Micro \u4e0a\u904b\u884c\u6642\u4e0d\u6703\u767c\u751f\u7248\u672c\u4e0d\u5339\u914d\u7684\u554f\u984c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7de8\u8b6f\u548c\u71d2\u9304-1\">\u7de8\u8b6f\u548c\u71d2\u9304<\/h2>\n\n\n\n<p>\u5b8c\u6210\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <a href=\"https:\/\/www.espressif.com\/en\/products\/sdks\/esp-idf\" target=\"_blank\" rel=\"noopener\">ESP-IDF<\/a> \u63d0\u4f9b\u7684\u547d\u4ee4\u9032\u884c\u7de8\u8b6f\u3001\u71d2\u9304\u548c\u76e3\u63a7\u3002<\/p>\n\n\n\n<p>\u5728 VS Code \u7684\u5de6\u4e0b\u89d2 ESP-IDF \u5de5\u5177\u5217\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ede\u9078 <strong>Build project<\/strong><\/li>\n\n\n\n<li>\u9ede\u9078 <strong>Flash device<\/strong><\/li>\n\n\n\n<li>\u9ede\u9078 <strong>Monitor device<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u7a0b\u5f0f\u555f\u52d5\u5f8c\uff0c\u53ef\u4ee5\u5f9e ESP_Log \u67e5\u770b\u8f38\u51fa\u7d50\u679c :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">I (282) TFLM_DEMO: Starting TensorFlow Lite Micro\nI (282) TFLM_DEMO: Input: 0.33, 0.43, Prediction: 0.422\nI (2282) TFLM_DEMO: Input: 0.62, 0.29, Prediction: 0.434\nI (4282) TFLM_DEMO: Input: 0.00, 0.08, Prediction: 0.522\nI (6282) TFLM_DEMO: Input: 0.52, 0.56, Prediction: 0.374\nI (8282) TFLM_DEMO: Input: 0.56, 0.19, Prediction: 0.465\nI (10282) TFLM_DEMO: Input: 0.11, 0.51, Prediction: 0.436\nI (12282) TFLM_DEMO: Input: 0.43, 0.05, Prediction: 0.519\nI (14282) TFLM_DEMO: Input: 0.08, 0.93, Prediction: 0.366\nI (16282) TFLM_DEMO: Input: 0.30, 0.66, Prediction: 0.385\nI (18282) TFLM_DEMO: Input: 0.69, 0.32, Prediction: 0.422\nI (20282) TFLM_DEMO: Input: 0.17, 0.47, Prediction: 0.436\nI (22282) TFLM_DEMO: Input: 0.72, 0.68, Prediction: 0.329\nI (24282) TFLM_DEMO: Input: 0.80, 0.23, Prediction: 0.441<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8ad6\">\u7d50\u8ad6<\/h2>\n\n\n\n<p>\u900f\u904e <a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\" rel=\"noopener\">TensorFlow<\/a> Lite Micro\uff0c\u6211\u5011\u6210\u529f\u5c07\u4e00\u500b\u8a13\u7df4\u597d\u7684\u795e\u7d93\u7db2\u8def\u6a21\u578b\u90e8\u7f72\u5230\u8cc7\u6e90\u6709\u9650\u7684 ESP32 \u4e0a\uff0c\u5be6\u73fe\u771f\u6b63\u7684\u908a\u7de3 AI \u63a8\u8ad6\u3002\u6574\u500b\u6d41\u7a0b\u5f9e\u6a21\u578b\u8a13\u7df4\u3001\u8f49\u63db\u70ba <code>.tflite<\/code>\u3001\u5d4c\u5165\u70ba C \u9663\u5217\uff0c\u518d\u5230\u4f7f\u7528 <code>esp_log<\/code> \u5217\u5370\u9810\u6e2c\u7d50\u679c\uff0c\u5168\u7a0b\u4e0d\u4f9d\u8cf4\u4efb\u4f55\u5916\u90e8\u611f\u6e2c\u5668\u6216\u96f2\u7aef\u8cc7\u6e90\uff0c\u662f AIoT \u958b\u767c\u7684\u7d55\u4f73\u5165\u9580\u7bc4\u4f8b\u3002<\/p>\n\n\n\n<p>\u9019\u4e0d\u53ea\u662f\u5c55\u793a\u6280\u8853\u53ef\u80fd\uff0c\u66f4\u8c61\u5fb5\u8457\u300cAI for everyone, everywhere\u300d\u7684\u7cbe\u795e\u3002\u73fe\u5728\uff0c\u4f60\u53ea\u9700\u8981\u4e00\u584a ESP32 \u548c\u4e00\u9ede\u71b1\u60c5\uff0c\u5c31\u80fd\u958b\u59cb\u6253\u9020\u4f60\u7684 AIoT \u8d85\u7d1a\u5927\u8166\uff01<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6e96\u5099\u597d\u4e86\uff0c\u4e0b\u4e00\u6b65\u53ef\u4ee5\u8a66\u8a66\u52a0\u5165\u611f\u6e2c\u5668\u3001\u7db2\u8def\u9023\u7dda\u6216\u5be6\u4f5c\u4f60\u7684\u81ea\u8a02\u6a21\u578b\uff0c\u8b93 ESP32 \u4e0d\u53ea\u662f\u6703\u904b\u7b97\uff0c\u66f4\u6703\u300c\u601d\u8003\u300d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AIoT\uff08\u4eba\u5de5\u667a\u6167\u7269\u806f\u7db2\uff09\u6b63\u6539\u8b8a\u6211\u5011\u7684\u751f\u6d3b\uff0c\u800c\u8981\u8b93\u667a\u6167\u88dd\u7f6e\u5177\u5099\u5373\u6642\u5224\u65b7\u80fd\u529b\uff0c\u300c\u908a\u7de3 AI\u300d\u6210\u70ba\u95dc\u9375\u6280\u8853\u3002\u908a\u7de3  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3708,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,53,83],"tags":[27,21,44,9,11],"class_list":["post-3686","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","category-esp32","category-featured","tag-c","tag-ide","tag-programming-language","tag-python","tag-tutorial"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/3686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/comments?post=3686"}],"version-history":[{"count":20,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/3686\/revisions"}],"predecessor-version":[{"id":3933,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/3686\/revisions\/3933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media\/3708"}],"wp:attachment":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media?parent=3686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/categories?post=3686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/tags?post=3686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}