I am using a custom ESP-32 microcontroller, which was designed one of my colleagues who isn’t working here anymore.
Upon updating the wiring according to the schematic of my board, here is my new code:
#include <HardwareSerial.h>
#define RXD1 18 //(RX2)
#define TXD1 17 //(TX2)
HardwareSerial HC12(1);
void setup()
{
Serial.begin(115200); // Serial port to computer
pinMode(5, OUTPUT);
digitalWrite(5, LOW); //Normally HIGH, LOW for settings
delay(1000);
HC12.begin(9600, SERIAL_8N1, RXD1, TXD1); // Serial port to HC12
if (!HC12) { // If the object did not initialize, then its configuration is invalid
Serial.println("Invalid EspSoftwareSerial pin configuration, check config");
while (1) { // Don't continue with invalid configuration
delay (1000);
}
}
}
void loop() {
while (HC12.available())
{
Serial.println("Received Data from HC12");
Serial.print("r = ");
Serial.println(HC12.read());
}
while (Serial.available())
{
Serial.println("Reading user serial input");
String userInput = Serial.readString();
// what are we about to send?
Serial.print("sending w = ");
Serial.println(userInput);
// punch it!
for(char dataByte : userInput)
{
HC12.write(dataByte);
}
}
}
I am assuming since it says TX1 and RX1, this is Serial 1? That’s why I created the HardwareSerial object HC12(1) instead of HC12(2). However, I still get no response.
Also, I tried to create a SoftwareSerial object using the following code:
#include <SoftwareSerial.h>
#define MYPORT_TX 17
#define MYPORT_RX 18
EspSoftwareSerial::UART myPort;
void setup(){
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
Serial.begin(115200); // Standard hardware serial port
myPort.begin(9600, SWSERIAL_8N1, MYPORT_RX, MYPORT_TX, false);
delay(1000);
if (!myPort) { // If the object did not initialize, then its configuration is invalid
Serial.println("Invalid EspSoftwareSerial pin configuration, check config");
while (1) { // Don't continue with invalid configuration
delay (1000);
}
}
else{
Serial.println("It works??");
}
}
void loop(){
while (myPort.available()) { // If HC-12 has data
Serial.write(myPort.read()); // Send the data to Serial monitor
}
while (Serial.available()) { // If Serial monitor has data
myPort.write(Serial.read()); // Send that data to HC-12
}
}
But I yet again didn’t get any results. The code prints out “It works??” which means that the HC12 is initialized, but I get no response when I type in “AT”.
Did I make any mistake by making any of these assumptions?
I would give HC12(2) a go. Normally the first serial port will be defined as the USB connection but it is hard to say given that this is a custom ESP32. Do you get a different result if you change that value in the code?
GPIO17/18 are not default pins for any ESP32 that I can see. According to the HardwareSerial library it is ‘0’ and ‘1’ that have defaults, but ‘2’ requires the GPIO to be nominated. According to the ESP documentation UART0 and UART2 will default, and UART1 can be (and sometimes must be) explicitly reconfigured. In that case using SoftwareSerial and nominating the GPIO pins is perhaps a safer option. AFAIK 17/18 is as good a choice as any, but you should confirm that the schematic really does match the board, because 17/16 seems more likely.
Your ‘OK’ message confirms that the serial port was configured - it does not confirm that there is anything attached to the port, or if something is attached, that it is working.
Do you have another MCU handy? A UNO or similar can be configured with a software serial port and some code to copy everything received to console, and everything typed at console to the port. That way you can confirm send and receive separately, and you aren’t relying on the HC12 to respond. That removes one piece from the puzzle.
Sorry, for this could I check which message you are referring to? I didn’t receive back any “OK” upon sending “AT” for any of the serial ports.
I have an Arduino Uno, and I received back “OK” when I entered “AT” there, which means the HC-12 isn’t the problem. I have already tried to get the esp-32 and HC-12 to communicate by getting the esp-32 to send a float value using the HC-12 and reading it using another HC-12 on the Arduino Uno, but the Uno didn’t seem to have received any reading. Is that what you want me to check?
So the possibilities are:
The HC12 did not receive the ‘AT’. Sending from the MCU to the HC12 is not working.
The HC12 received the ‘AT’ but didn’t respond (wrong mode).
The HC12 Received the ‘AT’ and responded but the MCU didn’t see it. Receiving to the MCU from the HC12 isn’t working.
To narrow down those possibilities, connect a different device in place of the HC12 so you can see the data being sent and received from both devices. An Arduino configured with a simple program to send all data received on the software serial port to the console on the hardware serial port, and send all console data to the software serial port will do that. Note that you can have multiple Arduino IDEs open at the same time - one for ESP32 to send commands and see the response, and one for the Arduino to see the data received and enter the data for the response.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX eg for Arduino
void setup() {
// Open serial communications for console
Serial.begin(115200);
// Open serial port for DUT
mySerial.begin(9600);
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Sorry, I’m slightly confused, are you suggesting I connect the RX and TX pin on my esp32 to the RX and TX pin respectively on my Arduino, and there should be no HC-12 involved?
Yes. The Arduino is being used as a simple serial comms test device. You need to prove that your code is referring to the GPIO pins that you have the device connected to (or the other way around) and that those pins are usable for serial I/O at the baud rate you have selected. You are, in effect, mimicking the HC12, with the difference that you can observe everything that happens on the console. Without knowing for sure that you have the serial setup correct any investigation of the HC12 operation could be a waste of time. But note that you will connect RX to TX and TX to RX (assuming the pin identification is consistent). Note that this test does not confirm the required line termination of the commands - that still needs to be done by experiment once you have confirmed the serial I/O setup and reconnected the HC12.
I think that you skipped this step because you assumed that the initial number that was being displayed was coming from the HC12, but that was more likely just something left over from the power-on procedure.
Hi, while waiting for your confirmation, I was reading on another forum and decided to connect the Tx pin on my HC-12 and to the RXD pin on my esp32, and connect the Rx pin on my HC-12 pin on my HC-12 to the TXD pin on my esp32. After selecting serial 2, and sending the “AT” command, I now get back “OK”! I think you suggested doing this in your latest message too.
If I want to transmit data now, should I set the “SET” pin to HIGH and keep the connections as they are?
The official ESP32 datasheet says that the IO PINs and ESP32 are “not” 5v tolerant.
While some users have tested 5v and state it was OK, I would tend to stick to the datasheet and be safe.
As such I would always recommend putting in a logic level shifter if connecting the ESP32 (uart etc) to a 5V uart etc.
My understanding is that the HC-12 can run at 3.3 or 5v, so it may be OK. But to be safe I would ensure your supply voltage be within the safe range and match your micro controller (e.g. 3.3 V for the ESP32).
(So if connecting to different devices/uC, either check the supply and pin voltages match (or are safe based on the datasheet) or put in a line level converter.
Note: I accept the ESP32 3.3V v 5Volt is a big discussion out there, but for safety, stick to what the datasheet says.
The reason I say this is it seems things got connected to the ESP32 then the Uno (which, from memory is 5v)
Some devices state the IO pin save voltage is the Vdd + something, so while it may be 5v safe, it may only be safe if run at 5v.