Hello everyone,
(originally posted in the wrong topic at: HC-12 433MHz module problems programming - Programming Questions - Arduino Forum)
I have difficulties programming an Arduino and HC-12 433 MHz module to transmit and (more importantly) receive a readable and useable text string. In short, I need to transmit an "ALARM" or whatever string from one HC-12 to the other, and identify it by means of an Arduino to take some action.
The idea is to connect an autonomous alarm system in one building with a GSM alarm system in the other building wirelessly. In my case only one alarm activation will be allowed by the GSM Alarm manufacturer (hence, no re-arming required), but still can't get it working.
So long the Tx code is:
#include <SoftwareSerial.h>
SoftwareSerial hc12(9,11); // RX, TX
int PCNPin = 3; // dry contact of the autonomous alarm system
boolean alarmOn = false;
void setup() {
pinMode(7,OUTPUT); // setPin on hc12
pinMode(PCNPin,INPUT_PULLUP); // pin connected to PCN3 (output) of the alarm system
hc12.begin(9600); // Start the software serial port
// setup of the hc12 module
digitalWrite(7,LOW); // enter AT command mode
hc12.print(F("AT+DEFAULT\r\n")); // 9600, CH1, FU3, (F) to bypass flash memory
delay(100);
digitalWrite(7,HIGH); // enter transparent mode
}
void loop() {
if(digitalRead(PCNPin) == LOW && alarmOn == false) {
for(int i = 0; i < 5; i++)
{
hc12.println("ALARM");
delay(1000);
}
alarmOn = true; // After five tries (to eliminate any interference) Arduino can go to sleep
hc12.end();
}
}
And the draft Rx code goes:
#include <SoftwareSerial.h>
SoftwareSerial hc12(9,11); // RX, TX
int relayPin = 3;
boolean alarmOn = false;
void setup() {
pinMode(7,OUTPUT); // setPin on hc12
pinMode(relayPin, OUTPUT);
Serial.begin(9600); // Start the hardware serial port
hc12.begin(9600); // Start the software serial port
// setup of the hc12 module
digitalWrite(7,LOW); // enter AT command mode
hc12.print(F("AT+DEFAULT\r\n")); // 9600, CH1, FU3, (F) to bypass flash memory
delay(100);
digitalWrite(7,HIGH); // enter transparent mode
}
void loop() {
if(hc12.available() > 0 && alarmOn == false) { //TO BE MODIFIED!!!
Serial.write(hc12.read());
if(Serial.find("ALARM")) {
digitalWrite(relayPin, HIGH); // The control line of the GSM alarm system to be cut only once
delay(5000);
digitalWrite(relayPin, LOW);
alarmOn = true;
}
}
}
In fact, this is merely my raw draft, Not sure that even "hc12.println()" is the best choice...
I'm a complete beginner and don't know how to correctly receive a String with HC-12 (hope, the Tx is correct) and identify the trigger word "ALARM" in it. To keep it human-friendly I would like to maintain the word "Alarm" or the like. All I have tried so far doesn't work. Any advice would be so much appreciated! Thanks)