Skip to content

Commit

Permalink
Add CAN transport layer (mysensors#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSlowik committed Jun 26, 2022
1 parent 423b9d7 commit 09cabc1
Show file tree
Hide file tree
Showing 28 changed files with 4,446 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .ci/doxygen.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(config) {
Documentation/doxygen.sh"""
warnings canComputeNew: false, canResolveRelativePaths: false,
defaultEncoding: '',
excludePattern: '''.*/hal/architecture/Linux/drivers/.*,.*/hal/transport/PJON/driver/.*,.*/hal/architecture/AVR/drivers/.*,.*/drivers/TinyGSM/.*''',
excludePattern: '''.*/hal/architecture/Linux/drivers/.*,.*/hal/transport/PJON/driver/.*,.*/hal/transport/CAN/driver/.*,.*/hal/architecture/AVR/drivers/.*,.*/drivers/TinyGSM/.*''',
failedTotalAll: '', healthy: '', includePattern: '', messagesPattern: '',
parserConfigurations: [[parserName: 'Doxygen', pattern: config.repository_root+'doxygen.log']],
unHealthy: '', unstableTotalAll: '0'
Expand Down
3 changes: 2 additions & 1 deletion .mystools/cppcheck/config/suppressions.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
// 3rd party
*:hal/architecture/Linux/*
*:drivers/*
*:hal/transport/PJON/driver/*
*:hal/transport/PJON/driver/*
*:hal/transport/CAN/driver/*
51 changes: 50 additions & 1 deletion MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,52 @@
*/
//#define MY_RS485

/**
* @def MY_CAN
* @brief Define this to use the CAN wired transport for sensor network communication.
*/
//#define MY_CAN
/**
* @def MY_DEBUG_VERBOSE_CAN
* @brief Define this for verbose debug prints related to the %CAN driver.
*/
//#define MY_DEBUG_VERBOSE_CAN
/**
* @def CAN_INT
* @brief Message arrived interrupt pin.
*/
#ifndef CAN_INT
#define CAN_INT (2u)
#endif
/**
* @def CAN_CS
* @brief Chip select pin.
*/
#ifndef CAN_CS
#define CAN_CS (10u)
#endif
/**
* @def CAN_SPEED
* @brief Baud rate. Allowed values can be found in mcp_can_dfs.h
*/
#ifndef CAN_SPEED
#define CAN_SPEED CAN_250KBPS
#endif
/**
* @def CAN_CLOCK
* @brief can clock. Allowed values can be found in mcp_can_dfs.h
*/
#ifndef CAN_CLOCK
#define CAN_CLOCK MCP_8MHZ
#endif
/**
* @def CAN_BUF_SIZE
* @brief assemble buffer size. Since long messages can be sliced and arrive mixed with other messages, assemble buffer is required.
*/
#ifndef CAN_BUF_SIZE
#define CAN_BUF_SIZE (8u)
#endif

/**
* @def MY_RS485_BAUD_RATE
* @brief The RS485 BAUD rate.
Expand Down Expand Up @@ -2275,7 +2321,7 @@
#endif

// Enable sensor network "feature" if one of the transport types was enabled
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined(MY_PJON)
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined(MY_PJON) || defined(MY_CAN)
#define MY_SENSOR_NETWORK
#endif

Expand Down Expand Up @@ -2443,6 +2489,9 @@
// PJON
#define MY_PJON
#define MY_DEBUG_VERBOSE_PJON
// CAN
#define MY_CAN
#define MY_DEBUG_VERBOSE_CAN
// RF24
#define MY_RADIO_RF24
#define MY_RADIO_NRF24 //deprecated
Expand Down
11 changes: 9 additions & 2 deletions MySensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,13 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#else
#define _PJONCNT 0 //!< _PJONCNT
#endif
#if defined(MY_CAN)
#define __CANCNT 1 //!< __CANCNT
#else
#define __CANCNT 0 //!< __CANCNT
#endif

#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT + _PJONCNT > 1)
#if (__RF24CNT + __NRF5ESBCNT + __RFM69CNT + __RFM95CNT + __RS485CNT + _PJONCNT + __CANCNT> 1)
#error Only one forward link driver can be activated
#endif
#endif //DOXYGEN
Expand All @@ -302,7 +307,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#endif

// TRANSPORT INCLUDES
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined (MY_PJON)
#if defined(MY_RADIO_RF24) || defined(MY_RADIO_NRF5_ESB) || defined(MY_RADIO_RFM69) || defined(MY_RADIO_RFM95) || defined(MY_RS485) || defined (MY_PJON) || defined(MY_CAN)
#include "hal/transport/MyTransportHAL.h"
#include "core/MyTransport.h"

Expand Down Expand Up @@ -386,6 +391,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#elif defined(MY_RADIO_RFM95)
#include "hal/transport/RFM95/driver/RFM95.cpp"
#include "hal/transport/RFM95/MyTransportRFM95.cpp"
#elif defined(MY_CAN)
#include "hal/transport/CAN/MyTransportCAN.cpp"
#elif defined(MY_PJON)
#include "hal/transport/PJON/driver/PJON.h"
#include "hal/transport/PJON/driver/PJONSoftwareBitBang.h"
Expand Down
4 changes: 3 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MySensors options:
MQTT publish topic prefix.
--my-mqtt-subscribe-topic-prefix=<PREFIX>
MQTT subscribe topic prefix.
--my-transport=[none|rf24|rfm69|rfm95|rs485]
--my-transport=[none|rf24|rfm69|rfm95|rs485|can]
Set the transport to be used to communicate with other nodes. [rf24]
--my-rf24-channel=<0-125> RF channel for the sensor net. [76]
--my-rf24-pa-level=[RF24_PA_MAX|RF24_PA_HIGH|RF24_PA_LOW|RF24_PA_MIN]
Expand Down Expand Up @@ -660,6 +660,8 @@ elif [[ ${transport_type} == "rfm95" ]]; then
CPPFLAGS="-DMY_RADIO_RFM95 $CPPFLAGS"
elif [[ ${transport_type} == "rs485" ]]; then
CPPFLAGS="-DMY_RS485 $CPPFLAGS"
elif [[ ${transport_type} == "can" ]]; then
CPPFLAGS="-DMY_CAN $CPPFLAGS"
else
die "Invalid transport type ${transport_type}." 3
fi
Expand Down
120 changes: 120 additions & 0 deletions examples/CANSwitch/CANSwitch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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 <[email protected]>
* Copyright (C) 2013-2019 Sensnology AB
* Full contributor list: https://github.com/mysensors/MySensors/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
*
* Interrupt driven binary switch example with dual interrupts
* Author: Patrick 'Anticimex' Fallberg
* Connect one button or door/window reed switch between
* digital I/O pin 3 (BUTTON_PIN below) and GND and the other
* one in similar fashion on digital I/O pin 2.
* This example is designed to fit Arduino Nano/Pro Mini
*
*/


// Enable debug prints to serial monitor
#define MY_DEBUG
//#define MY_DEBUG_VERBOSE_CAN

// Enable and select radio type attached
#define MY_CAN
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

#include <MySensors.h>

#define SKETCH_NAME "Binary Sensor"
#define SKETCH_MAJOR_VER "1"
#define SKETCH_MINOR_VER "0"

//#define PRIMARY_CHILD_ID 3
#define SECONDARY_CHILD_ID 4

//#define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch

//#if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
//#error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
//#endif
#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
#endif
//#if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
//#error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
//#endif
//#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
//#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
//#endif


// Change to V_LIGHT if you use S_LIGHT in presentation below
//MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);

void setup()
{
// Setup the buttons
// pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
}

void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);

// Register binary input sensor to sensor_node (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
// present(PRIMARY_CHILD_ID, S_DOOR);
present(SECONDARY_CHILD_ID, S_DOOR);
}

// Loop will iterate on changes on the BUTTON_PINs
void loop()
{
uint8_t value;
static uint8_t sentValue=2;
static uint8_t sentValue2=2;

// Short delay to allow buttons to properly settle
sleep(5);

// value = digitalRead(PRIMARY_BUTTON_PIN);

// if (value != sentValue) {
// Value has changed from last transmission, send the updated value
// send(msg.set(value==HIGH));
// sentValue = value;
// }

value = digitalRead(SECONDARY_BUTTON_PIN);

if (value != sentValue2) {
// Value has changed from last transmission, send the updated value
send(msg2.set(value==HIGH));
sentValue2 = value;
}

// Sleep until something happens with the sensor
sleep(SECONDARY_BUTTON_PIN-2, CHANGE, 0);
}
102 changes: 102 additions & 0 deletions examples/GatewaySerialCAN/GatewaySerialCAN.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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 <[email protected]>
* Copyright (C) 2013-2019 Sensnology AB
* Full contributor list: https://github.com/mysensors/MySensors/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 serial 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 received. In inclusion mode will blink fast only on presentation received
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
* - ERR (red) - fast blink on error during transmission error or receive crc error
*
*/

// Enable debug prints to serial monitor
#define MY_DEBUG
//#define MY_DEBUG_VERBOSE_CAN


// Enable and select radio type attached
#define MY_CAN
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#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 Arduinos 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>

void setup()
{
// Setup locally attached sensors
}

void presentation()
{
// Present locally attached sensors
}

void loop()
{
// Send locally attached sensor data here
}
Loading

0 comments on commit 09cabc1

Please sign in to comment.