Go Down

Topic: HC-12 & Nano - hardware or software problem? (Read 277 times) previous topic - next topic

Geek Emeritus

May 23, 2019, 09:57 pm Last Edit: May 24, 2019, 10:51 pm by Geek Emeritus
I am starting a program to manage HC-12s. Read the current configuration, or reconfigure as necessary.

It compiles, it tells me it is talking to the HC-12, and nothing comes back. It could be my code, my setup, or my HC-12s. If anyone has a spare HC-12 and a spare minute could you tell me if this works?

Code: [Select]
//based on code from: Tom Heylen

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define stPin 7

SoftwareSerial HC12(rxPin, txPin);
long baud = 9600;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(rxPin,  INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(stPin, OUTPUT);
  Serial.begin(baud);
  Serial.println(); Serial.println();
  while (!Serial) // wait for serial port to connect. Needed for native USB port only
  {
    ;
  }
  digitalWrite(stPin, HIGH);
  Serial.println("HC-12 Test & configure");
  HC12.begin(9600);
  Serial.println("HC-12 started"); 
  if (HC12.isListening())
  {
   Serial.println("HC-12 is Listening ");
  }
  else
  {
    Serial.println("NOK");
  }
  {
   if (!HC12.available())
   Serial.println("HC-12 is not responding");
   //while(1);
  }

  //test HC-12
  digitalWrite(stPin, LOW);
  delay(100);
  Serial.println("Sending AT+RX");
  HC12.print("AT+RX");                // Send different commands to see which method works
  Serial.println("command sent");
  Serial.println("setup end"); Serial.println();
  delay(500);
  digitalWrite(stPin, HIGH);
}

void loop()
{
  if (HC12.available())
 
   {
    Serial.print("HC12.available()");
   }
   
  while (HC12.available())         // If HC-12 has data
  {
    Serial.write(HC12.read());     // Send the data to Serial monitor
  }
   digitalWrite(stPin, LOW);
  while (Serial.available())       // If Serial monitor has data
  {
   HC12.write(Serial.read());      // Send that data to HC-12
  }
   digitalWrite(stPin, LOW); 
}
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

cattledog

The data sheets I see indicate that the set pin needs to be GND to be in AT mode. Are you doing that?

Geek Emeritus

All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

cattledog

Code: [Select]
HC12.print("AT"); 

Try different line terminations. I don't know if this module takes a special set, but usually all command lines must start with "AT" and end with a carriage return character. This is for when you are sending an AT command from within the code, and not entering the command in the serial monitor. The line endings from the serial monitor should be echoed in the software serial communication to the module.

Code: [Select]
HC12.print("AT\r"); 
HC12.print("AT\n"); 
HC12.print("A\r\n"); 


Make sure that you are coming out of the set mode when you are not sending AT commands.

When you send an AT command from within the code, you will need code to read the response and sent it back to the monitor. Something like this

Code: [Select]
HC12.print("AT\r");
delay(100);
while (HC12.available() )
{
Serial.print(HC12.read());
}


In general, I think you should be using the .print() instead of .write().



mauried

The HC12 needs the AT commands to be sent with no gaps between the characters, otherwise the command parser times out.

A command like this
HC12.print("AT\r");

will work, and the HC12 will respond with OK usually.

Typing the same chars in the serial monitor wont work.



Geek Emeritus

#5
May 24, 2019, 10:50 pm Last Edit: May 24, 2019, 11:11 pm by Geek Emeritus
4 HC-12s, 2 nanos, tried everything above, always !HC12.available

finally, one works

Code: [Select]
HC-12 Test & configure
HC-12 started
HC-12 is Listening
HC-12 is not responding
Sending AT+RX
command sent
setup end

HC12.available()OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3


so I get to make 2 links with one radio...

amended program in original post

edit: it worked once. I think the HC-12s burned up. they are only getting 3.45 V, and they cooked
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

Go Up