最近在做一個有趣的小場景功能,其實已經有成熟產品,但是考慮到沒法實現場景擴展,所以自己開始動手做。
場景描述:玄關人體感應,有人進門,致歡迎詞,有人離開,致歡送詞。
硬件設備:WeMOS D1 + PIR 【HC-SR501 】 + Android手機
數據流:
從WeMOS D1開始
關于WeMos D1的參考鏈接:
Arduino文檔閱讀筆記-WeMos D1 ESP8266 WIFI開發板入門_wemos d1手冊-CSDN博客
ArduiNo(WeMos D1)基礎(一)_arduino d1-CSDN博客
ESP8266之WiFiClient庫學習-CSDN博客
WeMos D1主要是是作為TCPClient,將接收到的PIR信息推送給android系統,并接收反饋信息
/*This sketch establishes a TCP connection to a "quote of the day" service.It sends a "hello" message, and then prints received data.
*/#include <ESP8266WiFi.h>#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endifconst char* ssid = STASSID;
const char* password = STAPSK;const char* host = "djxmmx.net";
const uint16_t port = 17;void setup() {Serial.begin(115200);// We start by connecting to a WiFi networkSerial.println();Serial.println();Serial.print("Connecting to ");Serial.println(ssid);/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,would try to act as both a client and an access-point and could causenetwork-issues with your other WiFi-devices on your WiFi-network. */WiFi.mode(WIFI_STA);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}Serial.println("");Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());
}void loop() {static bool wait = false;Serial.print("connecting to ");Serial.print(host);Serial.print(':');Serial.println(port);// Use WiFiClient class to create TCP connectionsWiFiClient client;if (!client.connect(host, port)) {Serial.println("connection failed");delay(5000);return;}// This will send a string to the serverSerial.println("sending data to server");if (client.connected()) { client.println("hello from ESP8266"); }// wait for data to be availableunsigned long timeout = millis();while (client.available() == 0) {if (millis() - timeout > 5000) {Serial.println(">>> Client Timeout !");client.stop();delay(60000);return;}}// Read all the lines of the reply from server and print them to SerialSerial.println("receiving from remote server");// not testing 'client.connected()' since we do not need to send data herewhile (client.available()) {char ch = static_cast<char>(client.read());Serial.print(ch);}// Close the connectionSerial.println();Serial.println("closing connection");client.stop();if (wait) {delay(300000); // execute once every 5 minutes, don't flood remote service}wait = true;
}
PIR功能的開發測試鏈接:
在 Arduino 上使用 HC-SR501 人體熱釋電(PIR)傳感器 – Arduino 實驗室 (nxez.com)
其中的有些代碼還是要調整的:
int pirPin = D5; // PIR傳感器連接的引腳void setup() {pinMode(pirPin, INPUT); // 將PIR傳感器引腳設置為輸入模式pinMode(LED_BUILTIN, OUTPUT);Serial.begin(115200); // 初始化串口通信
}void loop() {int pirValue = digitalRead(pirPin); // 讀取PIR傳感器的值if (pirValue == HIGH) { // 如果檢測到運動digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOWdelay(1000); Serial.println("Motion detected!"); // 在串口打印消息delay(1000); // 延遲1秒} else {digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOWdelay(1000);}
}
通過Android系統,搭建TCPServer接收 WeMos發送的PIR的測試信息.
Android功能待更新...