Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created December 17, 2010 13:26
Show Gist options
  • Save laclefyoshi/744925 to your computer and use it in GitHub Desktop.
Save laclefyoshi/744925 to your computer and use it in GitHub Desktop.
Arduino code for posting messages to im.kayac.com
/**
Copyright: (c) SAEKI Yoshiyasu
License : MIT-style license
<http://www.opensource.org/licenses/mit-license.php>
last updated: 2010/12/03
**/
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
byte mac[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
byte ip[] = {192, 168, 254, 100};
byte imkayaccom_server[] = {202, 218, 201, 26};
char imkayaccom_username[] = "YOUR_USERNAME";
char imkayaccom_password[] = "YOUR_PASSWORD";
char message_body[256];
char message[200];
char message_length[4];
Client client(imkayaccom_server, 80);
void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
if (client.connect()) {
client.print("POST /api/post/");
client.print(imkayaccom_username);
client.println(" HTTP/1.0");
client.println("Host: im.kayac.com");
client.println("Content-Type: application/x-www-form-urlencoded");
strcat(message_body, "message=");
urlencode(message, "Hello. I'm your Arduino.");
strcat(message_body, message);
strcat(message_body, "&password=");
strcat(message_body, imkayaccom_password);
sprintf(message_length, "%d", strlen(message_body));
client.print("Content-Length: ");
client.println(message_length);
client.println();
client.println(message_body);
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
void urlencode(char* dest, char* src) {
int i;
for(i = 0; i < strlen(src); i++) {
char c = src[i];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~') {
char t[2];
t[0] = c; t[1] = '\0';
strcat(dest, t);
} else {
char t[4];
snprintf(t, sizeof(t), "%%%02x", c & 0xff);
strcat(dest, t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment