DFRobot and loop execution

userHead DENNIS.BRANDT 2026-08-13 01:41:13 5 Views0 Replies

I have been working on this sketch for several days and tried various alternatives.  When I make a sketch of the code between the //***************'s it works fine. When I add it to the overall sketch it won't cycle or turn on the relay.

Any Suggestions

 

#include "DFRobot_DF2301Q.h"

#include <esp_now.h>

#include <WiFi.h>

uint8_t broadcastAddress[] = { 0x14, 0x08, 0x08, 0xa5, 0xd7, 0x68 };

DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D7, /*tx =*/D6);

const byte BUTTON_PIN = D2;

const byte RELAY_PIN = D0;


 

volatile byte buttonPressCount = 0;

volatile unsigned long lastDebounceTime = 0;

const unsigned long DEBOUNCE_DELAY = 1000;


 

bool isCycling = true;


 

typedef struct struct_message {

  int b;

} struct_message;

struct_message myData;

esp_now_peer_info_t peerInfo;

DFRobot_DF2301Q_I2C asr;

void OnDataSent(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) {

  if (status == ESP_NOW_SEND_SUCCESS) {

    Serial.println("Send Success");

  } else {

    Serial.println("Send Fail");

  }

}


 

void setup() {

  Serial.begin(115200);

  myData.b = 0;

  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {

    Serial.println("Error initializing ESP-NOW");

    return;

  }

  esp_now_register_send_cb(OnDataSent);

  memcpy(peerInfo.peer_addr, broadcastAddress, 6);

  peerInfo.channel = 0;

  peerInfo.encrypt = false;

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {

    Serial.println("Failed to add peer");

    return;

  }


 

  while (!(asr.begin())) {

    Serial.println("Communication with device failed, please check connection");

    delay(3000);

  }

  Serial.println("Begin ok!");

  asr.setVolume(7);

  asr.setMuteMode(0);

  asr.setWakeTime(255);


 

  uint8_t wakeTime = 0;

  wakeTime = asr.getWakeTime();

  Serial.print("wakeTime 4= ");

  Serial.println(wakeTime);


 

  // asr.playByCMDID(1);   // Wake-up command


 

  /**

     @brief Play the corresponding reply audio according to the ID

     @param CMDID - command word ID

  */

  pinMode(RELAY_PIN, OUTPUT);

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING);

  digitalWrite(RELAY_PIN, LOW);

}


 

void loop() {


 

  uint8_t CMDID = 0;

  CMDID = DF2301Q.getCMDID();

  Serial.print("CMDID = ");

  Serial.println(CMDID);

  if (CMDID == 5) {

  //****************

    if (buttonPressCount >= 5) {

      isCycling = false;

    }


 

    if (isCycling) {

      digitalWrite(RELAY_PIN, HIGH);

      delay(1000);

      digitalWrite(RELAY_PIN, LOW);

      delay(1000);

    } else {

      digitalWrite(RELAY_PIN, LOW);

    }

  }

}


 

void handleButtonPress() {

  unsigned long currentTime = millis();

  if ((currentTime - lastDebounceTime) > DEBOUNCE_DELAY) {

    buttonPressCount++;

    lastDebounceTime = currentTime;

  }

}

  //***************