Arduino Ethernet Shield kullanarak arduino kartınızı birkaç dakika içerisinde internete bağlayabilirsiniz. Ethernet Shield’i arduino üzerine takınız, ve RJ45 kablosunu da ethernet shield’e takınız, birkaç adımı takip ederek arduino’yu internet üzerinden kontrol etmeye başlayabilirsiniz.
Arduino, Ethernet Shield işlemcisi olan Wiznet W5100 ve SD kart arduino ile SPI hattı üzerinden haberleşmektedir. Bu pinler UNO’da 11,12 ve 13, MEGA’da 50,51 ve 52. pinlerdir. Her iki modelde de 10. pin W5100’ün, 4. pin ise SD kartın seçim pinidir. Bu pinler genel giriş çıkış pini olarak kullanılmazlar. Arduino MEGA’da donanımsal SS pini (53. pin) SD kart ya da W5100 için kullanılmamaktadır. Fakat bu pin mutlaka çıkış pini olarak ayarlanmalıdır.
Unutulmaması gereken önemli bir nokta W5100 ve SD kart aynı SPI hattını kullandığından aynı anda sadece biri aktif olmaktadır. Eğer iki haberleşme aynı anda yapılmak istenirse kullanılan kütüphanenin ortak olmasına dikkat edilmelidir. Aynı anda ikisini kullanmayacaksanız kullanmadığınız kısmı pasif etmeniz gerekmektedir. SD kartı pasif etmek için 4. pini çıkış olarak gösterip bu pinden 5V (high) çıkış vermeniz gerekmektedir. W5100’ü pasif hale getirmek için ise 10. pini çıkış olarak gösterip 5V (high) çıkış vermeniz gerekmektedir.
Arduino Ethernet Modülü ile IP üzerinden Röle Kontrolü
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 111);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Relay state and pin
String relay1State = "Off";
const int relay = 7;
// Client variables
char linebuf[80];
int charcount=0;
void setup() {
// Relay module prepared
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// Open serial communication at a baud rate of 9600
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
// Display dashboard page with on/off button for relay
// It also print Temperature in C and F
void dashboardPage(EthernetClient &client) {
client.println("<!DOCTYPE HTML><html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");
client.println("<h3>Arduino Web Server - <a href=\"/\">Refresh</a></h3>");
// Generates buttons to control the relay
client.println("<h4>Relay 1 - State: " + relay1State + "</h4>");
// If relay is off, it shows the button to turn the output on
if(relay1State == "Off"){
client.println("<a href=\"/relay1on\"><button>ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay1State == "On"){
client.println("<a href=\"/relay1off\"><button>OFF</button></a>");
}
client.println("</body></html>");
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// 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) {
dashboardPage(client);
break;
}
if (c == '\n') {
if (strstr(linebuf,"GET /relay1off") > 0){
digitalWrite(relay, HIGH);
relay1State = "Off";
}
else if (strstr(linebuf,"GET /relay1on") > 0){
digitalWrite(relay, LOW);
relay1State = "On";
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
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");
}
}
İncelemeler
Henüz inceleme yapılmadı.