HC-12 Transceiver Teletype App - Arduino
// 2018 GordPayne Back to HausOfPayne/Arduino
// Have two computers running Arduino Serial Monitor communicate with each other via HC-12 transceivers
#include <SoftwareSerial.h>
SoftwareSerial HC12(2, 3); // HC-12 TX Pin, HC-12 RX
String myName = "";
String msg = "";
boolean nameSet = false;
boolean msgSent = true;
boolean mbFlag = false;
String theMsg = "";
void setup() {
Serial.begin(9600);
HC12.begin(9600);
Serial.println("Enter your name. End with a ':'");
}
void loop() {
if (Serial.available() > 0) {
if (nameSet == false) {
setName();
}
else {
buildMsg();
}
}
else {
delay(200);
if ((msgSent == false) && (theMsg.length() == 0)) {
sendMsg();
}
}
String theString = readALine(); // read the data stream and store it in a string
if (theString.length() > 0)Serial.println(theString); // if there's data in the string, print it out
}
void setName() {
char theChar = Serial.read();
myName = myName + theChar;
Serial.flush();
if (theChar == ':') {
nameSet = true;
Serial.println(myName);
}
}
void buildMsg() {
char theChar = Serial.read();
msg = msg + theChar;
mbFlag = true;
msgSent = false;
}
void sendMsg() {
theMsg = myName + " " + msg;
for (int i = 0; i < theMsg.length(); i++) {
HC12.write(theMsg.charAt(i));
}
Serial.println(theMsg);
theMsg = "";
msg = "";
msgSent = true;
}
String readALine() { // read the data stream and build a string with the characters
//Serial.println("in readline");
int incomingByte;
String buildString = "";
while (HC12.available() > 0) {
delay(5);// delay before next grab from buffer
incomingByte = HC12.read();// read the incoming byte:
buildString = buildString + char(incomingByte);
}
if ((!HC12.available() > 0) && (buildString.length() > 0)) { // if we've got characters and the data stream now empty
return buildString;
} else {
return "";
}
}
// Have two computers running Arduino Serial Monitor communicate with each other via HC-12 transceivers
#include <SoftwareSerial.h>
SoftwareSerial HC12(2, 3); // HC-12 TX Pin, HC-12 RX
String myName = "";
String msg = "";
boolean nameSet = false;
boolean msgSent = true;
boolean mbFlag = false;
String theMsg = "";
void setup() {
Serial.begin(9600);
HC12.begin(9600);
Serial.println("Enter your name. End with a ':'");
}
void loop() {
if (Serial.available() > 0) {
if (nameSet == false) {
setName();
}
else {
buildMsg();
}
}
else {
delay(200);
if ((msgSent == false) && (theMsg.length() == 0)) {
sendMsg();
}
}
String theString = readALine(); // read the data stream and store it in a string
if (theString.length() > 0)Serial.println(theString); // if there's data in the string, print it out
}
void setName() {
char theChar = Serial.read();
myName = myName + theChar;
Serial.flush();
if (theChar == ':') {
nameSet = true;
Serial.println(myName);
}
}
void buildMsg() {
char theChar = Serial.read();
msg = msg + theChar;
mbFlag = true;
msgSent = false;
}
void sendMsg() {
theMsg = myName + " " + msg;
for (int i = 0; i < theMsg.length(); i++) {
HC12.write(theMsg.charAt(i));
}
Serial.println(theMsg);
theMsg = "";
msg = "";
msgSent = true;
}
String readALine() { // read the data stream and build a string with the characters
//Serial.println("in readline");
int incomingByte;
String buildString = "";
while (HC12.available() > 0) {
delay(5);// delay before next grab from buffer
incomingByte = HC12.read();// read the incoming byte:
buildString = buildString + char(incomingByte);
}
if ((!HC12.available() > 0) && (buildString.length() > 0)) { // if we've got characters and the data stream now empty
return buildString;
} else {
return "";
}
}