ESP32使用433模块通讯

发布时间 2023-04-09 19:44:48作者: ahuo

发送端,25pin

#include <Arduino.h>
#include <WiFi.h>
#include <RCSwitch.h>

#define TXD2 25

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(115200);
  
  mySwitch.enableTransmit(TXD2);
  
  // Optional set protocol (default is 1, will work for most outlets)
  // mySwitch.setProtocol(2);

  // Optional set pulse length.
  mySwitch.setPulseLength(311);
  
  // Optional set number of transmission repetitions.
  // mySwitch.setRepeatTransmit(15);  
}

void loop() {
  /* See Example: TypeA_WithDIPSwitches */
  mySwitch.switchOn("01010", "10000");

  Serial.println("Switch On");
  delay(1000);
  mySwitch.switchOff("01010", "10000");
  Serial.println("Switch Off");
  delay(1000);
}

  接收端,27pin

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

#define RXD2 27

void setup() {
  Serial.begin(115200);
  Serial.print("Ready to receive.");  
  mySwitch.enableReceive(RXD2); 
}

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

    mySwitch.resetAvailable();
  }
}