Created
July 25, 2016 08:12
-
-
Save LarsBergqvist/04a78df8f61441339f591c20dd606b39 to your computer and use it in GitHub Desktop.
Digispark USB board sketch for sending an 433MHz RF signal when a door is opened
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A sketch for a DigiSpark USB device that detects when a door is opened | |
// The door should have a reed switch with NC (normally closed) connected to the Vin wire of the device | |
// When the door is opened, the reed switch is closed and the device is started | |
// A 32-nit message is then sent via a 433 MHz signal that can be picked up by an appropriate receiver | |
// (a Raspberry Pi in my case). | |
// When the door is closed, the reed switch is opened and the device is shut down | |
// | |
// Depends on the RCSwitch library https://github.com/sui77/rc-switch | |
// and the eeprom-library | |
// | |
#include "RCSwitch.h" | |
#include <EEPROM.h> | |
// Unique ID:s (4 bits, 0-15) for each measurement type so that the receiver | |
// understands how to interpret the data on arrival | |
#define DOOR_MEASUREMENT_ID 4 | |
#define TX_PIN 1 // PWM output pin to use for transmission | |
// A rolling sequence number for each measurement so that the receiver can handle duplicate data | |
// Restarts at 0 after seqNum=15 has been used. Stored in EEPROM between restarts of the device. | |
byte seqNum=0; | |
RCSwitch transmitter = RCSwitch(); | |
void setup() | |
{ | |
if ( EEPROM.read(0) == 42 ) | |
{ | |
// We have stored the previously used sequence number in EEPROM | |
seqNum = EEPROM.read(1); | |
} | |
seqNum++; | |
if (seqNum > 15) | |
{ | |
seqNum = 0; | |
} | |
EEPROM.write(0,42); | |
EEPROM.write(1,seqNum); | |
transmitter.enableTransmit(TX_PIN); | |
transmitter.setRepeatTransmit(25); | |
} | |
bool valueHasBeenSent = false; | |
void loop() | |
{ | |
if (!valueHasBeenSent) | |
{ | |
// Send the message several times to increase | |
// detection by the receiver | |
sendDoorOpenSignal(seqNum); | |
delay(2000); | |
sendDoorOpenSignal(seqNum); | |
delay(2000); | |
sendDoorOpenSignal(seqNum); | |
delay(2000); | |
valueHasBeenSent = true; | |
} | |
} | |
void sendDoorOpenSignal(int sequenceNumber) | |
{ | |
// use alternating bits for the value for better reliability | |
unsigned long valueToSend = 0b0101010; | |
unsigned long dataToSend = code32BitsToSend(DOOR_MEASUREMENT_ID,sequenceNumber,valueToSend); | |
transmitter.send(dataToSend, 32); | |
} | |
unsigned long code32BitsToSend(int measurementTypeID, unsigned long seq, unsigned long data) | |
{ | |
unsigned long checkSum = measurementTypeID + seq + data; | |
unsigned long byte3 = ((0x0F & measurementTypeID) << 4) + (0x0F & seq); | |
unsigned long byte2_and_byte_1 = 0xFFFF & data; | |
unsigned long byte0 = 0xFF & checkSum; | |
unsigned long dataToSend = (byte3 << 24) + (byte2_and_byte_1 << 8) + byte0; | |
return dataToSend; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment