MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem Topic is solved

Moderator: leecollings

piokuc

MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Hi
I start build MySensors Gateway on my Rpi3. I use radio RFM69HW and connected it as show on page to GPIO.
https://www.mysensors.org/build/raspberry
I build it by command:

Code: Select all

./configure --my-transport=rfm69 --my-rfm69-frequency=868 --my-is-rfm69hw --my-gateway=serial --my-serial-is-pty --my-serial-pty=/dev/ttyUSB1
Then

Code: Select all

make
.

Then

Code: Select all

sudo make install
and finally add

Code: Select all

sudo systemctl enable mysgw.service
On finish i reboot my RPI3.
But in Domoticz when i run and add to hardware MySensors Gateway USB i have error:

Code: Select all

2017-12-27 17:05:09.419 MySensors: Using serial port: /dev/ttyUSB1
2017-12-27 17:05:09.419 MySensors: Gateway Version: 2.2.0-rc.2
2017-12-27 17:05:09.420 Error: Serial Port closed!... Error: End of file
2017-12-27 17:05:10.419 MySensors: retrying in 30 seconds...
2017-12-27 17:05:39.422 MySensors: Using serial port: /dev/ttyUSB1
2017-12-27 17:05:39.422 Error: Serial Port closed!... Error: End of file
2017-12-27 17:05:40.422 MySensors: retrying in 30 seconds...
2017-12-27 17:06:09.425 MySensors: Using serial port: /dev/ttyUSB1
2017-12-27 17:06:09.425 Error: Serial Port closed!... Error: End of file
2017-12-27 17:06:10.425 MySensors: retrying in 30 seconds...
2017-12-27 17:06:39.427 MySensors: Using serial port: /dev/ttyUSB1
2017-12-27 17:06:39.428 Error: Serial Port closed!... Error: End of file
2017-12-27 17:06:40.428 MySensors: retrying in 30 seconds...
2017-12-27 17:07:09.430 MySensors: Using serial port: /dev/ttyUSB1
2017-12-27 17:07:09.430 Error: Serial Port closed!... Error: End of file
Port ttyUSB1 is empty on my RPI3 as i check.

Please help me where is the problem.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

Why are you using the serial port and not ethernet?

I use: --my-transport=rfm69 --my-rfm69-frequency=868 --my-is-rfm69hw and connect through ethenet (default) and port 5003 (default)
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Because i use it on raspberry the same as i have Domoticz.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

piokuc wrote: Wednesday 27 December 2017 21:28 Because i use it on raspberry the same as i have Domoticz.
Then compile with ethernet and connect domoticz via 127.0.0.1 port 5003
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

I try and works. But i have node on MySensors 2.1.1 and it is not connected to this Gateway Ethernet on MySensors 2.2.0-rc2
Why ? I must also node build on 2.2.0-rc2 ?
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

Yes, you have to use the same version for the nodes
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

I want build MySensors Gateway on 2.1.1 but i dont know how...
When i try correct my sketch NODE to 2.2.0-rc2 but i have a problem:

Sketch node

Code: Select all

/*
   Relay with button sketch
   modified to work with no uplink
   to gateway and try to maintain sync to controller
*/


#define MY_DEBUG                               // Enable debug prints to serial monitor

#define MY_RADIO_RFM69
#define MY_RFM69_FREQUENCY 868
#define MY_IS_RFM69HW

//#define MY_NODE_ID 203                       // Node id defaults to AUTO (tries to fetch id from controller) 

#define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds

#define MY_REPEATER_FEATURE                    // Enabled repeater feature for this node

#include <MySensors.h>
#include <Bounce2.h>

#define RELAY_PIN  5      // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
#define CHILD_ID 1        // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce();
int oldValue = 0;
bool uplinkAvailable = true;
bool state = false;
bool requestState;
bool firstStart = true;
unsigned long uplinkCheckTime ;                // holder for uplink checks
unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
unsigned long returnWait = 1000;               // how long to wait for return from controller in milliseconds.. adjust as needed
unsigned long oldTime = 0;
unsigned long newTime = 0;
MyMessage msg(CHILD_ID, V_STATUS);

void setup(){
  pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
  
  debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
  debouncer.interval(5);

  pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
  digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
}

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("1xRelay & Button", "2.2.0-rc2");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID, S_BINARY);
}


void loop(){
  if (firstStart) {                            // this code is only run once at startup
    Serial.println("First run started");
    requestTime();                             // get time from controller
    wait (returnWait);                         // delay to allow time to return
    if (oldTime == 0){                         // check to see if there was a return from the time request
      Serial.println("uplink not available");
      uplinkAvailable = false;                 // no uplink established
      uplinkCheckTime = millis();
    }
     else{
      Serial.println("uplink available");
      request( CHILD_ID, V_STATUS);            // get status of switch on controller
      wait (returnWait);                       //wait needed to allow request to return from controller
      Serial.print("controller state --- ");
      Serial.println(requestState);
      if (requestState != state) {             // check that controller is corectly showing the current relay state
        send(msg.set(state), false);           // notify controller of current state
      } 
     }   
  firstStart = false;                                          // set firstStart flag false to prevent code from running again
 }

  debouncer.update();
  int value = debouncer.read();                               // Get the update value
  if (value != oldValue && value == 0) {                      // check for new button push
    state =  !state;                                          // Toggle the state
    digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
    requestTime();
    wait (returnWait);                                               // delay to allow time to return
    if (oldTime != newTime){                                  // if times are different then uplink is available
      send(msg.set(state), false);
      oldTime = newTime;
    }
    else{                                                    // if times are the same no uplink is available
     Serial.println("uplink not available");
      uplinkAvailable = false;                                // no uplink available, set flag false
      uplinkCheckTime = millis();                             // start the timer from now
    }

  }
  oldValue = value;
 
  if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
    uplinkCheck();                                                                  // call uplink checking function
  }

}

/*-------------------start of functions--------------------------*/

void receive(const MyMessage &message) {
  if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
    switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
      case C_SET:                                                   //message is a set command  from controller to update relay state
        state = message.getBool();                                  // get the new state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
        uplinkAvailable = true;                                     //  uplink established
        /*---- Write some debug info----*/
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
        break;
      case C_REQ:                                               // message is a returning request from controller
        requestState = message.getBool();                       // update requestState with returning state
        break;
    }
  }
}

void uplinkCheck() {
    requestTime();
    wait (returnWait);                       // wait for time return.. this may need to be varied for your system
   if (oldTime != newTime){
     Serial.println("uplink re-established");
     request( CHILD_ID, V_STATUS);
     wait (returnWait);                        //wait needed to allow request to return from controller
    if (requestState != state) {              // check that controller is corectly showing the current relay state
      send(msg.set(state), false);            // notify controller of current state no ack
      uplinkAvailable = true;                 //  uplink established
      oldTime = newTime;
    }
  }
  uplinkCheckTime = millis();                // reset the checktime
  Serial.println("uplinkchecktime reset");
}


void receiveTime(unsigned long time)
{
  if (firstStart){
    oldTime = time;
    newTime = time;
  }
  else{
  newTime = time;
}
  Serial.print("time received---- " );
  Serial.println(time);
}
but when compiling i have info:

Code: Select all

In file included from C:\Users\Piotrek\Documents\Arduino\libraries\MySensors/MySensors.h:347:0,

                 from C:\Users\Piotrek\Desktop\1xrelay-butt_with_test_controller\1xrelay-butt_with_test_controller.ino:20:

C:\Users\Piotrek\Documents\Arduino\libraries\MySensors/hal/transport/MyTransportRFM69.cpp: In function 'bool transportInit()':

C:\Users\Piotrek\Documents\Arduino\libraries\MySensors/hal/transport/MyTransportRFM69.cpp:168:72: warning: large integer implicitly truncated to unsigned type [-Woverflow]

  if (_radio.initialize(MY_RFM69_FREQUENCY, _address, MY_RFM69_NETWORKID)) {

                                                                        ^
User avatar
sincze
Posts: 1302
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by sincze »

Do you keep your mysensors versions in separate Arduino folders?
Otherwise it will become 1 big compiling mess.

Example of dir:
Arduino-2.1.0
Arduino-2.1.1
Arduino-3.3.2-rc2

And before compiling switch to the correct .info folder in Arduino-Sketches.
FIle->Prefrences -> Sketchbook location
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

You need to use the new rfm69 driver

#define MY_RFM69_NEW_DRIVER // 2.2.0-beta needs new driver on every RFM69 node
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Ok write again new sketch to my arduino mini pro and he is not connected to MyGateway on RPI3

This is sketch:

Code: Select all

/*
   Relay with button sketch
   modified to work with no uplink
   to gateway and try to maintain sync to controller
*/


#define MY_DEBUG                               // Enable debug prints to serial monitor

#define MY_RADIO_RFM69
#define MY_RFM69_FREQUENCY 868
#define MY_IS_RFM69HW
#define MY_RFM69_NEW_DRIVER

//#define MY_NODE_ID 203                       // Node id defaults to AUTO (tries to fetch id from controller) 

#define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds

#define MY_REPEATER_FEATURE                    // Enabled repeater feature for this node

#include <MySensors.h>
#include <Bounce2.h>

#define RELAY_PIN  5      // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
#define CHILD_ID 1        // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce();
int oldValue = 0;
bool uplinkAvailable = true;
bool state = false;
bool requestState;
bool firstStart = true;
unsigned long uplinkCheckTime ;                // holder for uplink checks
unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
unsigned long returnWait = 1000;               // how long to wait for return from controller in milliseconds.. adjust as needed
unsigned long oldTime = 0;
unsigned long newTime = 0;
MyMessage msg(CHILD_ID, V_STATUS);

void setup(){
  pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
  
  debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
  debouncer.interval(5);

  pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
  digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
}

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("1xRelay & Button", "2.2.0-rc2");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID, S_BINARY);
}


void loop(){
  if (firstStart) {                            // this code is only run once at startup
    Serial.println("First run started");
    requestTime();                             // get time from controller
    wait (returnWait);                         // delay to allow time to return
    if (oldTime == 0){                         // check to see if there was a return from the time request
      Serial.println("uplink not available");
      uplinkAvailable = false;                 // no uplink established
      uplinkCheckTime = millis();
    }
     else{
      Serial.println("uplink available");
      request( CHILD_ID, V_STATUS);            // get status of switch on controller
      wait (returnWait);                       //wait needed to allow request to return from controller
      Serial.print("controller state --- ");
      Serial.println(requestState);
      if (requestState != state) {             // check that controller is corectly showing the current relay state
        send(msg.set(state), false);           // notify controller of current state
      } 
     }   
  firstStart = false;                                          // set firstStart flag false to prevent code from running again
 }

  debouncer.update();
  int value = debouncer.read();                               // Get the update value
  if (value != oldValue && value == 0) {                      // check for new button push
    state =  !state;                                          // Toggle the state
    digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
    requestTime();
    wait (returnWait);                                               // delay to allow time to return
    if (oldTime != newTime){                                  // if times are different then uplink is available
      send(msg.set(state), false);
      oldTime = newTime;
    }
    else{                                                    // if times are the same no uplink is available
     Serial.println("uplink not available");
      uplinkAvailable = false;                                // no uplink available, set flag false
      uplinkCheckTime = millis();                             // start the timer from now
    }

  }
  oldValue = value;
 
  if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
    uplinkCheck();                                                                  // call uplink checking function
  }

}

/*-------------------start of functions--------------------------*/

void receive(const MyMessage &message) {
  if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
    switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
      case C_SET:                                                   //message is a set command  from controller to update relay state
        state = message.getBool();                                  // get the new state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
        uplinkAvailable = true;                                     //  uplink established
        /*---- Write some debug info----*/
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
        break;
      case C_REQ:                                               // message is a returning request from controller
        requestState = message.getBool();                       // update requestState with returning state
        break;
    }
  }
}

void uplinkCheck() {
    requestTime();
    wait (returnWait);                       // wait for time return.. this may need to be varied for your system
   if (oldTime != newTime){
     Serial.println("uplink re-established");
     request( CHILD_ID, V_STATUS);
     wait (returnWait);                        //wait needed to allow request to return from controller
    if (requestState != state) {              // check that controller is corectly showing the current relay state
      send(msg.set(state), false);            // notify controller of current state no ack
      uplinkAvailable = true;                 //  uplink established
      oldTime = newTime;
    }
  }
  uplinkCheckTime = millis();                // reset the checktime
  Serial.println("uplinkchecktime reset");
}


void receiveTime(unsigned long time)
{
  if (firstStart){
    oldTime = time;
    newTime = time;
  }
  else{
  newTime = time;
}
  Serial.print("time received---- " );
  Serial.println(time);
}

Some days ago i use MySensors Gateway on Arduino Nano on USB but on version 2.1.1 and this sketch works ok. Now i installed my MySensor Gateway on RPI3 on serial connection and it is build on version 2.2.0-rc2 and now all not working. Node is not find in Domoticz on Gateway.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

When you starts mysgw manually with -d, do you see the node send information?
Also using RFM69HW on node?
Did you 'enable accept new hardware' in Domoticz?
I have 10+ nodes running using 2.2.0-rc2 gateway and node sw and integrated with domoticz.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

Should'nt

#define MY_RFM69_FREQUENCY 868
be
RFM69_868MHZ

#define RFM69_868MHZ (868000000ul) //!< RFM69_868MHZ

or not defined at all (use module freq). I never set this
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Ok
I test without define freq and with define freq and also the same. No connection...

Code: Select all

/*
   Relay with button sketch
   modified to work with no uplink
   to gateway and try to maintain sync to controller
*/


#define MY_DEBUG                               // Enable debug prints to serial monitor

#define MY_RADIO_RFM69
#define RFM69_868MHZ
#define MY_IS_RFM69HW
#define MY_RFM69_NEW_DRIVER

//#define MY_NODE_ID 203                       // Node id defaults to AUTO (tries to fetch id from controller) 

#define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds

#define MY_REPEATER_FEATURE                    // Enabled repeater feature for this node

#include <MySensors.h>
#include <Bounce2.h>

#define RELAY_PIN  5      // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
#define CHILD_ID 1        // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce();
int oldValue = 0;
bool uplinkAvailable = true;
bool state = false;
bool requestState;
bool firstStart = true;
unsigned long uplinkCheckTime ;                // holder for uplink checks
unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
unsigned long returnWait = 1000;               // how long to wait for return from controller in milliseconds.. adjust as needed
unsigned long oldTime = 0;
unsigned long newTime = 0;
MyMessage msg(CHILD_ID, V_STATUS);

void setup(){
  pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
  
  debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
  debouncer.interval(5);

  pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
  digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
}

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("1xRelay & Button", "2.2.0-rc2");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID, S_BINARY);
}


void loop(){
  if (firstStart) {                            // this code is only run once at startup
    Serial.println("First run started");
    requestTime();                             // get time from controller
    wait (returnWait);                         // delay to allow time to return
    if (oldTime == 0){                         // check to see if there was a return from the time request
      Serial.println("uplink not available");
      uplinkAvailable = false;                 // no uplink established
      uplinkCheckTime = millis();
    }
     else{
      Serial.println("uplink available");
      request( CHILD_ID, V_STATUS);            // get status of switch on controller
      wait (returnWait);                       //wait needed to allow request to return from controller
      Serial.print("controller state --- ");
      Serial.println(requestState);
      if (requestState != state) {             // check that controller is corectly showing the current relay state
        send(msg.set(state), false);           // notify controller of current state
      } 
     }   
  firstStart = false;                                          // set firstStart flag false to prevent code from running again
 }

  debouncer.update();
  int value = debouncer.read();                               // Get the update value
  if (value != oldValue && value == 0) {                      // check for new button push
    state =  !state;                                          // Toggle the state
    digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
    requestTime();
    wait (returnWait);                                               // delay to allow time to return
    if (oldTime != newTime){                                  // if times are different then uplink is available
      send(msg.set(state), false);
      oldTime = newTime;
    }
    else{                                                    // if times are the same no uplink is available
     Serial.println("uplink not available");
      uplinkAvailable = false;                                // no uplink available, set flag false
      uplinkCheckTime = millis();                             // start the timer from now
    }

  }
  oldValue = value;
 
  if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
    uplinkCheck();                                                                  // call uplink checking function
  }

}

/*-------------------start of functions--------------------------*/

void receive(const MyMessage &message) {
  if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
    switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
      case C_SET:                                                   //message is a set command  from controller to update relay state
        state = message.getBool();                                  // get the new state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
        uplinkAvailable = true;                                     //  uplink established
        /*---- Write some debug info----*/
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
        break;
      case C_REQ:                                               // message is a returning request from controller
        requestState = message.getBool();                       // update requestState with returning state
        break;
    }
  }
}

void uplinkCheck() {
    requestTime();
    wait (returnWait);                       // wait for time return.. this may need to be varied for your system
   if (oldTime != newTime){
     Serial.println("uplink re-established");
     request( CHILD_ID, V_STATUS);
     wait (returnWait);                        //wait needed to allow request to return from controller
    if (requestState != state) {              // check that controller is corectly showing the current relay state
      send(msg.set(state), false);            // notify controller of current state no ack
      uplinkAvailable = true;                 //  uplink established
      oldTime = newTime;
    }
  }
  uplinkCheckTime = millis();                // reset the checktime
  Serial.println("uplinkchecktime reset");
}


void receiveTime(unsigned long time)
{
  if (firstStart){
    oldTime = time;
    newTime = time;
  }
  else{
  newTime = time;
}
  Serial.print("time received---- " );
  Serial.println(time);
}

Code: Select all

/*
   Relay with button sketch
   modified to work with no uplink
   to gateway and try to maintain sync to controller
*/


#define MY_DEBUG                               // Enable debug prints to serial monitor

#define MY_RADIO_RFM69
#define MY_IS_RFM69HW
#define MY_RFM69_NEW_DRIVER

//#define MY_NODE_ID 203                       // Node id defaults to AUTO (tries to fetch id from controller) 

#define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds

#define MY_REPEATER_FEATURE                    // Enabled repeater feature for this node

#include <MySensors.h>
#include <Bounce2.h>

#define RELAY_PIN  5      // Arduino Digital I/O pin number for relay 
#define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
#define CHILD_ID 1        // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce();
int oldValue = 0;
bool uplinkAvailable = true;
bool state = false;
bool requestState;
bool firstStart = true;
unsigned long uplinkCheckTime ;                // holder for uplink checks
unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
unsigned long returnWait = 1000;               // how long to wait for return from controller in milliseconds.. adjust as needed
unsigned long oldTime = 0;
unsigned long newTime = 0;
MyMessage msg(CHILD_ID, V_STATUS);

void setup(){
  pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
  
  debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
  debouncer.interval(5);

  pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
  digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
}

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("1xRelay & Button", "2.2.0-rc2");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID, S_BINARY);
}


void loop(){
  if (firstStart) {                            // this code is only run once at startup
    Serial.println("First run started");
    requestTime();                             // get time from controller
    wait (returnWait);                         // delay to allow time to return
    if (oldTime == 0){                         // check to see if there was a return from the time request
      Serial.println("uplink not available");
      uplinkAvailable = false;                 // no uplink established
      uplinkCheckTime = millis();
    }
     else{
      Serial.println("uplink available");
      request( CHILD_ID, V_STATUS);            // get status of switch on controller
      wait (returnWait);                       //wait needed to allow request to return from controller
      Serial.print("controller state --- ");
      Serial.println(requestState);
      if (requestState != state) {             // check that controller is corectly showing the current relay state
        send(msg.set(state), false);           // notify controller of current state
      } 
     }   
  firstStart = false;                                          // set firstStart flag false to prevent code from running again
 }

  debouncer.update();
  int value = debouncer.read();                               // Get the update value
  if (value != oldValue && value == 0) {                      // check for new button push
    state =  !state;                                          // Toggle the state
    digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
    requestTime();
    wait (returnWait);                                               // delay to allow time to return
    if (oldTime != newTime){                                  // if times are different then uplink is available
      send(msg.set(state), false);
      oldTime = newTime;
    }
    else{                                                    // if times are the same no uplink is available
     Serial.println("uplink not available");
      uplinkAvailable = false;                                // no uplink available, set flag false
      uplinkCheckTime = millis();                             // start the timer from now
    }

  }
  oldValue = value;
 
  if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
    uplinkCheck();                                                                  // call uplink checking function
  }

}

/*-------------------start of functions--------------------------*/

void receive(const MyMessage &message) {
  if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
    switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
      case C_SET:                                                   //message is a set command  from controller to update relay state
        state = message.getBool();                                  // get the new state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
        uplinkAvailable = true;                                     //  uplink established
        /*---- Write some debug info----*/
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
        break;
      case C_REQ:                                               // message is a returning request from controller
        requestState = message.getBool();                       // update requestState with returning state
        break;
    }
  }
}

void uplinkCheck() {
    requestTime();
    wait (returnWait);                       // wait for time return.. this may need to be varied for your system
   if (oldTime != newTime){
     Serial.println("uplink re-established");
     request( CHILD_ID, V_STATUS);
     wait (returnWait);                        //wait needed to allow request to return from controller
    if (requestState != state) {              // check that controller is corectly showing the current relay state
      send(msg.set(state), false);            // notify controller of current state no ack
      uplinkAvailable = true;                 //  uplink established
      oldTime = newTime;
    }
  }
  uplinkCheckTime = millis();                // reset the checktime
  Serial.println("uplinkchecktime reset");
}


void receiveTime(unsigned long time)
{
  if (firstStart){
    oldTime = time;
    newTime = time;
  }
  else{
  newTime = time;
}
  Serial.print("time received---- " );
  Serial.println(time);
}
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

Well it should work, please verify your MySensors setup first and then domoticz integration
As mentioned earlier, start mysgw with -d option
sudo service mysgw stop
sudo mysgw -d

and check the output, you should see your node messages when you reset the node.
If that works check is domoticz can connect to 127.0.0.1 port 5003 (verify domoticz log)
Then enable 'Accept new hardware' in domoticz setup->settings Hardware/Devices section and reset your node again.

see previous note: http://www.domoticz.com/forum/viewtopic ... 05#p163598
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Sorry works ok.I reboot my raspberry and after rebot is ok. Probably problem will be when i use debug and Domoticz at the same time.

I also build Gateway on RPI3 by serial. I install clear image and then Domoticz and run SPI and Serial by Sudo raspi-config.
This probably resolved my problem and now Gateway works on /dev/ttyUSB42 and works ok.


Did you know how build Gateway on RPI3 on version MySensors 2.1.1 ? Becuase default build on 2.2.0-rc2
Last edited by piokuc on Thursday 28 December 2017 19:38, edited 2 times in total.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

piokuc wrote: Thursday 28 December 2017 19:35 This probably resolved my problem and now Gateway works on /dev/ttyUSB42 and works ok.
Can you mark this topic solved?
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

Did you know how build Gateway on RPI3 on version MySensors 2.1.1 ? Becuase default build on 2.2.0-rc2

And where i can mark as solved ?


And i found bug in Domoticz BETA 8796. No reconized version gateway in HARDWARE. On stable 8153 works with no problem and correct reconized version gateway 2.2.0-rc2... But gateway works correct and control Nodes...
And again problem with this:

Code: Select all

2017-12-28 20:01:32.587 MySensors: retrying in 30 seconds...
2017-12-28 20:02:08.752 MySensors: Using serial port: /dev/serial0
2017-12-28 20:02:08.752 MySensors: Gateway Version: 2.2.0-rc.2
2017-12-28 20:02:08.752 Error: Serial Port closed!... Error: End of file
2017-12-28 20:02:09.752 MySensors: retrying in 30 seconds...
2017-12-28 20:02:38.755 MySensors: Using serial port: /dev/serial0
2017-12-28 20:02:38.755 Error: Serial Port closed!... Error: End of file
2017-12-28 20:02:39.755 MySensors: retrying in 30 seconds...
2017-12-28 20:03:08.757 MySensors: Using serial port: /dev/serial0
2017-12-28 20:03:08.761 Error: Serial Port closed!... Error: End of file
2017-12-28 20:03:09.758 MySensors: retrying in 30 seconds...
On beta is problem.
On stable works correct.
SweetPants

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by SweetPants »

piokuc wrote: Thursday 28 December 2017 19:38 And where i can mark as solved ?
Just 'accept this answer' it's the V like-sign in the upper right corner near the "
piokuc

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by piokuc »

but now problem is not solved because after install beta domoticz issue come back.
gohan
Posts: 36
Joined: Monday 13 February 2017 23:42
Target OS: Windows
Domoticz version:
Contact:

Re: MySensors Gateway on GPIO RPi 3 with RFM69HW-Problem

Post by gohan »

I'd rather use the ethernet gateway, runs fine without issues and I can have multiple connections to it
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest