--- title: 'ESP32' disqus: hackmd --- ESP32 === ###### tags: `ESP32` ## 目錄 [TOC] ## :memo: ESP32 ![](https://i.imgur.com/uffxVKs.jpg) :::info 購買連結:[ NodeMCU-32S Lua WiFi](https://www.taiwaniot.com.tw/product/nodemcu-32s-lua-wifi/) 售價:333元 ::: ### GPIO ![](https://i.imgur.com/N2Z2QDa.png) :::info * 16 x Analog-to-Digital:可接類比或數位感測(亮綠色標示) * 3 x SPI interfaces(MOSI,MISO,SCK,SS):高速資料傳輸 SD card, TFT, RFID(右邊橘色標示) * 3 x UART interfaces:藍芽、相機(不限腳位編號) * 2 x I2C interfaces(SDA,SCL) and I2S interfaces:LCD、氣壓、陀螺儀(GPIO 21,22) * 16 x PWM output channels:數位輸出也可類比輸出(不限定腳位編號,但需要宣告channel) * 2 x Digital-to-Analog Converters (DAC):不太需要用 * 10 x Capacitive sensing GPIOs:提供觸控電容(紫色標示) * GPIO 34,35,36,39:Input only(不能作為輸出腳位) * GPIO 0, 6, 7, 8, 9, 10, 11:系統用,勿使用 * GPIO 18:重開機 * 還有一些個人遇到的問題(不清楚原因,但會搞死你) 1. DHT11不可以用 GPIO 05、26 2. GPIO 02, 12 燒錄時不可接任何裝置,請空接,燒錄完成後,再接回,否則會上傳失敗。 3. WiFi啟動後,02, 04, 12, 13, 14, 15, 25, 26, 27 僅能數位讀取,不可類比 * [ESP32 Pinout Reference: Which GPIO pins should you use?](https://randomnerdtutorials.com/esp32-pinout-reference-gpios/) ::: ### 安裝ESP32核心程式 :::info 開啟Arduino IDE後,選擇功能表的 檔案 > 偏好設定 > 額外的開發版管理員網址 新增下方網址 ``` http://arduino.esp8266.com/stable/package_esp8266com_index.json https://dl.espressif.com/dl/package_esp32_index.json ``` ![](https://i.imgur.com/yFlxGLE.jpg =75%x) ::: :::info 工具 > 開發板 > 開發板管理員 搜尋 ESP8266 ![](https://i.imgur.com/YzET8qN.png) ![](https://i.imgur.com/LgZNjzZ.png) ``` 工具 > 開發板 > ESP8266 Boards > NodeMCU 1.0 (ESP-12E Module) ``` ![](https://i.imgur.com/5VikJNb.png) ::: ## :memo: [ESP32 MQTT](https://hackmd.io/@gn01743915/H1RWYY9Y_) ### Step 1: 安裝 Libraries – PubSubClient :::info 草稿碼 > 匯入程式庫 > 管理程式庫 Sketch > Include Library > Manage Library 搜尋 : PubSubClient (by Nick O'Leary) ::: ![](https://i.imgur.com/dfGziCm.png) ![](https://i.imgur.com/AG1FQub.png) ### Step 2: Codeing ```arduino=0 #include <ESP8266WiFi.h> #include <PubSubClient.h> const char* ssid = "REPLACE_WITH_YOUR_SSID"; //更改為您的 WiFi 帳號 const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //更改為您的 WiFi 密碼 const char* mqtt_server = "192.168.2.112"; const char* pub_topic = "SYSID_001"; const char* sub_topic = "SYSID_002"; WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int GPIOpin[] = {25, 26, 27}; void setup() { Serial.begin(115200); setup_wifi(); delay(50); client.setServer(mqtt_server, 1883); client.setCallback(callback); delay(50); pinMode(GPIOpin[0],OUTPUT); pinMode(GPIOpin[1],OUTPUT); pinMode(GPIOpin[2],OUTPUT); delay(50); digitalWrite(GPIOpin[0],HIGH); digitalWrite(GPIOpin[1],HIGH); digitalWrite(GPIOpin[2],HIGH); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); delay(100); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); if (client.connect(clientId.c_str())) { Serial.println("connected"); client.publish(pub_topic, "ESP8266 connected"); client.subscribe(sub_topic); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 3 seconds"); delay(3000); } } } void setup_wifi() { Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); if ((char)payload[0] == '1') { digitalWrite(GPIOpin[0],LOW); delay(250); } else if((char)payload[0] == '2') { digitalWrite(GPIOpin[1],LOW); delay(250); } else if((char)payload[0] == '3') { digitalWrite(GPIOpin[2],LOW); delay(250); } digitalWrite(GPIOpin[0],HIGH); digitalWrite(GPIOpin[1],HIGH); digitalWrite(GPIOpin[2],HIGH); } ``` ### 使用方式 Publish 發佈至「SYSID_002」(可自行更改) 發佈 1, 2, 3 數字去控制繼電器開關 ![](https://i.imgur.com/3qRRUIK.png) ### 參考資料 :::info * [MQTT教學(六):使用PubSubClient程式庫開發Arduino MQTT應用](https://swf.com.tw/?p=1021) * [NodeMCU-32S 引腳說明書](http://www.1zlab.com/wiki/micropython-esp32/pins-and-gpio/) ::: ## :memo: ESP32 WebServer ### Step 1: 安裝 Libraries – ESP Async Web Server [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip) [AsyncTCP](https://github.com/me-no-dev/AsyncTCP/archive/master.zip) :::info 草稿碼 > 匯入程式庫 > 加入.ZIP程式庫 Sketch > Include Library > Add .zip Library ::: ### Step 2: Codeing ```arduino=0 #include "WiFi.h" #include "ESPAsyncWebServer.h" const char* ssid = "REPLACE_WITH_YOUR_SSID"; //更改為您的 WiFi 帳號 const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //更改為您的 WiFi 密碼 AsyncWebServer server(80); void setup(){ Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } Serial.println(WiFi.localIP()); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ int paramsNr = request->params(); Serial.println(paramsNr); for(int i=0;i<paramsNr;i++){ AsyncWebParameter* p = request->getParam(i); //接到電腦端的 Arduino 要顯示的訊息 Serial.print("Param name: "); Serial.println(p->name()); Serial.print("Param value: "); Serial.println(p->value()); Serial.println("------"); //接到電腦端的 Arduino 要顯示的訊息 } request->send(200, "text/plain", "message received"); //透過網址進入該Arduino後所接收的訊息 }); server.begin(); } void loop(){} ``` ### 使用方式 #### 傳送訊息方式 在網址後方新增 ?x=y x為第幾個聲音接收裝置 y為接收到的數值 ![](https://i.imgur.com/dogV3WJ.png) #### 電腦端 Arduino 收到的資訊 ![](https://i.imgur.com/shnl4ZI.png) ### 參考資料 :::info * [第一篇 ESP32 Arduino開發環境架設(取代Arduino UNO及ESP8266首選)](https://youyouyou.pixnet.net/blog/post/119410732) * [ESP32 Async Web Server – Control Outputs with Arduino IDE (ESPAsyncWebServer library)](https://randomnerdtutorials.com/esp32-async-web-server-espasyncwebserver-library) ::: ## :memo: ESP32 Mesh ### 架構 ![](https://i.imgur.com/pgX14Ho.png) ### 參考資料 :::info * [ESP32 WiFi MESH資料收發應用範例](https://zanrobot.com/arduinoesp32-nodemcu-32s/5671/) * [ESP-MESH with ESP32 and ESP8266: Getting Started (painlessMesh library)](https://randomnerdtutorials.com/esp-mesh-esp32-esp8266-painlessmesh/) ::: ## :memo: 降低功耗 ### 參考資料 :::info * [如何降低ESP8266的功耗?](https://www.yiboard.com/thread-1550-1-1.html) * [| ESP32 教學 | MicroPython | 讓 ESP32 超省電!低功耗 DeepSleep Mode | 210 |](https://jimirobot.tw/esp32-micropython-deepsleep-tutorial-210/) ::: ## :memo: Code ### 參考資料 :::info * [How to Wire and Program a Button](https://docs.arduino.cc/built-in-examples/digital/Button/) :::