LoRa Module HC12 Demo Code (push button)

 Lora Module HC 12 Transmitter and receiver Code (push button)







This is For Transmitter ==

.....................................................................................................................................

//HC-12 Momentary Button Send

#include <SoftwareSerial.h>


SoftwareSerial mySerial(10, 11); //RX, TX


int buttonPin = 9;

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

}


..................................................................................................................................


This Is for Receiver ==


//HC-12 Momentary Button Receive

#include <SoftwareSerial.h>


SoftwareSerial mySerial(10, 11); // RX, TX


int ledPin = 2;


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

 

}

....................................................................................................................

Comments

Popular posts from this blog