Skip to content

Instantly share code, notes, and snippets.

@JasonLau77
Created January 17, 2019 02:35
Show Gist options
  • Select an option

  • Save JasonLau77/6831407c0bfbab669abe86f7004912bb to your computer and use it in GitHub Desktop.

Select an option

Save JasonLau77/6831407c0bfbab669abe86f7004912bb to your computer and use it in GitHub Desktop.
/*
LoRa Simple Client for Arduino :
Support Devices: LoRa Shield + Arduino
It is designed to Send data to Ddragino LG01-P
User need to use the modified RadioHead library from:
https://github.com/dragino/RadioHead
modified 11/12/2018
by Jason Lau
Singapore Polytechnic
*/
#include <SPI.h>
#include <RH_RF95.h>
// Singleton instance of the radio driver
RH_RF95 rf95;
float frequency = 433.0;
void setup()
{
Serial.begin(9600);
//while (!Serial) ; // Wait for serial port to be available
Serial.println("Start LoRa Client");
if (!rf95.init()) {
Serial.println("init failed");
while (1) {}
}
// Setup ISM frequency
rf95.setFrequency(frequency);
// Setup Power,dBm
rf95.setTxPower(13);
// Setup Spreading Factor (6 ~ 12)
rf95.setSpreadingFactor(7);
// Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000
//Lower BandWidth for longer distance.
rf95.setSignalBandwidth(125000);
// Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8)
rf95.setCodingRate4(5);
}
void loop()
{
delay(2000); // Wait a few seconds between measurements, minimum 2000ms
// Creates a payload that looks like "25.6,70.5,3.3", temperature first, humidity second, Voltage third
String tmp_buf = "26.5";
String hum_buf = "70.5";
String volt_buf = "3.3";
String Solarvolt_buff = "5.0";
String payload = tmp_buf + ",";
payload = payload + hum_buf + ",";
payload = payload + volt_buf + ",";
payload = payload + Solarvolt_buff;
Serial.println(payload);
Serial.println("Sending to LoRa Server");
// Send the payload to LoRa Server
rf95.send(payload.c_str(), payload.length() + 1); // +1 due to null termination
rf95.waitPacketSent();
Serial.println("Finished waitPacketSent");
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.waitAvailableTimeout(3000))
{
// Should be a reply message for us now
if (rf95.recv(buf, &len))
{
Serial.print("got reply: ");
Serial.println((char *)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
}
else
{
Serial.println("recv failed");
}
}
else
{
Serial.println("No reply, is LoRa server running?");
}
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment