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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <LoRa.h>
#include <SPI.h>
#include <Wire.h>
//Libraries for OLED Display
#include "SSD1306.h"
// Optionally include custom images
#include "images.h"
#include "boards.h"
//Librairies Bouton
#include <OneButton.h> // http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
/*
#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 433E6
*/
SSD1306Wire display(0x3c, I2C_SDA, I2C_SCL);
const byte pinBouton1 = 32;
//#define BUTTON1_PIN_BITMASK 0x100000000 // 2^32 in hex
const byte pinBouton2 = 33;
//#define BUTTON2_PIN_BITMASK 0x200000000 // 2^33 in hex
const byte pinBouton3 = 13;
//#define BUTTON3_PIN_BITMASK 0x20000 // 2^13 in hex
#define BUTTON_PIN_BITMASK 0x300002000 // 13-32-33 in hex
RTC_DATA_ATTR int bootCount = 0;
OneButton sw1(pinBouton1);
int value1;
OneButton sw2(pinBouton2);
int value2;
OneButton sw3(pinBouton3);
int value3;
// mesure tension d'alimentation d'un ESP32
extern "C" int rom_phy_get_vdd33();
// CALLBACKS BOUTON 1
void sw1Click() {loraMessage("1");}
void sw1DoubleClick() {loraMessage("11");}
void sw1DebutLong() {loraMessage("1D");}
void sw1FinLong() {loraMessage("1F");}
// CALLBACKS BOUTON 2
void sw2Click() {loraMessage("2");}
void sw2DoubleClick() {loraMessage("21");}
void sw2DebutLong() {loraMessage("2D");}
void sw2FinLong() {loraMessage("2F");}
// CALLBACKS BOUTON 3
void sw3Click() {loraMessage("3");}
void sw3DoubleClick() {loraMessage("31");}
//void sw3DebutLong() {loraMessage("3D");}
void sw3FinLong() {loraMessage("3F");}
//**********************************************
// SETUP
//**********************************************
void setup() {
Serial.begin(115200);
initBoard();
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
while (!Serial) yield();
SPI.begin(RADIO_SCLK_PIN,RADIO_MISO_PIN,MOSI,RADIO_MOSI_PIN );
LoRa.setPins(RADIO_CS_PIN,RADIO_RST_PIN,RADIO_DIO_PIN);
Serial.println("LoRa Sender");
if (!LoRa.begin(LoRa_frequency)) { // LoRa_frequency
Serial.println("Starting LoRa failed!");
while (true) yield();
}
Serial.println("init ok");
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.drawString(0 , 15 , "Emetteur: OK");
display.display();
delay(2000);
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
print_wakeup_reason();
//Print the GPIO used to wake up
print_GPIO_wake_up();
// BTN1
sw1.attachClick(sw1Click);
sw1.attachDoubleClick(sw1DoubleClick);
sw1.attachLongPressStart(sw1DebutLong);
sw1.attachLongPressStop(sw1FinLong);
// BTN2
sw2.attachClick(sw2Click);
sw2.attachDoubleClick(sw2DoubleClick);
sw2.attachLongPressStart(sw2DebutLong);
sw2.attachLongPressStop(sw2FinLong);
// BTN3
sw3.attachClick(sw3Click);
sw3.attachDoubleClick(sw3DoubleClick);
sw3.attachLongPressStart(sw3DebutLong);
sw3.attachLongPressStop(sw3FinLong);
/*
esp_sleep_enable_ext0_wakeup(GPIO_NUM_32,0); //1 = High, 0 = Low
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,0); //1 = High, 0 = Low
esp_sleep_enable_ext0_wakeup(GPIO_NUM_13,0); //1 = High, 0 = Low
*/
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
//Go to sleep now
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
//**********************************************
// LOOP
//**********************************************
void loop() {
}
//************************************************
static void sw3DebutLong(){
float voltage = (rom_phy_get_vdd33());
Serial.println(voltage);
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0 , 15 , String("Vbat: ")+ (voltage/100)+String("V"));
display.display();
delay(1000);
}
//***********************************************
void loraMessage(const char * message) {
LoRa.beginPacket();
LoRa.println(message);
LoRa.endPacket();
}
//**********************************************
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Réveil provoqué par un signal externe utilisant RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Réveil provoqué par un signal externe utilisant RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Réveil provoqué par la minuterie"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Réveil causé par le pavé tactile"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Réveil provoqué par le programme ULP"); break;
default : Serial.printf("Le réveil n'a pas été causé par le sommeil profond : %d\n",wakeup_reason); break;
}
}
void print_GPIO_wake_up(){
String str;
uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
Serial.print("GPIO that triggered the wake up: GPIO ");
Serial.println((log(GPIO_reason))/log(2), 0);
str = ToString(((log(GPIO_reason))/log(2), 0)) //<====
switch str
case "32": loraMessage("1"); break;
case "33": loraMessage("2"); break;
case "13": loraMessage("3"); break;
default : Serial.printf("Aucun GPIO n'a déclenché le réveil :"); break;
} |
Partager