这是我使用 HC-SR04 超声波传感器进行运动检测的代码,使用 Sinric Pro 运动传感器草图并与我为超声波传感器找到的 Intructables 代码相结合。但是,当我将代码上传到 NodeMCU 板时,我在串行监视器中得到以下输出:
14:55:28.265 ->
14:55:36.662 ->
14:55:36.662 -> ets Jan 8 2013,rst cause:4, boot mode:(3,6)
14:55:36.662 ->
14:55:36.662 -> wdt reset
14:55:36.662 -> load 0x4010f000, len 3424, room 16
14:55:36.662 -> tail 0
14:55:36.662 -> chksum 0x2e
14:55:36.662 -> load 0x3fff20b8, len 40, room 8
14:55:36.662 -> tail 0
14:55:36.662 -> chksum 0x2b
14:55:36.662 -> csum 0x2b
14:55:36.662 -> v00065ae0
14:55:36.662 -> ~ld
14:55:36.727 -> ����n��r��n|�$�$`#��|r�l�o��N�l`��r�d����
代码:
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif
#include "src/SinricPro.h"
#include "src/SinricProMotionsensor.h"
#define WIFI_SSID ""
#define WIFI_PASS ""
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define MOTIONSENSOR_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
#define trigPin 6
#define echoPin 5
bool lastMotionState = false;
unsigned long lastChange = 0;
void handleMotionsensor() {
unsigned long actualMillis = millis();
if (actualMillis - lastChange < 250) return; // debounce motionsensor state transitions (same as debouncing a pushbutton)
int duration, distance; //Define two intregers duration and distance to be used to save data
digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse
delayMicroseconds(500); //wait half a millisecond
digitalWrite(trigPin, LOW); //turn off the trigpin
duration = pulseIn(echoPin, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
distance = (duration/2) / 29.1; //distance is the duration divided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then devide by 29.1 to convert to centimeters
bool actualMotionState = false;
if (distance < 13) //if the distance is less than 13 CM
{
Serial.print("Motion detected. "); //execute the Light subroutine below
}
Serial.print(distance); //Dispaly the distance on the serial monitor
Serial.println(" CM"); //in centimeters
delay(500);
if (actualMotionState != lastMotionState) { // if state has changed
Serial.printf("Motion %s\r\n", actualMotionState?"detected":"not detected");
lastMotionState = actualMotionState; // update last known state
lastChange = actualMillis; // update debounce time
SinricProMotionsensor &myMotionsensor = SinricPro[MOTIONSENSOR_ID]; // get motion sensor device
bool success = myMotionsensor.sendMotionEvent(actualMotionState);
if(!success) {
Serial.printf("Something went wrong...could not send Event to server!\r\n");
}
}
}
// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
}
IPAddress localIP = WiFi.localIP();
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}
// setup function for SinricPro
void setupSinricPro() {
// add device to SinricPro
SinricProMotionsensor& myMotionsensor = SinricPro[MOTIONSENSOR_ID];
// setup SinricPro
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
pinMode(trigPin, OUTPUT); //set the trigpin to output
pinMode(echoPin, INPUT);
setupWiFi();
setupSinricPro();
}
void loop() {
handleMotionsensor();
SinricPro.handle();
}
我不确定我尝试组合这两个代码的方式是否有问题,或者是否有其他问题。有人可以通过识别我的错误或帮助我找到/创建新代码来帮助我吗?
14:55:36.727 -> ����n��r��n|� $ � $
��r�d�� �#��|r�l�o��N�l
有一个超声波传感器教程可以帮助您了解是否仍要使用超声波传感器:https://help.sinric.pro/pages/tutorials/custom-device-types/ultrasonic-sensor/ HC-SR04。尝试在没有 Sinric Pro 的情况下编码来检测运动,然后尝试集成它