facebook pixel מדריך: ספריה - Ethernet - דוגמה - WebClientRepeating - www.4project.co.il
Main logo www.4project.co.il
כל הרכיבים לפרוייקט שלכם
עגלת קניות

העגלה ריקה

לקוחות נכבדים, אלה שעות הפעילות של המחסן במהלך פסח 2024:
ערב חג וחג ראשון (22-23/04) - המחסן סגור
חול המועד (24-25/04) - המחסן יפעל בין 8:00 עד 15:00
ערב חג וחג שני (28-29/04) - המחסן סגור
נחזור לפעילות רגילה ביום שלישי 30/04
חג שמח!

ספריה - Ethernet - דוגמה - WebClientRepeating


2023-06-26 16:25:12
WebClientRepeating מדגים איך לייצר בקשת HTTP שחוזרות על עצמן בעזרת מגן Ethernet. דוגמה זו משתמשת ב-DNS, היא משייכת ל-Client את כתובת MAC, כתובת IP וגם כתובת DNS. הקוד פונה ל- http://www.arduino.cc/latest.txt ומדפיס את התוכן בחיבור ה-Serial.

ציוד נדרש

כרטיס פיתוח Arduino.
מגן Ethernet

מעגל

מגן ה-Etheret מאפשר לכם לחבר את בקר ה-Ethernet של חברת WizNet לכרטיס ה-Arduino דרך ערוץ תקשורת SPI. המגן משתמש בקווים 10, 11, 12 ו-13 לתקשורת SPI עם בקר של WizNet. דגמים האחרונים של מגן ה-Ethernet כוללים גם חיבור לכרטיס זכרון microSD. בכרטיסים אלה קו מספר 4 משמש לבחירת הרכיב המתקשר על המגן (Slave Select).

מגן ה-Ethernet צריך להיות מורכב על כרטיס הארדואינו ומחובר לרשת ה-Ethernet. תצטרכו לשנות את הגדרות הרשת בתוכנה כדי להתאים אותם לרשת שלכם.

שרטוט

אין צורך בשרטוט לדוגמה זו.

קוד

קוד: בחר הכל
/*
  Repeating Web client

This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.

This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 19 Apr 2012
by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati

http://www.arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.

*/

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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 1, 177);

// fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}



ראו גם:

מדריך לתחילת העבודה עם מגן Ethernet (אנגלית) - TODO
ספריית Ethernet
ChatServer - יצירת שרת צ'ט פשוט
AdvancedChatServer - שרת שמעביר את הנתונים לכל הלקוחות המחוברים חוץ מזה ששולח אותם
WebClient - שולח בקשות HTTP
WebServer - שרת WEB פשוט שמציג עמוד HTML עם ערך של חיישן אנלוגי
BarometricSensorWebServer - מציג תוצאות של חיישן לחץ ברומטרי כעמוד WEB
UDPSendReceiveString - קבלה ומשלוח הודעות טקסט דרך UDP
UdpNtpClient - קבלת זמן משרת NTP (שרת Network Time Protocol)
DnsWebClient - צד ב-Client שמבוסס על DHCP ו-DNS
DhcpChatServer - שרת צ'ט פשוט שמבוסס על DHCP
DhcpAddressPrinter - מקבל כתובת דרך DHCP ומדפיס אותה
TelnetClient - צד ה-Client פשוט לחיבור ל-Telnet



פירוט שפת תכנות לסביבת Arduino


עמוד זה הוא תרגום של WebClientRepeating לפי רישיון Creative Commons Attribution-ShareAlike 3.0.