Send_Frequency_Full Wait



  • hello
    yes the title of the post is not clear (I did not find another one) 🙂
    it means, how to regulate the sending of messages on the Controller/Gateway.

    History:
    In test for the reading of the Electric consumption, I found many sketches and some with MySensors.
    Here is the one of restraint: texte du lien

    The Problem and a burst of dialogue from the Messages, too much dialogue.

    On the sketch there is a line with : SEND_FREQUENCY_FULL
    but it doesn't work, nothing in the *.cpp and *.h files either
    I think that when a Library passes from hands to hands, some elements are deleted in part.

    Question :
    how to delay the sending of the values ?

    which choice ?
    . a [ SendTime = currentTime - previousMillis ] well placed in the program ? (it didn't work )
    . planting [Wait(time)] (nothing grows)
    . use Echo received and delay the send ? Echo receive
    . contact Henry Walton Jones Jr. to learn more about the origin of the library?
    (I don't have his number, and he's getting too old)

    The Genesis of the librayry ? : LibTeleinfo Gitub

    Thank you for your help



  • my sketch adapt and

    /*
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2018 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
    */
    //--------------------------------------------------------------------
    //   __|              _/           _ )  |                       
    //   _| |  |   ` \    -_)   -_)    _ \  |   -_)  |  |   -_)     
    //  _| \_,_| _|_|_| \___| \___|   ___/ _| \___| \_,_| \___|  
    //--------------------------------------------------------------------    
    // 2019/10/17 - FB V1.0.1
    //..........
      // errase for test
      //.....
    // 2022/09/25 - FB V2.0.1 - Correction sur détection TIC standard
    
    //-------------------------------------------------------------------------
    //******  Adaptation pour Mega2560 et apiRS485 de MyS  *********  nov.2022
    //-------------------------------------------------------------------------
    
    // Enable debug prints MySensors, Enable debug prints to serial monitor
      #define MY_DEBUG
    //  #define MY_DEBUG_LOCAL
    
    //----------------- MySensors --------------------------
    
      #define MY_NODE_ID 30                   /*Node en ID static*/
      #define MY_TRANSPORT_WAIT_READY_MS 3000  //Timeout (ms) pour mis en Com.(0 pour aucun)
    /* nécessaire lorsque pas connecté à la passerelle, pour des test*/
    
    //.......... RS485 ..........................
    
      #define MY_RS485  // Enable RS485 transport layer
      #define MY_RS485_DE_PIN 3  // Define this to enables Cmd DE-pin management on defined pin
      #define MY_RS485_BAUD_RATE 9600 // Set RS485 baud rate to use (9600 mode standard) 
    //#define MY_RS485_SOH_COUNT 1
      #define MY_RS485_HWSERIAL Serial3 // choix du port RX-Tx sur Mega2560
    //#define MY_SPLASH_SCREEN_DISABLED
    
    // ------------teleinfo -------------
    
      #define VERSION   "v2.0.1"
      #define DELAY_PREFIX  50
      #define DELAY_SEND    500
      #define DELAY_PRESENTATION  100
    
      #define MY_BAUD_RATE 9600    // mode standard ----------------------- pour le terminal de debug??
    
      #define MY_DEFAULT_ERR_LED_PIN 5  // Error led pin
      #define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin, on board LED
      #define MY_DEFAULT_TX_LED_PIN  6  // 
    
    //------------- Biblio ------------- 
    		#include <MySensors.h>
    		#include "LibTeleinfoLite.h"
    
    //------------------ ajout tempo dialogue --------------
     //unsigned long previousMillis1 = 0; 
     //Variable de compteur, compte les millisecondes depuis le dernier appel de fonction
     //const long interval1 = 120000; 
     //120000 millisecondes aka 120 secondes, l'intervalle de fréquence d'utilisation du capteur
    
    /*
    #ifdef __arm__
    // should use uinstd.h to define sbrk but Due causes a conflict
    extern "C" char* sbrk(int incr);
    #else  // __ARM__
    extern char *__brkval;
    #endif  // __arm__
    */
    
      const unsigned long SEND_FREQUENCY_FULL = 180000; // 3mn, Temps minimum entre les envois (in milliseconds).
      const unsigned long SEND_FREQUENCY_TIC  =  15000; // 15s,  Minimum time between send (in milliseconds). 
      
      unsigned long lastTime_full = 0;
      unsigned long lastTime_tic = 0;
      unsigned long startTime; // Date démarrage 
      _Mode_e mode_tic;
      boolean flag_first = true;
      boolean flag_full_tic = true;
      boolean flag_tic = true;
    
      TInfo tinfo;
    
      #define char_AMPERE "A"
      #define char_VOLT "V"
      #define char_WATT "W"
      #define char_WATT_HEURE "Wh"
      #define char_VA "VA"
      #define char_KVA "kVA"
      #define char_VAR_HEURE "VArh"
      #define char_MIN "min"
    
    //-------------  Mode Historique  ------------------------
    const char char_ADCO[] PROGMEM = "ADCO";
    const char char_OPTARIF[] PROGMEM = "OPTARIF";
    const char char_ISOUSC[] PROGMEM = "ISOUSC";
    const char char_BASE[] PROGMEM = "BASE";
    const char char_HCHC[] PROGMEM = "HCHC";
    const char char_HCHP[] PROGMEM = "HCHP";
      //..........
      // errase for test
      //.....
    const char char_PEJP[] PROGMEM = "PEJP";
    const char char_PTEC[] PROGMEM = "PTEC";
    const char char_DEMAIN[] PROGMEM = "DEMAIN";
    const char char_IINST[] PROGMEM = "IINST";
    const char char_ADPS[] PROGMEM = "ADPS";
    const char char_IMAX[] PROGMEM = "IMAX";
    const char char_PAPP[] PROGMEM = "PAPP";
    const char char_HHPHC[] PROGMEM = "HHPHC";
    const char char_MOTDETAT[] PROGMEM = "MOTDETAT";
      //..........
      // errase for test
      //.....
    
    //--------------- Define----------------------------------
    #define CHILD_ID_ADCO 1         // N°d'identification du compteur (12 caractères)
    #define CHILD_ID_OPTARIF 2      // Option tarifaire, type d'abonnement (4 car.)
    #define CHILD_ID_ISOUSC 3       // Intensité souscrite (2 car. unité = ampères)
    #define CHILD_ID_BASE 4
    #define CHILD_ID_HCHC 5       //Index heures creuses si option (9car.unité=Wh)
    #define CHILD_ID_HCHP 6       //Index heures pleines si option heures creuses (9car.unité=Wh)
      //..........
      // errase for test
      //.....
    #define CHILD_ID_PTEC 16      //PĂ©riode tarifaire en cours (4car.)
    #define CHILD_ID_DEMAIN 17
    #define CHILD_ID_IINST 18     //Intensité instantanée (3car. unité=ampères)
    #define CHILD_ID_ADPS 19
    #define CHILD_ID_IMAX 20      //Intensité maximale (3car. unité=ampères)
    #define CHILD_ID_PAPP 21      //Puissance apparente (5car. unité=Volt.ampères)
    #define CHILD_ID_HHPHC 22     //Groupe horaire si option creuses ou tempo (1car.)
    #define CHILD_ID_MOTDETAT 23  //Mot d'Ă©tat (autocontrĂ´le) (6car.)
      //..........
      // errase for test
      //.....
    
    //--------------  Mode Standard  ---------------------
    // errase for test
    //----------------------------------------------------
    	const char char_START[] PROGMEM = "START";
    	#define CHILD_ID_START    99            //etat du node
    
    	MyMessage msgTEXT( 0, V_TEXT);        // S_INFO
    	MyMessage msgCURRENT( 0, V_CURRENT ); // S_MULTIMETER
    	MyMessage msgVOLTAGE( 0, V_VOLTAGE ); // S_MULTIMETER
    	MyMessage msgWATT( 0, V_WATT );       // S_POWER
    	MyMessage msgKWH( 0, V_KWH );         // S_POWER
    	MyMessage msgVA( 0, V_VA );           // S_POWER
    	MyMessage msgPrefix( 0, V_UNIT_PREFIX );
    
    // ---------------------- freeMemory  ----------------------------------- 
    // errase for test
    // ----------------- freeMemory ---------------------------------- 
    // errase for test
    
    // ----------- change_etat_led_teleinfo -------------------------- 
      void clignote_led(uint8_t led, uint8_t nbr, int16_t delais) {
      int led_state;
      for (int i=0; i<nbr*2; i++) {
        led_state = !led_state;
        digitalWrite(led, led_state);
       // delay(delais);
        wait (delais);
      }
      digitalWrite(led, HIGH);
      }
    
    // ---------------- send_teleinfo_historique  ------------------------- 
      void send_teleinfo_historique(char *name, char *value) {
      if (strcmp_P(name, char_ADCO) == 0) {send(msgTEXT.setSensor(CHILD_ID_ADCO).set(value));return;}
      if (strcmp_P(name, char_OPTARIF) == 0) {send(msgTEXT.setSensor(CHILD_ID_OPTARIF).set(value));return;}
      if (strcmp_P(name, char_ISOUSC) == 0) {send(msgCURRENT.setSensor(CHILD_ID_ISOUSC).set(atol(value)));return;}
      if (strcmp_P(name, char_BASE) == 0) {send(msgKWH.setSensor(CHILD_ID_BASE).set(atol(value)));return;}
      if (strcmp_P(name, char_HCHC) == 0) {send(msgKWH.setSensor(CHILD_ID_HCHC).set(atol(value)));return;}
      if (strcmp_P(name, char_HCHP) == 0) {send(msgKWH.setSensor(CHILD_ID_HCHP).set(atol(value)));return;}
      //..........
      // errase for test
      //.....
      if (strcmp_P(name, char_PTEC) == 0) {send(msgTEXT.setSensor(CHILD_ID_PTEC).set(value));return;}
      if (strcmp_P(name, char_DEMAIN) == 0) {send(msgTEXT.setSensor(CHILD_ID_DEMAIN).set(value));return;}
      if (strcmp_P(name, char_IINST) == 0) {send(msgCURRENT.setSensor(CHILD_ID_IINST).set(atol(value)));return;}
      if (strcmp_P(name, char_ADPS) == 0) {send(msgCURRENT.setSensor(CHILD_ID_ADPS).set(atol(value)));return;}
      if (strcmp_P(name, char_IMAX) == 0) {send(msgCURRENT.setSensor(CHILD_ID_IMAX).set(atol(value)));return;}
      if (strcmp_P(name, char_PAPP) == 0) {send(msgVA.setSensor(CHILD_ID_PAPP).set(atol(value)));return;}
      if (strcmp_P(name, char_HHPHC) == 0) {send(msgTEXT.setSensor(CHILD_ID_HHPHC).set(value));return;}
      if (strcmp_P(name, char_MOTDETAT) == 0) {send(msgTEXT.setSensor(CHILD_ID_MOTDETAT).set(value));return;}
      //..........
      // errase for test
      //.....
      }
      
    // -----------------  send_teleinfo_standard  ------------------------------ 
      void send_teleinfo_standard(char *name, char *value) {
        // errase for test
      }
    
    // -------------- send_teleinfo  ---------------------------- 
      void send_teleinfo(char *etiq, char *val)  {
      if (mode_tic == TINFO_MODE_HISTORIQUE) {
          send_teleinfo_historique(etiq, val);
           }
       else 
          {
          send_teleinfo_standard(etiq, val);
          }
        }
         
    // --------------  init_speed_TIC ---------------------------- 
      _Mode_e init_speed_TIC()  {
      boolean flag_timeout = false;
      boolean flag_found_speed = false;
      uint32_t currentTime = millis();
      unsigned step = 0;
      _Mode_e mode;
    
      digitalWrite(MY_DEFAULT_ERR_LED_PIN, LOW);
      digitalWrite(MY_DEFAULT_RX_LED_PIN, LOW);
      
      // Test en mode historique 
      // Recherche des éléments de début, milieu et fin de trame (0x0A, 0x20, 0x20, 0x0D)
      Serial1.begin(1200); // mode historique
      while (!flag_timeout && !flag_found_speed) {
        if (Serial1.available()>0) {
          char in = (char)Serial1.read() & 127;  // seulement sur 7 bits
          // début trame
            if (in == 0x0A) {
            step = 1;
          }
          // premier milieu de trame
            if (step == 1 && in == 0x20) {
            step = 2;
          }
          // deuxième milieu de trame
            if (step == 2 && in == 0x20) {
            step = 3;
          }
          // fin trame
            if (step == 3 && in == 0x0D) {
            flag_found_speed = true;
            step = 0;
          }
        }
        if (currentTime + 10000 <  millis()) flag_timeout = true; // 10s de timeout
      }
    
      if (flag_timeout) { // trame avec vitesse histo non trouvée donc passage en mode standard
         mode = TINFO_MODE_STANDARD;
         Serial1.end();
         Serial1.begin(9600); // mode standard
         Serial1.println(F("TIC mode standard"));
         clignote_led(MY_DEFAULT_RX_LED_PIN, 3, 500);
      }
      else {
        mode = TINFO_MODE_HISTORIQUE;
        Serial1.println(F("TIC mode historique"));
        clignote_led(MY_DEFAULT_RX_LED_PIN, 5, 500);
      }
      digitalWrite(MY_DEFAULT_ERR_LED_PIN, HIGH);
      return mode;
      }
    
    /*======================================================================
    Function: NewFrame 
    Purpose :  rappel lorsque nous avons reçu une trame de téléinfo complète.
    Input   : pointeur de liste liée sur les données concernées
    Output  : - 
    Comments: elle n'est appelée que si une donnée de la trame est différente de la trame précédente.
              la trame précédente
    ====================================================================== */
    
      void UpdatedFrame(ValueList *vl_tic)  {
      //affiche_freeMemory();
      //  send_teleinfo (vl_tic, flag_full_tic);
        
      if (flag_full_tic) {
        send(msgTEXT.setSensor(CHILD_ID_START).set(startTime));
        flag_full_tic = false;
        }
      }
    //-------- This is called when a new time value was received ---------
    	void receiveTime(unsigned long controllerTime)  {
      // incoming time 
      Serial1.print(F(">> Time from gw: "));
      Serial1.println(controllerTime);
      startTime = controllerTime;
    	}
    //--------------------------------------------------------------------
    	void before() {
      pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT);
      pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT);
    
      mode_tic = init_speed_TIC();
      tinfo.init(mode_tic);
      }
    //-------------------------- SETUP ------------------------------------------
    	void setup()  {
        //affiche_freeMemory();
    	}
    //-------------------------- Presenstation MyS ------------------------------------------
    	void presentation() {
      sendSketchInfo("Teleinfo", VERSION);  // Send the sketch version info sur gateway and Controller
      requestTime();	 // RĂ©cupere l'heure de la gateway du controller
    
      if (mode_tic == TINFO_MODE_HISTORIQUE) {
        present (CHILD_ID_ADCO, S_INFO, F("ADCO"));
        present (CHILD_ID_OPTARIF, S_INFO, F("OPTARIF"));
        present (CHILD_ID_ISOUSC, S_MULTIMETER, F("ISOUSC"));
        present (CHILD_ID_BASE, S_POWER, F("BASE"));
        present (CHILD_ID_HCHC, S_POWER, F("HCHC"));
        wait(DELAY_PRESENTATION);
        present (CHILD_ID_HCHP, S_POWER, F("HCHP"));
        // errase for test
        present (CHILD_ID_PTEC, S_INFO, F("PTEC"));
        present (CHILD_ID_DEMAIN, S_INFO, F("DEMAIN"));
        present (CHILD_ID_IINST, S_MULTIMETER, F("IINST"));
        present (CHILD_ID_ADPS, S_MULTIMETER, F("ADPS"));
        present (CHILD_ID_IMAX, S_MULTIMETER, F("IMAX"));
        wait(DELAY_PRESENTATION);
        present (CHILD_ID_PAPP, S_POWER, F("PAPP"));
        present (CHILD_ID_HHPHC, S_INFO, F("HHPHC"));
        present (CHILD_ID_MOTDETAT, S_INFO, F("MOTDETAT"));
        // errase for test
      }
      else {
        // errase for test
      }
      present( CHILD_ID_START, S_INFO, F("START"));
      //affiche_freeMemory();
      }
    
    //----------------------- LOOP ---------------------------------------------
    	void loop()  {
    
      //uint32_t currentTime = millis();
    // Only send values at a maximum frequency or woken up from sleep
     // bool sendTime = currentTime - previousMillis1 > SEND_FREQUENCY_FULL;
      
      if (flag_first) {
    	// Send prefix au démarrage
    
      // errase for test
      /*
      * 
       */
        send(msgTEXT.setSensor(CHILD_ID_START).set(startTime));
              flag_first= false;
        }
      if (Serial1.available()) tinfo.process(Serial1.read());
      	}
    // -------------------------------- Fin du Pgm --------------------------------------------	
    
    

Log in to reply
 

Suggested Topics

  • 8
  • 9
  • 8
  • 21
  • 3
  • 2
  • 3
  • 24

116
Online

10.8k
Users

10.9k
Topics

111.8k
Posts