{"id":341,"date":"2026-03-17T12:21:57","date_gmt":"2026-03-17T04:21:57","guid":{"rendered":"https:\/\/szddt.com\/?p=341"},"modified":"2026-03-17T12:21:58","modified_gmt":"2026-03-17T04:21:58","slug":"341","status":"publish","type":"post","link":"https:\/\/szddt.com\/?p=341","title":{"rendered":""},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u8def\u7531\u5668 IoT \u7f51\u5173\u590d\u523b\u6307\u5357 (X-WRT\/OpenWrt + eMMC)<\/h2>\n\n\n\n<p>JDCloud \u8def\u7531\u5668\uff08X-WRT \u7cfb\u7edf\uff09\u4e0a\u6784\u5efa\u4e86\u4e00\u4e2a\u9ad8\u6548\u3001\u7a33\u5b9a\u7684 <strong>\u5d4c\u5165\u5f0f IoT \u6570\u636e\u7f51\u5173<\/strong>\u3002<\/p>\n\n\n\n<p>\u5229\u7528 128GB \u7684 eMMC\uff0c\u6211\u4eec\u907f\u5f00\u4e86\u8def\u7531\u5668 Flash \u7a7a\u95f4\u4e0d\u8db3\u7684\u9650\u5236\uff0c\u5b9e\u73b0\u4e86\u6570\u636e\u7684\u672c\u5730\u6301\u4e45\u5316\u4e0e Web \u53ef\u89c6\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001 \u5df2\u5b8c\u6210\u7684\u64cd\u4f5c\u603b\u7ed3<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u5b58\u50a8\u7a7a\u95f4\u4f18\u5316 (eMMC \u9002\u914d)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8def\u5f84\u91cd\u5b9a\u5411<\/strong>\uff1a\u5c06 <code>apk<\/code> \u5305\u7ba1\u7406\u5668\u7684\u6570\u636e\u5e93\uff08<code>\/lib\/apk<\/code>\uff09\u548c\u7f13\u5b58\u8fc1\u79fb\u5230 eMMC\u3002<\/li>\n\n\n\n<li><strong>\u73af\u5883\u51c6\u5907<\/strong>\uff1a\u5728 <code>\/mnt\/emmc<\/code> \u4e0b\u5efa\u7acb\u4e86 <code>opt<\/code>\u3001<code>db<\/code>\u3001<code>www<\/code>\u3001<code>cgi-bin<\/code> \u7b49\u76ee\u5f55\uff0c\u786e\u4fdd\u7cfb\u7edf\u91cd\u542f\u540e\u914d\u7f6e\u4e0d\u4e22\u5931\uff08\u901a\u8fc7 <code>rc.local<\/code> \u81ea\u52a8\u5efa\u7acb\u8f6f\u94fe\u63a5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u8f6f\u4ef6\u73af\u5883\u90e8\u7f72<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5305\u7ba1\u7406\u5668\u5207\u6362<\/strong>\uff1a\u786e\u8ba4\u7cfb\u7edf\u4f7f\u7528 <code>apk<\/code> \u800c\u975e <code>opkg<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u6838\u5fc3\u7ec4\u4ef6\u5b89\u88c5<\/strong>\uff1a\u5b89\u88c5\u4e86\u975e\u52a0\u5bc6\u7248\u7684 <code>mosquitto-nossl<\/code>\uff08\u964d\u4f4e\u8d44\u6e90\u5360\u7528\uff09\u548c <code>sqlite3-cli<\/code>\uff08\u8f7b\u91cf\u7ea7\u6570\u636e\u5e93\uff09\u3002<\/li>\n\n\n\n<li><strong>Web \u670d\u52a1\u914d\u7f6e<\/strong>\uff1a\u5f00\u542f\u4e86 <code>uhttpd<\/code> \u7684 CGI \u524d\u7f00\u652f\u6301\uff08<code>\/cgi-bin<\/code>\uff09\uff0c\u5e76\u5efa\u7acb\u4e86 Web \u8f6f\u94fe\u63a5\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u6570\u636e\u94fe\u8def\u6253\u901a<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u521d\u59cb\u5316<\/strong>\uff1a\u5efa\u7acb\u4e86\u652f\u6301\u5386\u53f2\u66f2\u7ebf\uff08<code>sensor_history<\/code>\uff09\u548c\u5b9e\u65f6\u72b6\u6001\uff08<code>sensor_latest<\/code>\uff09\u7684 SQLite \u8868\u7ed3\u6784\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5316\u91c7\u96c6<\/strong>\uff1a\u7f16\u5199\u4e86 Shell \u811a\u672c\uff08\u5229\u7528 <code>mosquitto_sub<\/code> \u7ba1\u9053\u6d41\uff09\u5b9e\u65f6\u76d1\u542c MQTT \u6d88\u606f\u5e76\u81ea\u52a8\u5199\u5165\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>API \u5f00\u53d1<\/strong>\uff1a\u5b9e\u73b0\u4e86\u57fa\u4e8e CGI \u7684 JSON \u63a5\u53e3\uff0c\u4f9b\u524d\u7aef\u7f51\u9875\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001 MQTT \u5b9e\u73b0\u67b6\u6784\u56fe<\/h2>\n\n\n\n<p>\u5f53\u524d\u7cfb\u7edf\u7684\u903b\u8f91\u67b6\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u91c7\u96c6\u5c42 (Data Collection)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bbe\u5907\u7aef<\/strong>\uff1a\u6e29\u6e7f\u5ea6\u8ba1\u3001\u6545\u969c\u6307\u793a\u5668\u3001STM32\uff08GaN \u4fe1\u53f7\u6e90\uff09\u7b49\u901a\u8fc7 Wi-Fi\/\u4ee5\u592a\u7f51\u53d1\u9001 MQTT \u62a5\u6587\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u9898\u8bbe\u8ba1<\/strong>\uff1a\u91c7\u7528\u5206\u7ea7\u4e3b\u9898\uff08\u5982 <code>env\/temp<\/code> \u6216 <code>device\/current<\/code>\uff09\u6765\u533a\u5206\u4e0d\u540c\u8bbe\u5907\u548c\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5904\u7406\u5c42 (Processing &amp; Storage)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MQTT Broker (Mosquitto)<\/strong>\uff1a\u8d1f\u8d23\u6d88\u606f\u7684\u4e2d\u8f6c\u548c\u5206\u53d1\u3002<\/li>\n\n\n\n<li><strong>Data Bridge (Shell Script)<\/strong>\uff1a\u5145\u5f53\u201c\u4e2d\u95f4\u4ef6\u201d\uff0c\u5b83\u8ba2\u9605\u611f\u5174\u8da3\u7684\u4e3b\u9898\uff0c\u5c06\u63a5\u6536\u5230\u7684\u5f02\u6b65\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u7ed3\u6784\u5316\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>Storage (SQLite)<\/strong>\uff1a\u5c06\u6570\u636e\u7269\u7406\u5b58\u50a8\u5728 128GB eMMC \u4e2d\u3002SQLite \u7684 ACID \u7279\u6027\u4fdd\u8bc1\u4e86\u5373\u4f7f\u5728\u65ad\u7535\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u8bb0\u5f55\u4e5f\u4e0d\u4f1a\u635f\u574f\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5c55\u793a\u5c42 (Visualization)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Web Server (uhttpd)<\/strong>\uff1a\u5904\u7406 HTTP \u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><strong>CGI Interface<\/strong>\uff1a\u8d1f\u8d23\u8fde\u63a5 Web \u548c\u6570\u636e\u5e93\uff0c\u6267\u884c SQL \u67e5\u8be2\u5e76\u5c06\u7ed3\u679c\u8f6c\u5316\u4e3a JSON \u683c\u5f0f\u3002<\/li>\n\n\n\n<li><strong>Dashboard (HTML5 + ECharts)<\/strong>\uff1a\u7528\u6237\u6d4f\u89c8\u5668\u8fd0\u884c JavaScript\uff0c\u5f02\u6b65\u6293\u53d6 JSON \u6570\u636e\u5e76\u7ed8\u5236\u51fa\u5e73\u6ed1\u7684\u7535\u6d41\u3001\u6e29\u5ea6\u5386\u53f2\u66f2\u7ebf\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001 \u67b6\u6784\u7684\u4f18\u52bf<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9ad8\u53ef\u9760\u6027<\/strong>\uff1a128GB eMMC \u51e0\u4e4e\u53ef\u4ee5\u5b58\u50a8\u51e0\u4ebf\u6761\u8bb0\u5f55\uff0c\u5b8c\u5168\u4e0d\u7528\u62c5\u5fc3\u7a7a\u95f4\u6ee1\u3002<\/li>\n\n\n\n<li><strong>\u4f4e\u8026\u5408<\/strong>\uff1a\u4f60\u53ef\u4ee5\u968f\u65f6\u589e\u52a0\u65b0\u7684\u4f20\u611f\u5668\uff0c\u53ea\u9700\u4fee\u6539 <code>mqtt_to_db.sh<\/code> \u91cc\u7684\u4e3b\u9898\u5339\u914d\u903b\u8f91\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u7a0b\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u96f6\u5916\u90e8\u4f9d\u8d56<\/strong>\uff1a\u6240\u6709\u6570\u636e\u90fd\u5728\u8def\u7531\u5668\u672c\u5730\u5904\u7406\uff0c\u4e0d\u9700\u8981\u4e0a\u4e91\uff0c\u4fdd\u62a4\u4e86\u5de5\u4e1a\/\u4e2a\u4eba\u6570\u636e\u7684\u79c1\u5bc6\u6027\uff0c\u4e14\u5728\u65ad\u7f51\u72b6\u6001\u4e0b\u4f9d\u7136\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff1aeMMC \u73af\u5883\u521d\u59cb\u5316<\/h2>\n\n\n\n<p>\u5728\u5237\u597d X-WRT \u56fa\u4ef6\u540e\uff0c\u9996\u5148\u5c06\u5b58\u50a8\u538b\u529b\u8f6c\u79fb\u5230 eMMC \u5206\u533a\u3002<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u521b\u5efa eMMC \u57fa\u7840\u76ee\u5f55\nmkdir -p \/mnt\/emmc\/apk\/lib_apk \/mnt\/emmc\/apk\/cache \/mnt\/emmc\/db \/mnt\/emmc\/www\/cgi-bin\n\n# 2. \u8fc1\u79fb apk \u6570\u636e\u5e93\u9632\u6b62\u7cfb\u7edf Flash \u6491\u7206\ncp -a \/lib\/apk \/mnt\/emmc\/apk\/lib_apk_backup\nrm -rf \/lib\/apk &amp;&amp; ln -s \/mnt\/emmc\/apk\/lib_apk \/lib\/apk\nrm -rf \/var\/cache\/apk &amp;&amp; ln -s \/mnt\/emmc\/apk\/cache \/var\/cache\/apk\n\n# 3. \u9a8c\u8bc1 apk \u662f\u5426\u53ef\u7528\napk update\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff1a\u6838\u5fc3\u8f6f\u4ef6\u5b89\u88c5<\/h2>\n\n\n\n<p>\u5378\u8f7d\u9ed8\u8ba4\u7684 SSL \u7248\u672c\uff08\u8282\u7701\u8d44\u6e90\uff0c\u89e3\u51b3\u51b2\u7a81\uff09\uff0c\u5b89\u88c5\u975e\u52a0\u5bc6\u7248\u3002<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u5207\u6362 Mosquitto \u7248\u672c\napk del mosquitto-ssl mosquitto-client-ssl\napk add mosquitto-nossl mosquitto-client-nossl\n\n# 2. \u5b89\u88c5\u6570\u636e\u5e93\u5de5\u5177\napk add libsqlite3-0 sqlite3-cli\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff1a\u6570\u636e\u5e93\u4e0e API \u914d\u7f6e<\/h2>\n\n\n\n<p>\u5efa\u7acb\u6570\u636e\u8868\u5e76\u6253\u901a Web \u63a5\u53e3\u3002<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u521d\u59cb\u5316\u6570\u636e\u5e93\u8868\u7ed3\u6784\nsqlite3 \/mnt\/emmc\/db\/iot.db &lt;&lt;EOF\nCREATE TABLE IF NOT EXISTS sensor_history (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    topic TEXT,\n    value REAL,\n    time DATETIME DEFAULT (datetime('now', 'localtime'))\n);\nCREATE TABLE IF NOT EXISTS device_events (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    device_id TEXT,\n    event_type TEXT,\n    description TEXT,\n    time DATETIME DEFAULT (datetime('now', 'localtime'))\n);\nEOF\n\n# 2. \u5f00\u542f Web \u670d\u52a1\u5668 CGI \u652f\u6301\nuci set uhttpd.main.cgi_prefix='\/cgi-bin'\nuci commit uhttpd\n\/etc\/init.d\/uhttpd restart\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff1a\u5199\u5165\u81ea\u52a8\u5316\u811a\u672c<\/h2>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u811a\u672c\u662f\u7cfb\u7edf\u7684\u6838\u5fc3\u201c\u6865\u6881\u201d\u3002<\/p>\n\n\n\n<p><strong>1. \u6570\u636e\u91c7\u96c6\u811a\u672c (<code>\/mnt\/emmc\/mqtt_to_db.sh<\/code>)<\/strong><\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &lt;&lt; 'EOF' &gt; \/mnt\/emmc\/mqtt_to_db.sh\n#!\/bin\/sh\nDB=\"\/mnt\/emmc\/db\/iot.db\"\nmosquitto_sub -t \"env\/#\" -t \"device\/#\" -v | while read -r line\ndo\n    TOPIC=$(echo $line | cut -d' ' -f1)\n    VAL=$(echo $line | cut -d' ' -f2-)\n    # \u5199\u5165\u5386\u53f2\u66f2\u7ebf\u8868\n    if echo \"$TOPIC\" | grep -Eq \"temp|humi|current\"; then\n        sqlite3 $DB \"INSERT INTO sensor_history (topic, value) VALUES ('$TOPIC', $VAL);\"\n    # \u5199\u5165\u4e8b\u4ef6\u65e5\u5fd7\u8868\n    elif echo \"$TOPIC\" | grep -Eq \"status|event\"; then\n        D_ID=$(echo $TOPIC | cut -d'\/' -f2)\n        sqlite3 $DB \"INSERT INTO device_events (device_id, event_type, description) VALUES ('$D_ID', 'EVENT', '$VAL');\"\n    fi\ndone\nEOF\nchmod +x \/mnt\/emmc\/mqtt_to_db.sh\n<\/code><\/pre>\n\n\n\n<p><strong>2. \u6570\u636e\u63a5\u53e3 API (<code>\/mnt\/emmc\/www\/cgi-bin\/data<\/code>)<\/strong><\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &lt;&lt; 'EOF' &gt; \/mnt\/emmc\/www\/cgi-bin\/data\n#!\/bin\/sh\necho \"Content-Type: application\/json\"\necho \"\"\nsqlite3 \/mnt\/emmc\/db\/iot.db \"SELECT json_group_array(json_object('time', time, 'topic', topic, 'val', value)) FROM (SELECT * FROM sensor_history ORDER BY id DESC LIMIT 100);\"\nEOF\nchmod +x \/mnt\/emmc\/www\/cgi-bin\/data\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e94\u6b65\uff1a\u8bbe\u7f6e\u5f00\u673a\u81ea\u542f<\/h2>\n\n\n\n<p>\u7f16\u8f91 <code>\/etc\/rc.local<\/code>\uff0c\u5728 <code>exit 0<\/code> \u4e4b\u524d\u52a0\u5165\uff1a<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5efa\u7acb Web \u76ee\u5f55\u6620\u5c04\nln -s \/mnt\/emmc\/www \/www\/iot\nmkdir -p \/www\/cgi-bin\nln -s \/mnt\/emmc\/www\/cgi-bin\/data \/www\/cgi-bin\/data\n\n# \u542f\u52a8\u6570\u636e\u91c7\u96c6\u540e\u53f0\n\/mnt\/emmc\/mqtt_to_db.sh &amp;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u67b6\u6784\u903b\u8f91\u56fe\u793a<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u7cfb\u7edf\u8fd0\u884c\u6d41\u7a0b\uff1a<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>MQTT \u7ec8\u7aef<\/strong>\uff1a\u53d1\u9001\u6570\u636e\u81f3\u8def\u7531\u5668 IP\u3002<\/li>\n\n\n\n<li><strong>Mosquitto<\/strong>\uff1a\u63a5\u6536\u6570\u636e\u6d41\u3002<\/li>\n\n\n\n<li><strong>Shell \u811a\u672c<\/strong>\uff1a\u4ece Mosquitto \u6293\u53d6\u6570\u636e\uff0c\u89e3\u6790\u540e\u901a\u8fc7 <code>sqlite3<\/code> \u547d\u4ee4\u63d2\u5165 eMMC \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u524d\u7aef\u7f51\u9875<\/strong>\uff1a\u7528\u6237\u8bbf\u95ee <code>\/iot\/index.html<\/code>\uff0cJS \u8c03\u7528 <code>\/cgi-bin\/data<\/code>\u3002<\/li>\n\n\n\n<li><strong>CGI \u63a5\u53e3<\/strong>\uff1a\u8bfb\u53d6\u6570\u636e\u5e93\u8fd4\u56de JSON\uff0c\u7531 ECharts \u5b8c\u6210\u7ed8\u56fe\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u8def\u7531\u5668 IoT \u7f51\u5173\u590d\u523b\u6307\u5357 (X-WRT\/OpenWrt + eMMC) JDCloud \u8def\u7531\u5668\uff08&hellip; <a href=\"https:\/\/szddt.com\/?p=341\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\"><\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":{"0":"post-341","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-4","7":"category-3","8":"h-entry","10":"h-as-article"},"_links":{"self":[{"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/szddt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341"}],"version-history":[{"count":1,"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/szddt.com\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/szddt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/szddt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/szddt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}