Good day everyone,
I have picked up a HC-12 Wireless Serial Module. It supports frequencies from 433.4-473.0MHz in 400KHz steps. I have some RF controlled wall plugs (similar to these) in my home and would like to use the powerful HC-12 to switch them. I already have an a lot weaker RF transmitter (similar to this one) which works fine.
My problem: When I try to send the same decimal codes, which work using the weaker transmitter, nothing is happening. Also the HC-12 does not pickup RF signals sent by the plug's remote or the other transmitter. Could someone point me in the right direction please?
I have found some very good looking code on AAC which I uploaded to a NodeMCU. I can send AT+? commands to change the HC-12's settings and receive its responses without any problems.
Thanks for any help in advance!
That's the code:
I have picked up a HC-12 Wireless Serial Module. It supports frequencies from 433.4-473.0MHz in 400KHz steps. I have some RF controlled wall plugs (similar to these) in my home and would like to use the powerful HC-12 to switch them. I already have an a lot weaker RF transmitter (similar to this one) which works fine.
My problem: When I try to send the same decimal codes, which work using the weaker transmitter, nothing is happening. Also the HC-12 does not pickup RF signals sent by the plug's remote or the other transmitter. Could someone point me in the right direction please?
I have found some very good looking code on AAC which I uploaded to a NodeMCU. I can send AT+? commands to change the HC-12's settings and receive its responses without any problems.
Thanks for any help in advance!
That's the code:
Code:
#include <SoftwareSerial.h>
const byte HC12RxdPin = 4; // "RXD" Pin on HC12
const byte HC12TxdPin = 5; // "TXD" Pin on HC12
const byte HC12SetPin = 6; // "SET" Pin on HC12
unsigned long timer = millis(); // Delay Timer
char SerialByteIn; // Temporary variable
char HC12ByteIn; // Temporary variable
String HC12ReadBuffer = ""; // Read/Write Buffer 1 for HC12
String SerialReadBuffer = ""; // Read/Write Buffer 2 for Serial
boolean SerialEnd = false; // Flag to indicate End of Serial String
boolean HC12End = false; // Flag to indiacte End of HC12 String
boolean commandMode = false; // Send AT commands
// Software Serial ports Rx and Tx are opposite the HC12 Rx and Tx
// Create Software Serial Port for HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin);
void setup() {
HC12ReadBuffer.reserve(64); // Reserve 64 bytes for Serial message input
SerialReadBuffer.reserve(64); // Reserve 64 bytes for HC12 message input
pinMode(HC12SetPin, OUTPUT); // Output High for Transparent / Low for Command
digitalWrite(HC12SetPin, HIGH); // Enter Transparent mode
delay(80); // 80 ms delay before operation per datasheet
Serial.begin(9600); // Open serial port to computer
HC12.begin(9600); // Open software serial port to HC12
}
void loop() {
while (HC12.available()) { // While Arduino's HC12 soft serial rx buffer has data
HC12ByteIn = HC12.read(); // Store each character from rx buffer in byteIn
HC12ReadBuffer += char(HC12ByteIn); // Write each character of byteIn to HC12ReadBuffer
if (HC12ByteIn == '\n') { // At the end of the line
HC12End = true; // Set HC12End flag to true
}
}
while (Serial.available()) { // If Arduino's computer rx buffer has data
SerialByteIn = Serial.read(); // Store each character in byteIn
SerialReadBuffer += char(SerialByteIn); // Write each character of byteIn to SerialReadBuffer
if (SerialByteIn == '\n') { // Check to see if at the end of the line
SerialEnd = true; // Set SerialEnd flag to indicate end of line
}
}
if (SerialEnd) { // Check to see if SerialEnd flag is true
if (SerialReadBuffer.startsWith("AT")) { // Has a command been sent from local computer
HC12.print(SerialReadBuffer); // Send local command to remote HC12 before changing settings
delay(100); //
digitalWrite(HC12SetPin, LOW); // Enter command mode
delay(100); // Allow chip time to enter command mode
Serial.print(SerialReadBuffer); // Echo command to serial
HC12.print(SerialReadBuffer); // Send command to local HC12
delay(500); // Wait 0.5s for a response
digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
delay(100); // Delay before proceeding
} else {
HC12.print(SerialReadBuffer); // Transmit non-command message
}
SerialReadBuffer = ""; // Clear SerialReadBuffer
SerialEnd = false; // Reset serial end of line flag
}
if (HC12End) { // If HC12End flag is true
if (HC12ReadBuffer.startsWith("AT")) { // Check to see if a command is received from remote
digitalWrite(HC12SetPin, LOW); // Enter command mode
delay(100); // Delay before sending command
Serial.print(SerialReadBuffer); // Echo command to serial.
HC12.print(HC12ReadBuffer); // Write command to local HC12
delay(500); // Wait 0.5 s for reply
digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
delay(100); // Delay before proceeding
HC12.println("Remote Command Executed"); // Acknowledge execution
} else {
Serial.print(HC12ReadBuffer); // Send message to screen
}
HC12ReadBuffer = ""; // Empty buffer
HC12End = false; // Reset flag
}
}