j'ai fait un code qui fonctionne bien en émetteur récepteur , mais je n'arrive pas a trouver comment passé en duplex, ça coupe l’émission.
// RX //
#include <WiFi.h>
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <SSD1306Wire.h>
#include "board_defs.h" // broches utilisées par le module LILYGO® TTGO LoRa32 V2.1_1.6
#include "images.h"
#include "my_fonts.h"
#define effet 13 // sortie effet
String tally; // pour afficher les pointillés
String packet; // paquet à envoyer sur LoRa
String rssi = "RSSI";
String packSize = "--";
float battery = 0; // Voltage de batterie
int value;
SSD1306Wire display(OLED_ADDRESS, OLED_SDA, OLED_SCL);
SPIClass loraSPI(VSPI); //Entrée pour utiliser le module LoRa sur le module VSPI (SPI3) Tension
void setup() {
pinMode(LED, OUTPUT); // Led verte
pinMode(LEDTally, OUTPUT); // effet
pinMode(VBAT, INPUT); // Entrée pour la tension de la batterie
Serial.begin(115200);
while (!Serial);
delay(500);
// OLED
display.init();
display.flipScreenVertically();
display.clear();
delay(100);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(Monospaced_bold_11);
// Module LoRa
loraSPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DI0);
LoRa.setSPI(loraSPI);
LoRa.setSpreadingFactor(12); //entre 7 et 12 (7 plus rapide, mais distance plus courte)
LoRa.setCodingRate4(8); //entre 5 et 8 (8 plus lent mais plus de sécurité)
LoRa.enableCrc(); //activer le contrôle CRC pour une communication garantie sans erreur
if (!LoRa.begin(LORA_BAND)) // 868 Mhz
{
display.drawString(0, 0, "Bande 868Mh Erreur");
display.display();
while (1)
;
}
display.drawString(0, 0, "Reception ON");
display.display();
// définir un mot de synchronisation à utiliser avec les deux modules pour la synchronisation
LoRa.setSyncWord(0xF3); // de 0 à 0xFF, par défaut 0x34, voir docs API
LoRa.receive();
delay(300);
// vérifier la batterie
battery = checkBattery(); // première vérification de la batterie au démarrage
display.drawString(0, 22, "Battery: " + String(battery) + "V");
display.display();
delay(1000);
display.drawString(0, 44, "Init OK");
display.display();
delay(2000);
display.clear();
}
void loop() {
// variables utilisées pour vérifier la batterie et donner une valeur moyenne
static uint16_t chkbat = 0;
static float tempbatt = 0;
display.clear();
display.drawString(11, 22, "RX ");
display.drawString(0, 33, String(battery) + "V");
display.drawString(75, 33, "868 Mhz");
display.drawString(0, 44, rssi);
value = map(LoRa.packetRssi(), -115, 0, 1, 70);
display.fillRect(60, 47, value, 8);
display.drawString(0, 55, tally);
display.display();
int packetSize = LoRa.parsePacket();
if (packetSize) getLoRaPacket(packetSize);
// tension de la batterie en moyenne sur 2000 lectures
tempbatt += checkBattery();
chkbat++;
if (chkbat == 2000) {
chkbat = 0;
battery = (tempbatt / 2000);
tempbatt = 0;
}
}
// Reconstruire le paquet LoRa
void getLoRaPacket(int packetSize) {
packet = "";
packSize = String(packetSize, DEC);
Serial.print("test: ");
Serial.println(packSize);
for (int i = 0; i < packetSize; i++) {
packet += (char)LoRa.read(); // reconstruire le paquet caractère par caractère
}
rssi = "RX:" + String(LoRa.packetRssi(), DEC);
if (packet == "1234") {
tally = ("------------------");
digitalWrite(effet, HIGH);
//LedOn();
if (value <= 1) {
tally = ("");
digitalWrite(effet, LOW);
//LedOff();
}
} else {
tally = ("");
digitalWrite(effet, LOW);
//LedOff();
}
}
void LedOn() {
digitalWrite(LED, HIGH);
}
void LedOff() {
digitalWrite(LED, LOW);
}
float checkBattery(void) {
return (float)(analogRead(VBAT)) / 4095 * 2 * 3.3 * 1.1;
}
pour le TX
#include <WiFi.h>
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <SSD1306Wire.h>
#include "board_defs.h" // broches utilisées par le module LILYGO® TTGO LoRa32 V2.1_1.6
#include "images.h"
#include "my_fonts.h"
#define Contact1 13 // entrée contact tally
String tally; // pour afficher les pointillés
String verif =("agito"); // paquet à envoyer sur LoRa pour verif liaison
uint32_t envoitally=1234; // variable pour envoi effet
float battery=0; // Voltage de batterie
SSD1306Wire display(OLED_ADDRESS, OLED_SDA, OLED_SCL);
SPIClass loraSPI(VSPI); // module LoRa sur le module VSPI (SPI3)
void setup()
{
//
pinMode(LED,OUTPUT); // Led verte carte
pinMode(VBAT,INPUT); // Entrée pour la tension de la batterie
pinMode(Contact1, INPUT_PULLUP); // contact effet
//
Serial.begin(115200);
while (!Serial);
delay(500);
// OLED
display.init();
// affichage antenne vers le haut
display.flipScreenVertically();
display.clear();
delay(100);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(Monospaced_bold_11);
// Module LoRa
loraSPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DI0);
LoRa.setSPI(loraSPI);
LoRa.setSpreadingFactor(12); //entre 7 et 12 (7 plus rapide, mais distance plus courte)
LoRa.setCodingRate4(8); //entre 5 et 8 (8 plus lent mais plus de sécurité)
LoRa.enableCrc(); //activer le contrôle CRC pour une communication garantie sans erreur
if (!LoRa.begin(LORA_BAND)) // 868 Mhz
{
display.drawString(0,0,"Liaison Erreur");
display.display();
while (1);
}
display.drawString(0,0,"Emission ON");
display.display();
// définir un mot de synchronisation à utiliser avec les deux modules pour la synchronisation
LoRa.setSyncWord(0xF3); // de-a: 0-0xFF, par defaut 0x34, voir API documentations
delay(300);
// test batterie
battery=checkBattery(); // première vérification de la batterie au démarrage
display.drawString(0,22,"Battery: "+String(battery)+"V");
display.display();
delay(1000);
display.drawString(0,44,"Init OK");
display.display();
delay(2000);
display.clear();
}
void loop()
{
// variables utilisées pour vérifier la batterie et donner une valeur moyenne
static uint8_t chkbat=0;
static float tempbatt=0;
// affichage principal
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(Monospaced_bold_11);
display.drawString(0,33,"TX ");
display.drawString(0,44,String(battery)+"V");
display.drawString(75,44,"868 Mhz");
display.drawString(0, 55,tally);
//display.drawString(48,44, String(counter));
display.display();
int T1 = digitalRead(Contact1); // lis le contact effet
if (T1 == LOW) {
tally =("------------------");
// envoyer un paquet sur LoRa
//packet="@"+String(envoitally)+"#";
LoRa.beginPacket();
LoRa.print(envoitally);
LoRa.endPacket();
}
else
{
tally =("");
LoRa.beginPacket();
LoRa.print(verif);
LoRa.endPacket();
}
// tension de la batterie en moyenne sur 20 lectures
tempbatt+=checkBattery();
chkbat++;
if (chkbat==20)
{
chkbat=0;
battery=(tempbatt/20);
tempbatt=0;
}
delay(50);
}
float checkBattery(void)
{
return (float)(analogRead(VBAT))/4095*2*3.3*1.1;
}