ho provato a connettermi al client con questa sketch, ma funziona solo in parte:
#include <SPI.h>
#include <Ethernet.h>
#include <Client.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
// il MAC Address lo posso inventare
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,108 };
byte server[] = { 192,168,0,107 }; // mio computer
Client client(server, 80);
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 7; // the number of the LED pin
// variables will change:
int buttonState = 0;
int buttonStateOld = 0; // variable for reading the pushbutton status
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// inizializza la libreria seriale:
Serial.begin(9600);
//inizializza la Ethernet shield, dopo un secondo:
delay(1000);
Serial.println("connecting...");
// se ottiene una connessione, stampa via seriale:
if (client.connect())
Serial.println("connected");
else {
// se non si connette al server:
Serial.println("connection failed");
}
}
void loop()
{buttonStateOld=buttonState;
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState != buttonStateOld)
{
if (buttonState == HIGH){
client.connect();//connette il client
Serial.println("connesso"); //stampa su seriale;
client.println("POST /led=1");//invia il comando "accendi led";
client.println();
Serial.println("led acces");
client.stop();
// turn LED on:
digitalWrite(ledPin, HIGH);
} }
else
if (buttonState == LOW){
!client.connect();
Serial.println("connesso"); //stampa su seriale;
client.println("POST /led=0");//invia il comando "spegni led";
client.println();
Serial.println("post led spento");
client.stop();
// turn LED off:
digitalWrite(ledPin, LOW);// accendo un led spia su client;
Serial.println("led spento");//stampa su seriale;
}delay (2000);//end delay before loop starts again
}
nel caso in cui il pin è HIGH, si connette e stampa LED=1, nel caso il pin è LOW si connette e stampa in continuazione LED=0. Come faccio a far stampare LED=0 solo una volta finchè il pin diventa di nuovo HIGH? grazie, a presto