Wemos D1 mini - HC-12 Mhz Module

Hello everyone,

I'm struggling with a project using an HC-12 module and a Wemos D1. This involves an MHz transmitter and receiver.

My goal is to configure the module to operate on the 433.92 MHz frequency and then receive the data transmitted by my remote control. Ideally, the Wemos would also display the protocol and method used by the remote.

Unfortunately, I am already stuck with configuring the module in AT mode because I don't get an OK response. Here is my sketch:

Wiring:
SET -> D5
VCC -> 5V
GND -> GND
TX -> D2
RX -> D1

I read that the SET must be set to HIGH, to setup the AT-Mode. in a other Datashett is called LOW....

#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>

// WiFi-Zugangsdaten
const char* ssid = "xxxx";        // Dein WiFi-Name
const char* password = "xxxx"; // Dein WiFi-Passwort

// HC-12 Pins
#define RX_PIN D1  // RX von HC-12
#define TX_PIN D2  // TX von HC-12
#define SET_PIN D5 // SET-Pin des HC-12 für den AT-Modus

// SoftwareSerial für das HC-12
SoftwareSerial hc12(RX_PIN, TX_PIN);

// Funktionsdeklarationen
void checkHc12Response();
void connectToWiFi();

void setup() {
  // Serielle Kommunikation für Debugging
  Serial.begin(9600);
  delay(10);

  // Verbinde mit WiFi
  connectToWiFi();

  // Set-Pin auf HIGH setzen, um in den AT-Modus zu gehen
  pinMode(SET_PIN, OUTPUT);
  digitalWrite(SET_PIN, LOW); // AT-Modus aktivieren
  delay(3000);  // Eine längere Verzögerung, um sicherzustellen, dass der HC-12 im AT-Modus ist

  // HC-12 Initialisierung
  hc12.begin(9600); // HC-12 Baudrate im AT-Modus
  Serial.println("HC-12 im AT-Modus");

  // AT-Befehl: Testen, ob der HC-12 im AT-Modus ist
  hc12.println("AT");
  delay(3000);  // Warten, um eine Antwort zu erhalten
  // Gebe die Antwort des HC-12 aus
  checkHc12Response();

  // AT-Befehl zum Kanal setzen (z.B. 433,92 MHz Frequenz)
  hc12.println("AT+C001");  // Kanal 1 (433,92 MHz)
  delay(3000);  // Warten, bis die Antwort empfangen wird
  checkHc12Response();

  // AT-Befehl: Sendeleistung auf Mittelstellung setzen
  hc12.println("AT+P10");
  delay(3000);  // Warten, bis die Antwort empfangen wird
  checkHc12Response();

  // Set-Pin auf LOW setzen, um den AT-Modus zu verlassen
  digitalWrite(SET_PIN, HIGH); // AT-Modus verlassen
  delay(5000);  // Warte eine Sekunde, um sicherzustellen, dass der HC-12 den AT-Modus verlässt

  Serial.println("HC-12 konfiguriert und bereit.");
}

void loop() {
  // Wenn Daten im seriellen Monitor eingegeben werden
  if (Serial.available()) {
    String input = Serial.readStringUntil('\n'); // Liest die Eingabe
    Serial.print("Eingabe empfangen: ");
    Serial.println(input);  // Gebe die Eingabe im seriellen Monitor aus

    // Sende die Eingabe über das HC-12
    hc12.println(input);
  }

  // Wenn Daten vom HC-12 empfangen werden
  if (hc12.available()) {
    String receivedData = hc12.readStringUntil('\n');
    Serial.print("Empfangen von HC-12: ");
    Serial.println(receivedData);  // Gib die empfangenen Daten aus
  }

  delay(100);  // Kleine Verzögerung
}

// Funktion zur Überprüfung der Antwort vom HC-12
void checkHc12Response() {
 String response = "";
  while (hc12.available()) {
    response += (char)hc12.read();
  }
  Serial.println(response);
}

// Funktion zur Verbindung mit dem WiFi
void connectToWiFi() {
  // Verbinde mit WiFi
  Serial.println();
  Serial.print("Verbinde mit WiFi...");
  WiFi.begin(ssid, password);

  // Warten, bis die WiFi-Verbindung hergestellt ist
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  // Erfolgreich verbunden
  Serial.println();
  Serial.println("Mit WiFi verbunden!");
  Serial.print("IP-Adresse: ");
  Serial.println(WiFi.localIP());
}

Thank you a lot

I moved your topic to an appropriate forum category @jumbo125 .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thank you
I will do it

initially test the ESP8266 <> HC-12 communications using a simple serial test
make sure you are using EspSoftwareSerial

// ESP8266 to HC-12 Software Serial test

// using library manager download and instal EspSoftwareSerial or instal ZIP
// https://github.com/plerup/espsoftwareserial

#include <SoftwareSerial.h>

// pins Rx GPIO5 (D1) and Tx GPIO 4 (D2)
SoftwareSerial swSer(5, 4);

// for HC-12 connect
// ESP8266 pin D2 TXD to HC-12 RXD
// ESP8266 pin D1 RXD to HC-12 TXD
// connect GND pins together and VCC to 3.3V
// for AT mode connect SET to GND else leave floating

void setup() {
  Serial.begin(115200);  //Initialize hardware serial with baudrate of 115200
  swSer.begin(9600);     //Initialize software serial with baudrate of 9600
  Serial.println("\nESP8266 Software serial test started");
  Serial.println("-  for loopback test connect pin D1 to pin D2\n");
}

void loop() {
  while (swSer.available() > 0) {  //wait for data at software serial
    Serial.write(swSer.read());    //Send data recived from software serial to hardware serial
  }
  while (Serial.available() > 0) {  //wait for data at hardware serial
    char ch = Serial.read();
    Serial.write(ch);      // uncomment if local echo required 
    swSer.write(ch);        //send data recived to software serial
  }
}

when AT commands entered Serial Monitor displays (note SET is taken to GND)

ESP8266 Software serial test started
-  for loopback test connect pin D1 to pin D2

AT
OK
AT+V
www.hc01.com  HC-12_V2.4
AT+RX
OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3

once that works you can start implementing the project

thats all:
ESP8266 Software serial test started

  • for loopback test connect pin D1 to pin D2

if you then type AT commands into the serial monitor (set baudrate to 115200 and line ending to Both NL & CR) it should echo the AT command and display any response

not work :frowning:

can you give a link to specific Wemos D1 module you are using?

what happens if you remove the HC-12 and connect D1 directly to D2
text entered on the keyboard should echo back to the display

i will try it.
connect D1 to D2. correct?

yes, this should create an loopback
keyboard text output on D2 should echo back to D1 to display on serial monitor

photo of my setup - I am using a D1 mini pro ESP8266 connected to the HC-12

The HC-12 module operates on the 433 MHz by default. You don't need to reconfigure the module for using that frequency.

Unless your remote is based on the same HC-12 module, you will not be able to connect the HC-12 to the remote control

this is my setup


my code:

// ESP8266 to HC-12 Software Serial test

// using library manager download and instal EspSoftwareSerial or instal ZIP
// https://github.com/plerup/espsoftwareserial

#include <SoftwareSerial.h>

// pins Rx GPIO5 (D1) and Tx GPIO 4 (D2)
SoftwareSerial swSer(5, 4);

// for HC-12 connect
// ESP8266 pin D2 TXD to HC-12 RXD
// ESP8266 pin D1 RXD to HC-12 TXD
// connect GND pins together and VCC to 3.3V
// for AT mode connect SET to GND else leave floating

void setup() {
  Serial.begin(115200);  //Initialize hardware serial with baudrate of 115200
  swSer.begin(9600);     //Initialize software serial with baudrate of 9600
  Serial.println("\nESP8266 Software serial test started");
  Serial.println("-  for loopback test connect pin D1 to pin D2\n");
}

void loop() {
  while (swSer.available() > 0) {  //wait for data at software serial
    Serial.write(swSer.read());    //Send data recived from software serial to hardware serial
  }
  while (Serial.available() > 0) {  //wait for data at hardware serial
    char ch = Serial.read();
    Serial.write(ch);      // uncomment if local echo required 
    swSer.write(ch);        //send data recived to software serial
  }
}

the pin settings in the code are

// pins Rx GPIO5 (D1) and Tx GPIO 4 (D2)
SoftwareSerial swSer(5, 4);

// for HC-12 connect
// ESP8266 pin D2 GPIO4 Tx to HC-12 RXD
// ESP8266 pin D1 GPIO5 Rx to HC-12 TXD

HC-12 pinout

looking at the photos in post 12 I think you have D1 and D2 connected the wrong way around

another photo of my setup

try exchanging D1 and D2

did ist, now i get a message, when i press a key.
But the message is: ERROR

AT commands are in capital letters, e.g.

at
ERROR
AT
OK

it works now.

but i need to set it variable in AT mode. So i can't connect "set" to "gnd",
I need to set it to a pin, an change with LOW or HIGH the AT mode.
this don't work

#include <Arduino.h>

// ESP8266 to HC-12 Software Serial test

// using library manager download and instal EspSoftwareSerial or instal ZIP
// https://github.com/plerup/espsoftwareserial

#include <SoftwareSerial.h>
#define SET_PIN D5 // SET-Pin des HC-12 für den AT-Modus

// pins Rx GPIO5 (D1) and Tx GPIO 4 (D2)
SoftwareSerial swSer(5, 4);

// for HC-12 connect
// ESP8266 pin D2 TXD to HC-12 RXD
// ESP8266 pin D1 RXD to HC-12 TXD
// connect GND pins together and VCC to 3.3V
// for AT mode connect SET to GND else leave floating

void setup() {
  Serial.begin(9600);  //Initialize hardware serial with baudrate of 115200
  swSer.begin(9600);     //Initialize software serial with baudrate of 9600
  Serial.println("\nESP8266 Software serial test started");
  Serial.println("-  for loopback test connect pin D1 to pin D2\n");
  
  pinMode(SET_PIN, OUTPUT);
  digitalWrite(SET_PIN, LOW); // AT-Modus aktivieren
  delay(1000);
   // AT-Befehl: Testen, ob der HC-12 im AT-Modus ist
  swSer.println("AT");
  swSer.println("AT+C002");
  delay(3000);  // Warten, um eine Antwort zu erhalten
  digitalWrite(SET_PIN, HIGH); // AT-Modus aktivieren
  Serial.println("Fertig konfiguriert");
}

void loop() {
    if (swSer.available()) {
    String receivedData = ""; // Speicher für die empfangenen Daten

    // Lies die verfügbaren Daten
    while (swSer.available()) {
      char c = swSer.read();
      receivedData += c; // Zeichenweise speichern
    }
    
    // Gib die empfangenen Daten auf der seriellen Konsole aus
    Serial.println("Empfangen: " + receivedData);
  }
}

What's exactly"don't work "? The code doesn't print "OK" after your command?
In the code above you start to read hc-12's output too late to see an answer of the module.

Sorry for the long time

it works now. The missing answer from the mhz module, is the failing protokoll.
it isn'T set for raw datas. sorry, its my fail.
so i will pay the fa1000
thank you fir your help