Partage
  • Partager sur Facebook
  • Partager sur Twitter

Envoie de données SigFox + Bluetooth

    30 juillet 2019 à 12:05:14

    Bonjour à tous !

    J'ai comme projet un PoC avec une carte MKR FOX 1200.
    J'aimerais pourvoir envoyer mes données toutes les 15min, et certaines alertes dans un intervalle d'1h pour éviter trop de messages ! Je n'arrives pas à créer les deux. J'ai simplement l'envoie toutes les 15min, et je n'arrives pas a visualiser comment faire pour l'autre envoie critique.

    Car cette carte utilise le réseau SigFox. 

    C'est un devkit et je suis limité a 140msg (12octets) envoie et 4 downlink(8octets).

    J'utilises une application APK pour gérer mon bluetooth pour pouvoir allumer/éteindre un relay. Sauf qu'avec mon code je n'arrives pas à gérer mon bluetooth pendant l'envoie des données à SigFox avec le délai de 15min ou même sans délai. 

    Sachant que j'ai des seuil indicative et certaines conditions (critique/alerte) qui servent à rien vue que je n'arrives pas à y envoyer !

    Mon code : 

    // ====================================/ LIBRARY UTILITIES \===================================
    #include <ArduinoLowPower.h>
    #include <SigFox.h> // library Sigfox
    #include <TH02_dev.h> // library Grove Temperature & humidity
    #include "Arduino.h"
    #include "Wire.h"
    #include <math.h> //library Grove Temperature Sensor
    #include <ZSharpIR.h> //library Grove Infrared Proximity Sensor
    
    
    // ==========================/ GROVE INFRARED PROXIMITY SENSOR - PIN A3 \===================
    
    #define ir A3 // ir: the pin where your sensor is attached
    #define model 1080 // model: an int that determines your sensor: 1080 for GP2Y0A21Y
    ZSharpIR SharpIR(ir, model);
    
    // ===============================/ GROVE SOUND SENSOR - PIN A0 \==========================
    
    #define SOUND_SENSOR A0 // define pin capteur Sound A0
    #define THRESHOLD_VALUE 100 // Sound seuil critique
    
    // =========================/ GROVE TEMPERATURE & HUMIDITY - PIN I2C \=====================
    
    #define CRITIQUE_VALUE 10000 // Temperature sueil critique
    
    // ============================/ GROVE TEMPERATURE SENSOR - PIN A1 \=======================
    
    const int B = 4275;           // B value of the thermistor
    const int R0 = 100000;       // R0 = 100k
    #define pinTempSensor A1    // Grove - Temperature Sensor connect to A1
    #define MAX_VALUE 80
    #define MAX_VALUE_CRITIQUE 100
    // ===========================/ GROVE SPDT Relay(30A) - PIN D4 \========================
    
    int relay = 4; // define pin capteur Relay D4
    
    // ========================/ GROVE SERIAL BLUETOOTH - PIN SERIAL \=====================
    
    char state; // return '0' = OFF and '1' = ON by APK bluetooth
    
    // =============================/ PIEZO VIBRATOR - PIN A2 \==========================
    
    const int PIEZO_PIN = A2; // define pin capteur Vibrator A2
    
    // ===================================/ TYPE STRUCT ENVOI MSG \=======================
    
    typedef struct __attribute__ ((packed)) {
      int16_t temper; // 2 octet - Temperature pin I2C
      uint8_t humidity; // 1 octet - Humidity pin I2C
      uint8_t sensorValue; // 1 octet - Sound Sensor pin A0
      uint8_t temperature; // 1 octet - Temperature pin A1
      float vibra; // 4 octet - Vibration pin A2
    } sigfox_message_t;
    
    
    // ==================================================================================
    
    // stub for message which will be sent
    sigfox_message_t msg;
    
    
    // ====================================/ UTILITIES \=================================
    
    void reboot() {
      NVIC_SystemReset();
      while (1);
    }
    
    
    // ==================================================================================
    
    void setup() {
      Serial.begin(9600); // Start Serial with 115200
      Serial1.begin(9600); // Serial Port TX + RX
      TH02.begin(); // Start library Grove Temperature & humidity
      pinMode(relay, OUTPUT); // Initialize relay
    
      if (!SigFox.begin()) {
        Serial.println("SigFox error, rebooting");
        reboot();
      }
      
    
      // Enable debug prints and LED indication
      SigFox.debug();
    
    }
    
    
    void loop() {
    
      // ===================/ INFRARED PROXIMITY - PIN A3 WITH RELAY - PIN D4 AND BLUETOOTH - PIN SERIAL \===================
      
      bluetooth();
      
      // =====================================/ GROVE TEMPERATURE & HUMIDITY - PIN I2C \=====================
    
      temper();
    
      // ====================================/ GROVE SOUND SENSOR - PIN A0 \=====================
    
      sound();
    
      // =================================/ GROVE TEMPERATURE SENSOR - PIN A1 \==========================
    
      temper2();
    
      // ======================================/ PIEZO VIBRATOR - PIN D2 \=======================================
    
      vibrator();
    
      // =========================================================================================================
       // Send the data
      SigFox.beginPacket();
      SigFox.write((uint8_t*)&msg, 
      sizeof(sigfox_message_t));
    
      // Status 
      Serial.println("Status: ");
      Serial.println(SigFox.endPacket());
    
      delay(300000);
    
    
    }
    
    void bluetooth() {
    
      unsigned long pepe1 = millis();  // takes the time before the loop on the library begins
      int dis = SharpIR.distance();  // this returns the distance to the object you're measuring
      Serial.print("Distance : ");
      Serial.println(dis);
    
      if (Serial1.available() > 0) // if Serial1 is available, return Serial1.read in state
      {
        state = Serial1.read();
      }
    
      if ((dis < 400) && (state == '1')) // if distance of Infrared is < 30cm and state return '1'
      {
        digitalWrite(relay, HIGH); // RELAY ON
        Serial1.print("RELAY : OFF"); // Send back, to the phone, the String "RELAY : OFF"
        state = 0;
      }
    
      if (dis > 400) // if distance of Infrared is > 30cm
      {
        digitalWrite(relay, LOW); // RELAY OFF
      }
    
      if (state == '0') // if state return '0'
      {
        digitalWrite(relay, LOW); // RELAY OFF
        Serial1.print("RELAY : ON"); // Send back, to the phone, the String "RELAY : ON"
        state = 0;
      }
    }
    
    void vibrator() {
    
      // Read Piezo ADC value in, and convert it to a voltage
      
      float piezoV = analogRead(PIEZO_PIN) * 0.103125;
      msg.vibra = (float) (piezoV);
      Serial.println("Vibration :");
      Serial.println(msg.vibra); //  humidity
    
      if (msg.vibra > 1.30)
      {
        Serial.print("Sueil critique: ");
        Serial.println(msg.vibra);
        delay(15000);
      }
    
    }
    void temper2() {
      // Read and convert the module Temperature Sensor
    
      int a = analogRead(pinTempSensor);
      float R = 1023.0 / a - 1.0;
      R = R0 * R;
    
      msg.temperature = 1.0 / (log(R / R0) / B + 1 / 298.15) - 273.15; // convert to temperature via datasheet
    
      // Condition temp critical
      if (msg.temperature > MAX_VALUE && msg.temperature < MAX_VALUE_CRITIQUE)
      {
        Serial.print("Temperature sueil CRITIQUE : "); //  temp between 80° and 100°
        Serial.print(msg.temperature); //
        //delay(3600000); // 60min waiting
      }
      else
      {
        Serial.print("Temperature : "); //  temp
        Serial.print(msg.temperature);
        //delay(15000); // 15min waiting
      }
    }
    void sound() {
      // Read Sound Sensor
      msg.sensorValue = analogRead(SOUND_SENSOR);
    
      // Condition sueil
      if (msg.sensorValue > THRESHOLD_VALUE)
      {
        Serial.print("Sensor Sueil CRITIQUE :"); // seuil > 100
        Serial.println(msg.sensorValue);
        //delay(3600000); // 60min d'attente
      }
      else
      {
        Serial.print("sensorValue "); //  sueuil
        Serial.println(msg.sensorValue);
        //delay(15000); //15min waiting
      }
    }
    
    void temper() {
    
      // Read and convert the module temperature of Grove Temperature & humidity
      msg.temper = (int32_t) (TH02.ReadTemperature() * 100.0);
    
      // Condition temp mini / max / critical
      if (msg.temper > CRITIQUE_VALUE)
      {
        Serial.print("Temperature sueil CRITIQUE : "); //  higher 100°
        Serial.print(" (");
        Serial.print(msg.temper, HEX); // display what we will send in Hexadecimal
    
        Serial.print(msg.temper); // display what we will send in Decimal
        Serial.println(" x100 deg C)");
        //delay(3600000); // 60min waiting
      }
      else
      {
        Serial.print("Temperature : "); //  temp
        Serial.print(" (");
        Serial.print(msg.temper, HEX); // display what we will send in Hexadecimal
    
        Serial.print(msg.temper); // display what we will send in Decimal
        Serial.println(" x100 deg C)");
        //delay(15000); // 15min waiting
      }
    
      // Read and convert the module humidity
      float t = TH02.ReadHumidity();
      msg.humidity = (float) (t);
      Serial.print("Humidity :");
      Serial.println(msg.humidity); //  humidity
    }
    Je résume ce que je veux : 
    Faire des envoie toutes les 15 min de tout mes capteurs + envoie en cas d'un seuil dépassé (température, humidité etc.. ) avec un délai d'1h pour éviter le spam. 
    Donc je peux avoir la température qui remonte car elle dépasse un seuil, cette envoie rester bloqué 1h mais si l'humidité dépasse un seuil ça remonte la donnée et reste bloqué 1h. Que chaque remonté de données critiques ai son propre timer d'1 heure.
    Le tout en pouvant utiliser mon appli bluetooth pour éteindre/allumer mon relais, qu'elle ne soit pas bloqué par l'envoie des données à SigFox ou d'un délais ! 

    -
    Edité par MaxenceO 31 juillet 2019 à 9:24:22

    • Partager sur Facebook
    • Partager sur Twitter
      31 juillet 2019 à 13:49:21

      Bon j'ai réussi à faire ce que je voulais : 


      // ====================================/ LIBRARY UTILITIES \===================================
      #include <ArduinoLowPower.h>
      #include <SigFox.h> // library Sigfox
      #include <TH02_dev.h> // library Grove Temperature & humidity
      #include "Arduino.h"
      #include "Wire.h"
      #include <math.h> //library Grove Temperature Sensor
      #include <ZSharpIR.h> //library Grove Infrared Proximity Sensor
      
      
      // ==========================/ GROVE INFRARED PROXIMITY SENSOR - PIN A3 \===================
      
      #define ir A3 // ir: the pin where your sensor is attached
      #define model 1080 // model: an int that determines your sensor: 1080 for GP2Y0A21Y
      ZSharpIR SharpIR(ir, model);
      
      // ===============================/ GROVE SOUND SENSOR - PIN A0 \==========================
      
      #define SOUND_SENSOR A0 // define pin capteur Sound A0
      #define THRESHOLD_VALUE 200 // Sound seuil critique
      
      // =========================/ GROVE TEMPERATURE & HUMIDITY - PIN I2C \=====================
      
      #define CRITIQUE_VALUE 500 // Temperature sueil critique
      
      // ============================/ GROVE TEMPERATURE SENSOR - PIN A1 \=======================
      
      const int B = 4275;           // B value of the thermistor
      const int R0 = 100000;       // R0 = 100k
      #define pinTempSensor A1    // Grove - Temperature Sensor connect to A1
      #define MAX_VALUE 35
      
      // ===========================/ GROVE SPDT Relay(30A) - PIN D4 \========================
      
      int relay = 4; // define pin capteur Relay D4
      
      // ========================/ GROVE SERIAL BLUETOOTH - PIN SERIAL \=====================
      
      char state; // return '0' = OFF and '1' = ON by APK bluetooth
      
      // =============================/ PIEZO VIBRATOR - PIN A2 \==========================
      
      const int PIEZO_PIN = A2; // define pin capteur Vibrator A2
      
      // =============================/ DELAY DATA SIGFOX \=================================
      
      unsigned long previousMillis = 0;
      unsigned long previousMillisTempHumidity = 0;
      unsigned long previousMillisSound = 0;
      unsigned long previousMillisTemper = 0;
      unsigned long previousMillisVibrator = 0;
      
      const long interval = 900000; // 15 min
      const long interval2 = 3600000; // 1 hour
      
      // ===================================/ TYPE STRUCT ENVOI MSG \=======================
      
      typedef struct __attribute__ ((packed)) {
        int16_t temper; // 2 octet - Temperature pin I2C
        uint8_t humidity; // 1 octet - Humidity pin I2C
        uint8_t sensorValue; // 1 octet - Sound Sensor pin A0
        uint8_t temperature; // 1 octet - Temperature pin A1
        float vibra; // 4 octet - Vibration pin A2
      } sigfox_message_t;
      
      // ==================================================================================
      
      // stub for message which will be sent
      
      sigfox_message_t msg;
      
      // ====================================/ UTILITIES \=================================
      
      void reboot() {
        NVIC_SystemReset();
        while (1);
      }
      
      // ==================================================================================
      
      void setup() {
        Serial.begin(9600); // Start Serial with 115200
        Serial1.begin(9600); // Serial Port TX + RX
        TH02.begin(); // Start library Grove Temperature & humidity
        pinMode(relay, OUTPUT); // Initialize relay
      
        if (!SigFox.begin()) {
          Serial.println("SigFox error, rebooting");
          reboot();
        }
        
        // Enable debug prints and LED indication
        SigFox.debug();
      
      }
      
      void loop() {
        
        bluetooth();
        temperAndHumidity();
        sound();
        temper();
        vibrator();
        sigfox();
        
      }
      
      // ===================/ INFRARED PROXIMITY - PIN A3 WITH RELAY - PIN D4 AND BLUETOOTH - PIN SERIAL \===================
               
      void bluetooth() {
      
        unsigned long pepe1 = millis();  // takes the time before the loop on the library begins
        int dis = SharpIR.distance();  // this returns the distance to the object you're measuring
        Serial.print("Distance : ");
        Serial.println(dis);
      
        if (Serial1.available() > 0) // if Serial1 is available, return Serial1.read in state
        {
          state = Serial1.read();
        }
      
        if ((dis < 400) && (state == '1')) // if distance of Infrared is < 30cm and state return '1'
        {
          digitalWrite(relay, HIGH); // RELAY ON
          Serial1.print("RELAY : OFF"); // Send back, to the phone, the String "RELAY : OFF"
          state = 0;
        }
      
        if (dis > 400) // if distance of Infrared is > 30cm
        {
          digitalWrite(relay, LOW); // RELAY OFF
        }
      
        if (state == '0') // if state return '0'
        {
          digitalWrite(relay, LOW); // RELAY OFF
          Serial1.print("RELAY : ON"); // Send back, to the phone, the String "RELAY : ON"
          state = 0;
        }
      }
      
      // =====================================/ GROVE TEMPERATURE & HUMIDITY - PIN I2C \=========================
      
      void temperAndHumidity() {
      
        // Read and convert the module temperature of Grove Temperature & humidity
        msg.temper = (int32_t) (TH02.ReadTemperature() * 100.0);
      
        
        // Condition temp mini / max / critical
        if (msg.temper > CRITIQUE_VALUE)
        {
          unsigned long currentMillisTempHumidity = millis();
          if( currentMillisTempHumidity - previousMillisTempHumidity >= interval2)
          {
            previousMillisTempHumidity = currentMillisTempHumidity;
            
            Serial.print("Temperature sueil CRITIQUE : "); //  higher 50°
            Serial.print(" (");
            Serial.print(msg.temper, HEX); // display what we will send in Hexadecimal
        
            Serial.print(msg.temper); // display what we will send in Decimal
            Serial.println(" x100 deg C)"); 
            
            // Clears all pending interrupts
            SigFox.status();
            delay(1);
          
            // Send the data
            SigFox.beginPacket();
            SigFox.write((uint8_t*)&msg, sizeof(sigfox_message_t));
          
            // Status 
            Serial.print("Status: ");
            Serial.println(SigFox.endPacket());
          }
        }
        else
        {
          Serial.print("Temperature : "); // temp
          Serial.print(" (");
          Serial.print(msg.temper, HEX); // display what we will send in Hexadecimal
      
          Serial.print(msg.temper); // display what we will send in Decimal
          Serial.println(" x100 deg C)");
        }
      
        // Read and convert the module humidity
        float t = TH02.ReadHumidity();
        msg.humidity = (float) (t);
        Serial.print("Humidity : ");
        Serial.println(msg.humidity); // humidity 
      }
      
      // ====================================/ GROVE SOUND SENSOR - PIN A0 \====================================
      
      void sound() {
        
        // Read Sound Sensor
        msg.sensorValue = analogRead(SOUND_SENSOR);
        
        // Condition seuil
        if (msg.sensorValue > THRESHOLD_VALUE)
        {
          unsigned long currentMillisSound = millis();
          if( currentMillisSound - previousMillisSound >= interval2)
          {
            previousMillisSound = currentMillisSound;
            
            Serial.print("Sensor Seuil CRITIQUE :"); // seuil > 200
            Serial.println(msg.sensorValue);
      
            // Clears all pending interrupts
            SigFox.status();
            delay(1);
          
            // Send the data
            SigFox.beginPacket();
            SigFox.write((uint8_t*)&msg, sizeof(sigfox_message_t));
          
            // Status 
            Serial.print("Status: ");
            Serial.println(SigFox.endPacket());
          }
        }
        else
        {
          Serial.print("sensorValue : "); //  seuil
          Serial.println(msg.sensorValue);
        }
      }
      
      // =================================/ GROVE TEMPERATURE SENSOR - PIN A1 \=================================
      
      void temper() {
        
        // Read and convert the module Temperature Sensor
        int a = analogRead(pinTempSensor);
        float R = 1023.0 / a - 1.0;
        R = R0 * R;
      
        msg.temperature = 1.0 / (log(R / R0) / B + 1 / 298.15) - 273.15; // convert to temperature via datasheet
        
        // Condition temp critical
        if (msg.temperature > MAX_VALUE)
        {
          unsigned long currentMillisTemper = millis();
          if( currentMillisTemper - previousMillisTemper >= interval2)
          {
            previousMillisTemper = currentMillisTemper;
            
            Serial.print("Temperature sueil CRITIQUE : "); //  temp > 35° 
            Serial.print(msg.temperature); 
      
            // Clears all pending interrupts
            SigFox.status();
            delay(1);
          
            // Send the data
            SigFox.beginPacket();
            SigFox.write((uint8_t*)&msg, sizeof(sigfox_message_t));
          
            // Status 
            Serial.print("Status: ");
            Serial.println(SigFox.endPacket());
          }
        }
        else
        {
          Serial.print("Temperature 2 : ");
          Serial.println(msg.temperature);
        }
      }
      
      // ======================================/ PIEZO VIBRATOR - PIN D2 \=======================================
      
      void vibrator() {
      
        // Read Piezo ADC value in, and convert it to a voltage
        /*
          int piezoADC = analogRead(PIEZO_PIN);
          float piezoV = piezoADC * 0.103125; // (N*T/2^x)
          Serial.println(piezoV);
        */
        float piezoV = analogRead(PIEZO_PIN) * 0.103125;
        msg.vibra = (float) (piezoV);
        Serial.print("Vibration :");
        Serial.println(msg.vibra); 
        
        if (msg.vibra > 1.30)
        {
          unsigned long currentMillisVibrator = millis();
          if( currentMillisVibrator - previousMillisVibrator >= interval2)
          {
            previousMillisVibrator = currentMillisVibrator;
        
            Serial.print("Sueil critique: ");
            Serial.println(msg.vibra);
      
            // Clears all pending interrupts
            SigFox.status();
            delay(1);
          
            // Send the data
            SigFox.beginPacket();
            SigFox.write((uint8_t*)&msg, sizeof(sigfox_message_t));
          
            // Status 
            Serial.print("Status: ");
            Serial.println(SigFox.endPacket());
          }
        }
      }
      
      // ========================================/SigFox DATA \==================================================
      
      void sigfox(){
      
        unsigned long currentMillis = millis();
      
        if( currentMillis - previousMillis >= interval)
        {
          previousMillis = currentMillis;
          
          // Clears all pending interrupts
          SigFox.status();
          delay(1);
        
          // Send the data
          SigFox.beginPacket();
          SigFox.write((uint8_t*)&msg, sizeof(sigfox_message_t));
        
          // Status 
          Serial.print("Status: ");
          Serial.println(SigFox.endPacket());
        }
      }
      Si quelqu'un sait comment je peux améliorer ma fonction bluetooth, je suis preneur ! Actuellement quand j'ai la bonne distance + appuyer sur ON c'est allumer, si je m'éloignes ça s'éteins, mais si je me rapproche à la bonne distance ça reste éteins tant que j'ai pas appuyer sur ON de nouveau. J'aimerais qu'il garde en mémoire la dernière donnée "ON" donc "1" de ce fait si j'ai pas appuyer sur OFF quand je me rapproche à la bonne distance mon relais ce rallume. Cela m'évite d'appuyer à nouveau sur ON et de gagner du temps :)
      • Partager sur Facebook
      • Partager sur Twitter

      Envoie de données SigFox + Bluetooth

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown