16 17脚通过二极管直连22 23脚。
#include <RCSwitch.h>
#include "EEPROM.h"
#define R315 23
#define R433 22
#define led_pin 12
#define LED2 13
const int R315EN = 16;
const int R433EN = 17;
EEPROMClass eeprom_xuhao("eeprom0"); //unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol
EEPROMClass eeprom_leixing("eeprom1");
EEPROMClass eeprom_shuju("eeprom2");
EEPROMClass eeprom_changdu("eeprom3");
EEPROMClass eeprom_maikuan("eeprom4");
EEPROMClass eeprom_xieyi("eeprom5");
int address = 1;
int i;
//RCSwitch mySwitch = RCSwitch();
RCSwitch myShou315 = RCSwitch(); //收
RCSwitch myShou433 = RCSwitch(); //收
//uint num;
uint value;
uint xuhao, shuju, changdu, maikuan, xieyi, leixing;
uint L315=315;
uint L433=433;
void setup() {
Serial.begin(115200);
Serial.println("\nTesting EEPROM Library\n");
if (!eeprom_xuhao.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!eeprom_leixing.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!eeprom_shuju.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!eeprom_changdu.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!eeprom_maikuan.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!eeprom_xieyi.begin(0x200)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
Serial.print("EEPROM-length:");
Serial.println(eeprom_xuhao.length());
Serial.println(eeprom_leixing.length());
Serial.println(eeprom_shuju.length());
Serial.println(eeprom_changdu.length());
Serial.println(eeprom_maikuan.length());
Serial.println(eeprom_xieyi.length());
pinMode(R315EN, OUTPUT);
pinMode(R433EN, OUTPUT);
//pinMode(led_pin, OUTPUT);
pinMode(LED2, OUTPUT);
delay(1000);
address = 1;
while (address < eeprom_shuju.length()) {
leixing = eeprom_leixing.readUInt(address);
shuju = eeprom_shuju.readUInt(address);
changdu = eeprom_changdu.readUInt(address);
maikuan = eeprom_maikuan.readUInt(address);
xieyi = eeprom_xieyi.readUInt(address);
Serial.print(" 地址=");
Serial.print(address);
Serial.print(" 类型=");
Serial.print(leixing);
Serial.print(" 数据=");
Serial.print(shuju);
Serial.print(" 长度=");
Serial.print(changdu);
Serial.print(" 脉宽=");
Serial.print(maikuan);
Serial.print(" 协议=");
Serial.println(xieyi);
address += sizeof(unsigned int);
}
address = eeprom_xuhao.readUInt(0);
Serial.print(" 上次写入数据地址为:");
Serial.println(address);
myShou315.enableReceive(R315);
myShou433.enableReceive(R433);
delay(1000);
Serial.println("Ready to receive.");
//eeprom_init();
}
//-----------------------------------------------------------------------
void loop() {
shou315();
// digitalWrite(LED1, HIGH); //16脚
digitalWrite(LED2, LOW); //16脚
// digitalWrite(R315EN,HIGH);
// digitalWrite(R433EN,LOW);
shou433();
// digitalWrite(LED1, LOW); //16脚
digitalWrite(LED2, HIGH); //16脚
}
void shou315() {
digitalWrite(R315EN, LOW); //16脚
digitalWrite(R433EN, HIGH); // 17脚接收315
delay(100);
if (myShou315.available())
{
value = myShou315.getReceivedValue();
leixing = eeprom_leixing.readUInt(address);
shuju = eeprom_shuju.readUInt(address);
Serial.print("接收数据类型为:");
Serial.println("315");
Serial.print("接收数据为:");
Serial.println(value);
if ((value == shuju) && (leixing == 315)) {
Serial.println(" -------------------------------------重复接收-------------------------------------------------- ");
Serial.println("");
// Serial.print(" 数据长度为:");
// Serial.print(myShou315.getReceivedBitlength());
// Serial.print("bit ");
// Serial.print(" Protocol: ");
// Serial.println(myShou315.getReceivedProtocol());
// Serial.print(" / ");
// // Serial.println(myShou315.getReceivedDelay());
// Serial.println("");
// Serial.println("");
myShou315.resetAvailable();
} else {
Serial.println("-------------------------------新数据接收成功-----------------------------");
shuju = myShou315.getReceivedValue();
changdu = myShou315.getReceivedBitlength();
maikuan = myShou315.getReceivedDelay();
xieyi = myShou315.getReceivedProtocol();
address += sizeof(unsigned int);
init_address() ;
eeprom_xuhao.writeUInt(0, address); //写地址
eeprom_leixing.writeUInt(address, L315);
eeprom_shuju.writeUInt(address, shuju);
eeprom_changdu.writeUInt(address, changdu);
eeprom_maikuan.writeUInt(address, maikuan);
eeprom_xieyi.writeUInt(address, xieyi);
eeprom_xuhao.commit();
eeprom_leixing.commit();
eeprom_shuju.commit();
eeprom_changdu.commit();
eeprom_maikuan.commit();
eeprom_xieyi.commit();
Serial.print(" 写入地址");
Serial.print(address);
Serial.print(" 的类型:");
Serial.print(leixing);
Serial.print(" 数据:");
Serial.print(shuju);
Serial.print(" 长度:");
Serial.print(changdu);
Serial.print(" 脉宽:");
Serial.print(maikuan);
Serial.print(" 协议:");
Serial.println(xieyi);
delay(5);
Serial.println("-------------------------------写入数据成功---------------------------");
myShou315.resetAvailable();
}
}
}
void shou433() {
digitalWrite(R315EN, HIGH);
digitalWrite(R433EN, LOW); //接收315
delay(100);
if (myShou433.available())
{
value = myShou433.getReceivedValue();
leixing = eeprom_leixing.readUInt(address);
shuju = eeprom_shuju.readUInt(address);
Serial.print(" 接收数据类型为:");
Serial.println("433");
Serial.print(" 数据为:");
Serial.println(value);
if ((value == shuju) && (leixing == 433)) {
Serial.println(" -----------------重复接收---------------------------- ");
myShou433.resetAvailable();
} else {
Serial.println("-------------------新数据接收成功------------------------");
//myShou433.getReceivedValue(), myShou433.getReceivedBitlength(), myShou433.getReceivedDelay(), myShou433.getReceivedRawdata(),myShou433.getReceivedProtocol());
shuju = myShou433.getReceivedValue();
changdu = myShou433.getReceivedBitlength();
maikuan = myShou433.getReceivedDelay();
xieyi = myShou433.getReceivedProtocol();
address += sizeof(unsigned int);
init_address() ;
eeprom_xuhao.writeUInt(0, address); //写地址
eeprom_leixing.writeUInt(address, L433);
eeprom_shuju.writeUInt(address, shuju);
eeprom_changdu.writeUInt(address, changdu);
eeprom_maikuan.writeUInt(address, maikuan);
eeprom_xieyi.writeUInt(address, xieyi);
eeprom_xuhao.commit();
eeprom_leixing.commit();
eeprom_shuju.commit();
eeprom_changdu.commit();
eeprom_maikuan.commit();
eeprom_xieyi.commit();
Serial.print(" 写入地址");
Serial.print(address);
Serial.print(" 的类型:");
Serial.print("433");
Serial.print(" 数据:");
Serial.print(shuju);
Serial.print(" 长度:");
Serial.print(changdu);
Serial.print(" 脉宽:");
Serial.print(maikuan);
Serial.print(" 协议:");
Serial.println(xieyi);
delay(5);
Serial.println("-------------------------写入数据成功-----------------------------");
myShou433.resetAvailable();
}
}
}
void init_address() {
if (address > 1280) {
address = 1;
// for (i = 0; i < 300; i++) {
//EEPROM.write(i, 0);
// }
}
}
void eeprom_init() {
for (i = 0; i < eeprom_xuhao.length(); i++) {
eeprom_shuju.writeByte(i, 0);
// Serial.println(i);
}
for (i = 0; i < eeprom_shuju.length(); i++) {
eeprom_shuju.writeByte(i, 0);
// Serial.println(i);
}
for (i = 0; i < eeprom_leixing.length(); i++) {
eeprom_leixing.writeByte(i, 0);
// Serial.println(i);
}
for (i = 0; i < eeprom_changdu.length(); i++) {
eeprom_changdu.writeByte(i, 0);
// Serial.println(i);
}
for (i = 0; i < eeprom_maikuan.length(); i++) {
eeprom_maikuan.writeByte(i, 0);
// Serial.println(i);
}
for (i = 0; i < eeprom_xieyi.length(); i++) {
eeprom_xieyi.writeByte(i, 0);
// Serial.println(i);
}
eeprom_xuhao.writeUInt(0, 1);
address=1;
eeprom_xuhao.commit();
eeprom_leixing.commit();
eeprom_shuju.commit();
eeprom_changdu.commit();
eeprom_maikuan.commit();
eeprom_xieyi.commit();
delay(10);
Serial.println("数据清空完毕");
}