There doesn't appear to be a lot of documentation I can find when it comes to using the HC-12 transceivers with Arduino. I am trying to do something that, in theory, should be simple but I'm hitting some odd roadblocks.
I first tried the simplest of things from this video here - Getting startet with the HC-12 and Arduino for wireless communication - from Banggood - YouTube
//send
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Hello World");
delay(2000);
}
//Receive
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if(Serial.available() > 0)
{
String input = Serial.readString();
if(input == "Hello World")
{
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
}
}
}
It didn't work so I thought the modules might be faulty. I then found this website and their code worked so I knew the modules were functioning. Arduino HC12 example 2019 library range
//send
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int pot = A2;
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
pinMode(pot,INPUT);
}
void loop() {
int val = map(analogRead(pot),0,1024,0,255);
HC12.write(val); // Send that data to HC-12
}
//Receive
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int LED = 3;
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
pinMode(LED,OUTPUT);
}
void loop() {
while (HC12.available()) { // If HC-12 has data
int val = HC12.read();
Serial.println(HC12.read()); // Send the data to Serial monitor
analogWrite(LED,val);
}
}
What I need to do is push a button on one side and an led turns off for 2 seconds on the other side and then back on. So I then found this code on a website:
//send
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX
int buttonPin = 8;
boolean onOff = 0;
void setup() {
pinMode(buttonPin, INPUT);
mySerial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);//read button state
if(buttonState == 1){//if button is down
mySerial.println(1111);//send unique code to the receiver to turn on. In this case 1111
onOff = 1;//set boolean to 1
}
if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
mySerial.println(0000);//send unique code to the receiver to turn off. In this case 0000
}
delay(20);//delay little for better serial communication
}
//Receive
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int ledPin = 13;
void setup() {
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if(mySerial.available() > 1){
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(input == 1111){//if on code is received
digitalWrite(ledPin, HIGH);//turn LED on
}
if(input == 0000){//if off code is received
digitalWrite(ledPin, LOW);//turn LED off
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs
delay(20);//delay little for better serial communication
}
However this is also a no go. I'm more than confused why one worked fine and two did not. The only change I can see is that in the one that worked, it used HC12 to call functions instead of mySerial. I have also noticed some wiring shows RX to RX and TX to TX and others have them swapped.
I'm not sure if certain HC-12 modules require different stuff or what. Anyone have any suggestions? I'm currently using a Mega and an Uno until I get some Nanos in.