HC-12 Arduino Transmitter

Thread Starter

duff650

Joined Mar 15, 2019
38
I'm looking for some help with the HC-12 modules,

Code:
#include <SoftwareSerial.h>
const byte HC12RxdPin = 9;                      // "RXD" Pin on HC12
const byte HC12TxdPin = 10;                      // "TXD" Pin on HC12
const byte HC12SetPin = 11;                      // "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
  }
}
Two Arduino Uno's are connected to 2 different computers, in different rooms. I have type AT+RX into both units, output:
upload_2019-3-15_22-33-26.png

9600 baud rate
channel 1

code from this thread - https://www.allaboutcircuits.com/pr...enting-the-hc-12-wireless-transceiver-module/

when I type a message into the serial monitor nothing appears on the other unit, I have confirmed they are both on and working (all the AT commands work).

Both boards are Elegoo UNO R3 from Amazon, using Arduino IDE 1.8.8.

Context - Need to transmit a 2 digit number representing a heartrate every 10 seconds, not really important I just need to be able to transmit anything at this point.


Help??
 

Attachments

djsfantasi

Joined Apr 11, 2010
9,237
Have you got it working side-by-side in one room first?

There are some very specific power requirements in the article. Please provide clear pictures of your wiring (and a schematic).
 
Last edited:

AlbertHall

Joined Jun 4, 2014
12,635
That software allows you to send commands from one HC12 to the other.
If you change the power on one does it also change the power on the other?
 

Thread Starter

duff650

Joined Mar 15, 2019
38
Have you got it working side-by-side in one room first?
Yes I moved it to another room to check that they were not too close together, I've tested the code with a direct connection to ensure software serial is working. Attached pictures of connection to HC-12, both connections are identical. I have a third HC-12 I've placed it next to the connection so you can see the pins.

53581817_2149999751709973_8084080245889564672_n.jpg 53877521_1890347041077175_4160881822927945728_n.jpg
upload_2019-3-15_23-5-50.png
 

djsfantasi

Joined Apr 11, 2010
9,237
What is the small circuit board to the right of the second picture?

Read the article you linked to again. There is a very specific power requirement that you appear to be violating. And possibly a second. Just trying to ascertain that power is correct.
 

Thread Starter

duff650

Joined Mar 15, 2019
38
But not right next to each other - they can swamp the receiver set to 20dBm - keep them a couple of metres apart.

Are you using the 'springs' as aerials?
That's why they were in different rooms, the antenna came with the unit, soldered to ANT54204361_1000543120131688_1870792929849638912_n.jpg t
 

Thread Starter

duff650

Joined Mar 15, 2019
38
What is the small circuit board to the right of the second picture?

Read the article you linked to again. There is a very specific power requirement that you appear to be violating. And possibly a second. Just trying to ascertain that power is correct.
That's the circuit, powered with 5V with a diode in series and a capacitor across Vcc and GND to store charge. Both units are responding to AT commands on both computers so I would assume this means that it is powered correctly
 

Thread Starter

duff650

Joined Mar 15, 2019
38
Solved, I set both HC-12 units to FU1 using the AT-FU1 command and then set the power to P2 (AT+P2) which is 2dBm, worked perfectly. Just in time to cancel my order of 2 new units! Thanks for the help.
 
Top