This is my code for using an HC-SR04 ultrasonic sensor for motion detection using the Sinric Pro motion sensor sketch and combining with an Intructables code I found for the ultrasonic sensor. However, when I upload the code to my NodeMCU board, I get this output in Serial Monitor:
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����
Code:
#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();
}
I'm not sure if there's a problem with the way I've tried to combine the two codes or if its something else. Can someone help me by either identifying my mistake or by helping me find/create a new code?
There's an ultrasonic sensor tutorial that might help you to get an idea if you are still going to use an ultrasonic sensor: https://help.sinric.pro/pages/tutorials/custom-device-types/ultrasonic-sensor/HC-SR04. Try coding to detect motion without Sinric Pro and then try integrating it