-->
Page 1 of 2

Help with if statement using a 433mhz receiver

PostPosted: Sat Apr 13, 2019 12:11 pm
by mrlightsman
I am trying to use an ESP8266-01 and RXBX1 433mhz receiver to bridge my security RF signals to my wifi set up. Specifically, each time a sensor triggers, I want to receive its 433 signal, process it with a "sending" ESP and send it via udp to a second ESP for additional processing. (It may turn on lights, sound an alarm, or whatever.)

My problem is with the sending ESP. I can receive signals from the sensors and view them with serial monitor. I just don't know how to convert them to usable data so I can use an IF statement to determine what to do with it.

I have no problems with the udp code. My need is really just about analyzing the 433 signals.

Here is my code:

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUDP.h>
#include <RCSwitch.h>


ESP8266WiFiMulti wifiMulti;
WiFiUDP Udp;
RCSwitch mySwitch = RCSwitch();
   
/*
PINS:
0=Data Rx from RxB1
RX & TX to programmer and serial monitor
*/


void door() {
  Udp.beginPacket(IPAddress (192,168,1,26), 4500);
  Udp.write("doorsensor");
  Udp.endPacket();
  delay(500);
  }

void window() {
  Udp.beginPacket(IPAddress (192,168,1,26), 4500);
  Udp.write("windowsensor");
  Udp.endPacket();
  delay(500);
  } 


void setupWifi() {
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP("SSID_1", "PW_1");
  wifiMulti.addAP("SSID_2", "PW_2");
  wifiMulti.addAP("SSID_3", "PW_3");
  wifiMulti.addAP("SSID_4", "PW_4");

  Serial.println("Connecting Wifi...");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("Connected to SSID: ");
  Serial.println(WiFi.SSID());
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void setup() {
  Serial.begin(115200);
 
  setupWifi();

  Udp.begin(4500);
  mySwitch.enableReceive(0); 
}

void loop() {
  if (wifiMulti.run() != WL_CONNECTED) {
    Serial.println("WiFi not connected!");
    delay(1000);
    setupWifi();
  }

  if (mySwitch.available()) {
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );
    mySwitch.resetAvailable();
  }

  //create an if statement for each sensor in the house
  If(mySwitch.getReceviedValue() == 3372380) {
    window();  //or door depending on sensor type
  }


}


This code produces the following results in the serial monitor as expected:

Code: Select allConnecting Wifi....................
WiFi connected
Connected to SSID:
SSID_1
IP address:
192.168.1.3
Received 3372380 / 24bit Protocol: 1



You can see I am using the RC_Switch library. I am most interested in the "mySwitch.getReceviedValue()" since it is unique for each sensor. However, I don't know how to process it in the loop. I've tried:

Code: Select all  If(mySwitch.getReceviedValue() == 3372380) {
    window();  //or door() depending on sensor type
  }


But it does nothing. I am hoping to use a series of if statements (one for each sensor) to determine whether to call window() or door() functions.


Any help is greatly appreciated. I assume my problem is in the variable type, but I don't know how to correct it. Thanks.

Re: Help with if statement using a 433mhz receiver

PostPosted: Sat Apr 13, 2019 5:15 pm
by torntrousers
Try moving the
Code: Select allmySwitch.resetAvailable()
down to after all the if statements - resetAvailable is clearing the received values.

Re: Help with if statement using a 433mhz receiver

PostPosted: Sat Apr 13, 2019 7:12 pm
by mrlightsman
Yes. Exactly. Thank you. I feel silly that I missed that. I am now getting the responses I expected. However, it is reacting intermittently to the sensors. Sometimes it catches a trigger on the first try. Sometimes it takes 5-10 tries before it senses it.

I have a 17 cm antenna on the receiver and strong batteries in the transmitters. Any thoughts? At the moment, I am only less than a foot apart between transmitter and receiver.

I tried adding delay(50) and then delay(500) before the mySwitch.resetAvailable(). Neither seemed to help.

Re: Help with if statement using a 433mhz receiver

PostPosted: Sun Apr 14, 2019 10:01 am
by torntrousers
I've had good results using those RxB1 modules with ESP's so it should work ok, its possible you've a duff RxB1 I suppose. I'd start debugging that by removing as much as possible from the sketch - all the wifiMulti, UDP, window stuff, leaving just have the loop with printing in mySwitch.available() bit - to see if any of those are interfering with RCSwitch somehow.