發表文章

目前顯示的是 12月, 2021的文章

[心得] Arduino MQTT 控制、傳輸

#include #include const int led=2; //內建LED const char* ssid = "MISR"; const char* password = "WIFI密碼"; const char* mqttServer = "192.168.0.144"; const char* mqtt_server = "192.168.0.144"; const char* mqttUser = "bob01"; const char* mqttPassword = "123456"; const int mqttPort = 1883; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; float temperature = 0; float humidity = 0; void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); pinMode(led, OUTPUT); } void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println...

[心得] flask MQTT 連結 Socket 即時顯示MQTT傳送之訊息

Python import eventlet from flask import Flask, render_template from flask_mqtt import Mqtt from flask_socketio import SocketIO eventlet.monkey_patch() app = Flask(__name__) app.config['SECRET'] = 'my secret key' app.config['TEMPLATES_AUTO_RELOAD'] = True app.config['MQTT_BROKER_URL'] = 'broker.hivemq.com' app.config['MQTT_BROKER_PORT'] = 1883 app.config['MQTT_USERNAME'] = '' app.config['MQTT_PASSWORD'] = '' app.config['MQTT_KEEPALIVE'] = 5 app.config['MQTT_TLS_ENABLED'] = False app.config['MQTT_CLEAN_SESSION'] = True mqtt = Mqtt(app) socketio = SocketIO(app) @mqtt.on_connect() def handle_connect(client, userdata, flags, rc): mqtt.subscribe('esp/test') @mqtt.on_message() def handle_mqtt_message(client, userdata, message): data = dict( topic=message.topic, payload=message.payload.decode() ) # emit a mqtt_message event to the socket contain...

[心得] Reed Switch (リードスイッチ) Arduino Explame

For Esp32   int switchReed2=5; //GPIO int led=2; //內建LED int switchReed=18; //GPIO void setup(){ pinMode(led, OUTPUT); //設動輸出模式 pinMode(switchReed2, OUTPUT); //設動輸出模式 pinMode(switchReed, OUTPUT); //設動輸出模式 Serial.begin(9600); //設定鮑率 } void loop(){ // 判斷電磁開關是否接觸:是 if (digitalRead(switchReed)==HIGH){ digitalWrite(led, LOW); //設定低電位(關燈) Serial.println("Window Closed"); } // 判斷電磁開關是否接觸:否 else { digitalWrite(led, HIGH); //設定高電位(亮燈) digitalWrite(switchReed2, HIGH); //設定高電位 Serial.println("Window Open"); } delay(1000); } 修改至 Arduino筆記(72):使用MC-38 磁簧開關監測門窗開啟或關閉 | 天花板隨記 (atceiling.blogspot.com)