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 152 153 154 155 156 157 158 159 160 161
| /*************************************
EMETTEUR
************************************/
/***************************************
carte TTGO LoRa32-OLED V1
/----------------\
|19 18|
|20 17|
|21 16|
|22 15|
|23 14|
|24 13|
|25 12|
|26 11|
|27 10| GPIO35 vbatPin
MISO GPIO19 |28 9| GPIO32 volet
|29 8| GPI033 Porte
SS GPIO18 |30 7|
SCK GPIO05 |31 6| GPIO26 DI0
I2C_SCL GPIO15 |32 5| GPIO27 MOSI
|33 4| GPIO14 RST
I2C_SDA GPIO04 |34 3|
|35 2|
OLED_RST GPIO16 |36 1|
-----------------
*************************************/
#include <driver/rtc_io.h>
#include <LoRa.h>
#include <SPI.h>
//Libraries for OLED Display
#include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
// pins from https://randomnerdtutorials.com/ttgo-lora32-sx1276-arduino-ide/
// LoRa definition
#define SCK 5 // GPIO5 -- SX1278's SCK
#define MISO 19 // GPIO19 -- SX1278's MISO
#define MOSI 27 // GPIO27 -- SX1278's MOSI
#define SS 18 // GPIO18 -- SX1278's CS
#define RST 14 // GPIO14 -- SX1278's RESET
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND 866E6 //433E6 for Asia, 866E6 for Europe and 915E6 for North America
// OLED definition
#define I2C_SDA 4
#define I2C_SCL 15
#define OLED_RST 16
SSD1306Wire display(0x3c, I2C_SDA, I2C_SCL);
const gpio_num_t pinVolet = GPIO_NUM_32; // RTC GPIO for ESP32: 0, 2, 4, 12-15, 25-27, 32-39;
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 = 30ull; /* 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");
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 22, "Emetteur: OK");
display.display();
delay(100);
display.clear();
display.display();
//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