SHARE
TWEET

TTGO_V2_1-1_6_T3_Receiver_Sketch_From_Bob_Rader_Feb4_2020

a guest Feb 4th, 2020 70 in 125 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /*
  2.  **********************************     Receive Sketch For 433 Mhz Band     ***********************
  3.      
  4.  Original project by
  5.   Rui Santos
  6.   Complete original project details at https://RandomNerdTutorials.com/ttgo-lora32-sx1276-arduino-ide/
  7.          
  8.  
  9.  
  10.              TTGO V2.1_1.6 T3                --  Receiver Sketch --
  11.              Version 1-30-20
  12.              
  13.              Notes:  ** The LoRa RF/ Modulation parameters for the receiver MUST BE set the same as the SENDER **
  14.              
  15.              Modified by Bob Rader 1-22-20-- Added LoRa RF/ Modulation Parameters, internal green LED &
  16.                                              an external output for an LED on pin 12 to verify receipt of packet
  17.                                              Added to display an analog reading & a digital I/O status from sender
  18.                                              
  19.              Mod of 1-30-20 -- added call sign, modified for signal range test deleting the Digital or Analog sensors
  20.              Mod of 2-2-20  -- removed call sign from packet text, other edits
  21.              
  22.          For best & longest range: Use an external antenna & attach the module directly to the antenna with
  23.          no or minimal coax cable. The terrain must be open providing for a clear Line-of-sight which is clear
  24.          of any interveining obstructions -- just clear air --
  25.          Use the following RF/ Modulation parameters:  power= 17 to 20, SF=10, BW= 125E3, CR=5,
  26.          and for a transmission time lasting about 0.3 seconds use a preamble of 255.
  27.          Note,the larger the preamble number the longer the transmission time.  The longer the
  28.          transmission time and the higher the RF output the more average DC current required.
  29.          
  30.          Two very good articles explaining the LoRa RF parameters:
  31.          
  32.          "A Study of LoRa: Long Range & Low Power Networks for the Internet of Things"
  33.          explaining the LoRa parameters
  34.          https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5038744/
  35.              &
  36.          Decoding Lora
  37.          https://revspace.nl/DecodingLora        
  38.  
  39.    433E6 for Asia
  40.    866E6 for Europe
  41.    915E6 for North America
  42.  
  43.                     LoRa RF & Modilation Parameters    
  44.                                                                                                          
  45.                     Band          Band or Freq -- 433E6 for Asia, 866E6 Europe,
  46.                                                   915E6 North America ie: 915E6 or 922350000
  47.                                                   or any specific frequency
  48.                     Power         Tx Power 2 to 20 (1.6 mW to 100 mW)                default 17 (50 mW)
  49.                     SF            Spreading Factor 6 to 12                           default 7
  50.                     BW            Bandwith 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3,
  51.                                            41.7E3, 62.5E3, 125E3, & 250E3            default 125E3
  52.                     CR            Coding Rate 5 or 8                                 default 5
  53.                     Preamble      Preamble 6 to 65535                                default 8
  54.                     SyncWd        Sync Word 0 to 0xFF                                default 0x34
  55.                                        
  56.                     Each packet can contain up to 255 bytes
  57.                                        
  58. *******************************.....................Set RF/ Modulation Parameters
  59. */
  60. #define Band 433E6                     // Set Band or Frequency in Hz
  61. #define SF 10                          // Set Spreading Factor
  62. #define BW 125E3                       // Set Bandwidth
  63. #define CR 5                           // Set Coding Rate
  64. #define Preamble 255                   // Set Preamble
  65. #define SyncWd 0x12                    // Set Sync Word
  66.  
  67. //************************************.............. Libraries for LoRa
  68.  
  69. #include <SPI.h>
  70. #include <LoRa.h>
  71.  
  72. //************************************.............. Libraries for OLED Display
  73.  
  74. #include <Wire.h>
  75. #include <Adafruit_GFX.h>
  76. #include <Adafruit_SSD1306.h>
  77.  
  78. //************************************.............. Define pins used by LoRa module
  79.  
  80. #define SCK 5     // Serial Clock
  81. #define MISO 19   // Master Input
  82. #define MOSI 27   // Master Output
  83. #define SS 18     // Slave Select
  84. #define RST 23    // Reset
  85. #define DIO0 26   // DIO0 an interupt appearently used to indicate Tx-Done or Rx-Done
  86.  
  87. //************************************...............Define pins & Parameters used by OLED Display
  88.  
  89. #define OLED_SDA 21
  90. #define OLED_SCL 22
  91. #define OLED_RST 16
  92. #define SCREEN_WIDTH 128               // OLED display width, in pixels
  93. #define SCREEN_HEIGHT 64               // OLED display height, in pixels
  94.  
  95. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
  96.  
  97.           String LoRaData;
  98.           const int ledPin =  25;      // Use pin 25 the Grn Led on TTGO Module to indicate correctly
  99.                                        // received transmission
  100.           const int ledPin1 = 12;      // Use pin 12 for External Led to indicate a correctly
  101.                                        // received transmission
  102.           int Count = 0;               // Use for packet counter if desired
  103.  
  104.  
  105. void setup() {
  106.  
  107.           pinMode(ledPin, OUTPUT);     // Define ledPin as output
  108.           pinMode(ledPin1, OUTPUT);    // Define ledPin1 as output
  109.           pinMode(OLED_RST, OUTPUT);
  110.          
  111.           digitalWrite(OLED_RST, LOW); // reset OLED display via software
  112.                delay(20);
  113.           digitalWrite(OLED_RST, HIGH);
  114.  
  115.           digitalWrite(ledPin, HIGH);  // turn on grn LED
  116.  
  117. //************************************.... Initialize OLED ....... Address 0x3c
  118.  
  119.   Wire.begin(OLED_SDA, OLED_SCL);
  120.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false))  // Address 0x3c
  121.   {
  122.           Serial.println(F("SSD1306 allocation failed"));
  123.  for(;;);                             // Don't proceed, loop forever
  124.   }
  125. //***********************************.....Set-Up Display
  126.  
  127.          display.clearDisplay();
  128.          display.setTextColor(WHITE);
  129.          display.setTextSize(1);
  130.          display.setCursor(0,0);
  131.          display.print("LORA RECEIVER ");
  132.          display.display();
  133.  
  134. //******************************...........initialize Serial Monitor
  135.  
  136.    Serial.begin(115200);
  137.         Serial.println("LoRa Receiver Test");
  138.  
  139. //************************************  LoRa SPI pins
  140.  
  141.   SPI.begin(SCK, MISO, MOSI, SS);
  142. //************************************  setup LoRa transceiver module Frequency
  143.  
  144.        LoRa.setPins(SS, RST, DIO0); //
  145.  
  146.   if (!LoRa.begin(Band)) { // Please Set Correct Frequency/ Band above
  147.       Serial.println("LoRa Initalization failed!");
  148.     while (true);          // If failed -- Do Nothing just loop forever
  149.                          }
  150.  
  151.   //************************************  Setup LoRa Transmission Parameters ******************
  152.  
  153.   LoRa.setSpreadingFactor(SF);         // 6-12
  154.   LoRa.setSignalBandwidth(BW);         // 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3,
  155.                                        // 41.7E3, 62.5E3, 125E3, & 250E3
  156.   LoRa.setCodingRate4(CR);             // 5 or 8
  157.   LoRa.setPreambleLength(Preamble);    // 5 to 65535
  158.   LoRa.setSyncWord(SyncWd);            // byte val to use as sync word, 0 to 0xFF
  159.  
  160.       Serial.println("LoRa Initializing OK!");
  161.       display.setCursor(0,10);
  162.       display.println("LoRa Module Initialized OK!");
  163.       display.display();  
  164.              }
  165. //**************************    START LOOP     **********************************
  166. //*******************************************************************************
  167.  
  168. void loop() {
  169.  
  170. //*****************************......... parse data from packet
  171.  
  172.   int packetSize = LoRa.parsePacket();
  173.  if (packetSize)                        // when no more data in packet -- end
  174.         {
  175.                                         //received a packet
  176.      Serial.print("Received packet ");
  177.  
  178. //*****************************..........read packet data
  179.   while (LoRa.available()) {            // while data is in packet
  180.   LoRaData = LoRa.readString();         // read data
  181.       Serial.print(LoRaData);           // continue until all packet
  182.     }                                   // data has been read
  183.      
  184. //*****************************..........print RSSI
  185.     int rssi = LoRa.packetRssi();       // read the transceiver RSSI value
  186.     Serial.print(" with RSSI ");    
  187.     Serial.println(rssi);
  188.  
  189. //*****************************..........display information on Oled Display
  190.    display.clearDisplay();
  191.    display.setCursor(0,0);             // Set cursor to col 0 line 0
  192.    display.setTextSize(2);
  193.    display.print("LoRa 433Rx");        // Send "LORA RECEIVER" to top line of display
  194.    display.setCursor(10,18);           // Set cursor to col 10 line 18
  195.    display.print(LoRaData);
  196.    display.setCursor(0,54);            // Set cursor to col 0 line 54
  197.    display.setTextSize(1);
  198.    display.print("RSSI =  ");
  199.    display.setTextSize(2);
  200.    display.setCursor(48,50);           // Set cursor to col 48 line 50
  201.    display.print(rssi);
  202.    display.display();
  203.  
  204. //*******************    Test To Verify Recived Data Is The Same As The Transmitted Data, If     ******************
  205. //******************* Data Same -- Flash GRN LED 3 Times Otherwise GRN LED Will Remain On Steady ***********
  206.  
  207.  if (LoRaData == "Test 123")           // test to see if data received is
  208.      {                                 // same as data sent -- if yes,
  209.  for (int i = 0; i < 3; i++)           // blink ledPin 3 times                        
  210.    {
  211.                           digitalWrite(ledPin, HIGH);
  212.                           digitalWrite(ledPin1, HIGH);
  213.                           delay(200);
  214.                           digitalWrite(ledPin, LOW);
  215.                           digitalWrite(ledPin1, LOW);
  216.                           delay(200);
  217.                           Count++;      // increment count (if included)
  218.    }
  219.      }
  220.         }                        
  221.            }
  222.  
  223. //****************     END     ****************
  224. //
  225. //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top