@monte I was wondering if you'd used it or what your experience of it was. I'm also just enjoying learning by doing... but wanted to see the node to node communication working and more easily configurable. I'd like to configure this from the controller also perhaps.
I've moved much of the functionality to its own class so that it can be more easily added to nodes. Heres what I've got so far.
I used avdweb_Switch.h to simplify and abstract a little the button press/etc.
I'd still like to clear up a little and add a few more things, like having multiply nodes paired together etc.
Its still pretty much your origional code.
There was a small error that both nodeid & sensorid had to be different from the master and slave paired node... that was in gateway.ico
Gate Way.ico
/**
* 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.
*
*******************************
*
* DESCRIPTION
* The ArduinoGateway prints data received from sensors on the serial link.
* The gateway accepts input on seral which will be sent out on radio network.
*
* The GW code is designed for Arduino Nano 328p / 16MHz
*
* Wire connections (OPTIONAL):
* - Inclusion button should be connected between digital pin 3 and GND
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
*
* LEDs (OPTIONAL):
* - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
* - ERR (red) - fast blink on error during transmission error or recieve crc error
*
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_NRF5_ESB
#define MY_RADIO_RFM69
#define MY_RFM69_FREQUENCY RFM69_433MHZ
//#define MY_RADIO_RFM95
// 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
// 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
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Inverses the behavior of leds
//#define MY_WITH_LEDS_BLINKING_INVERSE
// Flash leds on rx/tx/err
// 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 <MySensors.h>
unsigned long currentTime = 0;
int waitTime = 10000;
int masterNodeAddr = 0;
bool masterNodeAddrPresent = 0;
int masterSensorAddr = 0;
char str1[8];
char str2[8];
bool pairing = 0;
//Messages for answering pairing requests
MyMessage msg(254,V_VAR2);
MyMessage msg2(254,V_VAR2);
//Time counter function instead delay
boolean isTimeUp(unsigned long *timeMark, unsigned long timeInterval) {
if (millis() - *timeMark >= timeInterval) {
*timeMark = millis();
return true;
}
return false;
}
void setup()
{
// Setup locally attached sensors
}
void loop()
{
//We are keeping ids of pairing nodes only for 10 seconds while pairing
if(isTimeUp(¤tTime, waitTime)) {
masterNodeAddr = 0;
masterNodeAddrPresent = 0;
}
}
void receive(const MyMessage &message) {
//I used specific message type for filtering out pairing request
if (message.type == V_VAR1) {
Serial.print("Incoming message. Node - ");
Serial.print(message.sender); Serial.print("-");
Serial.print(message.sensor);
Serial.println(message.getString());
// If we had already a pairing requests within the last 10 seconds ? Then lets setup ready to pair
if (!masterNodeAddrPresent) {
currentTime = millis();
masterNodeAddr = message.sender;
masterSensorAddr = message.sensor;
masterNodeAddrPresent = 1;
}
// Listening for the second node slave to request pairing.
else if (message.sender != masterNodeAddr ) { //&& message.sensor != masterSensorAddr) {
//Construct message string with node and sensor ids
snprintf(str2, 7, "%d;%d", message.sender, message.sensor);
snprintf(str1, 7, "%d;%d", masterNodeAddr, masterSensorAddr);
Serial.print("First address: "); Serial.print(str1); Serial.print("Second address: "); Serial.println(str2);
// Send answer to nodes
msg.setDestination(message.sender);
send(msg.set(str1), true);
wait(500);
Serial.println("Sent message to second address");
msg2.setDestination(masterNodeAddr);
send(msg2.set(str2), true);
wait(500);
Serial.println("Sent message to first address");
masterNodeAddr = 0;
masterSensorAddr = 0;
masterNodeAddrPresent = 0;
Serial.println("Pairing finished");
}
}
}
Button.ico
/*
* Description on node
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RFM69
#define MY_RFM69_FREQUENCY RFM69_433MHZ
#include <MySensors.h>
#define CHILD_ID 3
#include <avdweb_Switch.h>
#include "pair.h"
#define SET_BUTTON_PIN 5 // Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN 4 // Normal button PIN
Switch button = Switch(BUTTON_PIN);
PairLocal pairLocal = PairLocal(SET_BUTTON_PIN);
// I think send to controller the button regardless if its paired directly
void presentation()
{
// Send the sketch version information to the Controller in case node is'nt paired
//if (!paired) {
sendSketchInfo("Binary paired button", "1.0");
present(CHILD_ID, S_MOTION);
//}
}
void before()
{
pairLocal.before();
//optionally pass function in, This button node won't recieve messages to do something from a slave
uint16_t ptr = &arryChange;
pairLocal.setParedMessageRecievedFunction(ptr);
}
// Function called when a paired master node sends a message
void arryChange()
{
}
void setup()
{
pairLocal.setup();
}
void loop()
{
pairLocal.loop();
// Process our own sensors additional button
button.poll();
// button was pushed
if (button.pushed()) {
pairLocal.sendPairedMesage();
} else {
// if (!paired) {
//MyMessage msg(CHILD_ID, V_TRIPPED);
//send(msg.set(0)); // NOT SURE IF WE NEED THIS ????
// }
}
}
void receive(const MyMessage &message)
{
pairLocal.receive(message);
// Do any additional processing here.
}
Relay.ico
/*
* Description on node
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RFM69
#define MY_RFM69_FREQUENCY RFM69_433MHZ
#include <MySensors.h>
#define CHILD_ID 1
#define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
#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
#define SET_BUTTON_PIN 4
bool relayState;
MyMessage msg(CHILD_ID,V_LIGHT);
#include <avdweb_Switch.h>
#include "pair.h"
PairLocal pairLocal = PairLocal(SET_BUTTON_PIN);
// I think send to controller the button regardless if its paired directly
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay", "1.0");
present(CHILD_ID, S_LIGHT, "Test light", true);
// Send saved state to gateway (using eeprom storage)
send(msg.set(relayState),true);
}
void before()
{
pairLocal.before();
// Setup the relay
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, 1);
delay(1000);
digitalWrite(RELAY_PIN, 0);
// Set relay to last known state (using eeprom storage)
relayState = loadState(CHILD_ID);
digitalWrite(RELAY_PIN, relayState ? RELAY_ON : RELAY_OFF);
}
void setup()
{
pairLocal.setup();
//optionally pass function in, Relay change state
uint16_t ptr = &processPairMessageFromMaster;
pairLocal.setParedMessageRecievedFunction(ptr);
}
void loop()
{
pairLocal.loop();
}
void receive(const MyMessage &message)
{
pairLocal.receive(message);
// Additional process message from Gateway
if (message.type == V_LIGHT && !message.sender) {
// Change relay state
relayState = message.getBool();
digitalWrite(RELAY_PIN, relayState ? RELAY_ON : RELAY_OFF);
// Store state in eeprom
saveState(CHILD_ID, relayState);
// Write some debug info
Serial.print("Incoming change. New status:");
Serial.println(relayState);
}
}
// Function called when a paired master node sends a message
void processPairMessageFromMaster()
{
Serial.println("Relay changed state by master node");
digitalWrite(RELAY_PIN, relayState ? RELAY_OFF : RELAY_ON);
relayState = relayState ? 0 : 1;
}