1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| /*
* Emetteur sur FireBeetle DFR0478 + LoRa RFM95
* Soil moisture sensor sur ADC A3 (GPIO 35)
* Tension batterie sur A0 (GPIO 36)
* Commande Vcc soil sensor + Vcc module LoRa via MOSFET sur GPIO 12
* Deep sleep timer
* FF - mars 2023
*/
// Libraries for LoRa module SX1276
#include <SPI.h>
#include <LoRa.h>
#include <driver/rtc_io.h>
// Soil moisture sensor
#define soil 35
// Constant for dry sensor
const int DryValue = 2590;
// Constant for wet sensor
const int WetValue = 1430;
// Variables for soil moisture
int soilMoistureValue;
int soilMoisturePercent;
// (Vcc Soil + Vcc LoRa) enable pin
const int Vccenable = 12;
// Battery voltage
const int vinput = 36;
// Constant for voltage measurement
int valueVolt = 0;
float vbatt = 0.0;
// Define the pins used by the LoRa transceiver module
#define SCK 18
#define MISO 19
#define MOSI 23
#define SS 5
#define SS_GPIO_NUM GPIO_NUM_5
#define RST 14
#define RST_GPIO_NUM GPIO_NUM_14
#define DIO0 2
//433E6 for Asia
//868E6 for Europe
//915E6 for North America
#define BAND 868E6 // For France
// LoRa message variable
String outgoing;
// Addresses
byte localAddress = 0xCC; // 204 in Decimal
byte destination = 0xFF; // 255 in Decimal
// Count of outgoing messages
byte msgCount = 0;
// Count of LoRa sent times
byte lorasent;
const uint64_t uS_TO_S_FACTOR = 1000000ull; /* Conversion factor for micro
seconds to seconds */
const uint64_t TIME_TO_SLEEP = 10ull; /* Time ESP32 will go to sleep (in
seconds) */
RTC_DATA_ATTR uint32_t bootCount = 0;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
delay(500);
// Switch Vcc on
pinMode(Vccenable,OUTPUT);
digitalWrite(Vccenable, HIGH);
delay(1000);
rtc_gpio_hold_dis(SS_GPIO_NUM);
rtc_gpio_hold_dis(RST_GPIO_NUM);
// Define soil analog pin mode + set ADC to use 12 bits
pinMode(soil,INPUT);
analogReadResolution(12);
// Battery voltage reading
valueVolt = analogRead(vinput);
vbatt = (valueVolt * 3.3) / 4095.0;
// SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
// Setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
Serial.println("Lancement de LoRa échoué");
while (true); yield();
}
Serial.println("Initialisation LoRa OK !");
// Get soil moisture value
soilMoistureValue = analogRead(soil);
// Determine soil moisture percentage value
soilMoisturePercent = map(soilMoistureValue, DryValue, WetValue, 0, 100);
// Keep values between 0 and 100
soilMoisturePercent = constrain(soilMoisturePercent, 0, 100);
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Number of packets transmitted (default = 1)
for(lorasent=0 ; lorasent<1 ; lorasent++)
{
outgoing = byte(destination) + String("*") + byte(localAddress) + String("!") + String(soilMoisturePercent) + String("/") + String(vbatt);
// LoRa packet sent
LoRa.beginPacket();
LoRa.print(outgoing);
LoRa.endPacket();
delay(500);
}
msgCount++; // Increment message ID
Serial.print("LoRa MESSAGE SENT: ");
Serial.println(String(outgoing));
Serial.println("destination: " + String("0x") + String(destination, HEX));
Serial.println("localAddress: " + String("0x") + String(localAddress, HEX));
Serial.println("Humidity: " + String(soilMoisturePercent) + String("%"));
Serial.println("Battery: " + String(vbatt) + String("V"));
// Configure the wake up source (Timer)
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Stop LoRa module before going to deep sleep
LoRa.end();
delay(600);
// Set SS and RST before going to deep sleep
digitalWrite(SS, HIGH);
rtc_gpio_hold_en(SS_GPIO_NUM);
digitalWrite(RST, HIGH);
rtc_gpio_hold_en(RST_GPIO_NUM);
// Disable Vcc (soil moisture sensor + LoRa module)
digitalWrite(Vccenable, LOW);
delay(500);
// Start going to deep sleep
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
void loop() {} |
Partager