Receiving 433Mhz signal with ESP8266 nodemcu

See Post#5 and Post#8

.
I'm testing a P10 LED panel with a 433Mhz remote control.

Every time the 433Mhz control button is pressed, 1 line of text will be written to a .TXT file in LittleFS. from nodemcu12E.

The problem is that when we keep the button pressed, a burst (pulses) comes out of the 433Mhz remote control.

If this signal is not handled, the .TXT file would have infinite lines in a short time.

So writing should only occur when we release the button after holding it down. I.e. we tighten and then release. It is on the descent from this 'edge' that writing must occur. This is the condition.

What I tried to do was: As soon as you press the button, an int called counter goes to condition 1 and thus ignores the other pulses from the remote control.

Later on, I said that if the recebesinal is equal to zero and the contador is equal to 1, that is, if the remote control button is released. It will start a timer (cron++) as if it were Millis(). When this cron reaches a close value, the signal int becomes equal to one. The board's internal LED turns on and off monitoring the process.

recebesinal = 1 means that printing occurred in the.txt file.

After printing the line I need to make the recebesinal = 0;

But this is the problem, if I do this, if I keep the button pressed I will see one more line being printed in the .txt file and so I will have infinite lines being printed again if I keep the button pressed.

The correct option is to just print 1 line as soon as you release the button. Squeeze and then release.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const int ledinterno = D4;
int recebesinal,cron,cron1,contador;
unsigned long codigorecebido;

void setup() {
  Serial.begin(115200);
  pinMode(ledinterno, OUTPUT); 
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
}

void loop() {
  if (mySwitch.available()) {

    codigorecebido = mySwitch.getReceivedValue();
    Serial.print( mySwitch.getReceivedValue() );
    mySwitch.resetAvailable();
  } else {
      codigorecebido = 0;
    }

  if(codigorecebido == 167914277 && contador == 0) {
      contador = 1;
   }

   if(codigorecebido == 0 && contador == 1) {
      digitalWrite(ledinterno, LOW);
      cron++;
      Serial.println(cron);
      if(cron > 500) {
        digitalWrite(ledinterno, HIGH);
        cron = 0;
        contador = 0;
        recebesinal = 1;
        Serial.println(recebesinal);
      }
   }   

    
}

Does anyone know how to do it ?

Thanks

I am not entirely sure what all your code is supposed to be doing but if what you want is falling edge detection on a signal then this code should work.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

cosst int ledinterno = D4;
int recebesinal=0;
unsigned long codigorecebido;


void do_something_once(){
  Serial.println("Printing to txt file");
  digitalWrite(ledinterno, HIGH);
  delay(500);
  digitalWrite(ledinterno, LOW);
}

void setup() {
  Serial.begin(115200);
  pinMode(ledinterno, OUTPUT); 
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
}

void loop() {
  if (mySwitch.available()) {

    codigorecebido = mySwitch.getReceivedValue();
    Serial.print( mySwitch.getReceivedValue() );
    mySwitch.resetAvailable();
  }else{
    recebesinal = 0;//reset if button is no longer held
  }

  if(codigorecebido ==167914277 && recebesinal == 0){
    recebesinal = 1;
    do_something_once();
    delay(10);//simple debounce
  }
}

Thank you for showing interest. I tested it here but it didn't work. It is not so simple to detect the falling edge because when you keep the remote control button pressed, a constant train of pulses occurs. It would print infinite lines with this code of yours.

The big problem is that the pulse train itself is confusing because it contains rising edges and falling edges. And this tricks the nodemcu into 'thinking' they released the button, but it was NOT just the pulse train.

I think I should pause reception with a disableReceive. I'm researching this. And after printing use enableReceive. But it's just a very superficial opinion.

A simpler test that leads to the same success is: If I press the remote control button, nothing happens, but when I release the button, the led_builtin lights up (it stays on straight).

Ah, now I understand the problem.

You can used millis() to get current time and compare it to the last time the button was pressed:

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const int ledinterno = D4;
int recebesinal=0;
unsigned long codigorecebido;
long timePressed;

void do_something_once(){
  Serial.println("Printing to txt file");
  digitalWrite(ledinterno, HIGH);
  delay(500);
  digitalWrite(ledinterno, LOW);
}

void setup() {
  Serial.begin(115200);
  pinMode(ledinterno, OUTPUT); 
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
}


void loop() {
  if (mySwitch.available()) {
    codigorecebido = mySwitch.getReceivedValue();
    Serial.print( mySwitch.getReceivedValue() );
    mySwitch.resetAvailable();
    timePressed = millis();
    recebesinal == 1;
  }
  if(recebesinal == 1 && millis() > timePressed+500){//button has been pressed at least once and its been 500miliseconds since the last press
    recebesinal = 0;
    do_something_once();
    delay(10);//simple debounce
  }
}

Tested. It didn't. If I keep the control button pressed I see the button code (167914277) on the serial monitor being printed repeatedly. Never prints this: "Printing to txt file"

Even adjusting there at:
recebesinal = 1; (ao invés de recebesinal == 1;)

That is because I forgot a few lines of code. Apologies, I dont actually have an RC switch to test with.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const int ledinterno = D4;
int recebesinal = 0;
unsigned long codigorecebido;
long timePressed;

void do_something_once() {
  Serial.println("Printing to txt file");
  digitalWrite(ledinterno, HIGH);
  delay(500);
  digitalWrite(ledinterno, LOW);
}

void setup() {
  Serial.begin(115200);
  pinMode(ledinterno, OUTPUT);
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
}


void loop() {
  if (mySwitch.available()) {
    codigorecebido = mySwitch.getReceivedValue();
    Serial.print(mySwitch.getReceivedValue());
    mySwitch.resetAvailable();
  }

  if (codigorecebido == 167914277) {
    timePressed = millis();
    recebesinal == 1;
  }
  
  if (recebesinal == 1 && millis() > timePressed + 500) {  //button has been pressed at least once and its been 500miliseconds since the last press
    recebesinal = 0;
    do_something_once();
    delay(10);  //simple debounce
  }
}

The final code

Thank you very much

Health and peace
.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const int ledinterno = D4;
int recebesinal=0;
unsigned long codigorecebido;
long timePressed;

void do_something_once(){
  Serial.println("Printing to txt file");
  digitalWrite(ledinterno, LOW);
  delay(500);
  digitalWrite(ledinterno, HIGH);
}

void setup() {
  Serial.begin(115200);
  pinMode(ledinterno, OUTPUT); 
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
  digitalWrite(ledinterno, HIGH);
}


void loop() {
  if (mySwitch.available()) {
    codigorecebido = mySwitch.getReceivedValue();
    Serial.println(mySwitch.getReceivedValue());
    mySwitch.resetAvailable();
    timePressed = millis();
    recebesinal = 1;
  }
  if(codigorecebido == 167914277 && recebesinal == 1 && millis() > timePressed + 500) {  //button has been pressed at least once and its been 500miliseconds since the last press
    recebesinal = 0;
    do_something_once();
    delay(10);//simple debounce
  }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.