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
|
/*************************************
EMETTEUR
************************************/
#include <driver/rtc_io.h>
#include <LoRa.h>
#include <SPI.h>
// pins from https://randomnerdtutorials.com/ttgo-lora32-sx1276-arduino-ide/
// LoRa definition
#define SCK 18 // GPIO18 -- SX1278's SCK
#define MISO 19 // GPIO19 -- SX1278's MISO
#define MOSI 23 // GPIO23 -- SX1278's MOSI
#define SS 5 // GPIO5 -- SX1278's CS
#define RST 2 // GPIO2 -- SX1278's RESET
#define DI0 27 // GPIO27 -- SX1278's IRQ(Interrupt Request)
#define BAND 433E6 //433E6 for Asia, 866E6 for Europe and 915E6 for North America
const gpio_num_t pinVolet = GPIO_NUM_34;
const gpio_num_t pinPorte = GPIO_NUM_33;
const gpio_num_t vbatPin = GPIO_NUM_35;
#define BUTTON_PIN_BITMASK (1ull << pinVolet) | (1ull << pinPorte) //
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 int bootCount = 0;
//***********************************************
void loraMessage(const char * message) {
LoRa.beginPacket();
LoRa.println(message);
LoRa.endPacket();
}
//***********************************************
void envoiVbatBrut() {
const int tailleMax = 20; // y compris le '\0' de fin
static char vBatMessage[tailleMax]; // doit être assez grand pour contenir le message
int vBat = analogRead(vbatPin); // tension échantillonnée brute
Serial.print("Valeur Brute = "); Serial.println(vBat);
int n = snprintf(vBatMessage, sizeof vBatMessage, "V%d", vBat); // 'V' suivi de la tension échantillonnée brute
if ((n > 0) && (n < tailleMax)) // si on a pu écrire correctement le message
loraMessage(vBatMessage); // alors envoi du message
}
//***********************************************
void action() {
esp_sleep_wakeup_cause_t source_reveil = esp_sleep_get_wakeup_cause();
uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
switch (source_reveil) {
case ESP_SLEEP_WAKEUP_TIMER :
Serial.println("Réveil par un timer");
envoiVbatBrut();
break;
default :
Serial.print("Réveil pas causé par le Deep Sleep :");
Serial.println(source_reveil);
break;
}
if (GPIO_reason != 0) {
const uint64_t gpioWakeUp = __builtin_ctzll(GPIO_reason); // __builtin_ctzll finds the position of the rightmost 1
switch (gpioWakeUp) {
case pinVolet: loraMessage("1"); break;
case pinPorte: loraMessage("2"); break;
default : Serial.println("Aucun GPIO connu n'a déclenché le réveil"); break;
}
}
}
//***********************************************
void setup() {
Serial.begin(115200);
while (!Serial) yield();
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DI0);
Serial.println("LoRa : Emetteur");
if (!LoRa.begin(BAND)) { // LoRa_frequency
Serial.println("Le lancement de LoRa a échoué !");
while (true) yield();
}
Serial.println("init ok");
//Increment boot number and print it every reboot
++bootCount;
Serial.print("Boot number: "); Serial.println(bootCount);
action();
// go back to sleep
LoRa.end();
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_AUTO);
rtc_gpio_pullup_en(pinVolet); //https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html#external-wakeup-ext1
rtc_gpio_pullup_en(pinPorte);
rtc_gpio_pulldown_dis(pinVolet);
rtc_gpio_pulldown_dis(pinPorte);
Serial.println("Going to sleep");
Serial.flush(); // just for peace of mind, as esp_deep_sleep_start() will flush the contents of UART FIFOs
esp_deep_sleep_start();
}
void loop() {} |
Partager