Arduino Lato Client
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <WebServer.h>
#define BUTTONON 7 // PIN7 INPUT = BUTTON
#define BUTTONOFF 6 // PIN7 INPUT = BUTTON
int ts1st = HIGH; // ts1st LEGGE E MEMORIZZA IL LIVELLO DI BUTTON
int stato1 = HIGH; // stato1 MANTIENE LO STATO DI BUTTON
int ts2st = HIGH; // ts2st LEGGE E MEMORIZZA IL LIVELLO DI BUTTON
int stato2 = HIGH; // stato2 MANTIENE LO STATO DI BUTTON
int i = 0; // i CONTA LE VOLTE DI BUTTON ON PUSH
int j = 0; // j CONTA LE VOLTE DI BUTTON OFF PUSH
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x47 }; // MAC ETHERNET SHIELD
byte ip[] = { 10, 10, 11, 16 }; // IP CLIENT
byte server[] = { 10, 10, 11, 45 }; // IP SERVER
byte gateway[] = { 10, 10, 11, 1 }; // INDIRIZZO ROUTER GATEWAY
byte subnet[] = { 255, 255, 255, 0 }; // SUBNET
EthernetClient client;
void setup() {
Ethernet.begin(mac, ip, gateway, subnet); // INIZIALIZZA ETHERNET
Serial.begin(9600); // INIZIALIZZA LA SERIALE
pinMode(BUTTONON, INPUT); // IMPOSTA IL BUTTONON COME INPUT
pinMode(BUTTONOFF, INPUT); // IMPOSTA IL BUTTONOFF COME INPUT
delay(150); // RITARDO DI 150ms
}
void loop() {
ts1st = digitalRead(BUTTONON); // LEGGE E MEMORIZZA IL VALORE DI BUTTONON
ts2st = digitalRead(BUTTONOFF); // LEGGE E MEMORIZZA IL VALORE DI BUTTONOFF
if ( ts1st == HIGH && stato1 == HIGH );
else{
if ( ts1st == LOW && stato1 == HIGH ){ // PULSANTE PUSH FASE ON
stato1 = LOW; // IMPOSTA IL NUOVO STATO = LOW
delay(150); // RITARDO 150ms
i++; // INCREMENTA CONTEGGIO PUSH
Serial.print(" IL BOTTONE ON E' STATO PREMUTO "); // STAMPA IL NUMERO DI PUSH EFFETTUATI
Serial.print(i);
Serial.print(" VOLTE");
Serial.println();
if (client.connect(server, 23)){
client.print("ON ");
client.stop();
}
else{
Serial.println("Connessione ON fallita"); // Altrimenti
// Stampa su COM3 Connessione ON fallita
client.stop(); // Chiudi connessione
}
}
if ( ts1st == HIGH && stato1 == LOW ) // POSIZIONE DI RIPOSO FASE OFF
stato1 = HIGH; // AGGIORNA IL NUOVO STATO DI BUTTONON = HIGH
}
if ( ts2st == HIGH && stato2 == HIGH );
else{
if ( ts2st == LOW && stato2 == HIGH ){ // PULSANTE PUSH FASE ON
stato2 = LOW; // IMPOSTA IL NUOVO STATO = LOW
delay(150); // RITARDO 150ms
j++; // INCREMENTA CONTEGGIO PUSH
Serial.print(" IL BOTTONE OFF E' STATO PREMUTO "); // STAMPA IL NUMERO DI PUSH EFFETTUATI
Serial.print(j);
Serial.print(" VOLTE");
Serial.println();
if (client.connect(server, 23)){
client.print("OFF");
client.stop();
}
else{
Serial.println("Connessione OFF fallita"); // Altrimenti
// Stampa su COM3 Connessione ON fallita
client.stop(); // Chiudi connessione
}
}
if ( ts2st == HIGH && stato2 == LOW ) // POSIZIONE DI RIPOSO FASE OFF
stato2 = HIGH; // AGGIORNA IL NUOVO STATO DI BUTTON = HIGH
}
}
Arduino Lato Server
#include <SPI.h> // Importa Libreria SPI
#include <Ethernet.h> // Importa Libreria
// Ethernet
#define LED 7 // Porta digitale 7 LED
char comando[3] ;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Ethernet Shield del Server
byte ip[] = { 10, 10, 11, 17 }; // IP Arduino Server
EthernetServer server = EthernetServer(23); // Telnet porta 23
EthernetClient client = 0; // Inizializza il Client
void setup()
{
pinMode( LED, OUTPUT ); // Imposta LED OUTPUT
Ethernet.begin(mac, ip); // Inizializza Ethernet
server.begin();
}
void loop()
{
while ( client.connected() && server.available() >= 3 ) { // Client-To-Serve
char c = client.read(); // Legge i Byte su Client
comando[0] = c;
c = client.read(); // Legge i Byte su Client
comando[1] = c;
c = client.read(); // Legge i Byte su Client
comando[2] = c;
}
if ( comando[0] == 'O' && comando[1] == 'N' && comando[2] == ' ' )
digitalWrite( LED, HIGH );
if ( comando[0] == 'O' && comando[1] == 'F' && comando[2] == 'F' )
digitalWrite( LED, LOW );
}
Si Lesto non volevo dire che avevo fatto una cosa azzeccata anzi una sciocchezza enorme ho fatto, ma continua a non accendermi il LED messo sul PIN 7 del mio arduino Server
Grazie