共發表了 2 篇文章。
使用 Raspberry Pi Pico W 搭配 LoRa 模組 (假設使用 SX1262 LoRa 模組) 並透過 MQTT 進行通訊的程式範例。此範例將展示如何使用 MicroPython 從 LoRa 接收資料,並透過 Pico W 的 Wi-Fi 功能將資料發送到 MQTT Broker。
umqtt.simple
(MQTT 客戶端庫)以下程式假設 LoRa 模組連接至 Pico W 的 UART 引腳,並從 LoRa 接收資料後透過 MQTT 發送到指定的主題。
2025-02-23Raspberry Pi Pico W 連接 MQ-2 傳感器並透過 Make.com Webhook 上傳數據到 Notion ,並且當氣體濃度超過警戒值時,會透過 LINE Notify 來通知使用者。
設備 | 數量 |
---|---|
Raspberry Pi Pico W | 1 |
MQ-2 煙霧傳感器 | 1 |
面包板 | 1 |
杜邦線 | 若干 |
MQ-2 傳感器 | Raspberry Pi Pico W (GPIO) |
---|---|
VCC | 3.3V |
GND | GND |
A0 | A5 |
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <TridentTD_LineNotify.h>
// 🔹 WiFi 連線資訊
char SSID[] = "你的 WiFi 名稱";
char PASSWORD[] = "你的 WiFi 密碼";
// 🔹 Make.com Webhook URL(用於寫入 Notion)
const char* MAKE_WEBHOOK_URL = "https://hook.integromat.com/你的webhook網址";
// 🔹 LINE Notify Token(當氣體超標時發送警報)
#define LINE_TOKEN "你的 LINE Notify 金鑰"
// 🔹 MQ-2 煙霧傳感器
int gasSensor = A5;
int qualityLevel = 2; // 傳感器敏感度 (qualityLevel * 200 = gasValue)
// 🔹 氣體濃度警戒值
int GAS_WARNING_LEVEL = 500;
void setup() {
Serial.begin(115200);
// 連線到 WiFi
Serial.print("正在連接 WiFi: ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nWiFi 連線成功!");
Serial.print("IP 地址: ");
Serial.println(WiFi.localIP());
// 設定 LINE Notify Token
LINE.setToken(LINE_TOKEN);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(MAKE_WEBHOOK_URL);
http.addHeader("Content-Type", "application/json");
// 讀取 MQ-2 傳感器數值
int gasValue = analogRead(gasSensor) * qualityLevel;
bool isOverLimit = gasValue > GAS_WARNING_LEVEL;
Serial.print("偵測到的氣體濃度: ");
Serial.println(gasValue);
// 建立 JSON 數據
String jsonPayload = "{";
jsonPayload += "\"timestamp\": \"" + String(millis()) + "\",";
jsonPayload += "\"sensor_id\": \"MQ-2\",";
jsonPayload += "\"value\": " + String(gasValue) + ",";
jsonPayload += "\"over_limit\": \"" + String(isOverLimit ? "是" : "否") + "\"";
jsonPayload += "}";
// 發送資料到 Make.com
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
Serial.print("數據發送成功, HTTP 回應碼: ");
Serial.println(httpResponseCode);
} else {
Serial.print("數據發送失敗, 錯誤: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
// 🔹 如果超標,發送 LINE Notify 警報
if (isOverLimit) {
String message = "⚠️ 煙霧警報!\n氣體濃度超標!\n";
message += "當前濃度:" + String(gasValue) + "\n";
message += "請儘速查看環境狀況!";
LINE.notify(message);
}
} else {
Serial.println("WiFi 未連線, 無法發送數據");
}
delay(60000); // 每 60 秒發送一次數據
}
創建 Webhook
2025-02-24