Created
December 19, 2013 05:41
-
-
Save zsup/8034902 to your computer and use it in GitHub Desktop.
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
TCPClient client; | |
void setup() { | |
Serial.begin(9600); | |
Spark.function("connect", connect_to_my_server); | |
for (int pin = D0; pin <= D7; ++pin) { | |
pinMode(pin, OUTPUT); | |
} | |
} | |
void loop() { | |
if (client.connected()) { | |
if (client.available()) { | |
char pin = client.read(); | |
char level = client.read(); | |
digitalWrite(pin - '0' + D0, 'h' == level ? HIGH : LOW); | |
} | |
} | |
} | |
int connect_to_my_server(String ip) { | |
byte server_address[4]; | |
ip_array_from_string(server_address, ip); | |
if (client.connect(server_address, 9000)) { | |
return 1; // successfully connected | |
} else { | |
return -1; // failed to connect | |
} | |
} | |
void ip_array_from_string(byte ip_array[], String ip_string) { | |
String first = ip_string.substring(0, ip_string.indexOf('.')); | |
String remaining1 = ip_string.substring(ip_string.indexOf('.')+1); | |
String second = remaining1.substring(0, remaining1.indexOf('.')); | |
String remaining2 = remaining1.substring(remaining1.indexOf('.')+1); | |
String third = remaining2.substring(0, remaining2.indexOf('.')); | |
String fourth = remaining2.substring(remaining2.lastIndexOf('.')+1); | |
ip_array[0] = first.toInt(); | |
ip_array[1] = second.toInt(); | |
ip_array[2] = third.toInt(); | |
ip_array[3] = fourth.toInt(); | |
// This is how this should work, but it's not compiling. I added it to Asana. | |
// | |
// for (int i = 0; i < 4; i++) { | |
// ip_array[i] = ip_string.substring(0, ip_string.indexOf('.')).toInt(); | |
// ip_string = ip_string.substring(ip_string.indexOf('.') + 1); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment