Arduino radio hc-12: Difference between revisions

From wikiluntti
Line 129: Line 129:
== Transmit and Receive floating point numbers ==
== Transmit and Receive floating point numbers ==


https://forum.arduino.cc/t/transferring-float-data-via-hc12/515156/8


<syntaxhighlight lang="C">
union{
  float num;
  uint8_t bytes[sizeof(float)];
} f_tx;


#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int ref = 0;
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}
void loop() {
  if (ref > 9) {
    ref = 0;
  }else {
    ref ++;
  f_tx.num = ref/10;
 
  // if (mySerial.available()) {  (this does not work when I remove the //)
    mySerial.write(sizeof(float)); //start byte
    for(uint8_t i=0; i<sizeof(float);++i){
    mySerial.write(f_tx.bytes[i]);
    delay(100);
  }
  Serial.print("f_tx.num =      ");
  Serial.println(f_tx.num,DEC);
  delay(100);
  // }
}
</syntaxhighlight>
<syntaxhighlight lang="C">
</syntaxhighlight>


== Transmit Tx ==
== Transmit Tx ==

Revision as of 16:13, 1 February 2025

Introduction

LoRa, hc12, hc-12

  • Frequency band is from 433.4 MHz to 473.0 MHz
  • 100 channels with a stepping of 400 KHz between each channel
  • Transmitting power is from -1dBm (0.79mW) to 20dBm (100mW). A half-duplex 20 dBm (100 mW) transmitter
  • Receiving sensitivity is from -117dBm (0.019pW) to -100dBm (10pW)

Connect a 22 µF to 1 mF reservoir capacitor in parallel with the HC-12 "Gnd" and "Vcc" pins.

Terminal program

Terminal br@dy https://sites.google.com/site/terminalbpp/

PuTTY (on Windows):

Set

Pins:

  • Set pin of the module to Ground (set the pin to low logic level).
  • Vcc 3.3V and GND to GND
  • TXD -> 11
  • RXD -> 10

Note that the pins needs to be reversed: The TX pin in the HC-12 needs to be plugged to RX pin of the Arduino.

The syntax of the softaware serial is:

SoftwareSerial(rxPin, txPin, inverse_logic)

Code for setting AT commands:

#include <SoftwareSerial.h>
SoftwareSerial HC12(11, 10);      // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600); 
  HC12.begin(9600); 
}

void loop() {
  while (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
}

AT commands. The most important commands are (remember to use capital letters)

  1. AT – test command. It will return OK if AT interface is enabled
  2. AT+Bxxxx – set serial port baud rate. For example, AT+B57600 set baud rate to 57600bps, AT+B9600. Available baud rates: 1200 bps, 2400 bps, 4800 bps, 9600 bps, 19200 bps, 38400 bps, 57600 bps, and 115200 bps. Default: 9600 bps.
  3. AT+Cxxx – set radio channel. Channels start from 001 at 433,4MHz. Each next channel adds 400kHz. Channel 100 is 473,0MHz. AT+C002 will set frequency to 433,8MHz. Two HC-12 devices that creates a wireless link have to operate on the same frequency
  4. AT+FUx – set device mode: FU1, FU2, FU3 or FU4. Two HC-12 devices that creates a wireless link have to use the same mode
  5. AT+Px – set device transmitting power. For example AT+P2 sets power to 2dBm (1.6mW)
    1. -1dBm (0.8mW)
    2. 2dBm (1.6mW)
    3. 5dBm (3.2mw)
    4. 8dBm (6.3mW)
    5. 11dBm (12mW)
    6. 14dBm (25mW)
    7. 17dBm (50mW)
    8. 20dBm (100mW)
  6. AT+RX – retrieve all parameters: mode, channel, baud rate, power
  7. AT+V – retrieve module version
  8. AT+DEFAULT – reset module parameters to default settings

Software Serial Commands

HC-12

  • print (turns number into a string and sends it)
  • write (Serial.write() sends one byte)
  • Serial.flush() function does not empty the input buffer. It is only relevant when the Arduino is sending data and its purpose is to block the Arduino until all outgoing the data has been sent.

Transmit and Receive text

Transmit numbers continuously as text with one sec in between:

#include <SoftwareSerial.h>
SoftwareSerial HC12(11, 10);      // HC-12 TX Pin, HC-12 RX Pin

int x = 100;
unsigned long previousMillis = 0; 
const long interval = 1000;  

void setup() { 
 HC12.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      x = x + 1;
      HC12.println(x);
  }
}

and receive it using

#include <SoftwareSerial.h>
SoftwareSerial HC12(11, 10);      // HC-12 TX Pin, HC-12 RX Pin

void setup() { 
 Serial.begin(9600);
 HC12.begin(9600);
}

void loop() {
  if(HC12.available()) {   
    String texto = HC12.readStringUntil('\n');
    Serial.println(texto);
  }
 }

(Source: https://community.appinventor.mit.edu/t/radio-frequency-hc-12-arduino-bluetooth-hc-06/60935/3)

Transmit and Receive floating point numbers

https://forum.arduino.cc/t/transferring-float-data-via-hc12/515156/8

union{
  float num;
  uint8_t bytes[sizeof(float)];
} f_tx;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int ref = 0;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}
void loop() {
  if (ref > 9) {
    ref = 0;
  }else {
    ref ++;
 }  
  f_tx.num = ref/10;
  
  // if (mySerial.available()) {  (this does not work when I remove the //)
    mySerial.write(sizeof(float)); //start byte
    for(uint8_t i=0; i<sizeof(float);++i){
    mySerial.write(f_tx.bytes[i]);
    delay(100);
  } 
  Serial.print("f_tx.num =      ");
  Serial.println(f_tx.num,DEC);
  delay(100);
  // }
}


Transmit Tx

Receive Rx

References

https://forum.arduino.cc/t/serial-input-basics-updated/382007/2

https://duckduckgo.com/?q=Serial.parseFloat()&ia=web