Skip to content

Instantly share code, notes, and snippets.

@ikeating
Last active September 27, 2021 22:10
Show Gist options
  • Select an option

  • Save ikeating/26bb4b0eb252eb038e481296d5543255 to your computer and use it in GitHub Desktop.

Select an option

Save ikeating/26bb4b0eb252eb038e481296d5543255 to your computer and use it in GitHub Desktop.
// Feather32u4 rfm96_TX
// -*- mode: C++ -*-
// ** tango ** messaging client (transmitter)
//Isaiah Keating is the dude
#include <SPI.h>
#include <RH_RF95.h>
#include <RHReliableDatagram.h>
#define CLIENT_ADDRESS 42
#define SERVER_ADDRESS 128
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// for feather32u4
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7
#define LED 13
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
int reading = 0;
// 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
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// 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);
RHReliableDatagram manager(rf95, CLIENT_ADDRESS);
void xyGridLine (double xLength, double yLength, double xZero, double yZero, double numTicks)
{
double llx = xZero;
double lrx = xZero + (xLength/numTicks);
double lly = yZero;
double uly = yZero - (yLength/numTicks);
for (int i = 0;i < numTicks;i++)
{
tft.drawLine(llx,lly,lrx,lly,0xFFFF);
tft.drawLine(lrx,lly,lrx,lly+5, 0xFFFF);
tft.drawLine(lrx,lly,lrx,lly-5, 0xFFFF);
lrx=lrx+(xLength/numTicks);
}
for (int i = 0;i < numTicks;i++)
{
tft.drawLine(llx,lly,llx,uly, 0xFFFF);
tft.drawLine(llx,uly,llx+5,uly, 0xFFFF);
tft.drawLine(llx,uly,llx-5,uly, 0xFFFF);
uly=uly-(yLength/numTicks);
}
}
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
pinMode(LED, OUTPUT);
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);
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);
//xyGridLine (/*double xLength*/200, /*double yLength*/100, /*double xZero*/5, /*double yZero*/315, /*double numTicks*/10);
//*********** Circle Tics ******************************
/*
for( int i = 0; i < 360; i++)
{
double radius = 55;//tic is 5 because the circle is 50
double radian = (i * 0.0174532925)* 10;//one tic every 10 rad
double dataX = radius * cos(radian);
double dataY = radius * sin(radian);
tft.drawLine( 120, 240, dataX+120, dataY+240, 0xFFE0);
}
*/
//******************************************************
}
int16_t packetnum = 0; // packet counter, we increment per xmission
char radiopacket[60] = "Temp Voltage N0TSU ROMEO # ";
char vBat[5] = {0};
char temp[10] = {0};
void loop()
{
temp[9] = {0};
vBat[4] = NULL;
radiopacket[59] = NULL;
delay(1000); // Wait 2.5 second between transmits, could also 'sleep' here!
Serial.println("Transmitting..."); // Send a message to rf95_server
//reading temperature and coverting it to string and storing it in an array
reading = analogRead(sensorPin);
float voltage = reading * 3.3;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
dtostrf(temperatureF, 4, 2, temp);
//Serial.print(temperatureF);
radiopacket[6] = temp[0];
radiopacket[7] = temp[1];
radiopacket[8] = temp[2];
radiopacket[9] = temp[3];
radiopacket[10] = temp[4];
//reading voltage and coverting it to string and storing it in an array
#define VBATPIN A9
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2;
measuredvbat *= 3.3;
measuredvbat /= 1024;
Serial.print("VBat: ");
Serial.println(measuredvbat);
dtostrf(measuredvbat, 3, 2, vBat);
radiopacket[21] = vBat[0];
radiopacket[22] = vBat[1];
radiopacket[23] = vBat[2];
radiopacket[24] = vBat[3];
itoa( packetnum++, radiopacket+40, 10);
Serial.print("Sending ");
Serial.println(radiopacket);
Serial.println("Sending...");
digitalWrite(LED, HIGH); delay(30);
digitalWrite(LED, LOW);delay(30);
digitalWrite(LED, HIGH); delay(30);
digitalWrite(LED, LOW);delay(30);
digitalWrite(LED, HIGH); delay(30);
digitalWrite(LED, LOW);delay(30);
digitalWrite(LED, HIGH); delay(30);
//delay(10);
manager.sendtoWait(radiopacket, sizeof(radiopacket), SERVER_ADDRESS);
Serial.println("Waiting for packet to complete...");
delay(10);
manager.waitPacketSent();
digitalWrite(LED, LOW);
// Now wait for a reply
Serial.println("Waiting for reply...");
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
uint8_t from;
if (rf95.waitAvailableTimeout(1500)) // needed to increase timeout
{
digitalWrite(LED, HIGH); delay(200);
// Should be a reply message for us now
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("Got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
digitalWrite(LED, LOW);
// gfx
tft.setCursor(0,0);
//tft.fillScreen(BACKCOLOR);
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.println((char*)buf);
tft.println("");
tft.println(packetnum++);
}
else
{
Serial.println("Receive failed");
}
}
else
{
Serial.println("No reply, is there a listener around?");
}
}
// Feather9x_TX
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messaging client (transmitter)
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example Feather9x_RX
#include <SPI.h>
#include <RH_RF95.h>
// for feather32u4
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7
/* for feather m0
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3
*/
/* for shield
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 7
*/
/* Feather 32u4 w/wing
#define RFM95_RST 11 // "A"
#define RFM95_CS 10 // "B"
#define RFM95_INT 2 // "SDA" (only SDA/SCL/RX/TX have IRQ!)
*/
/* Feather m0 w/wing
#define RFM95_RST 11 // "A"
#define RFM95_CS 10 // "B"
#define RFM95_INT 6 // "D"
*/
#if defined(ESP8266)
/* for ESP w/featherwing */
#define RFM95_CS 2 // "E"
#define RFM95_RST 16 // "D"
#define RFM95_INT 15 // "B"
#elif defined(ESP32)
/* ESP32 feather w/wing */
#define RFM95_RST 27 // "A"
#define RFM95_CS 33 // "B"
#define RFM95_INT 12 // next to A
#elif defined(NRF52)
/* nRF52832 feather w/wing */
#define RFM95_RST 7 // "A"
#define RFM95_CS 11 // "B"
#define RFM95_INT 31 // "C"
#elif defined(TEENSYDUINO)
/* Teensy 3.x w/wing */
#define RFM95_RST 9 // "A"
#define RFM95_CS 10 // "B"
#define RFM95_INT 4 // "C"
#endif
// Change to 434.0 or other frequency, must match RX's freq!
//#define RF95_FREQ 434.0
#define RF95_FREQ 915.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
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);
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(23, 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] = "N0TSU Romeo # ";
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 reply, is there a listener around?");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment