Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active September 17, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/131274f0d16e7253829576d9cbe87723 to your computer and use it in GitHub Desktop.
Save jenschr/131274f0d16e7253829576d9cbe87723 to your computer and use it in GitHub Desktop.
/*
Basic client for Adafruit Feather LoRa radios.
This is based on the RadioHead library for RF95, but has all
the required pins and setup for Feather LoRa's.
NOTE: You need to select a unique radio address for this to work
*/
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>
// Unique client address can be any number from 0-255 (as HEX)
#define CLIENT_ADDRESS 0xTypeUniqueAddressHere
#define SERVER_ADDRESS 2
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3 // Feather LoRa M0 = 3, Feather LoRa 32U4 = 7
#define RF95_FREQ 868.0
// Singleton instance of the radio driver
RH_RF95 driver(RFM95_CS, RFM95_INT);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
char data[20];
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(9600);
delay(100);
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!manager.init()) {
Serial.println("Datagram init failed");
while (1);
}
Serial.println("Datagram init OK!");
if (!driver.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// The default transmitter power is 13dBm, using PA_BOOST.
driver.setTxPower(23, false);
}
void loop()
{
int dataTosend = analogRead(A0);
sprintf(data,"%d", dataTosend);
Serial.print("Sending to server:");
Serial.println(data);
// Send a message to manager_server
if (manager.sendtoWait((uint8_t*)data, sizeof(data), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is rf95_reliable_datagram_server running?");
}
// Take a random break
delayMicroseconds( random(300) );
}
else
Serial.println("sendtoWait failed");
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment