Skip to content

Commit bc1fe55

Browse files
committed
Add CAN examples
1 parent 492aa63 commit bc1fe55

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

examples/CANSwitch/CANSwitch.ino

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2019 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
19+
*******************************
20+
*
21+
* DESCRIPTION
22+
*
23+
* Interrupt driven binary switch example with dual interrupts
24+
* Author: Patrick 'Anticimex' Fallberg
25+
* Connect one button or door/window reed switch between
26+
* digital I/O pin 3 (BUTTON_PIN below) and GND and the other
27+
* one in similar fashion on digital I/O pin 2.
28+
* This example is designed to fit Arduino Nano/Pro Mini
29+
*
30+
*/
31+
32+
33+
// Enable debug prints to serial monitor
34+
#define MY_DEBUG
35+
36+
// Enable and select radio type attached
37+
#define MY_CAN
38+
//#define MY_RADIO_NRF5_ESB
39+
//#define MY_RADIO_RFM69
40+
//#define MY_RADIO_RFM95
41+
42+
#include <MySensors.h>
43+
44+
#define SKETCH_NAME "Binary Sensor"
45+
#define SKETCH_MAJOR_VER "1"
46+
#define SKETCH_MINOR_VER "0"
47+
48+
//#define PRIMARY_CHILD_ID 3
49+
#define SECONDARY_CHILD_ID 4
50+
51+
//#define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
52+
#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
53+
54+
//#if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
55+
//#error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
56+
//#endif
57+
#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
58+
#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
59+
#endif
60+
//#if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
61+
//#error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
62+
//#endif
63+
//#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
64+
//#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
65+
//#endif
66+
67+
68+
// Change to V_LIGHT if you use S_LIGHT in presentation below
69+
//MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
70+
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
71+
72+
void setup()
73+
{
74+
// Setup the buttons
75+
// pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
76+
pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
77+
}
78+
79+
void presentation()
80+
{
81+
// Send the sketch version information to the gateway and Controller
82+
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
83+
84+
// Register binary input sensor to sensor_node (they will be created as child devices)
85+
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
86+
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
87+
// present(PRIMARY_CHILD_ID, S_DOOR);
88+
present(SECONDARY_CHILD_ID, S_DOOR);
89+
}
90+
91+
// Loop will iterate on changes on the BUTTON_PINs
92+
void loop()
93+
{
94+
uint8_t value;
95+
static uint8_t sentValue=2;
96+
static uint8_t sentValue2=2;
97+
98+
// Short delay to allow buttons to properly settle
99+
sleep(5);
100+
101+
// value = digitalRead(PRIMARY_BUTTON_PIN);
102+
103+
// if (value != sentValue) {
104+
// Value has changed from last transmission, send the updated value
105+
// send(msg.set(value==HIGH));
106+
// sentValue = value;
107+
// }
108+
109+
value = digitalRead(SECONDARY_BUTTON_PIN);
110+
111+
if (value != sentValue2) {
112+
// Value has changed from last transmission, send the updated value
113+
send(msg2.set(value==HIGH));
114+
sentValue2 = value;
115+
}
116+
117+
// Sleep until something happens with the sensor
118+
sleep(SECONDARY_BUTTON_PIN-2, CHANGE, 0);
119+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2019 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
19+
*******************************
20+
*
21+
* DESCRIPTION
22+
* The ArduinoGateway prints data received from sensors on the serial link.
23+
* The gateway accepts input on serial which will be sent out on radio network.
24+
*
25+
* The GW code is designed for Arduino Nano 328p / 16MHz
26+
*
27+
* Wire connections (OPTIONAL):
28+
* - Inclusion button should be connected between digital pin 3 and GND
29+
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
30+
*
31+
* LEDs (OPTIONAL):
32+
* - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
33+
* - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
34+
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
35+
* - ERR (red) - fast blink on error during transmission error or receive crc error
36+
*
37+
*/
38+
39+
// Enable debug prints to serial monitor
40+
#define MY_DEBUG
41+
42+
43+
// Enable and select radio type attached
44+
#define MY_CAN
45+
//#define MY_RADIO_NRF5_ESB
46+
//#define MY_RADIO_RFM69
47+
//#define MY_RADIO_RFM95
48+
49+
// Set LOW transmit power level as default, if you have an amplified NRF-module and
50+
// power your radio separately with a good regulator you can turn up PA level.
51+
//#define MY_RF24_PA_LEVEL RF24_PA_LOW/
52+
53+
// Enable serial gateway
54+
#define MY_GATEWAY_SERIAL
55+
56+
// Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
57+
//#if F_CPU == 8000000L/
58+
//#define MY_BAUD_RATE 38400/
59+
//#endif/
60+
61+
// Enable inclusion mode
62+
#define MY_INCLUSION_MODE_FEATURE
63+
// Enable Inclusion mode button on gateway
64+
//#define MY_INCLUSION_BUTTON_FEATURE
65+
66+
// Inverses behavior of inclusion button (if using external pullup)
67+
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
68+
69+
// Set inclusion mode duration (in seconds)
70+
//#define MY_INCLUSION_MODE_DURATION 60
71+
// Digital pin used for inclusion mode button
72+
//#define MY_INCLUSION_MODE_BUTTON_PIN 3
73+
74+
// Set blinking period
75+
//#define MY_DEFAULT_LED_BLINK_PERIOD 300
76+
77+
// Inverses the behavior of leds
78+
//#define MY_WITH_LEDS_BLINKING_INVERSE
79+
80+
// Flash leds on rx/tx/err
81+
// Uncomment to override default HW configurations
82+
//#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
83+
//#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
84+
//#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
85+
86+
#include <MySensors.h>
87+
88+
void setup()
89+
{
90+
// Setup locally attached sensors
91+
}
92+
93+
void presentation()
94+
{
95+
// Present locally attached sensors
96+
}
97+
98+
void loop()
99+
{
100+
// Send locally attached sensor data here
101+
}

0 commit comments

Comments
 (0)