Created
June 25, 2014 16:25
-
-
Save tprynn/4d491a7676f607fdc9f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/********** | |
* SmartThings Arduino Shield On/Off | |
* For use on Arduino Mega | |
* author: github.com/tprynn | |
* Attach Shield normally with switch on 2/3. | |
* Jumper pins: | |
* - 18 (Tx1) to Shield 3 (Rx) | |
* - 19 (Rx1) to Shield 2 (Tx) | |
**********/ | |
#define BUF_SIZE 512 | |
void setup() { | |
delay(1500); | |
Serial.begin(115200); | |
Serial1.begin(2400); | |
} | |
byte buffer[BUF_SIZE]; | |
int bytes; | |
void loop() { | |
if(Serial.available()) { | |
memset(buffer, 0, BUF_SIZE); | |
bytes = Serial.readBytes((char *)buffer, BUF_SIZE-1); | |
// local echo: | |
Serial.write(buffer, bytes); | |
Serial1.write(buffer, bytes); | |
} | |
if(Serial1.available()) { | |
memset(buffer, 0, BUF_SIZE); | |
bytes = Serial1.readBytes((char *)buffer, BUF_SIZE-1); | |
Serial.write(buffer, bytes); | |
String message = parse(buffer); | |
if(message != "") | |
handle(message); | |
} | |
} | |
void hexdump(byte * buffer, int length) { | |
for(int i = 0; i < length; i++) { | |
if(buffer[i] < 0x10) Serial.print('0'); | |
Serial.print(buffer[i], HEX); | |
Serial.print(' '); | |
if((i+1) % 16 == 0) Serial.print('\n'); | |
} | |
} | |
String parse(byte * message) { | |
if(strncmp((char *)message, "\r\nT00000000:RX", 14) == 0) { | |
int plStart = 0; | |
while(message[plStart] != '[') | |
plStart++; | |
plStart++; | |
int plEnd = plStart; | |
while(message[plEnd] != ']') | |
plEnd++; | |
char buf[plEnd-plStart+1]; | |
memset(buf, 0, plEnd-plStart+1); | |
for(int i = plStart; i < plEnd; i++) { | |
buf[i - plStart] = message[i]; | |
} | |
String payload = ""; | |
char * current = buf; | |
char c; | |
// This code is not robust - parsing strings in C is awful | |
while(*current != '\0') { | |
c = (char)(strtoul(current, NULL, 16)); | |
payload += c; | |
//Serial.println(current); | |
current += 2; | |
while(*current == ' ') | |
current++; | |
} | |
return payload; | |
} | |
return ""; | |
} | |
// Code repurposed from SmartThings library | |
void send(String message) { | |
Serial.println("Sending: " + message); | |
Serial1.print("raw 0x0 { 00 00 0A 0A "); | |
for(int i = 0; i < message.length(); i++) | |
{ | |
char c = message[i]; | |
Serial1.print(c, HEX); | |
Serial1.print(' '); | |
} | |
Serial1.print('}'); | |
Serial1.print('\n'); | |
Serial1.print("send 0x0 1 1"); | |
Serial1.print('\n'); | |
} | |
void handle(String message) { | |
Serial.println("Received: " + message); | |
if(message == "\non") { | |
Serial.println("-> on"); | |
send("on"); | |
} | |
if(message == "\noff") { | |
Serial.println("-> off"); | |
send("off"); | |
} | |
if(message == "\nhello") { | |
Serial.println("-> hello"); | |
send("colors!"); | |
delay(500); | |
send("fancy"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment