MQTT-Nord-RED顯示超音波測距數據+控制LED 超音波模組(HC-SC04)測試回顧- 點我回顧 MQTT相關布暑- 點我回顧 #include <Ultrasonic.h> #include <WiFi.h> #include <PubSubClient.h> Ultrasonic ultrasonic ( 32 , 33 ); const char * ssid = "---" ; const char * password = "-----" ; const char * mqttServer = "192.168.67.22" ; //wifi客戶端及MQTT客戶端代數 WiFiClient espClient ; PubSubClient client ( espClient ); //設定wifi void wifiConn (){ WiFi . begin ( ssid , password ); while ( WiFi . status () != WL_CONNECTED ){ Serial . print ( "." ); delay ( 50 ); } Serial . println ( "Connected successful!" ); Serial . println ( WiFi . localIP ()); } //檢查訂閱的字串是否含有on和off,如果比對下來是 //nord-red裡的標頭一樣,而且包含on就開燈,off就關燈 void callback ( char * topic , byte * message , unsigned int length ){ String messageDist ; Serial . print ( "Message arrived on topic: " ); Serial . print ( topic ); Serial . print ( &quo