Possible problem with sketch?



  • Hi everyone

    I haven't been around all that often recently, had stuff happening in my life for the last couple years, but things are getting better and I am keen to get back into this and start enjoying myself again, even though I am very much a noob.

    So I recently dusted off my Mysensors gear and and getting it all up and running again. A couple days ago I managed to redo my Ethernet Gateway and connect it successfully to my Vera controller. I even managed to connect a couple old sensors. As I was just testing at the time, today I deleted those old sensors with the plan to rebuild what I have and set them up again. Unfortunately I seem to be having issues with connecting my first 'new' Mysensor to the gateway and showing in Vera. The node is a repeater with 2 relays. I did have it working a couple days ago, but today I changed the code as I needed the relays to just open momentarily, and the Arduino IDE saved the new sketch over the old working one. Anyways, this is the sketch:

    /**
     * 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 control physical relays.
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <MySensors.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    
    void before()
    {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            if (loadState(sensor) == RELAY_ON) {
    digitalWrite(pin, RELAY_ON);
    wait(1000);
    digitalWrite(pin, RELAY_OFF);
    }
        }
    }
    
    void setup()
    {
    
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Relay", "1.0");
    
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
        }
    }
    
    
    void loop()
    {
    
    }
    
    void receive(const MyMessage &message)
    {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_STATUS) {
            // Change relay state
            if (message.getBool() == RELAY_ON) {
    digitalWrite(message.sensor-1+RELAY_1, RELAY_ON);
    wait(1000);
    digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
    }
            // Store state in eeprom
            saveState(message.sensor, message.getBool());
            // Write some debug info
            Serial.print("Incoming change for sensor:");
            Serial.print(message.sensor);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
        }
    }
    

    I have restarted the Gateway, but the issue is still present. I thought I would see if you all could review the sketch in case I did something wrong to it, because I can't see a problem.

    Thanks


  • Mod

    @homer could you describe the nature of this potential problem? Does the sketch not compile? If so, what error message are you getting? Does the sketch not behave as you planned? If so, how does it behave and how do you expect it to behave? A debug log from the sensor could be useful as well.

    Use the auto formatting feature in the Arduino IDE. It will make it easier to read the code, for yourself and for everyone trying to help you.



  • Thank you for your reply.

    Good news; it's working, but I don't know why exactly. The last thing I did was to send the sketch to the arduino twice in a row. Anyways, the gateway now registers the node, so that is great!

    I will have to look into the auto formatting, because I thought that I had uploaded the sketch in an easy to view way. Anyways I will look into it if it will help with assistance!

    Now though I have another issue.... I have it so the relay will open, wait a second, and then close. The controller though is not updating and showing that the relay has closed. Time to look for a fix! 🙂



  • I managed to do a workaround. I have created a scene in Vera where it will automatically turn the device off after two seconds whenever it's operated.

    If there was something I could have added to the sketch to make this happen without the scene, I'm keen to know!



  • Hi @Homer
    The reason that the controller does not reflect the status of the relay is that you are not telling it that the status has changed.

    This is a snippet of how I have coded the same thing:

    void receive(const MyMessage &message) {
      if (message.type == V_STATUS) {   
          state = message.getBool();          // Change relay state
          setRelayState(RELAY_PIN, state);    
          saveState(CHILD_ID_RELAY, state);        // Store state in eeprom
          // Write some debug info
          printStateChangedDebug(CHANGE_STATE_SOURCE_RADIO, CHILD_ID_RELAY, state);
      }
    } 
    
    // Set status of relay pin
    void setRelayState(byte relayPin, bool value) {
      digitalWrite(relayPin, value ? RELAY_ON : RELAY_OFF);
      send(msgSW.set(state), false);           // acknowledge new state to controller, no ack requested
    }
    

    When the message is received, I first change the relay, then I confirm the status with the send-message.


  • Mod

    @bgunnarb just a note: calling send from within receive will overwrite the received message. In your case it is ok, since you're not using the message variable after calling setRelayState (which calls send). But for example if printStateChangedDebug would use the message variable, the data would contain the sent message, not the received message.



  • Thank you for everyone's replies!


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 2
  • 1
  • 6

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts