Can't even get a base Mysensors sketch to work



  • Hi all

    I want to create a sensor which is a motion sensor and a controller of two dimmable LED strips. When I upload the sketch that I hope will work, it looks like the motion part of it doesn't stop sending info, so I decided to start small to try and work out the issue.

    When I upload the Dimming LED sketch on its own, it all seems to work fine according to the serial monitor. Then I wipe the arduino and upload the motion part only, and the serial monitor goes crazy! The Mysensors sign or whatever it's called shows in the serial monitor, then it scrolls on and on, printing either a '1' or '0' on each line and it doesn't stop, and it's FAST!

    I am using an Arduino Nano. The motion sketch is the base example provided on Mysensors, but here it is anyways:

    
    // Enable debug prints
    // #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    
    uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Motion Sensor", "1.0");
    
        // Register all sensors to gw (they will be created as child devices)
        present(CHILD_ID, S_MOTION);
    }
    
    void loop()
    {
        // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
        Serial.println(tripped);
        send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
    
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    

    I have even tried this sketch as it is on another Arduino Nano and I still get the same result. I'm keen to hear your thoughts.

    Thanks



  • OK.... I have solved the biggest issue by rebooting my computer. Silly me! The base motion sketch works as it should, kind of.... it does seem to be sending messages quite quickly. Anyways I have uploaded my motion and 2 led dimmer sketch and am in the process of joining it to my Vera Controller, but it still does seem to be sending messages very often. Can someone review my sketch and offer feedback? Please note that I did take the very easy route and adapted a RGB controller sketch for the led dimming part; I plan to just be using 2 of the lights in the sketch to control my 2 led strips. When I have more time I plan to learn how to do it properly.

    Here is the sketch as it stands:

    
    #define SN   "Motion+2 LED Dimmer"
    #define SV   "v1"
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    //#define MY_REPEATER_FEATURE
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>  
    
    uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 7   // Id of the sensor child
    
    // Arduino pin attached to MOSFET Gate pin
    #define RED_PIN 7   
    #define GREEN_PIN 5
    #define BLUE_PIN 6
    
    // Define message name and type to send sensor info
    MyMessage RedStatus(RED_PIN, V_DIMMER);   
    MyMessage GreenStatus(GREEN_PIN, V_DIMMER);
    MyMessage BlueStatus(BLUE_PIN, V_DIMMER);
    MyMessage Status(1, V_DIMMER);
    MyMessage rgbShowState(0, V_LIGHT);
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
        
    // Serial.print translate sensor id to sensor name
    char color[][6] = {"","","","RED","","GREEN","BLUE"}; 
       
    // Vars for rgbShow function
    int redval = 0;
    int greenval = 0;
    int blueval = 0;
    long time=0;
    int isShow;
         
    void setup() 
    {
      // Define pin mode (pin number, type)
      pinMode(RED_PIN, OUTPUT);   
      pinMode(GREEN_PIN, OUTPUT);
      pinMode(BLUE_PIN, OUTPUT);
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    
      // Correct saved RGB value for first start
      saveState(RED_PIN, constrain((int8_t)loadState(RED_PIN), 0, 100)); 
      saveState(GREEN_PIN, constrain((int8_t)loadState(GREEN_PIN), 0, 100)); 
      saveState(BLUE_PIN, constrain((int8_t)loadState(BLUE_PIN), 0, 100)); 
                 
      // Get value from eeprom and write to output
      analogWrite(RED_PIN, 255 * loadState(RED_PIN) / 100);     
      analogWrite(GREEN_PIN, 255 * loadState(GREEN_PIN) / 100);
      analogWrite(BLUE_PIN, 255 * loadState(BLUE_PIN) / 100);
             
      // Write some debug info
      Serial.print("Load from eeprom RED: "); 
      Serial.print(loadState(RED_PIN)); 
      Serial.println("%"); 
      Serial.print("Load from eeprom GREEN: "); 
      Serial.print(loadState(GREEN_PIN)); 
      Serial.println("%"); 
      Serial.print("Load from eeprom BLUE: "); 
      Serial.print(loadState(BLUE_PIN)); 
      Serial.println("%");  
      
      // Send RGB value to controler (request ack back: true/false)
      Serial.println("Send eeprom value to controler"); 
      send( RedStatus.set(loadState(RED_PIN)), false );    
      send( GreenStatus.set(loadState(GREEN_PIN)), false );
      send( BlueStatus.set(loadState(BLUE_PIN)), false );
      
      // Correct RGB show state for first start and load it (set to 'On' at first start)
      saveState(0, constrain((int8_t)loadState(0), 0, 1));
      isShow=loadState(0);
           
      // Send RGB show state to controler (request ack back: true/false)
      send( rgbShowState.set(isShow), false);
      
      if (isShow==1){Serial.println("RGB show running..."); }
      Serial.println("Ready to receive messages...");  
    }
    
    void presentation()  {
      // Present sketch (name, version)
      sendSketchInfo(SN, SV);        
           
      // Register sensors (id, type, description, ack back)
      present(RED_PIN, S_DIMMER, "RED LED", false);
      present(GREEN_PIN, S_DIMMER, "GREEN LED", false);
      present(BLUE_PIN, S_DIMMER, "BLUE LED", false);
      present(0, S_LIGHT, "Show button LEDs", false);
      // Register Motion sensor to gw (they will be created as child devices)
      present(CHILD_ID, S_MOTION);
    }
    
    void loop()
    {
        // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
        Serial.println(tripped);
        send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
    
        
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    
    void receive(const MyMessage &message)
    {
      if (message.isAck())
      {
        Serial.println("Got ack from gateway");
      }
      if (message.type == V_LIGHT)
      {
        // Incoming on/off command sent from controller ("1" or "0")
        int lightState = message.getString()[0] == '1';
      
        // if receive RGB Show On commands, start the show
        if (message.sensor==0 && lightState==1){ rgbShowOn(); }
            // if receive RGB Show Off commands, stop the show
        else if (message.sensor==0 && lightState==0){ rgbShowOff(); }
           
        // if receive RGB switch On command
        else if (lightState==1)
        {
          // Write some debug info
                Serial.print("Incoming change for ");
                Serial.print(color[message.sensor]);
                Serial.println(": On");
                Serial.print("Load from eeprom: ");
              
          if ( loadState(message.sensor) == 0)
          {
            // Pick up last saved dimmer level from the eeprom
                    analogWrite(message.sensor, 255 * loadState(10*message.sensor) / 100);
                    // Save loaded value to current
                    saveState(message.sensor, loadState(10*message.sensor));
                    Serial.print(loadState(10*message.sensor)); 
                    Serial.println("%");
                    // Send value to controler
                    Serial.println("Send value to controler");
                    send(Status.setSensor(message.sensor).set(loadState(10*message.sensor)),false);
                }
                else
                {
                    // Pick up last saved dimmer level from the eeprom
                    analogWrite(message.sensor, 255 * loadState(message.sensor) / 100);
                    Serial.print(loadState(message.sensor));
                    Serial.println("%"); 
                    // Send value to controler
                    Serial.println("Send value to controler");
                    send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
                } 
                // Stop the show if it's running
                if (isShow==1){ rgbShowStop(message.sensor); }
            }
        // if recieve switch Off command
        else if (lightState==0)
        {
          // Write output to 0 (Off)
                analogWrite(message.sensor, 0);
                // Save old value to eeprom if it'was not zero
                if ( loadState(message.sensor) != 0 )
                {
                    saveState(10*message.sensor, constrain((int8_t)loadState(message.sensor), 0, 100)); 
                }
                // Save new value to eeprom
                saveState(message.sensor, 0); 
                // Write some debug info
          Serial.print("Incoming change for ");
          Serial.print(color[message.sensor]);
          Serial.print(": ");
          Serial.println("Off");  
                Serial.print("Store old value: ");
                Serial.print(loadState(10*message.sensor));  
                Serial.println("%");
                // Send value to controler
                Serial.println("Send value to controler");
                send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
          // Stop the show if it's running
          if (isShow==1){ rgbShowStop(message.sensor); }
        }
      }
      else if (message.type == V_DIMMER)
      {    
          uint8_t incomingDimmerStatus = message.getByte();
          // limits range of sensor values to between 0 and 100 
          incomingDimmerStatus = constrain((int8_t)incomingDimmerStatus, 0, 100);
          // Change Dimmer level
          analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
          //Save value to eeprom
          saveState(message.sensor, incomingDimmerStatus); 
          // Write some debug info
          Serial.print("Incoming change for ");
          Serial.print(color[message.sensor]);
          Serial.print(": ");
          Serial.print(incomingDimmerStatus);
          Serial.println("%");
            // Send value to controler
            Serial.println("Send value to controler");
            send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
          // Stop the show if it's running
          if (isShow==1){ rgbShowStop(message.sensor); }
        }
    }
       
    void rgbShow()
    {
      time = millis();
      redval = 128+250*cos(2*PI/300000*time);
      greenval = 128+250*cos(2*PI/300000*time-222);
      blueval = 128+250*cos(2*PI/300000*time-111);
      // limits range of sensor values to between 0 and 255 
      redval = constrain(redval, 0, 255);
      greenval = constrain(greenval, 0, 255);
      blueval = constrain(blueval, 0, 255);
    }
    
    void rgbShowOn()
    {
      // define show On
      isShow=1;
      // Save state
      saveState(0, 1); 
      // Write some debug info
      Serial.println("Show must go on");
    }
       
    void rgbShowOff()
    {
      // define show Off
      isShow=0;
      // Save state
      saveState(0, 0);
      // Save RGB value to eeprom
      saveState(RED_PIN, 100 * redval / 255); 
      saveState(GREEN_PIN, 100 * greenval / 255);
      saveState(BLUE_PIN, 100 * blueval / 255);
      // Write some debug info
      Serial.println("Stop the show");
      // Send actual RGB value and state to controler and request ack back (true/false)
      Serial.println("Send eeprom value to controler"); 
      send( RedStatus.set(loadState(RED_PIN)), false );    
      send( GreenStatus.set(loadState(GREEN_PIN)), false );
      send( BlueStatus.set(loadState(BLUE_PIN)), false );
      send( rgbShowState.set(0), false);
    }
    
    void rgbShowStop(int sensor)
    {
       // define show Off
       isShow=0;
       // Save state
       saveState(0, 0);
       // Write some debug info
       Serial.println("Stop the show");
       // Send actual RGB value and state to controler and request ack back (true/false)
       Serial.println("Send eeprom value to controler"); 
       if (sensor != RED_PIN)
       {
            saveState(RED_PIN, 100 * redval / 255); 
            send( RedStatus.set(loadState(RED_PIN)), false );  
        }
        if (sensor != GREEN_PIN)
        {
            saveState(GREEN_PIN, 100 * greenval / 255); 
            send( GreenStatus.set(loadState(GREEN_PIN)), false );
        }
        if (sensor != BLUE_PIN)
        {
            saveState(BLUE_PIN, 100 * blueval / 255);
            send( BlueStatus.set(loadState(BLUE_PIN)), false );
        }
        send( rgbShowState.set(0), false);
    }
    

  • Mod

    @homer I've had good results by introducing a short wait time before sleeping ; e.g use wait(30000) to ignore any false triggers from the sensor.



  • @yveaux: Would 30 seconds be a short wait time? 😉



  • @homer what information is shown on the serial monitor?


  • Mod

    @flyingdomotic said in Can't even get a base Mysensors sketch to work:

    Would 30 seconds be a short wait time?

    For a PIR sensor I would say yes.
    A PIR triggers an alarm or switches on a lamp, so usually there is no need to do this more often than once every 30 seconds I would say.
    So what's the use in reporting the PIR triggers more often?



  • @yveaux : you're right! PIR are not so fast.


Log in to reply
 

Suggested Topics

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts