一、 Ubidots STEM 帳號設定
- 註冊 Ubidots 帳號:
- 訪問
並點擊 "Sign up for free" 或類似按鈕。https://ubidots.com/ - 選擇 "Ubidots STEM" 方案並完成註冊。
- 訪問
- 取得您的 API Token:
- 登入 Ubidots 帳號。
- 點擊右上角的用戶頭像,選擇 "API Credentials" (或 "My Profile" -> "API Credentials")。
- 您會看到一個 API Token,例如
BBFF-YOUR_UBIDOTS_TOKEN_HERE。將這個 Token 複製下來,稍後在程式碼中會用到。
- 建立一個 Device (設備):
- 在 Ubidots 介面左側導航欄,點擊 "Devices"。
- 點擊右上角的 "Add Device" (或 "+")。
- 選擇 "Blank Device" (或 "Create a new device")。
- 為您的設備命名,例如
MyESP32DHT22。您也可以給它一個描述。 - 建立後,您會看到這個設備的頁面。目前它沒有任何變數 (Variables)。當 ESP32 第一次發送數據時,Ubidots 會自動為您創建變數。
二、 硬體準備與接線
- ESP32 開發板: 任何 ESP32 開發板。
- DHT22 溫度濕度感測器: 如果是模組化的 DHT22,通常會有 VCC, GND, Data 三條線。如果不是模組化,則需要額外一個 10K 歐姆的拉高電阻 (pull-up resistor) 接在 Data 腳位和 VCC 之間。
- 麵包板 (Breadboard) 和杜邦線 (Jumper Wires)。
- USB 傳輸線: 用於連接 ESP32 到電腦。
接線圖 (Wiring Diagram):
- DHT22 VCC (或 +): 連接到 ESP32 的 3.3V 或 5V (DHT22 通常支持 3.3V-5V,但 ESP32 的 GPIO 是 3.3V)。
- DHT22 Data (或 Out): 連接到 ESP32 的一個 GPIO pin,例如 GPIO4 (程式碼中定義為
DHTPIN 4)。- 如果是非模組化的 DHT22: 在 Data pin 和 VCC 之間串接一個 10K 歐姆的拉高電阻。
- DHT22 GND (或 -): 連接到 ESP32 的 GND。
三、 Arduino IDE 環境設定
- 安裝 ESP32 Boards: (如果之前已經安裝過則跳過)
- 打開 Arduino IDE。
- 進入 "檔案" -> "偏好設定"。
- 在 "額外的開發板管理員網址" (Additional Boards Manager URLs) 中加入:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - 進入 "工具" -> "開發板" -> "開發板管理員"。
- 搜尋 "esp32" 並安裝 "esp32 by Espressif Systems"。
- 安裝 Ubidots ESP32 函式庫:
- 進入 "工具" -> "管理程式庫"。
- 搜尋 "Ubidots" 並安裝 "Ubidots ESP32 by Ubidots" (或 "Ubidots MQTT/HTTP for ESP32 & ESP8266")。
- 安裝 DHT sensor library by Adafruit:
- 進入 "工具" -> "管理程式庫"。
- 搜尋 "DHT sensor library" 並安裝 "DHT sensor library by Adafruit"。
- 安裝 Adafruit Unified Sensor by Adafruit: (DHT sensor library 的依賴函式庫)
- 進入 "工具" -> "管理程式庫"。
- 搜尋 "Adafruit Unified Sensor" 並安裝 "Adafruit Unified Sensor by Adafruit"。
四、 ESP32 Arduino 程式碼
#include "UbidotsEsp32Mqtt.h"
#include "WiFi.h" // ESP32 Wi-Fi 函式庫,通常 Ubidots 函式庫會自動引入,但明確寫出更清楚
#include "DHT.h" // DHT 感測器函式庫
#include "Adafruit_Sensor.h" // DHT 函式庫的依賴函式庫
/****************************************
* Define Constants
****************************************/
const char *UBIDOTS_TOKEN = "BBUS-gSzHu1TrGDMRr728ESUIXmxqGRWYv0xXR"; // 替換成您的 Ubidots TOKEN
const char *WIFI_SSID = "Wokwi-GUEST"; // 替換成您的 Wi-Fi SSID
const char *WIFI_PASS = ""; // 替換成您的 Wi-Fi 密碼
const char *DEVICE_LABEL = "vr"; // 替換成您在 Ubidots 建立的設備標籤,必須完全一致
const char *VARIABLE_LABEL1 = "temp"; // Put here your Variable label to which data will be published
const char *VARIABLE_LABEL2 = "humi"; // Put here your Variable label to which data will be published
// 不再需要單一的 VARIABLE_LABEL,因為我們會發送多個變數
const int PUBLISH_FREQUENCY = 5000; // 發佈頻率,單位毫秒 (5秒)
// ---------- DHT22 感測器設定 ----------
#define DHTPIN 4 // DHT22 連接的 ESP32 GPIO Pin (例如 GPIO4)
#define DHTTYPE DHT22 // 或 DHT11
// 初始化 DHT 感測器物件
DHT dht(DHTPIN, DHTTYPE);
// Ubidots 客戶端物件
Ubidots ubidots(UBIDOTS_TOKEN);
unsigned long lastSendTime = 0;
/****************************************
* Auxiliar Functions
****************************************/
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();
}
/****************************************
* Main Functions
****************************************/
void setup()
{
Serial.begin(115200);
delay(100); // 給序列埠一點時間啟動
Serial.println("\nStarting Ubidots DHT22 Sender...");
// 初始化 DHT 感測器
dht.begin();
// 連接到 Wi-Fi
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
ubidots.setCallback(callback); // 設定 MQTT 訂閱訊息回調函數 (如果需要從 Ubidots 接收命令)
ubidots.setup(); // 執行 Ubidots 函式庫的設置
ubidots.reconnect(); // 連接到 Ubidots MQTT Broker
}
void loop()
{
// 保持 Ubidots MQTT 連線
if (!ubidots.connected())
{
ubidots.reconnect();
}
ubidots.loop();
// 每隔 PUBLISH_FREQUENCY 時間發送一次數據
unsigned long now = millis();
if (now - lastSendTime >= PUBLISH_FREQUENCY) {
lastSendTime = now;
// 讀取 DHT22 感測器數據
float h = dht.readHumidity();
float t = dht.readTemperature(); // 讀取攝氏溫度
// 檢查是否有讀取錯誤
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return; // 讀取失敗則不發送數據
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
// 將溫度和濕度數據添加到 Ubidots 隊列中
// 'temperature' 和 'humidity' 將會是您在 Ubidots 設備下的變數名稱
//ubidots.add("temperature", t);
//ubidots.add("humidity", h);
ubidots.add(VARIABLE_LABEL1, t); // Insert your variable Labels and the value to be sent
ubidots.add(VARIABLE_LABEL2, h); // Insert your variable Labels and the value to be sent
// 發送所有添加到隊列中的數據到指定的設備標籤
bool published = ubidots.publish(DEVICE_LABEL);
if (published) {
Serial.println("Data sent to Ubidots successfully!");
} else {
Serial.println("Failed to send data to Ubidots.");
}
}
}








沒有留言:
張貼留言