codingan untuk arduino shield board



ini adalah sorce code untuk arduino shield
untuk penjelasan tentang arduino shield bisa kunjungi youtube chanel

selamat membaca dan jangan lupa share ya :D


untuk web client
*/

#include <SPI.h>
#include <Ethernet.h>

// Masukkan alamat MAC untuk pengontrol Anda di bawah ini.

//Perisai Ethernet yang lebih baru memiliki alamat MAC yang tercetak pada stiker pada perisai
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//Jika Anda tidak ingin menggunakan DNS (dan kurangi ukuran sketsa Anda)

// Gunakan IP numerik bukan nama untuk server:
//IPAddress server (74,125,232,128); // IP numerik untuk Google (tidak ada DNS)Server char [] = "www.google.com"; // alamat nama untuk Google (menggunakan DNS)

// Tetapkan alamat IP statis untuk digunakan jika DHCP gagal ditetapkan 

Ip IPAddress (192,168,0,177);

// Inisialisasi pustaka klien Ethernet 
// dengan alamat IP dan port server 
// yang ingin Anda hubungkan (port 80 adalah default untuk HTTP): 
Klien EthernetClient;


void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}


untuk web server

 */

#include <SPI.h>
#include <Ethernet.h>

// Masukkan alamat MAC dan alamat IP untuk pengontrol Anda di bawah ini.
// Alamat IP akan tergantung pada jaringan lokal Anda:
Byte mac [] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
Ip IPAddress (192,168,1,177);

/ / Inisialisasi perpustakaan server Ethernet
// dengan alamat IP dan port yang ingin Anda gunakan
// (port 80 adalah default untuk HTTP):
Server EthernetServer (80);
void setup() { // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
      client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");      
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

sekian mungkin segitu saja sharing saya yang saya tau begitu...wkwkwkwk :D

semoga bermanfaat
Artikel Selanjutnya Artikel Sebelumnya
Belum Ada Komentar : Pejalan Wisata
Tambahkan Komentar
Comment url
Post Terkait :
arduino
close