Skip to content

Instantly share code, notes, and snippets.

@ktknest
Created December 24, 2014 15:09
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 ktknest/8fe3afd0f88b92620001 to your computer and use it in GitHub Desktop.
Save ktknest/8fe3afd0f88b92620001 to your computer and use it in GitHub Desktop.
TCPClient client;
char server[] = "MY_SERVER_HOST";
const int SENSOR_PIN = A7;
int value = 0;
boolean isPush = false;
void setup() {
Serial.begin( 9600 );
Serial.println("Serial OK");
}
void loop() {
value = analogRead(SENSOR_PIN); // 0 - 4095
if (value < 4000) {
Serial.println(value);
if (isPush == false) {
isPush = true;
// API実行
message();
}
} else if (isPush == true) {
isPush = false;
Serial.println("Released");
}
delay(100);
}
void message() {
Serial.println("## connecting...");
if (client.connect(server, 80))
{
Serial.println("connected");
client.println("GET /MY_API_URL HTTP/1.1");
client.println("Host: MY_SERVER_HOST");
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("Disconnecting.");
}
else
{
Serial.println("connection failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment