LoRaWAN communication calcul

Bonjour j'ai ce sketch:

/*******************************************************************************
   Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman

   Permission is hereby granted, free of charge, to anyone
   obtaining a copy of this document and accompanying files,
   to do whatever they want with them without any restriction,
   including, but not limited to, copying, modification and redistribution.
   NO WARRANTY OF ANY KIND IS PROVIDED.

   This example sends a valid LoRaWAN packet with payload "Hello,
   world!", using frequency and encryption settings matching those of
   the (early prototype version of) The Things Network.

   Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in g1,
    0.1% in g2).

   Change DEVADDR to a unique address!
   See http://thethingsnetwork.org/wiki/AddressSpace

   Do not forget to define the radio type correctly in config.h.

 *******************************************************************************/

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>

#define LMIC_DEBUG_LEVEL 2

// Change this!
// This should be in little endian format.
static const u1_t PROGMEM DEVEUI[8] = {0xca, 0xde, 0xe3, 0xaf, 0x1c, 0xde, 0xad, 0x0b};
void os_getDevEui (u1_t* buf) {
  memcpy_P(buf, DEVEUI, 8);
}

// Change this!
static const u1_t PROGMEM APPKEY[16] = {0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
void os_getDevKey (u1_t* buf) {
  memcpy_P(buf, APPKEY, 16);
}

static osjob_t sendjob;

// Schedule TX every this many seconds
const unsigned TX_INTERVAL = 40;

// Pin mapping
const lmic_pinmap lmic_pins = {
  .nss = 10,
  .rxtx = LMIC_UNUSED_PIN,
  .rst = 9,
  .dio = {2, 6, 7},
};

void onEvent (ev_t ev) {
  Serial.print(os_getTime());
  Serial.print(": ");
  switch (ev) {
    case EV_SCAN_TIMEOUT:
      Serial.println(F("EV_SCAN_TIMEOUT"));
      break;
    case EV_BEACON_FOUND:
      Serial.println(F("EV_BEACON_FOUND"));
      break;
    case EV_BEACON_MISSED:
      Serial.println(F("EV_BEACON_MISSED"));
      break;
    case EV_BEACON_TRACKED:
      Serial.println(F("EV_BEACON_TRACKED"));
      break;
    case EV_JOINING:
      Serial.println(F("EV_JOINING"));
      break;
    case EV_JOINED:
      Serial.println(F("EV_JOINED"));
      break;
    case EV_RFU1:
      Serial.println(F("EV_RFU1"));
      break;
    case EV_JOIN_FAILED:
      Serial.println(F("EV_JOIN_FAILED"));
      break;
    case EV_REJOIN_FAILED:
      Serial.println(F("EV_REJOIN_FAILED"));
      break;
      break;
    case EV_TXCOMPLETE:
      Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
      if (LMIC.dataLen) {
        // data received in rx slot after tx
        Serial.print(F("Data Received: "));
        Serial.write(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
        Serial.println();
      }
      // Schedule next transmission
      os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
      break;
    case EV_LOST_TSYNC:
      Serial.println(F("EV_LOST_TSYNC"));
      break;
    case EV_RESET:
      Serial.println(F("EV_RESET"));
      break;
    case EV_RXCOMPLETE:
      // data received in ping slot
      Serial.println(F("EV_RXCOMPLETE"));
      break;
    case EV_LINK_DEAD:
      Serial.println(F("EV_LINK_DEAD"));
      break;
    case EV_LINK_ALIVE:
      Serial.println(F("EV_LINK_ALIVE"));
      break;
    default:
      Serial.println(F("Unknown event"));
      break;
  }
}

byte buffer[52];

void do_send(osjob_t* j) {
  // Check if there is not a current TX/RX job running
  if (LMIC.opmode & OP_TXRXPEND) {
    Serial.println(F("OP_TXRXPEND, not sending"));
  } else {
    // Prepare upstream data transmission at the next possible time.
    String message = "IoT is cool";
    message.getBytes(buffer, message.length() + 1);
    Serial.println("Sending: " + message);
    LMIC_setTxData2(1, (uint8_t*) buffer, message.length() , 0);
    Serial.println(F("Packet queued"));
  }
  // Next TX is scheduled after TX_COMPLETE event.
}

// Leave this empty
static const u1_t PROGMEM APPEUI[8] = {};
void os_getArtEui (u1_t* buf) {
  memcpy_P(buf, APPEUI, 8);
}

void setup() {
  Serial.begin(115200);
  Serial.println(F("Starting"));

#ifdef VCC_ENABLE
  // For Pinoccio Scout boards
  pinMode(VCC_ENABLE, OUTPUT);
  digitalWrite(VCC_ENABLE, HIGH);
  delay(1000);
#endif

  // LMIC init
  os_init();
  // Reset the MAC state. Session and pending data transfers will be discarded.
  LMIC_reset();
  LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);

  LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI);      // g-band
  LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7B),  BAND_CENTI);      // g-band
  LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band

  // Disable link check validation
  LMIC_setLinkCheckMode(0);
//  LMIC_setDrTxpow(DR_SF7, 23);
//  LMIC_setDrTxpow(DR_SF8, 23);
//  LMIC_setDrTxpow(DR_SF9, 23);
//  LMIC_setDrTxpow(DR_SF10, 23);
//  LMIC_setDrTxpow(DR_SF11, 23);
//  LMIC_setDrTxpow(DR_SF12, 23);

  // Start job
  do_send(&sendjob);
}

void loop() {
  os_runloop_once();
}

et ce sketch:


const int trigPin = 9;  
const int echoPin = 10; 
float duration, distance;

void setup(){
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  Serial.print("temps en secondes : ");
  Serial.println(millis()/1000);
  static bool isOccupied=false;
  static unsigned long occupiedStartTime=0;
  static unsigned long lastTransitionTime=0;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.0343) / 2;
  Serial.print("Distance en cm : ");
  Serial.println(distance);
  if(!isOccupied && distance>6 && millis()==0){
    Serial.println(isOccupied);
  }
  if(!isOccupied && distance <= 6){
    Serial.println(isOccupied);
    if(millis()-lastTransitionTime>=20000){
      isOccupied=true;
      Serial.println(isOccupied);
      lastTransitionTime=millis();
    }
    //Serial.println(isOccupied);
  }else if(!isOccupied && distance>6){
    Serial.println(isOccupied);
    occupiedStartTime=millis();
    lastTransitionTime=millis();
  }else if(isOccupied && distance>6){
    Serial.println(isOccupied); 
    if(millis()-lastTransitionTime>=20000){
      isOccupied=false;
      Serial.println(isOccupied);
      lastTransitionTime=millis();
      
  }
  //Serial.println(isOccupied); 
  
  }else if (isOccupied && distance <= 6) { // Si l'objet était occupé et est maintenant à une distance <= 6
    Serial.println(isOccupied); 
    occupiedStartTime = millis(); // Réinitialiser le temps de début d'occupation
    lastTransitionTime = millis(); // Réinitialiser le temps de la dernière transition
    //Serial.println(isOccupied); 
  } 
  if(isOccupied){
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(8, LOW);
    

  } else if (!isOccupied){
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(8, HIGH);
  }
  //Serial.println(millis());
  delay(1000);
}

J;aimerais que la variable isOccupied que je calcule dans le deuxeme sketch soit envoyes par paquets LORAWAN en demarrant le premier code.
mon probleme c'est que je n'arrive pas a envoyer isOccupied au premier sketch pour qu'il l'envoie par paquet et en essayant de fusionner les 2 codes en un seul, j'obtiens des resultats decevants.. comment faire ?

donc en bref, mon probleme c'est que je n'arrive pas a communiquer isOccupied au premier code

Je n'ai encore j'aimais utilisé LoRaWAN, je ne suis pas sur d'être une grande aide.

Mais déjà tu dit "en essayant de fusionner les 2 codes en un seul, j'obtiens des resultats decevants.."

Un point de départ serait peut être de nous montrer tes tentatives pour que l'on t'aiguille sur comment corriger.

Pour moi "je n'arrive pas a envoyer isOccupied au premier sketch " n'est pas très parlant, ou plutôt pas très précis. L'utilisation du terme envoyer me fait penser à une communication entre deux microcontroleur.

Mais tu ne décris pas du tout ton projet matériel sur lequel s'exécute tes deux codes. Ni comment ils intéragissent.

Ou alors, comme je crois comprendre plus tôt, il ne s'agit pas d'envoyer une variable, mais d'implémenter le fonctionnel du second sketch dans le premier. Mais pour ca, on a besoin de voir tes tentatives pour aider à corriger.

vous comprenez le premier code ?

Oui, bonjour !
Excusez-moi, je me suis mal exprimé :sweat_smile:
oui, c'est vrai vous avez tout à fait raison... ce que je souhaite faire, c'est d'implémenter le fonctionnel du second sketch dans le premier... Le deuxième sketch fait envoyer des paquets contenant le message "IoT is cool"... j'aimerais, à la place de cette chaîne de caractères, envoyer des paquets contenant la variable isOccupied calculer dans le premier sketch... :upside_down_face:

Bonjour :slightly_smiling_face:
Voici le lien du site qui m'aide dans ma documentation à propos du sujet...
C'est un peu grâce à lui que j'essaie de comprendre le code, mais je crois qu'il me reste encore beaucoup à apprendre, car certains détails m'échappent :innocent:
http://wiki.lahoud.fr/doku.php?id=exploring_lorawan

si vous débutez, je vous conseille de prendre une MKR WAN 1300 ou 1310 et de regarder les bibliothèques fournies avec l'environnement Arduino (la MKRWAN library, développée par Arduino et Arduino LoRalibrary, de Sandeep Mistry).

https://docs.arduino.cc/learn/communication/lorawan-101/

vous n'aurez pas à gérer toute la config bas niveau et vous pourrez vous concentrer sur l'envoi de messages.

(on trouve aussi des produits ESP32 avec Lora intégré)

Merci beaucoup !!

le protocole LoRaWAN devra y être géré avec la bibliothèque LMIC (ou équivalente) , @toniboudi resterait confronté au problème qu'il rencontre déjà

Pour cela il existe , outre la carte MKRWAN1310 , (peut poser qq pb avec le résau TTN par manque de suivi de la bibliothèque)
le module LoRa E5 GRove
le module Crowtail LoRaWAN RA-08H (j'en attends un pour essai)
le petit module E78 -868LN22 , pas évident à souder

le protocole LoRAWAN est , avec ces solutions, pris en charge par un petit microcontrolleur dédié , il suffit de les piloter par quelques commandes AT

@toniboudi : as tu validé à 100% ton premier programme, une passerelle LoRaWAN est elle active dans ton secteur pour relayer tes trames vers le serveur d'un réseau qui t'es ouvert ?

Oui c’est vrai j’avais en tête Lora et pas Lorawan ce qui complique un peu

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.