#include <SPI.h>
#include <Ethernet.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 100 };
byte dns[] = { 192, 168, 1, 1 };
byte gateway[] = { 192, 168, 1, 1 };
char server[] = "agnosthings.com";
EthernetClient ethClient;
// set variabel pin untuk led
const int ledPin = 13;
void setup()
{
// reset network
system("ifdown eth0");
system("ifup eth0");
delay(2000);
// connecting network
Serial.begin(9600);
Serial.println("connecting...");
if (Ethernet.begin(mac) != 1) {
Ethernet.begin(mac,ip);
}
delay(1000);
Serial.print("Running on: ");
Serial.println(Ethernet.localIP());
// initialing LED PIN as Output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
// send request
// change link with your own link
boolean requestStatus = httpRequest("/bc0dfdba-f5be-11e5-8001-XXXXXXXXXXXX/field/last/feed/00/led");
if(requestStatus == true){
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while(ethClient.available())
{
// read data from server
String line = ethClient.readString();
Serial.println(line);
// Turn On LED while data is On
if (line.indexOf("value\":\"on") != -1) {
Serial.println("Turn On LED");
digitalWrite(ledPin, HIGH);
}
// Turn Off LED while data is not On
else {
Serial.println("Turn Off LED");
digitalWrite(ledPin, LOW);
}
}
} // end if requestStatus
// close any connection before send a new request.
// This will free the socket on the Ethernet shield
ethClient.stop();
delay(1000);
}
boolean httpRequest(String link) {
// if you get a connection, report back via serial:
Serial.println("Reading data from server");
if (ethClient.connect(server, 80)) {
// Make a HTTP request:
ethClient.println("GET " + link + " HTTP/1.0");
ethClient.println("Host: agnosthings.com");
ethClient.println();
return true;
} else {
// You couldn't make the connection
Serial.println("Connection Failed");
ethClient.stop();
return false;
}
}