The test project uses a TMP36 sensor in the transmitter to send data wirelessly to the receiver, yet it has failed numerous times. I have double-checked the connections and the configuration of LoRa such as spread factor, bandwidth, coding rate, and frequency in Asia, yet it keeps failing. I need your help if I have overlooked some factors to solve the issue.
Below are the schematic diagram and the code.
[Transmitter]
[Receiver]
TX Code
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Library for pin assignments
#include <Wire.h>
//define the pins used by the transceiver module
#define SS 10
#define RESET 9
#define DIO0 2
#define SCK 13
#define MISO 12
#define MOSI 11
//433E6 for Asia
#define LORA_FREQUENCY 433E6 // Set your desired frequency (in Hz)
#define LORA_SPREADING_FACTOR 12
#define LORA_SIGNAL_BANDWIDTH 125E3
#define LORA_CODING_RATE 7
const int tmp36Pin = A0;
float temperature;
void setup() {
Serial.begin(115200);
while (!Serial);
if (!LoRa.begin(LORA_FREQUENCY)){ // Initialize LoRa module
Serial.println("LoRa initialization failed. Check your connections.");
while (1);
}
LoRa.setSpreadingFactor(LORA_SPREADING_FACTOR);
LoRa.setSignalBandwidth(LORA_SIGNAL_BANDWIDTH);
LoRa.setCodingRate4(LORA_CODING_RATE);
Serial.println("LoRa transmitter started.");
}
void loop() {
int sensorValue = analogRead(tmp36Pin); // Read temperature from TMP36 sensor
float voltage = sensorValue * (5.0 / 1023.0);
temperature = (((voltage - 0.5) * 100.0)* 1.8) +32;
// Send temperature data over LoRa
Serial.print("Sending temperature: ");
Serial.println(temperature);
LoRa.beginPacket();
LoRa.print(temperature);
LoRa.endPacket();
delay(5000); // Send data every 5 seconds
}
RX Code
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
//define the pins used by the transceiver module
#define ss 4
#define rst 14
#define dio0 21
void setup() {
//initialize Serial Monitor
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
//replace the LoRa.begin(---E-) argument with your location's frequency
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
while (!LoRa.begin(433E6)) {
Serial.println(".");
delay(500);
}
// Change sync word (0xF3) to match the receiver
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0-0xFF
LoRa.setSyncWord(0xF3);
Serial.println("LoRa Initializing OK!");
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Note: The assigned pins for ESP32 Devkit V1 for NSS & DIO0 have been changed due to conditions in the ESP32 pinout hence why it was specified in the RX code.