Hi, trying to make a Arduino to reset the router plus having it store a website where it shows temp off the room.
But i having some problems understanding "Ethernetserver" "Ethernetclient" what the difference?
This is the Router reset part. Not my code just copy past.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,22);
char server[] = "www.google.com";
unsigned long lastConnectionTime = 0;
#define postingInterval 10000
boolean canConnect = false;
boolean canConnect_prev = false;
#define redLed 3
#define greenLed 5
#define yellowLed 6
#define routerPin1 8
#define routerPin2 9
#define resetRouterTime 30000
EthernetClient client;
void setup() {
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");
}
// Initial LEDs
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(routerPin1, OUTPUT);
pinMode(routerPin2, OUTPUT);
digitalWrite(routerPin1, HIGH);
digitalWrite(routerPin2, HIGH);
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
digitalWrite(yellowLed, HIGH);
delay(1000);
connect_to_server();
digitalWrite(yellowLed, LOW);
}
if(canConnect) {
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
}
else {
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
resetRouter();
}
}
void connect_to_server(void) {
if (client.connect(server, 80)) {
canConnect = true;
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
client.stop();
canConnect = false;
}
lastConnectionTime=millis();
}
void resetRouter(void) {
digitalWrite(routerPin1, LOW);
digitalWrite(routerPin2, LOW);
delay(resetRouterTime);
digitalWrite(routerPin1, HIGH);
digitalWrite(routerPin2, HIGH);
delay(resetRouterTime);
}
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
Where should i put this code so it fit the other one?
Thanks.