Skip to content

Instantly share code, notes, and snippets.

@ikeating
Created October 9, 2021 02:21
Show Gist options
  • Select an option

  • Save ikeating/1fd0a97478ecd673f9865f1e06b70243 to your computer and use it in GitHub Desktop.

Select an option

Save ikeating/1fd0a97478ecd673f9865f1e06b70243 to your computer and use it in GitHub Desktop.
// node1
#include <SPI.h> //Serial Peripheral Interface
#include <RH_RF95.h>
#include <RHReliableDatagram.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 5
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7
// for TFT Screen
#if defined (__AVR_ATmega32U4__) || defined(ARDUINO_SAMD_FEATHER_M0) || defined (__AVR_ATmega328P__) || \
defined(ARDUINO_SAMD_ZERO) || defined(__SAMD51__) || defined(__SAM3X8E__) || defined(ARDUINO_NRF52840_FEATHER)
#define STMPE_CS 6
#define TFT_CS 9
#define TFT_DC 10
#define SD_CS 5
#endif
// Pallete - Where you assign names to colors you like
#define BACKCOLOR 0x0000
#define PRINTCOL 0x07E0
#define CHARBACK 0x0000
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
RHReliableDatagram manager(rf95, CLIENT_ADDRESS);
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(9600);
delay(100);
Serial.println("Feather LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!manager.init())
{
Serial.println("LoRa radio init failed");
Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info");
while (1);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ))
{
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
// setup gfx
tft.begin();
tft.fillScreen(BACKCOLOR);
tft.setCursor(0,0);
tft.setTextWrap(true);
tft.setTextColor(PRINTCOL,CHARBACK);
tft.setTextSize(3);
}
int16_t packetnum = 1; // packet counter, we increment per xmission
char radiopacket[30] = "node1 ";
//uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
char buf[RH_RF95_MAX_MESSAGE_LEN];
void loop()
{
radiopacket[29] = NULL;
delay(1000); // Wait 2.5 second between transmits, could also 'sleep' here!
Serial.println("Transmitting..."); // Send a message to rf95_server
itoa( packetnum++, radiopacket+10, 10);//in to array
Serial.print("Sending ");
Serial.println(radiopacket);
Serial.println("Sending...");
delay(10);
manager.sendtoWait(radiopacket, sizeof(radiopacket), SERVER_ADDRESS);
Serial.println("Waiting for packet to complete...");
delay(10);
manager.waitPacketSent();
// Now wait for a reply
Serial.println("Waiting for reply...");
uint8_t len = sizeof(buf);
uint8_t from;
if (rf95.waitAvailableTimeout(2000)) // needed to increase timeout
{
// Should be a reply message for us now
//if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
if (manager.recvfromAckTimeout(buf, &len, &from))
{
RH_RF95::printBuffer("Received: ", buf, len);
Serial.println(": ");
Serial.print("Got reply from : 0x");
Serial.print( from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
// gfx
tft.setCursor(0,0);
tft.print("RSSI: "); tft.print(rf95.lastRssi(), DEC);
tft.println("");
tft.print("freq: ");
tft.println(RF95_FREQ);
tft.print("SERVER: "); tft.print(SERVER_ADDRESS);
tft.println("");
tft.print("from: "); tft.print(from, HEX);
tft.println("");
tft.println((char*)buf);
}
else
{
Serial.println("Receive failed");
}
}
else
{
Serial.println("No reply, is there a listener around?");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment