Skip to content

Instantly share code, notes, and snippets.

@jones2126
Created July 7, 2022 16:07
Show Gist options
  • Save jones2126/73eb93df2c8eead98ce4fab4d6cfe5fe to your computer and use it in GitHub Desktop.
Save jones2126/73eb93df2c8eead98ce4fab4d6cfe5fe to your computer and use it in GitHub Desktop.
Testing code for master/tractor/server to interact with slave/remote control/client using a TTGO board and LoRa communication. This code runs on the server which is meant to be the laptop on the tractor.
/*
* Program: ttgo_adafruit_server.ino
* a simple messaging server(sender)with the RH_RF95 class.
*/
#include <SPI.h>
#include <RH_RF95.h>
//#define RFM95_CS 2 // "E"
//#define RFM95_RST 16 // "D"
//#define RFM95_INT 15 // "B"
// LoRa pins for TTGO Semtech SX1276 ref: https://microcontrollerslab.com/ttgo-lora32-sx1276-oled-board-pinout-getting-started-with-arduino-ide/
#define RFM95_MISO 19
#define RFM95_MOSI 27
#define RFM95_SCK 5
#define RFM95_CS 18 // #define RFM95_CS 2
#define RFM95_INT 26 // #define RFM95_INT 15
#define RFM95_RST 14 // #define RFM95_RST 16
#define LORA_BAND 915E6 //433E6 Asia; 866E6 Europe; 915E6 North America
//#define LORA_TxPower 20 // defaults to 17; 20 is highest
#define LORA_TxPower 5 // defaults to 17; 20 is highest
#define LORA_SyncWord 0xF1 // changes the sync word to match the receiver; ranges from 0-0xFF, default 0x34
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
// LoRaMessage = String(readingID) + "/" + String(TempF) + "&" + String(humidity) + "#" + String(pressure);
struct dataStruct{
float pressure;
float humidity;
float TempF;
unsigned long counter;
}myData;
byte tx_buf[sizeof(myData)] = {0};
#define LED 2 // On board LED to blink for feedback
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(115200);
while (!Serial) {
delay(1);
}
delay(100);
Serial.println("Feather LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
SPI.begin(RFM95_SCK, RFM95_MISO, RFM95_MOSI, RFM95_CS); //SPI LoRa pins
while (!rf95.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(5, false);
}
int16_t packetnum = 0; // packet counter, we increment per xmission
void loop()
{
delay(1000); // Wait 1 second between transmits, could also 'sleep' here!
Serial.println("Transmitting..."); // Send a message to rf95_server
char radiopacket[20] = "from tractor ";
// itoa(-1567, str, 10) should return string "-1567"
itoa(packetnum++, radiopacket+13, 10);
Serial.print("Sending "); Serial.println(radiopacket);
radiopacket[19] = 0;
Serial.println("Sending...");
delay(10);
rf95.send((uint8_t *)radiopacket, 20);
Serial.println("Waiting for packet to complete...");
delay(10);
rf95.waitPacketSent();
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
Serial.println("Waiting for reply...");
if (rf95.waitAvailableTimeout(1000))
{
// 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("Receive failed");
}
}
else
{
Serial.println("No messages being received");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment