Sensor flooding gateway



  • Hi,

    I just can't see what's wrong with this sensor. It should only be sending from S0 once every minute but it floods the gateway with messages as fast as it can.

    I'm using 1.6.5 for this sensor.

    Here's my code:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to create a node thay repeates messages
     * from nodes far from gateway back to gateway. 
     * It is important that nodes that has enabled repeater mode calls  
     * gw.process() frequently. Repeaters should never sleep. 
     */
     
    #include <FastSPI_LED.h>
    #include <DigitalIO.h>
    #include <MySensor.h>
    #include <SPI.h>
    #include <MyTransportNRF24.h>
    #include <EEPROM.h>
    
    // Radio Variables
    #define RADIO_CE_PIN        5  // radio chip enable
    #define RADIO_SPI_SS_PIN    6  // radio SPI serial select
    
    // LED Variables
    #define CHILD_ID 1
    #define NUM_LEDS 150
    
    
    // Motion sensor Variables
    #define DIGITAL_INPUT_SENSOR 2   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID_MOTION 2   // Id of the sensor child
    
    // Light sensor Variables
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 3
    unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
    
    // Sometimes chipsets wire in a backwards sort of way
    struct CRGB { unsigned char b; unsigned char r; unsigned char g; };
    // struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
    struct CRGB *leds;
    
    MyTransportNRF24 transport(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RF24_PA_LEVEL_GW);  
    
    MyHwATMega328 hw;
    
    MySensor gw(transport, hw /*, signer*/);
    MyMessage msg(CHILD_ID,V_DIMMER);
    MyMessage motionMsg(CHILD_ID_MOTION, V_TRIPPED); // Motion sensor message
    MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); // Light sensor message
    int lastLightLevel;
    int lastMillis;
    boolean lastTripped;
    long RGB_v[3] = {0,0,0};
    
    void setup()  
    {  
        // Setup FastSPI
        FastSPI_LED.setLeds(NUM_LEDS);
        FastSPI_LED.setChipset(CFastSPI_LED::SPI_LPD6803);
        FastSPI_LED.init();
        FastSPI_LED.start();
        leds = (struct CRGB*)FastSPI_LED.getRGBData(); 
        
      // The third argument enables repeater mode.
      gw.begin(incomingMessage, 8, false);
      //Send the sensor node sketch version information to the gateway
      gw.sendSketchInfo("RGB", "1");
    
      // Motion sensor
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_DIMMER); 
      gw.present(CHILD_ID_MOTION, S_MOTION);
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
      
    
      prog3(120);
    }
    
    void loop() 
    {
      // Motion sensor stuff
        // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
    
      if (tripped != lastTripped) {
    //    Serial.println(tripped);
        gw.send(motionMsg.set(tripped?"1":"0"));  // Send tripped value to gw 
        lastTripped = tripped;
      }
    
      // Light sensor stuff
      int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
    
      if((lastMillis+SLEEP_TIME) < millis()) {
    //    Serial.println(lightLevel);
          gw.send(lightMsg.set(lightLevel));
          //lastLightLevel = lightLevel;
          lastMillis = millis();
      }    
      // By calling process() you route messages in the background
      gw.process();
      //gw.wait(SLEEP_TIME);
    }
    
    void incomingMessage(const MyMessage &message) {
      if (message.type == V_DIMMER) {
        //Serial.println( "V_DIMMER command received..." );
        //Serial.println( message.data );
        int prog= atoi( message.data );
        switch(prog) { 
          case 1: prog1(); break;
          //case 2: prog2(); break;
          case 3: prog3(120); break;
          case 4: prog4(); break;
        }
      }
      else if (message.type == V_RGB) {
    //    Serial.println( "V_RGB cmd received... " );
        //Serial.println( message.data );
        int addr_r = 10;
        int addr_g = 11;
        int addr_b = 12;
        String hexstring = message.getString();
    
        // Get rid of '#' and convert it to integer
        long number = (long) strtol( &hexstring[0], NULL, 16);
        
        // Split them up into r, g, b values
        RGB_v[0] = number >> 16;
        RGB_v[1] = number >> 8 & 0xFF;
        RGB_v[2] = number & 0xFF;
        EEPROM.update(addr_r, RGB_v[0]);
        EEPROM.update(addr_g, RGB_v[1]);
        EEPROM.update(addr_b, RGB_v[2]);
        prog3(120);
      }
      else {
        //Serial.println( "OTHER cmd received..." );
      }
    }
        
    // Program 1 - Turn on the light
    void prog1() {
      // one at a time
        memset(leds, 0, NUM_LEDS * 3);
        for(int i = 0 ; i < NUM_LEDS; i++ ) {
            leds[i].r = 255; 
            leds[i].g = 255; 
            leds[i].b = 255; 
          FastSPI_LED.show();
          delay(30);
        }
    }
    // Program 3 - Static decoration light
    void prog3(int p3level) {
    //    Serial.println( "Starting prog3" );
      // Fade in/fade out
        int r_value = 255;
        int g_value = 255;
        int b_value = 255;
        int address_r = 10;
        int address_g = 11;
        int address_b = 12;
        r_value = EEPROM.read(address_r);
        g_value = EEPROM.read(address_g);
        b_value = EEPROM.read(address_b);
    //    Serial.println( "Setting color to: ");
        memset(leds, 0, NUM_LEDS * 3);
        for(int k = 0; k < 256; k++) { 
          for(int i = 0; i < ((NUM_LEDS/3)*2); i++ ) {
              if(k < b_value) {
                leds[i].b = k;
              }
              if(k < g_value) {
                leds[i].g = k;
              }
              if(k < r_value) {
                leds[i].r = k;
              }
          }
          FastSPI_LED.show();
          delay(3);
        }
        delay(300); 
        for(int k = 0; k <= p3level; k++) {
          for(int i = 0; i < (NUM_LEDS/3); i++ ) {
              if(b_value > 0) {
                leds[i].b = b_value;
              }
              if(g_value > 0) {
                leds[i].g = g_value;
              }
              //if(k > (r_value+30)) {
              if(r_value > 0) {
                leds[i].r = r_value;
              }
              //}
          }
          if(k=p3level) {
            for(int i = (NUM_LEDS/3)*2; i < (NUM_LEDS); i++ ) {
              leds[i].b = 0;
              leds[i].g = 0;
              leds[i].r = 0;
            }
              //}
          }
          b_value--;
          g_value--;
          r_value--;
          FastSPI_LED.show();
          delay(3);
        }
    }
    // Program 4 - Turn off the light
    void prog4() {
      // one at a time
        memset(leds, 0, NUM_LEDS * 3);
        for(int i = 0 ; i < NUM_LEDS; i++ ) {
            leds[i].r = 0; 
            leds[i].g = 0; 
            leds[i].b = 0; 
          FastSPI_LED.show();
          delay(3);
        }
    }
    

    This is what it's sending:

    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    send: 8-8-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:100
    

    😞


  • Mod

    Uncomment
    Serial.println(tripped);
    and you'll probably see what is wrong. The print statements are there to help you debug 🙂


  • Contest Winner

    @Aloha with this statement on line 120: lastMillis = millis();

    You try to put an "unsigned long" (16 bits) in to an integer (8bits)
    this will change also the next variable in your case "bool lastTripped"

    Try to change line 71:

    int lastMillis;
    

    into

    unsigned long lastMillis;
    


  • @mfalkvidd said:

    Uncomment
    Serial.println(tripped);
    and you'll probably see what is wrong. The print statements are there to help you debug 🙂

    Hehe, yes you are right. The problem is the size of my sketch. It's really on the limit, if I just uncomment 1 print statement the sketch is too big. 😓 So I have to comment out or remove something else in order to uncomment something. ☺

    @BartE said:

    @Aloha with this statement on line 120: lastMillis = millis();

    You try to put an "unsigned long" (16 bits) in to an integer (8bits)
    this will change also the next variable in your case "bool lastTripped"

    Try to change line 71:

    int lastMillis;
    

    into

    unsigned long lastMillis;
    

    Wow, I'm impressed you saw that!
    It's actually working now. 👌

    With one less thing to worry about I might get some good sleep tonight. Thanks! 😄


Log in to reply
 

Suggested Topics

22
Online

11.2k
Users

11.1k
Topics

112.5k
Posts