Guten Morgen,
ich benötige für mein Projekt eine Funktion mit der ich prüfen kann ob ein Lokaler Router oder Switch im Netzwerk verfügbar ist. Mein Gedanke war die ICMP Ping Library zu benutzen doch diese funktioniert bei mir leider nicht. Dann habe ich gedacht, dass ich einfach das Webclientrepeating sketch umformen kann und statt des Get requests einfach nur versuche die Verbindung herzustellen und dann sofort zu "closen".
Das Sketch hat ja sogar schon die if abfrage ob die Verbindung zum Server hergestellt werden konnte oder nicht. Doch leider funktioniert es nicht so wie gehofft. Ich habe die lokale ip eines Routers eingetragen (mit dieser ip kommt man auch auf die Konfigurationsseite) doch der Arduino kann Anscheinend keine Verbindung zum Router herstellen. Ich habe die DNS Konfiguration ausgeschaltet da ich ja nur lokal unterwegs bin. Die IP habe ich auch ausgeschaltet, da der DHCP Server mir immer die gleiche IP gibt (durch gleiche mac adresse).
Hier der Sketch
/*
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://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet2.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(10,10,5,7);
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 Leonardo 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);
// 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 PUT request:
// client.println("GET /general/status.html HTTP/1.0");
client.println("Host: 10.10.5.7");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
Serial.println("Verbindung zu Router: 1 möglich.");
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
Serial.println("Keine Verbindung zu Router: 1 möglich.");
}
}
Wäre Dankbar für Vorschläge und Lösungsansätze