Ciao, ho un problema con una MEGA ADK e l'ETHERNET SHIELD. Devo controllare con questa scheda un ventola, un led e un servo motore via internet. Per i primi 2 minuti sembra funzionare tutto correttamente. Dopo si blocca e non riceve piu nulla e l'IDE non riconosce piu la seriale attaccata. Posto il codice perchè magari mi sono dimenticato qualcosa.
void setup(){
Serial.begin( 9600 );
Serial.println("Start");
//CONFIGURAZIONE ETHERNET
Serial.println("...Configuro la connessione ethernet...");
Ethernet.begin(mac); //configuro lo shield ethernet
Serial.println("Connessione Configurata.");
delay(1000);//aspetto un secondo per far avviare lo shield ethernet
Serial.println("Programma Avviato, Setup Terminato!");
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
server.begin();
client.flush();
// initialize the FAN pin as an output:
pinMode(fanPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinFan, INPUT);
// initialize the LED pin as an output:
pinMode(LedPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinLed, INPUT);
myservo.attach(3); // attaches the servo on pin 3 to the servo object
delay(2000);
}
void loop(){
time = millis();
setServo();
setFan();
setLed();
client = server.available();
if (client)
{
Serial.println("Request incoming..");
WaitForRequest(client);
ParseReceivedRequest();
PerformRequestedCommands();
Serial.println("Richiesta servita");
delay(100);
client.flush();
client.stop();
Serial.flush();
delay(100);
}
delay(50);
Serial.flush();
}
//CONTROLLO IL POTENZIOMETRO E SETTO IL SERVOMOTORE
void setServo(){
valServo = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
valServo = map(valServo, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(valServo); // sets the servo position according to the scaled value
delay(15);
}
//CON IL BOTTONE VADO AD AZIONE LE VENTOLE
void setFan(){
valFan = digitalRead(buttonPinFan);
//check if input is HIGH
if ((valFan == HIGH) && (old_val_Fan == LOW)) {
buttonFan = 1 - buttonFan;
delay(50);
}
old_val_Fan = valFan;
if (buttonFan == 1) {
// turn LED on:
digitalWrite(fanPin, HIGH);
}
else {
// turn LED off:
digitalWrite(fanPin, LOW);
}
}
//CON IL BOTTONE VADO AD AZIONARE IL LED
void setLed(){
valLed = digitalRead(buttonPinLed);
//check if input is HIGH
if ((valLed == HIGH) && (old_val_Led == LOW)) {
buttonLed = 1 - buttonLed;
delay(50);
}
old_val_Led = valLed;
if (buttonLed == 1) {
// turn LED on:
digitalWrite(LedPin, HIGH);
}
else {
// turn LED off:
digitalWrite(LedPin, LOW);
}
}
//-- Commands and parameters (sent by browser) --
char cmd[15]; // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];
void WaitForRequest(EthernetClient client) // Sets buffer[] and bufferSize
{
bufferSize = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n')
break;
else
if (bufferSize < bufferMax)
buffer[bufferSize++] = c;
else
break;
}
}
PrintNumber("bufferSize", bufferSize);
}
void ParseReceivedRequest()
{
Serial.println("in ParseReceivedRequest");
Serial.println(buffer);
//Received buffer contains "GET /cmd/param1/param2 HTTP/1.1". Break it up.
char* slash1;
char* slash2;
char* slash3;
char* space2;
slash1 = strstr(buffer, "/") + 1; // Look for first slash
slash2 = strstr(slash1, "/") + 1; // second slash
slash3 = strstr(slash2, "/") + 1; // third slash
space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
if (slash3 > space2) slash3=slash2;
PrintString("slash1",slash1);
PrintString("slash2",slash2);
PrintString("slash3",slash3);
PrintString("space2",space2);
// strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
cmd[0] = 0;
param1[0] = 0;
param2[0] = 0;
strncat(cmd, slash1, slash2-slash1-1);
strncat(param1, slash2, slash3-slash2-1);
strncat(param2, slash3, space2-slash3-1);
PrintString("cmd",cmd);
PrintString("param1",param1);
PrintString("param2",param2);
}
void PerformRequestedCommands()
{
if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
if ( strcmp(cmd,"digitalRead") == 0 ) RemoteDigitalRead();
if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
if ( strcmp(cmd,"analogWrite") == 0 ) RemoteAnalogWrite();
}
void RemoteDigitalWrite()
{
int ledPin = param1[0] - '0'; // Param1 should be one digit port
int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
digitalWrite(ledPin, ledState);
if(ledPin==fanPin) buttonFan = 1 - buttonFan;
if(ledPin==LedPin) buttonLed = 1 - buttonLed;
//-- Send response back to browser --
server.print("D");
server.print(ledPin, DEC);
server.print(" is ");
server.print( (ledState==1) ? "ON" : "off" );
//-- Send debug message to serial port --
Serial.println("RemoteDigitalWrite");
PrintNumber("ledPin", ledPin);
PrintNumber("ledState", ledState);
}
void RemoteDigitalRead()
{
int ledPin = param1[0] - '0'; // Param1 should be one digit port
int ledState; // Param2 should be either 1 or 0
ledState = digitalRead(ledPin);
//-- Send response back to browser --
server.print("D");
server.print(ledPin, DEC);
server.print(" is ");
server.print( (ledState==1) ? "ON" : "off" );
//-- Send debug message to serial port --
Serial.println("RemoteDigitalRead");
PrintNumber("ledPin", ledPin);
PrintNumber("ledState", ledState);
}
void RemoteAnalogRead()
{
// If desired, use more server.print() to send http header instead of just sending the analog value.
int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
int analogValue = analogRead(analogPin);
//-- Send response back to browser --
server.print("A");
server.print(analogPin, DEC);
server.print(" is ");
server.print(analogValue,DEC);
//-- Send debug message to serial port --
Serial.println("RemoteAnalogRead");
PrintNumber("analogPin", analogPin);
PrintNumber("analogValue", analogValue);
}
void RemoteAnalogWrite()
{
// If desired, use more server.print() to send http header instead of just sending the analog value.
int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
int analogValue = param2[0] - '0';
if(analogPin!=3) analogWrite(analogPin, analogValue);
else{
myservo.write((analogValue*20)-1);
delay(15);
}
//-- Send response back to browser --
server.print("A");
server.print(analogPin, DEC);
server.print(" is ");
server.print(analogValue,DEC);
//-- Send debug message to serial port --
Serial.println("RemoteAnalogWrite");
PrintNumber("analogPin", analogPin);
PrintNumber("analogValue", analogValue);
}
void PrintString(char* label, char* str)
{
Serial.print(label);
Serial.print("=");
Serial.println(str);
}
void PrintNumber(char* label, int number)
{
Serial.print(label);
Serial.print("=");
Serial.println(number, DEC);
}