I have been running the same HC12 basic Rx/TX code for a year on Arduino Nano. A few days ago when I uploaded an existing sketch the Nano reset every few seconds. Has there been an update to SoftwareSerial lately or maybe something screwed within my IDE to cause this?
/*02 Slave Rx/Tx
All commands sent from Slaves to Main (Eg: ATxxxx) are acknowledged or this
sketch will be caught in an endless loop
All commands received from Main are acknowledged or error produced
Alpha Numerical Designations:
A = Room #
T = Transmit from Slave
t = acknowledgement from Main
xx = Slave #
yy = command
*/
//Constants
const String Room = "A"; //**** change for each Room ****
const String Slave = ("02"); //**** change for each Slave ****
//variables
String CMD;
String HC12ReadBuffer;
String ReadBuffer;
String SndBuffer;
int i;
#include <SoftwareSerial.h>
const byte HC12RxdPin = 2; // "RXD" Pin on HC12
const byte HC12TxdPin = 3; // "TXD" Pin on HC12
const byte HC12SetPin = 4; // "SET" Pin on HC12
// Create Software Serial Port for HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin);
void setup() {
// ****** setup HC12 communications ******
digitalWrite(HC12SetPin, HIGH); // enter run mode
HC12.begin(9600);
delay(10);
HC12.setTimeout(100); //set speed of HC12 send/poll (default = 1000)
// ****** setup Serial port ********
Serial.begin(19200);
delay(100);
//Report Slave start to Main
CMD = "00"; //where AT0200 = slave 02 is online
CmdToMain(); //send "slave is online" CMD to Main
Serial.println("Command ok");
}//end setup
void loop() {
delay(200);
//add prop code here
}//end main loop
//CMD has String yy ,send "ATxxyy" to Main and wait for "Atxxyy" acknowledge
void CmdToMain() {
SndBuffer = (Room + "T" + Slave + CMD); // SndBuffer = CMD to VB6
ReadBuffer = (Room + "t" + Slave + CMD); // ReadBuffer = anticipated ack return from Main
Serial.println(SndBuffer + " Sent");
i = 0;
sndit:
HC12.println(SndBuffer);
delay(400);
//wait for acknowledge "Atxxyy"
if (HC12.available()){
delay(10);
HC12ReadBuffer = HC12.readString();
HC12ReadBuffer = (HC12ReadBuffer.substring(0,6)); //read 6 bytes
}
i++;
if (i > 40){ //40 attempts = about 10 second delay till error kicks in
Serial.println("Error"); //send error to screen
goto endit;
}
if(ReadBuffer != HC12ReadBuffer){goto sndit;}
Serial.println(ReadBuffer + " Returned");
endit:
delay(10);
}//end CmdToMain
I cleared out most of the WORK part of the routine and the resulting .ino is as above. If I attempt to even program a Nano with an HC12 installed I get constant resets and the following errors:
Arduino: 1.8.6 (Windows 7), Board: "Arduino Nano, ATmega328P (Old Bootloader)"
Sketch uses 6208 bytes (20%) of program storage space. Maximum is 30720 bytes.
Global variables use 395 bytes (19%) of dynamic memory, leaving 1653 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_recv(): read error: Access is denied.
avrdude: stk500_recv(): programmer is not responding
avrdude: ser_send(): write error: sorry no info avail
avrdude: ser_recv(): read error: Access is denied.