• Basic source code

    Yann Guidon / YGDES10/14/2021 at 20:36 0 comments

    The program is quite simple. This version only displays the current configuration. Many AT commands are available (look at the datasheet) and are commented out, enable them at your convenience.

    /*
     * HC12config.ino
     * created 20211014 by Yann Guidon
     * 
     * Configure a HC12 module for custom operation.
     * Not interactive : define the parameters in the #defines.
     * 
     * Connections:
     * 
     * Arduino:   HC12:
     *  D4        RXD
     *  D5        TXD
     *  D6        SET
     */
    
    #include "Arduino.h"
    #include "SoftwareSerial.h"
    
    #define INIT_BPS    (9600)
    
    #define HC12RX    (4)
    #define HC12TX    (5)
    #define HC12SET   (6)
    #define LED_RED  (13)
    
    SoftwareSerial HC12(HC12TX, HC12RX);
      // actual order from the Arduino point of view : RX, TX
    
    void HC12_flush() {
      while (HC12.available()) {
        Serial.print("Flushing ");
        Serial.println(HC12.read());
      }
    }
    
    // Put the HC12 is config mode:
    void HC12_SET() {
      pinMode(HC12SET, OUTPUT);
      digitalWrite(HC12SET, 0);
    }
    
    // Exit from config mode:
    void HC12_UNSET() {
      pinMode(HC12SET, INPUT_PULLUP);
    }
    
    // Send a given command and display the result
    void ATcmd(const char* cmd) {
      for (uint8_t i=0; cmd[i]; i++) {
         HC12.write(cmd[i]);
         Serial.write(cmd[i]);
      }
      HC12.write(13); // replace the trailing null byte
         // by the <CR> character.
      Serial.print('\n');
      
      delay(200); // Might need increase if speed is very low
      while (HC12.available()) {
        Serial.write(HC12.read());
      }
    }
    
    void setup() {
      pinMode(LED_RED, OUTPUT);
      digitalWrite(LED_RED, 1);
      Serial.begin(9600);
      Serial.println("Probing HC12...");
      HC12_UNSET();
    
      HC12.begin(INIT_BPS);
      delay(100);
      HC12_SET();
      delay(100);
      HC12_flush();
    
      // First check : send the AT command
      // and expect AT<CR><LF>
      HC12.write('A');
      HC12.write('T');
      HC12.write(13); // optional ?
      delay(200);
    
      uint8_t a=HC12.available();
      if ((a != 4)
       || (HC12.read() != 'O')
       || (HC12.read() != 'K')
       || (HC12.read() != 13)
       || (HC12.read() != 10)) {
        Serial.print(a);
        Serial.println(" char received.\nHC12 KO ?\n");
        HC12_flush();
        return;
      }
    
    //  ATcmd("AT+DEFAULT"); // Restore default settings
      ATcmd("AT+V");       // display firmware version
    //  ATcmd("AT+C042");    // change to channel 42
    //  ATcmd("AT+B1200");   // change serial speed
    //  ATcmd("AT+FU4");     // sets the interface to 1200bps, and OTA to 500bps
      ATcmd("AT+RX");       // Display all the configuration parameters
    
      HC12_UNSET();
      Serial.println("\nHC12 ok\n");   
    }
    
    void loop() {
      // nothing to be done here.
      digitalWrite(LED_RED, 0);
    }
    

    Operation is pretty simple :

    1. Wire the boards as shown in the code.
    2. Open the source code in the Aruino IDE
    3. Plug the board on the USB port.
    4. Open the serial monitor to display all the messages from the Arduino board.
    5. Compile & upload the code
    6. Examine the messages on the monitor : If "HC12 KO ?" appears, check your connections and the settings, in particular INIT_BPS. For example when INIT_BPS=9600 and the HC12 is at 1200bps, the flush will display a number of 128 and 0.
    7. The ATcmd() calls are executed: So far only configuration parameters are read. You can uncomment the desired commands to suit your needs.
    8. Goto 5 until you reach the configuration you desire.

    .