diff --git a/arduino/LoRareceiver/LoRaReceiver.ino b/arduino/LoRareceiver/LoRaReceiver.ino new file mode 100644 index 0000000..38c22dd --- /dev/null +++ b/arduino/LoRareceiver/LoRaReceiver.ino @@ -0,0 +1,57 @@ + +#include +#include "boards.h" + +void setup() +{ + + initBoard(); + // When the power is turned on, a delay is required. + delay(1500); + + Serial.println("LoRa Receiver"); + + LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN); + if (!LoRa.begin(LoRa_frequency)) { + Serial.println("Starting LoRa failed!"); + while (1); + } + +} + +void loop() +{ + // try to parse packet + int packetSize = LoRa.parsePacket(); + if (packetSize) { + // received a packet +// Serial.print("Received packet '"); + + String recv = ""; + // read packet + while (LoRa.available()) { + recv += (char)LoRa.read(); + } + + Serial.println(recv); + +// // print RSSI of packet +// Serial.print("' with RSSI "); +// Serial.println(LoRa.packetRssi()); +//// + +#ifdef HAS_DISPLAY + if (u8g2) { + u8g2->clearBuffer(); + char buf[256]; + u8g2->drawStr(0, 12, "Received OK!"); + u8g2->drawStr(0, 26, recv.c_str()); + snprintf(buf, sizeof(buf), "RSSI:%i", LoRa.packetRssi()); + u8g2->drawStr(0, 40, buf); + snprintf(buf, sizeof(buf), "SNR:%.1f", LoRa.packetSnr()); + u8g2->drawStr(0, 56, buf); + u8g2->sendBuffer(); + } +#endif + } +} diff --git a/arduino/LoRareceiver/boards.h b/arduino/LoRareceiver/boards.h new file mode 100644 index 0000000..c2c4d48 --- /dev/null +++ b/arduino/LoRareceiver/boards.h @@ -0,0 +1,171 @@ +#include +#include +#include +#include "utilities.h" + +#ifdef HAS_SDCARD +#include +#include +#endif + +#ifdef HAS_DISPLAY +#include +U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr; +#endif + +#if defined(LILYGO_TBeam_V1_0) || defined(LILYGO_TBeam_V1_1) +#include +AXP20X_Class PMU; + +bool initPMU() +{ + if (PMU.begin(Wire, AXP192_SLAVE_ADDRESS) == AXP_FAIL) { + return false; + } + /* + * The charging indicator can be turned on or off + * * * */ + // PMU.setChgLEDMode(LED_BLINK_4HZ); + + /* + * The default ESP32 power supply has been turned on, + * no need to set, please do not set it, if it is turned off, + * it will not be able to program + * + * PMU.setDCDC3Voltage(3300); + * PMU.setPowerOutPut(AXP192_DCDC3, AXP202_ON); + * + * * * */ + + /* + * Turn off unused power sources to save power + * **/ + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_DCDC2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); + PMU.setPowerOutPut(AXP192_EXTEN, AXP202_OFF); + + /* + * Set the power of LoRa and GPS module to 3.3V + **/ + PMU.setLDO2Voltage(3300); //LoRa VDD + PMU.setLDO3Voltage(3300); //GPS VDD + PMU.setDCDC1Voltage(3300); //3.3V Pin next to 21 and 22 is controlled by DCDC1 + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_ON); + + pinMode(PMU_IRQ, INPUT_PULLUP); + attachInterrupt(PMU_IRQ, [] { + // pmu_irq = true; + }, FALLING); + + PMU.adc1Enable(AXP202_VBUS_VOL_ADC1 | + AXP202_VBUS_CUR_ADC1 | + AXP202_BATT_CUR_ADC1 | + AXP202_BATT_VOL_ADC1, + AXP202_ON); + + PMU.enableIRQ(AXP202_VBUS_REMOVED_IRQ | + AXP202_VBUS_CONNECT_IRQ | + AXP202_BATT_REMOVED_IRQ | + AXP202_BATT_CONNECT_IRQ, + AXP202_ON); + PMU.clearIRQ(); + + return true; +} + +void disablePeripherals() +{ + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); +} +#else +#define initPMU() +#define disablePeripherals() +#endif + +SPIClass SDSPI(HSPI); + + +void initBoard() +{ + Serial.begin(115200); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + +#ifdef HAS_GPS + Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#if OLED_RST + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, HIGH); delay(20); + digitalWrite(OLED_RST, LOW); delay(20); + digitalWrite(OLED_RST, HIGH); delay(20); +#endif + + initPMU(); + +#ifdef HAS_SDCARD + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS); + if (!SD.begin(SDCARD_CS, SDSPI)) { + Serial.println("setupSDCard FAIL"); + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + Serial.print("setupSDCard PASS . SIZE = "); + Serial.print(cardSize); + Serial.println(" MB"); + } +#endif + +#ifdef BOARD_LED + /* + * T-BeamV1.0, V1.1 LED defaults to low level as trun on, + * so it needs to be forced to pull up + * * * * */ +#if LED_ON == LOW + gpio_hold_dis(GPIO_NUM_4); +#endif + pinMode(BOARD_LED, OUTPUT); + digitalWrite(BOARD_LED, LED_ON); +#endif + + +#ifdef HAS_DISPLAY + Wire.beginTransmission(0x3C); + if (Wire.endTransmission() == 0) { + Serial.println("Started OLED"); + u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); + u8g2->begin(); + u8g2->clearBuffer(); + u8g2->setFlipMode(0); + u8g2->setFontMode(1); // Transparent + u8g2->setDrawColor(1); + u8g2->setFontDirection(0); + u8g2->firstPage(); + do { + u8g2->setFont(u8g2_font_inb19_mr); + u8g2->drawStr(0, 30, "LilyGo"); + u8g2->drawHLine(2, 35, 47); + u8g2->drawHLine(3, 36, 47); + u8g2->drawVLine(45, 32, 12); + u8g2->drawVLine(46, 33, 12); + u8g2->setFont(u8g2_font_inb19_mf); + u8g2->drawStr(58, 60, "LoRa"); + } while ( u8g2->nextPage() ); + u8g2->sendBuffer(); + u8g2->setFont(u8g2_font_fur11_tf); + delay(5000); + } +#endif + +} + + diff --git a/arduino/LoRareceiver/sketch_may11a.ino b/arduino/LoRareceiver/sketch_may11a.ino new file mode 100644 index 0000000..0b7d8b6 --- /dev/null +++ b/arduino/LoRareceiver/sketch_may11a.ino @@ -0,0 +1,100 @@ +#include +#include +#include "boards.h" +#include +#include +#include + +PN532_I2C pn532_i2c(Wire); +NfcAdapter nfc = NfcAdapter(pn532_i2c); +String tagId = "None"; +String cleanString = ""; + + +void setup() +{ + Serial.begin(9600); + Serial.println("System initialized"); + nfc.begin(); + + initLoRa(); +} + +void loop() +{ + readNFC(); + if (!cleanString.isEmpty()) { + sendLoRaData(cleanString); + } +} + +void initLoRa() +{ + Serial.println("Initializing LoRa..."); + LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN); + if (!LoRa.begin(LoRa_frequency)) + { + Serial.println("Starting LoRa failed!"); + while (1); + } + Serial.println("LoRa initialized successfully!"); +} + +void sendLoRaData(String data) +{ + String message = "node3 " + data; // Concatenate "node3 " with the value of cleanString + + LoRa.beginPacket(); + LoRa.print(message); + LoRa.endPacket(); +} + +void readNFC() +{ + if (nfc.tagPresent()) + { + NfcTag tag = nfc.read(); + tagId = tag.getUidString(); + Serial.print("Tag ID: "); + Serial.println(tagId); + + if (tag.hasNdefMessage()) + { + NdefMessage message = tag.getNdefMessage(); + int recordCount = message.getRecordCount(); + Serial.print("Number of NDEF records: "); + Serial.println(recordCount); + + for (int i = 0; i < recordCount; i++) + { + NdefRecord record = message.getRecord(i); + int payloadLength = record.getPayloadLength(); + byte payload[payloadLength]; + record.getPayload(payload); + + String payloadAsString = ""; + for (int c = 0; c < payloadLength; c++) + { + payloadAsString += (char)payload[c]; + } + + cleanString = payloadAsString; + cleanString.remove(0, 3); + Serial.print("Payload: "); + Serial.println(cleanString); + + String uid = record.getId(); + if (uid != "") + { + Serial.print("Record ID: "); + Serial.println(uid); + } + } + } + else + { + Serial.println("No NDEF message found on the tag."); + } + delay(2000); + } +} diff --git a/arduino/LoRareceiver/utilities.h b/arduino/LoRareceiver/utilities.h new file mode 100644 index 0000000..95e78ca --- /dev/null +++ b/arduino/LoRareceiver/utilities.h @@ -0,0 +1,167 @@ + +#pragma once + +/* +* arduinoLoRa Library just only support SX1276/Sx1278,Not support SX1262 +* */ +// #define LILYGO_TBeam_V0_7 +#define LILYGO_TBeam_V1_0 +// #define LILYGO_TBeam_V1_1 +// #define LILYGO_T3_V1_0 +// #define LILYGO_T3_V1_6 +// #define LILYGO_T3_V2_0 +// #define LILYGO_T95_V1_0 + +/* +* if you need to change it, +* please open this note and change to the frequency you need to test +* Option: 433E6,470E6,868E6,915E6 +* */ + +#define LoRa_frequency 868E6 + + +#define UNUSE_PIN (0) + +#if defined(LILYGO_TBeam_V0_7) +#define GPS_RX_PIN 12 +#define GPS_TX_PIN 15 +#define BUTTON_PIN 39 +#define BUTTON_PIN_MASK GPIO_SEL_39 +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define BOARD_LED 14 +#define LED_ON HIGH +#define LED_OFF LOW + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_TBeam_V1_0) || defined(LILYGO_TBeam_V1_1) + +#define GPS_RX_PIN 34 +#define GPS_TX_PIN 12 +#define BUTTON_PIN 38 +#define BUTTON_PIN_MASK GPIO_SEL_38 +#define I2C_SDA 21 +#define I2C_SCL 22 +#define PMU_IRQ 35 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define BOARD_LED 4 +#define LED_ON LOW +#define LED_OFF HIGH + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_T3_V1_0) +#define I2C_SDA 4 +#define I2C_SCL 15 +#define OLED_RST 16 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V1_6) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 25 +#define LED_ON HIGH + +#define ADC_PIN 35 + +#define HAS_SDCARD +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V2_0) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN UNUSE_PIN +#define RADIO_BUSY_PIN UNUSE_PIN + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 0 +#define LED_ON LOW + +#define HAS_DISPLAY +#define HAS_SDCARD + +#elif defined(LILYGO_T95_V1_0) + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 18 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 23 +#define RADIO_CS_PIN 5 +#define RADIO_DI0_PIN 26 +#define RADIO_RST_PIN 4 +#define RADIO_DIO1_PIN 33 +#define RADIO_DIO2_PIN 32 +#define RADIO_BUSY_PIN UNUSE_PIN + +#define ADC_PIN 35 +#define HAS_DISPLAY + +#else +#error "Please select the version you purchased in utilities.h" +#endif