Problem getting the code to run with MySensors library 2.0



  • Hello guys,

    I am trying to write a simple code using the MySensors library 2.0. Its a simple switch. I am generating interrupt on pin 2 of Arduino Uno. Switch is connected between Pin 2 and GND. The interrupt is invoked when I change the state of the switch and then LED on pin 13 toggles.

    First I wrote the code without MySensors library. Just a simple Arduino code to make sure this thing works. It works flawlessly.

    Below is the code for this without MySensors library: This code works. I have commented out the lines which involve use o MySensors library.

    const byte ledPin = 13;
    volatile byte state = LOW;
    
    //MySensors
    // Enable debug prints
    //#define MY_DEBUG
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    
    //#include <SPI.h>
    //#include <MySensors.h>
    
    const byte DIGITAL_INPUT_SENSOR = 2;
    //#define CHILD_ID 1   // ID of the sensor child
    
    // Initialize message
    //MyMessage msg(CHILD_ID, V_LIGHT);
    
    void setup() {
      pinMode(ledPin, OUTPUT);
      pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), state_change, CHANGE);
    
      Serial.begin(115200);
    }
    
    //void presentation()  {
    //  // Send the sketch version information to the gateway and Controller
    //  sendSketchInfo("Switch", "1.0");
    //
    //  // Register all sensors to gw (they will be created as child devices)
    //  present(CHILD_ID, S_LIGHT);
    //}
    void loop() {
    
     // boolean status = digitalRead(DIGITAL_INPUT_SENSOR);
      digitalWrite(ledPin, state);
      
      //Serial.println(digitalRead(ledPin));
    
    
      //Serial.println(status);
      //send(msg.set((status)?"1":"0"));  //Send status value to the GW
    }
    
    void state_change() {
      Serial.println(state);
      state = !state;
    }
    

    The problem starts is when I add the line #include<MySensors.h>. When I add this line to the code, LED doesn't toggle when I flip the switch. I don't know what the problem is.
    I really appreciate the help.

    Thank You,
    Vaibhav


  • Mod

    @palande.vaibhav does the problem start when you only uncomment include MySensors.h, or does it start if you uncomment more than that line?

    If you activate the MySensors radio functions (by uncommenting MY_RADIO_NRF24) pin 13 will be used for communicating with the radio. See https://www.mysensors.org/build/connect_radio



  • @mfalkvidd @ayo Thanks for the reply.

    I have tried changing the pin to Pin8.

    I now have tried a Arduino library LED blink code. I attached the LED to Pin 8. It blinks without the #include<MySensors.h> line. But when I add #define MY_RADIO_NRF24 and #include <MySensors.h>, it stops working.

    I have to add #define MY_RADIO_NRF24 because if I don't it throws an error saying if I don't have a radio then adding MySensors.h is pointless. And I get it, its pointless.

    So, problem starts after uncommenting #include<Mysensors.h> and #define MY_RADIO_NRF24.

    /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.
    
      Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
      it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care 
      of use the correct LED pin whatever is the board used.
      If you want to know what pin the on-board LED is connected to on your Arduino model, check
      the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
      
      This example code is in the public domain.
    
      modified 8 May 2014
      by Scott Fitzgerald
      
      modified 2 Sep 2016
      by Arturo Guadalupi
    */
    
    //#define MY_RADIO_NRF24
    //#include <MySensors.h>
    
    // the setup function runs once when you press reset or power the board
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(8, OUTPUT);
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(500);                       // wait for a second
      digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
      delay(500);                       // wait for a second
    }```


  • I believe the problem is a result of the pin 13 you are using for the Led. The Mysensor & SPI both require pin 13 (Pin 13 is SCK pin for SPI) for the radio communication. Try and move your Led Pin to another pin from Pin 8 downward.



  • @ayo
    I did. The problem is still there. I also tried simplest of the simple LED blink code. Check my new post.

    Thank You



  • @palande.vaibhav said:

    @mfalkvidd @ayo

    Even uncommenting #define MY_RADIO_NRF24 WORKS.

    So the problem starts when I uncomment #include <MySensors.h>.



  • @mfalkvidd @ayo

    I just tested with older library version 1.5.4.

    Both Blink and my Switch code work with MySensors library version 1.5.4 but doesn't work with version 2.0.



  • So I tired the blinking thing on my own Arduino setup using the MySensors 2.0 installed var the Arduino Library manager.

    The Led blinks with the mysensors library attached.

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level. 
    #define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 8000000L
    #define MY_BAUD_RATE 38400
    #endif
    
    // Flash leds on rx/tx/err
    #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Inverses the behavior of leds
    //#define MY_WITH_LEDS_BLINKING_INVERSE
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    #define MY_INCLUSION_BUTTON_FEATURE
    
    // Inverses behavior of inclusion button (if using external pullup)
    //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
    
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60 
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    
    #include <SPI.h>
    #include <MySensors.h>  
    
    void setup() { 
      // Setup locally attached sensors
      pinMode(8, OUTPUT);
    }
    
    void presentation() {
     // Present locally attached sensors 
    }
    
    void loop() { 
      // Send locally attached sensor data here 
       digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
      wait(1000);              // wait for a second
      digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
      wait(1000);              // wait for a second
    }
    

    Maybe you can give that a short...



  • This post is deleted!


  • @ayo
    OK. I will give this a shot.

    One more thing is installing the library 2.0 from Arduino library manager gives an error "
    CRC doesn't match. File is corrupted." I am using Arduino IDE version 1.6.12.

    I am downloading the zip from GitHub and pasting it in the Documents/Arduino/libraries folder. This is OK right??



  • @ayo
    Something weird is happening right now. I am now running MySensors version 2.0. The code you gave works with library version 2.0.

    I was just experimenting with the lines of the code to see which line makes difference and I found that #define MY_GATEWAY_SERIAL line makes difference.
    If I just add that line to my Blink code, it works with MySensors.h

    I don't know why this happens.

    //// Enable debug prints to serial monitor
    //#define MY_DEBUG 
    //
    //
    //// Enable and select radio type attached
    #define MY_RADIO_NRF24
    ////#define MY_RADIO_RFM69
    //
    //// Set LOW transmit power level as default, if you have an amplified NRF-module and
    //// power your radio separately with a good regulator you can turn up PA level. 
    //#define MY_RF24_PA_LEVEL RF24_PA_LOW
    //
    //// Enable serial gateway
    #define MY_GATEWAY_SERIAL
    //
    //// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    //#if F_CPU == 8000000L
    //#define MY_BAUD_RATE 38400
    //#endif
    //
    //// Flash leds on rx/tx/err
    //#define MY_LEDS_BLINKING_FEATURE
    //// Set blinking period
    //#define MY_DEFAULT_LED_BLINK_PERIOD 300
    //
    //// Inverses the behavior of leds
    ////#define MY_WITH_LEDS_BLINKING_INVERSE
    //
    //// Enable inclusion mode
    //#define MY_INCLUSION_MODE_FEATURE
    //// Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    //
    //// Inverses behavior of inclusion button (if using external pullup)
    ////#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
    //
    //// Set inclusion mode duration (in seconds)
    //#define MY_INCLUSION_MODE_DURATION 60 
    //// Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    
    #include <SPI.h>
    #include <MySensors.h>  
    
    void setup() { 
      // Setup locally attached sensors
      pinMode(8, OUTPUT);
    }
    
    void presentation() {
     // Present locally attached sensors 
    }
    
    void loop() { 
      // Send locally attached sensor data here 
       digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);              // wait for a second
      digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);              // wait for a second
    }
    

    This code works but only if I have #define MY_GATEWAY_SERIAL line included. Can you try this on your setup?

    In my code which was not working previously, If I just add #define MY_GATEWAY_SERIAL , it works.

    In your Blink code, if you just comment out the line #define MY_GATEWAY_SERIAL, it won't work. Can you try this?



  • @mfalkvidd
    Any idea why this is happening?
    Cause I can't use MY_GATEWAY_SERIAL for a node. Right?



  • Okay....so you are really having a strange problem here.
    Am using the Arduino version 1.6.8,
    Can you consider upgrading your arduino version maybe that could fix the CRC thing...


  • Mod

    @palande.vaibhav said:

    Cause I can't use MY_GATEWAY_SERIAL for a node. Right?

    Yes you can. But I read that there is a bug in MySensors 2.0 that causes the presentation function to not run, so you'll need the latest code from the MySensors development branch on Github.



  • @ayo

    1.6.8 is the older version. 1.6.12 is the latest version.

    And the CRC error is the known bug. So, if your IDE and MySensors work then don't change anything.



  • @ayo
    did you try commenting #define MY_GATEWAY_SERIAL and running your Blink code?
    Can you please try that?



  • @mfalkvidd said:

    @palande.vaibhav said:

    Cause I can't use MY_GATEWAY_SERIAL for a node. Right?

    Yes you can. But I read that there is a bug in MySensors 2.0 that causes the presentation function to not run, so you'll need the latest code from the MySensors development branch on Github.

    But am I right that for MySensors version 2.0, we must have #define MY_GATEWAY_SERIAL line in order for the code to work? OR is it just me?

    And I don't even have anything in the presentation function. I think my problem is loop function not working if I don;t have #define MY_GATEWAY_SERIAL line in the code.



  • @mfalkvidd said:

    @palande.vaibhav said:

    Cause I can't use MY_GATEWAY_SERIAL for a node. Right?

    Yes you can. But I read that there is a bug in MySensors 2.0 that causes the presentation function to not run, so you'll need the latest code from the MySensors development branch on Github.

    I tried with the library from Development branch on GitHub. It still doesn't work.
    I must have #define MY_GATEWAY_SERIAL line in the code for the code to work with MySensors.h.

    Library version:
    0_1474709782623_javaw_2016-09-24_02-35-41.png

    Code below doesn't work cause I have commented #define MY_GATEWAY_SERIAL: Just uncomment it and the same code works

    //MySensors
    // Enable debug prints
    //#define MY_DEBUG
    
    //// Enable and select radio type attached
    #define MY_RADIO_NRF24
    ////#define MY_RADIO_RFM69
    
    //#define MY_GATEWAY_SERIAL
    
    #include <SPI.h>
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    const byte 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(1, V_LIGHT);
    
    void setup() { 
      // Setup locally attached sensors
      pinMode(8, OUTPUT);
    }
    
    void presentation() {
     // Present locally attached sensors 
    }
    
    void loop() { 
      // Send locally attached sensor data here 
       digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);              // wait for a second
      digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);              // wait for a second
    }
    


  • @mfalkvidd

    I saw the post about the presentation function not working in 2.0. But that has been fixed in development version.

    My problem is still there that code doesn't work at all. To which I think my loop function stops working if I don't have #define MY_GATEWAY_SERIAL line in the code.


  • Mod

    @palande.vaibhav what does the debug output say?
    You can also add a few Serial.println in the code to see where the execution stops.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.