dirakit communityHabibi Mustafa
Published © CC BY

Cara Membuat Kendali Digital Pada Intel Galileo Menggunakan

Penjelasan singkat cara membuat kendali digital di Agnothings.

IntermediateProtip1 hour40
Cara Membuat Kendali Digital Pada Intel Galileo Menggunakan

Things used in this project

Hardware components

LED (generic)
LED (generic)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Kabel LAN
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
notepad++

Story

Read more

Code

sketch

Arduino
#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;
  }
}

Credits

dirakit community

dirakit community

2 projects • 93 followers
Indonesia IoT Community
Habibi Mustafa

Habibi Mustafa

1 project • 1 follower
Dicoding Indonesia

Comments

Add projectSign up / Login