This Arduino code updates the HC-12 radio modules' non-volatile configuration
To make the experience fit your profile, pick a username and tell us what interests you.
We found and based on your interests.
HC12config.inoino - 2.42 kB - 10/14/2021 at 20:32 |
|
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 :
.
Create an account to leave a comment. Already have an account? Log In.
Become a member to follow this project and never miss any updates
I noticed something frustrating :
When I set mode AT+FU4, I can't seem to get the AT+B2400 setting to work despite the datasheet saying it's possible, and AT+RX confirming the mode is accepted. When I restart the boad, I can only talk to it with INIT_BPS=1200.