Advertisement
hwthinker

LoraSenderWithOLED-433MHz

Jan 30th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //modified lora library by Sandeep Mistry for TTGO ESP32 Lora
  2. // lora Sender
  3. #include <SPI.h>
  4. #include <LoRa.h>
  5. #include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
  6. #include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
  7.  
  8. // GPIO5  -- SX1278's SCK
  9. // GPIO19 -- SX1278's MISO
  10. // GPIO27 -- SX1278's MOSI
  11. // GPIO18 -- SX1278's CS
  12. // GPIO14 -- SX1278's RESET
  13. // GPIO26 -- SX1278's IRQ(Interrupt Request)
  14.  
  15. #define SS      18
  16. #define RST     14
  17. #define DI0     26
  18. #define BAND    433E6
  19. int counter = 0;
  20. int state = 0;
  21.  
  22. // Initialize the OLED display using Wire library
  23. SSD1306Wire  display(0x3c, OLED_SDA, OLED_SCL); // OLED_SDA=4, OLED_SCL=15
  24.  
  25. void setup() {
  26.   // START aktivas Oled
  27.   pinMode(LED_BUILTIN, OUTPUT);
  28.   pinMode(16, OUTPUT);
  29.   digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  30.   delay(50);
  31.   digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high、
  32.  
  33.   // Initialising the UI will init the display too.
  34.   display.init();
  35.   display.flipScreenVertically();
  36.   display.setFont(ArialMT_Plain_10);
  37.   // clear the display
  38.   display.clear();
  39.   // aktivasi Oled END
  40.  
  41.   pinMode(25, OUTPUT); //Send success, LED will bright 1 second
  42.  
  43.   Serial.begin(115200);
  44.   while (!Serial); //If just the the basic function, must connect to a computer
  45.  
  46.   SPI.begin(5, 19, 27, 18);
  47.   LoRa.setPins(SS, RST, DI0);
  48.   //  Serial.println("LoRa Sender");
  49.  
  50.   if (!LoRa.begin(BAND)) {
  51.     Serial.println("Starting LoRa failed!");
  52.     while (1);
  53.   }
  54.   Serial.println("LoRa Initial OK!");
  55. }
  56.  
  57. void loop() {
  58.   display.setFont(ArialMT_Plain_24);
  59.   display.drawString(0, 0, "HwThinker");
  60.   display.display();
  61.  
  62.   display.setFont(ArialMT_Plain_16);
  63.   display.drawString(0, 24, "Lora Sender");
  64.   display.display();
  65.  
  66.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  67.   display.setFont(ArialMT_Plain_10);
  68.   display.drawString(0, 45, "Sending packet : " + String(counter));
  69.   display.display();
  70.  
  71.   Serial.print("Sending packet: ");
  72.   Serial.println(counter);
  73.  
  74.   // send packet
  75.   LoRa.beginPacket();
  76.   LoRa.print("hello ");
  77.   LoRa.print(counter);
  78.   LoRa.endPacket();
  79.  
  80.   counter++;
  81.   digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
  82.   delay(1000);                       // wait for a second
  83.   digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
  84.   delay(1000);                       // wait for a second
  85.  
  86.   delay(3000);
  87.   digitalWrite(LED_BUILTIN, (state) ? HIGH : LOW);
  88.   state = !state;
  89.   //or   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  90.   display.clear();
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement