Good evening friends,
I am working on a project that will allow the user to control a 16 channel relay board with a remote transmitter at a distance exceeding 1 km. I decided to utilize LoRa technology to accomplish this. After doing research on the internet I purchased and connected the following parts for the transmitter and receiver:
Transmitter:
Receiver:
I have also drawn a crude rendition of how I have them hooked up and added it as an attachment.
Here is the code that I have running as the transmitter:
#include <IRremote.h>
const int RECV_PIN = A7;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//removed following code because when nano is connected it sends serial data to the usb not the lora module
delay(50);
//Serial.print("AT+IPR=115200\r\n");
//delay(50);
//Serial.print("AT+ADDRESS=2\r\n");
//delay(50);
//Serial.print("AT+NETWORKID=5\r\n");
//delay(50);
//Serial.print("AT+MODE=0\r\n");
//delay(50);
//Serial.print("AT+BAND=915000000\r\n");
//delay(50);
//Serial.print("AT+PARAMETER=10,7,1,7\r\n");
//delay(50);
pinMode(RECV_PIN,INPUT);
delay(50);
irrecv.enableIRIn();
delay(50);
irrecv.blink13(true);
delay(50);
}
void loop(){
if (irrecv.decode(&results)){
switch(results.value){
case 0XFF30CF: //Keypad button "1"
Serial1.println("AT+SEND=1,6,R1");
delay(500);
}
switch(results.value){
case 0XFF18E7: //Keypad button "2"
Serial1.println("AT+SEND=1,6,R2");
delay(500);
}
switch(results.value){
case 0XFF7A85: //Keypad button "3"
Serial1.println("AT+SEND=1,6,R3");
delay(500);
}
switch(results.value){
case 0XFF10EF: //Keypad button "4"
Serial1.println("AT+SEND=1,6,R4");
delay(500);
}
switch(results.value){
case 0XFF38C7: //Keypad button "5"
Serial1.println("AT+SEND=1,6,R5");
delay(500);
}
switch(results.value){
case 0XFF5AA5: //Keypad button "6"
Serial1.println("AT+SEND=1,6,R6");
delay(500);
}
switch(results.value){
case 0XFF42BD: //Keypad button "7"
Serial1.println("AT+SEND=1,6,R7");
delay(500);
}
switch(results.value){
case 0XFF4AB5: //Keypad button "8"
Serial1.println("AT+SEND=1,6,R8");
delay(500);
}
switch(results.value){
case 0XFF52AD: //Keypad button "9"
Serial1.println("AT+SEND=1,6,R9");
delay(500);
}
switch(results.value){
case 0XFF6897: //Keypad button "0"
Serial1.println("AT+SEND=1,6,R0");
delay(500);
}
switch(results.value){
case 0XFFE21D: //Keypad button "CH+"
Serial1.println("AT+SEND=1,6,CH+");
delay(500);
}
switch(results.value){
case 0XFFA25D: //Keypad button "CH-"
Serial1.println("AT+SEND=1,6,CH-");
delay(500);
}
irrecv.resume();
}
}
I have been adjusting the code over the past couple of days in order to help isolate different parts of the project to determine what was operating properly. That is why some of the setup code was turned into comments. I decided to separately program the Lora module because I was not sure if the Arduino Nano was sending the serial info through the USB when it was connected to the computer instead of the TX1 and RX0 pins. If someone could inform me of whether or not this is true that would be helpful.
Here is the code that the receiver is operating:
//Define PIN constant
const int relay_1 = 22;
const int relay_2 = 23;
const int relay_3 = 24;
int Current_relay = 0; //Define integer to remember the current active relay
int toggleState_1 = 0; //Define integers to remember the toggle state for switchs 1-3
int toggleState_2 = 0;
int toggleState_3 = 0;
String incomingString;
void setup()
{
Serial1.begin(115200);
delay(50);
Serial.begin(115200);
delay(50);
Serial1.print("AT+IPR=115200\r\n");
delay(50);
Serial1.print("AT+ADDRESS=1\r\n");
delay(50);
Serial1.print("AT+NETWORKID=5\r\n");
delay(50);
Serial1.print("AT+MODE=0\r\n");
delay(50);
Serial1.print("AT+BAND=915000000\r\n");
delay(50);
Serial1.print("AT+PARAMETER=10,7,1,7\r\n");
delay(50);
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
}
void relayOnOff(int relay){
switch(relay){
case 1:
if(toggleState_1 == 0){
digitalWrite(relay_1, HIGH); // turn on relay 1
toggleState_1 = 1;
Current_relay = 1;
}
else{
digitalWrite(relay_1, LOW); // turn off relay 1
toggleState_1 = 0;
Current_relay = 0;
}
delay(100);
break;
case 2:
if(toggleState_2 == 0){
digitalWrite(relay_2, HIGH); // turn on relay 2
toggleState_2 = 1;
Current_relay = 2;
}
else{
digitalWrite(relay_2, LOW); // turn off relay 2
toggleState_2 = 0;
Current_relay = 0;
}
delay(100);
break;
case 3:
if(toggleState_3 == 0){
digitalWrite(relay_3, HIGH); // turn on relay 3
toggleState_3 = 1;
Current_relay = 3;
}else{
digitalWrite(relay_3, LOW); // turn off relay 3
toggleState_3 = 0;
Current_relay = 0;
}
delay(100);
break;
default : break;
}
}
void loop() {
if(Serial1.available()) {
incomingString = Serial1.readString();
if(incomingString.indexOf("R1") >0) {
relayOnOff(1);
Serial.println("111111");
}
else if(incomingString.indexOf("R2") >0) {
relayOnOff(2);
Serial.println("222222222");
}
else if(incomingString.indexOf("R3") >0) {
relayOnOff(3);
Serial.print("3333333");
}
else if(incomingString.indexOf("CH+") >0) {
relayOnOff(Current_relay);
Current_relay=Current_relay+1;
relayOnOff(Current_relay);
}
else if(incomingString.indexOf("CH-") >0) {
relayOnOff(Current_relay);
relayOnOff(Current_relay-1);
}
delay(100);
}
}
I am a beginner to Arduino and this is my first project. I have been troubleshooting the different parts of the project and I believe the issue is with the communication between the Lora modules or the Arduinos with the Lora modules. I am able to get the Nano to print the associated AT commands to the serial monitor when I connect it to the computer and use the IR remote. I am able to send AT commands to the Mega to control the relay board through the serial monitor and all of the power supplies work well.
All of this to say that I am hoping someone with more experience with me can look through my code and hardware to see if I am making a stupid mistake somewhere along the way or if there are any issues with the way I am setting up the Lora modules. Thanks for taking the time to help me solve this problem.
Ethan