String mensaje = "Bienvenido";
String presion;
String rpm;
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button3State = 0; // variable for reading the pushbutton status
int button4State = 0; // variable for reading the pushbutton status
byte localAddress = 0xBB; // address of this device
byte destination = 0xFF; // destination to send to
long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends
const int button1Pin = 2; // the number of the pushbutton pin
const int button2Pin = 3; // the number of the pushbutton pin
const int button3Pin = 4; // the number of the pushbutton pin
const int button4Pin = 5; // the number of the pushbutton pin
void setup() {
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex with callback");
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
//LoRa.setTxPower(20);
LoRa.onReceive(onReceive);
LoRa.receive();
Serial.println("LoRa init succeeded.");
}
////////////////////////////// PROGRAMA ////////////////////////////////////
while (LoRa.available()) { // can't use readString() in callback, so
incoming += (char)LoRa.read(); // add bytes one by one
}
if (incomingLength != incoming.length()) { // check length for error
Serial.println("error: message length does not match length");
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != localAddress && recipient != 0xFF) {
Serial.println("This message is not for me.");
return; // skip rest of function
}
I am using the following condigo with arduino uno and shield lora RMF95. The code works but only has a range of 50m and I need at least 1km.
How can I modify the power?
////////////////////////////////////// LIBRERIAS /////////////////////////////////
include // include libraries
include
include
include
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
///////////////////////////// VARIABLES ///////////////////////////////////////
String outgoing; // outgoing message
String mensaje = "Bienvenido"; String presion; String rpm; int button1State = 0; // variable for reading the pushbutton status int button2State = 0; // variable for reading the pushbutton status int button3State = 0; // variable for reading the pushbutton status int button4State = 0; // variable for reading the pushbutton status
//////////////////////////////// CONSTANTES /////////////////////////////////
byte localAddress = 0xBB; // address of this device byte destination = 0xFF; // destination to send to long lastSendTime = 0; // last send time int interval = 2000; // interval between sends const int button1Pin = 2; // the number of the pushbutton pin const int button2Pin = 3; // the number of the pushbutton pin const int button3Pin = 4; // the number of the pushbutton pin const int button4Pin = 5; // the number of the pushbutton pin
/////////////////////////////// SETUP /////////////////////////////////////
void setup() { lcd.init(); // initialize the lcd lcd.init(); lcd.backlight(); pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(button3Pin, INPUT); pinMode(button4Pin, INPUT); Serial.begin(9600); // initialize serial while (!Serial);
Serial.println("LoRa Duplex with callback"); if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz Serial.println("LoRa init failed. Check your connections."); while (true); // if failed, do nothing } //LoRa.setTxPower(20); LoRa.onReceive(onReceive); LoRa.receive(); Serial.println("LoRa init succeeded."); }
////////////////////////////// PROGRAMA ////////////////////////////////////
void loop() { button1State = digitalRead(button1Pin); button2State = digitalRead(button2Pin); button3State = digitalRead(button3Pin); button4State = digitalRead(button4Pin); if (button1State == HIGH) { String message = "ON"; // send a message sendMessage(message); Serial.println("Sending " + message); Serial.println(); delay(1000); } else if (button2State == HIGH) { String message = "OFF"; // send a message sendMessage(message); Serial.println("Sending " + message); Serial.println(); delay(1000); } else if (button3State == HIGH) { String message = "UP"; // send a message sendMessage(message); Serial.println("Sending " + message); Serial.println(); delay(1000); } else if (button4State == HIGH) { String message = "DOWN"; // send a message sendMessage(message); Serial.println("Sending " + message); Serial.println(); delay(1000); }else { lcd.clear(); delay(100); lcd.setCursor(0,0); lcd.print(mensaje); lcd.setCursor(0,1); lcd.print(presion); lcd.setCursor(6,1); lcd.print(rpm); delay(1000); LoRa.receive(); // go back into receive mode } }
void sendMessage(String outgoing) { LoRa.beginPacket(); // start packet LoRa.write(destination); // add destination address LoRa.write(localAddress); // add sender address //LoRa.write(msgCount); // add message ID LoRa.write(outgoing.length()); // add payload length LoRa.print(outgoing); // add payload LoRa.endPacket(); // finish packet and send it lcd.clear(); delay(200); lcd.setCursor(0,0); lcd.print("Enviando"); lcd.setCursor(0,1); lcd.print("comando " + outgoing); delay(1000); }
void onReceive(int packetSize) { if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes: int recipient = LoRa.read(); // recipient address byte sender = LoRa.read(); // sender address byte incomingPresion = LoRa.read(); // incoming msg ID byte incomingRPM = LoRa.read(); // incoming rpm byte incomingLength = LoRa.read(); // incoming msg length
String incoming = ""; // payload of packet
while (LoRa.available()) { // can't use readString() in callback, so incoming += (char)LoRa.read(); // add bytes one by one }
if (incomingLength != incoming.length()) { // check length for error Serial.println("error: message length does not match length"); return; // skip rest of function }
// if the recipient isn't this device or broadcast, if (recipient != localAddress && recipient != 0xFF) { Serial.println("This message is not for me."); return; // skip rest of function }
// if message is for this device, or broadcast, print details: Serial.println("Received from: 0x" + String(sender, HEX)); Serial.println("Sent to: 0x" + String(recipient, HEX)); //Serial.println("Message ID: " + String(incomingMsgId)); //Serial.println("Message length: " + String(incomingLength)); Serial.println("Message: " + incoming); mensaje = incoming; presion = "P:" + String(incomingPresion); rpm = "RPM: " + String(incomingRPM) + "00"; //Serial.println("RSSI: " + String(LoRa.packetRssi())); //Serial.println("Snr: " + String(LoRa.packetSnr())); Serial.println(); }