-
-
Notifications
You must be signed in to change notification settings - Fork 655
Description
I recently encountering an issue i want to use ESP32S with LORA RYL896 but issue is from the Sender i send the data to my Receiver but unable to receive it although Lora respond OK while configuring with AT Commands maybe the pin out of RX TX not be corrected can anyone help me guiding the right pin outs for it
Sender Code
#include <Wire.h>
#include <Adafruit_INA219.h>
#include "HX710B.h"
#include <DHT.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
// Sensor Pins
#define MHSENSOR_PIN A0
#define MQ135_SENSOR_PIN A2
#define SENSOR_LED_PIN 8
#define LDR_SENSOR_PIN A1
#define RAIN_DROP_ANALOG_PIN A3
#define DHT_SENSOR_PIN 5
#define DHT_TYPE DHT11 // Change to DHT22 if needed
const int PRESSURE_DOUT_PIN = 2;
const int PRESSURE_SCLK_PIN = 3;
Adafruit_INA219 ina219;
HX710B pressure_sensor;
DHT dht(DHT_SENSOR_PIN, DHT_TYPE);
const float MAX_BATTERY_VOLTAGE = 8.4;
const float MIN_BATTERY_VOLTAGE = 6.0;
bool isFirstTransmission = true;
void setup() {
Serial.begin(115200);
Wire.begin();
if (!ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1);
}
pressure_sensor.begin(PRESSURE_DOUT_PIN, PRESSURE_SCLK_PIN, 128);
dht.begin();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR_LED_PIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(SENSOR_LED_PIN, HIGH);
Serial.print("AT\r\n");
delay(100);
Serial.print("AT+ADDRESS=115\r\n");
delay(100);
Serial.print("AT+PARAMETER=10,7,1,7\r\n");
delay(100);
Serial.print("AT+BAND=868500000\r\n");
delay(100);
Serial.print("AT+NETWORKID=6\r\n");
delay(100);
Serial.print("AT+POWER=20\r\n");
delay(100);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(SENSOR_LED_PIN, HIGH);
// Read sensors
float humidity = (analogRead(MHSENSOR_PIN) / 1023.0) * 100.0;
float gasConcentration = (analogRead(MQ135_SENSOR_PIN) / 1023.0) * 100.0;
float lightLevel = (analogRead(LDR_SENSOR_PIN) / 1023.0) * 100.0;
float rainAnalog = (analogRead(RAIN_DROP_ANALOG_PIN) / 1023.0) * 100.0;
float temperature = dht.readTemperature();
float humidityDHT = dht.readHumidity();
// Battery calculation
float busVoltage = ina219.getBusVoltage_V();
float battery_percentage = ((busVoltage - MIN_BATTERY_VOLTAGE) / (MAX_BATTERY_VOLTAGE - MIN_BATTERY_VOLTAGE)) * 100.0;
String battery_flag = (battery_percentage <= 25.0) ? "00" :
(battery_percentage <= 50.0) ? "01" :
(battery_percentage <= 75.0) ? "10" : "11";
// ✅ Wake up LoRa before measurement
Serial.print("AT+MODE=0\r\n");
delay(100);
// ✅ Measure current **right before** sending data
float current_mA = ina219.getCurrent_mA();
// ✅ Build JSON message
String jsonMessage = "{";
jsonMessage += "\"moist\":" + String(humidity, 1) + ",";
jsonMessage += "\"gas\":" + String(gasConcentration, 1) + ",";
jsonMessage += "\"light\":" + String(lightLevel, 1) + ",";
jsonMessage += "\"rain\":" + String(rainAnalog, 1) + ",";
jsonMessage += "\"temp\":" + String(temperature, 1) + ",";
jsonMessage += "\"humidity\":" + String(humidityDHT, 1) + ",";
jsonMessage += "\"bat\":\"" + battery_flag + "\",";
jsonMessage += "\"current\":" + String(current_mA, 2);
jsonMessage += "}";
// Send payload to Reciever
int payloadLength = jsonMessage.length();
String command = "AT+SEND=117," + String(payloadLength) + "," + jsonMessage;
Serial.print(command + "\r\n");
// ✅ Wait for LoRa module to confirm sending
unsigned long startTime = millis();
while (millis() - startTime < 2000) { // Wait max 2 seconds
if (Serial.available()) {
String response = Serial.readString();
if (response.indexOf("OK") != -1) {
break; // Exit loop if "OK" is received
}
}
}
// Put LoRa module to sleep
Serial.print("AT+MODE=1\r\n");
delay(500);
// ✅ Turn off sensors
turnOffSensors();
// ✅ Put Arduino to deep sleep
goToSleep();
}
void turnOffSensors() {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(SENSOR_LED_PIN, LOW);
digitalWrite(MHSENSOR_PIN, LOW);
digitalWrite(MQ135_SENSOR_PIN, LOW);
digitalWrite(LDR_SENSOR_PIN, LOW);
digitalWrite(RAIN_DROP_ANALOG_PIN, LOW);
digitalWrite(DHT_SENSOR_PIN, LOW);
pinMode(MHSENSOR_PIN, INPUT);
pinMode(MQ135_SENSOR_PIN, INPUT);
pinMode(LDR_SENSOR_PIN, INPUT);
pinMode(RAIN_DROP_ANALOG_PIN, INPUT);
pinMode(DHT_SENSOR_PIN, INPUT);
}
void goToSleep() {
Serial.println("Going to sleep...");
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_enable();
// Sleep for 8 seconds
wdt_enable(WDTO_8S);
sei();
sleep_cpu();
// Waking up after 8 seconds, go back to sleep for 2 more seconds
sleep_disable();
wdt_disable();
wdt_enable(WDTO_2S);
sleep_enable();
sleep_cpu();
// Fully wake up
sleep_disable();
wdt_disable();
Serial.println("Waking up...");
wakeUpSensors();
}
void wakeUpSensors() {
pinMode(MHSENSOR_PIN, OUTPUT);
pinMode(MQ135_SENSOR_PIN, OUTPUT);
pinMode(LDR_SENSOR_PIN, OUTPUT);
pinMode(RAIN_DROP_ANALOG_PIN, OUTPUT);
pinMode(DHT_SENSOR_PIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(SENSOR_LED_PIN, HIGH);
dht.begin();
pressure_sensor.begin(PRESSURE_DOUT_PIN, PRESSURE_SCLK_PIN, 128);
Serial.print("AT+MODE=0\r\n"); // Wake up LoRa module
delay(100);
}
Receiver SIDE ESP32S
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Mi 13";
const char* password = "12345678";
const char* databaseURL = "https://green-bot-d7200-default-rtdb.firebaseio.com/sensorData.json";
void sendToFirebase(String data) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(databaseURL);
http.addHeader("Content-Type", "application/json");
int httpCode = http.PUT(data);
if (httpCode == 200) {
Serial.println("Data sent to Firebase successfully.");
} else {
Serial.println("Failed to send data to Firebase. Error code: " + String(httpCode));
}
http.end();
} else {
Serial.println("WiFi not connected.");
}
}
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
delay(100);
Serial.println("Initializing LoRa module...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("IP address: " + WiFi.localIP().toString());
Serial1.print("AT\r\n");
delay(100);
Serial1.print("AT+ADDRESS=117\r\n");
delay(100);
Serial1.print("AT+PARAMETER=10,7,1,7\r\n");
delay(100);
Serial1.print("AT+BAND=868500000\r\n");
delay(100);
Serial1.print("AT+NETWORKID=6\r\n");
delay(100);
Serial1.print("AT+POWER=20\r\n");
}
void loop() {
if (Serial1.available()) {
String receivedLine = Serial1.readStringUntil('\n');
receivedLine.trim();
Serial.println("Received: " + receivedLine);
if (receivedLine.startsWith("+RCV=")) {
// Extract payload after "+RCV="
String payload = receivedLine.substring(5);
payload.trim();
// Extract address and length parts
int addrComma = payload.indexOf(',');
if (addrComma == -1) return;
int lengthComma = payload.indexOf(',', addrComma + 1);
if (lengthComma == -1) return;
// Extract the actual message (JSON data)
String message = payload.substring(lengthComma + 1);
message.trim();
// Ensure we have valid JSON format by keeping only the first '}' occurrence
int jsonEnd = message.lastIndexOf('}');
if (jsonEnd != -1) {
message = message.substring(0, jsonEnd + 1); // Keep only valid JSON part
}
Serial.println("JSON: " + message);
// Send JSON to Firebase
sendToFirebase(message);
}
}
}