SHOW:
|
|
- or go back to the newest paste.
1 | // home_automation_lights.ino | |
2 | #include <SerialCommand.h> | |
3 | #include <RCSwitch.h> | |
4 | ||
5 | #define APP_SERIAL_DEBUG // comment this when in production mode | |
6 | ||
7 | const unsigned int SERIAL_PORT_BAUDRATE = 9600; // change this accordingly | |
8 | const byte RC_SWITCH_TX_PIN = 10; // change this accordingly | |
9 | char* LIGHT_ON_CODE = "000000000001010100010001"; // change this accordingly | |
10 | char* LIGHT_OFF_CODE = "000000000001010100010001"; // change this accordingly | |
11 | ||
12 | SerialCommand sCmd(Serial); // the SerialCommand object used for string parsing over serial port | |
13 | RCSwitch rcSwitch = RCSwitch(); // the RCSwitch object used for RF commands | |
14 | ||
15 | // this gets called when you send "LIGHT_ON" string on the serial port | |
16 | void lightOn() { | |
17 | #ifdef APP_SERIAL_DEBUG | |
18 | Serial.println(F("Sending LIGHT_ON command...")); | |
19 | #endif | |
20 | ||
21 | rcSwitch.send(LIGHT_ON_CODE); | |
22 | } | |
23 | ||
24 | - | Serial.begin(SERIAL_PORT_BAUDRATE); |
24 | + | |
25 | void lightOff() { | |
26 | #ifdef APP_SERIAL_DEBUG | |
27 | - | sCmd.addCommand(F("LIGHT_ON"), lightOn); // Turns Light on |
27 | + | Serial.println(F("Sending LIGHT_OFF command...")); |
28 | - | sCmd.addCommand(F("LIGHT_OFF"), lightOff); // Turns Light off |
28 | + | #endif |
29 | ||
30 | rcSwitch.send(LIGHT_OFF_CODE); | |
31 | } | |
32 | ||
33 | - | // this is just for debugging - you can comment this out in production |
33 | + | void noCmdMatched() { |
34 | Serial.println(F("Invalid command sent!")); | |
35 | } | |
36 | ||
37 | void setup() { | |
38 | Serial.begin(SERIAL_PORT_BAUDRATE); // set serial port communications baudrate | |
39 | ||
40 | // Setup callbacks for SerialCommand commands | |
41 | sCmd.addCommand(F("LIGHT_ON"), lightOn); // handler/callback setup for turning light on | |
42 | sCmd.addCommand(F("LIGHT_OFF"), lightOff); // handler/callback setup for turning light off | |
43 | sCmd.setDefaultHandler(noCmdMatched); // handler/callback setup for unknown command | |
44 | ||
45 | // Transmitter is connected to Arduino Pin RC_SWITCH_TX_PIN set up above | |
46 | rcSwitch.enableTransmit(RC_SWITCH_TX_PIN); | |
47 | ||
48 | #ifdef APP_SERIAL_DEBUG | |
49 | Serial.println(F("System ready...")); | |
50 | #endif | |
51 | } | |
52 | ||
53 | void loop() { | |
54 | sCmd.readSerial(); // process serial commands | |
55 | } |