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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// TTGO Lora
#include <LoRa.h>
#include <SPI.h>
//Libraries for Wifi
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
//Libraries annexes
#include "secret.h"
#include "Image.h"
#include <BlynkSimpleEsp32.h>
#include <OneButton.h> // http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
//Libraries for OLED Display
#include "SSD1306.h"
// 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 433E6
// OLED definition
#define I2C_SDA 4
#define I2C_SCL 15
SSD1306Wire display(0x3c, I2C_SDA, I2C_SCL);
// LED definition
#define LED1 32 // Volet BAL
#define LED2 33 // Porte BAL
#define LED3 13 // Acquit
const int pinAQT = 35;
OneButton AQT(pinAQT,false,false);
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
char auth[] = BLYNK_AUTH_TOKEN;
unsigned long currentTime=0;
unsigned long previousTime=0;
const long interval = 30000; // Période de Liaison
float VBAT = 0;
byte BAL = 0;
int xx= 36;
int yy =0;
int tt =100;
//*****************************
// SETUP
//*****************************
void setup() {
Serial.begin(115200);
pinMode(pinAQT,INPUT_PULLDOWN);
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
while (!Serial) yield();
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.setPins(SS,RST,DI0);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (true) yield();
}
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.drawString(0 , 15 , "Recepteur: OK");
display.display();
delay(100);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
AQT.attachDoubleClick(doubleClick);
WiFi.mode(WIFI_STA);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
}
//******************************
// LOOP
//******************************
void loop() {
currentTime=millis();
if (WiFi.status() != WL_CONNECTED) {
Serial.print("connexion WIFI : ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(2000);
}
Serial.println("\nConnecté.");
}
Blynk.run();
AQT.tick();
delay(10);
int packetSize = LoRa.parsePacket();
if (packetSize)
{
previousTime=currentTime;
char buffer[packetSize + 1]; // +1 pour mettre un caractère nul à la fin
size_t position = 0;
// on récupère le message dans le buffer
while (LoRa.available()) buffer[position++]=(char)LoRa.read();
// on termine le message (on suppose que c'est de l'ASCII)
buffer[position]*= '\0';
// On imprime le message
Serial.print("Reception: "); Serial.println(buffer);
switch (buffer[0]) {
case '1':
Serial.println("Ouverture Volet");
digitalWrite(LED1,HIGH);
delay(500);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED3,HIGH);
delay(50);
BAL = 1;
courrier();
IOT_Etat();
break;
case '2':
Serial.println("Ouverture Porte");
digitalWrite(LED2,HIGH);
delay(500);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED3,HIGH);
delay(50);
BAL = 1;
colis();
IOT_Etat();
break;
case '3':
Serial.println("Acquit");
digitalWrite(LED3,LOW);
delay(50);
BAL = 0;
IOT_Etat();
display.clear();
display.display();
break;
case 'V':
if (BAL == 0) {
Serial.println("- Liaison OK -");
previousTime=currentTime;
digitalWrite(LED3,HIGH);
delay(500);
digitalWrite(LED3,LOW);
delay(50);
}
int valeurBrute = atoi(buffer+1); // +1 pour sauter le 'V' et commencer à analyser à partir du premier chiffre
VBAT = (valeurBrute / 4095.0)*2.0*3.3*1.1; // mettre .0 pour le traitment en decimal et 4095
AffVBAT();
break;
}
} else if((currentTime-previousTime)>3*interval) { // Perte Liaison <===========================
previousTime=currentTime;
AffLiaison();
}
}
//// Perte Liaison ////
void AffLiaison() {
liaison();
Serial.print("- Perte Liaison -");
}
//// Battery Voltage ////
void AffVBAT() {
Serial.print("Vbat= "); Serial.print(VBAT); Serial.print(" Volts");
display.clear();
display.display();
if (VBAT <= 3.4) {
batterielow();
} else {
int progress = ((VBAT-3.2)*100);
Serial.print(" % = "); Serial.println(progress);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Batterie");
display.drawProgressBar(0, 32, 120, 10, progress);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 15, String(progress) + "%");
display.display();
delay(5000);
}
IOT_Etat();
display.clear();
display.display();
}
//// Envoi Blynk ////////
void IOT_Etat() {
Serial.println();
Serial.println("Envoi ...");
Blynk.virtualWrite(V0, BAL);
int pVBAT = ((VBAT-3.2)*100);
Blynk.virtualWrite(V1, pVBAT);
delay(1000);
} |
Partager