Created
October 9, 2021 02:21
-
-
Save ikeating/c25c346e7a015708ffcd45fd7cd063bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // node2 | |
| #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 0xF800 | |
| #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, SERVER_ADDRESS); | |
| void setup() | |
| { | |
| pinMode(RFM95_RST, OUTPUT); | |
| digitalWrite(RFM95_RST, HIGH); | |
| Serial.begin(9600); | |
| delay(100); | |
| Serial.println("Feather LoRa RX Test!"); | |
| // manual reset | |
| digitalWrite(RFM95_RST, LOW); | |
| delay(10); | |
| digitalWrite(RFM95_RST, HIGH); | |
| delay(10); | |
| //while (!rf95.init()) { | |
| 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] = "node2 "; | |
| //uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | |
| char buf[RH_RF95_MAX_MESSAGE_LEN]; | |
| void loop() | |
| { | |
| if (manager.available()) | |
| { | |
| // Should be a message for us now | |
| uint8_t len = sizeof(buf); | |
| uint8_t from; | |
| if (rf95.waitAvailableTimeout(2000)) | |
| { | |
| //if (manager.recvfromAck(buf,&len, 2000, &from)) | |
| if (manager.recvfromAck(buf,&len, &from)) | |
| { | |
| delay(1000); | |
| RH_RF95::printBuffer("Received: ", buf, len); | |
| Serial.println(""); | |
| Serial.print("got transmission 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("CLIENT: "); tft.print(CLIENT_ADDRESS); | |
| tft.println(""); | |
| tft.print("from: "); tft.print(from, HEX); | |
| tft.println(""); | |
| tft.println((char*)buf); | |
| // Send a reply | |
| radiopacket[29] = NULL; | |
| itoa( packetnum++, radiopacket+10, 10);//in to array | |
| manager.sendtoWait(radiopacket, sizeof(radiopacket), CLIENT_ADDRESS); | |
| delay(10); | |
| manager.waitPacketSent(); | |
| Serial.println("Sent a reply"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment