Skip to content

Instantly share code, notes, and snippets.

@zsup
Created December 19, 2013 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsup/8034992 to your computer and use it in GitHub Desktop.
Save zsup/8034992 to your computer and use it in GitHub Desktop.
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) {
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