Ultima modifica della giornata e poi basta!
Creazione della pagina htm su micro sd della shield e accensione/spegnimento led da tasti.
Codice Arduino (con lettura dalla sd)
/*
Vins
24-05-2013
*/
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <SD.h>
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
/*
byte ip[] = { 192, 168, 0, 110 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
*/
EthernetServer server(80);
int ledPin2 = 2; // LED pin2
int ledPin3 = 3; // LED pin3
int ledPin5 = 5; // LED pin5
String readString = String(30);
String elaboraString = String(30);
String fineString = String(30);
String onoff;
String pin;
int numPin;
int val;
char carray[3];
File webFile;
boolean incoming = 0;
/*
URL DI ESEMPIO - LED SUI PIN 2,3,5
indirizzoIp/?LED=102 (Accende Led sul pin 2)
indirizzoIp/?LED=002 (Spegne Led sul pin 2)
indirizzoIp/?LED=103 (Accende Led sul pin 3)
indirizzoIp/?LED=003 (Spegne Led sul pin 3)
indirizzoIp/?LED=105 (Accende Led sul pin 5)
indirizzoIp/?LED=005 (Spegne Led sul pin 5)
*/
void setup(){
//DHCP
Ethernet.begin(mac);
Serial.begin(9600);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
Serial.println(Ethernet.localIP());
}
void loop(){
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(readString.length() < 100){
readString += c;
}
Serial.print(c);
if (c == '\n') {
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
if (readString.indexOf("?LED=") <0){
//Niente
}
else{
int val = readString.indexOf("?LED=");
elaboraString = readString.substring(val+5,val+8);
onoff = elaboraString.substring(0,1);
pin = elaboraString.substring(1,3);
//Serve per modificare la stringa pin in un intero
pin.toCharArray(carray, sizeof(carray));
numPin = atoi(carray);
if(onoff == "0"){
digitalWrite(numPin, LOW);
}
else if(onoff == "1"){
digitalWrite(numPin, HIGH);
}
}
/*
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
*/
readString="";
client.stop();
}
}
}
}
}
Codice paginetta htm (con jquery)
<!DOCTYPE html>
<html>
<head>
<title>Vins - ArduinoLed</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
$(function(){
$("#led2on").button().click(function(){
alert("Acceso led PIN2");
$(location).attr("href","?LED=102");
});
$("#led2off").button().click(function(){
alert("Spento led PIN2");
$(location).attr("href","?LED=002");
});
$("#led3on").button().click(function(){
alert("Acceso led PIN3");
$(location).attr("href","?LED=103");
});
$("#led3off").button().click(function(){
alert("Spento led PIN2");
$(location).attr("href","?LED=003");
});
$("#led5on").button().click(function(){
alert("Acceso led PIN5");
$(location).attr("href","?LED=105");
});
$("#led5off").button().click(function(){
alert("Spento led PIN5");
$(location).attr("href","?LED=005");
});
});
</script>
<style type="text/css">
*{
margin:0px;
padding:0px;
}
body{
background-color: #CCC;
}
h1{
font-family: Verdana, Geneva, sans-serif;
font-size: 1.8em;
color: #FFF;
margin: 20px;
}
.led{
width:80%;
margin-left:auto;
margin-right:auto;
background-color: #FFF;
padding: 10px;
margin-bottom: 30px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0px 0px 5px rgba(50, 50, 50, 0.36);
-moz-box-shadow: 0px 0px 5px rgba(50, 50, 50, 0.36);
box-shadow: 0px 0px 5px rgba(50, 50, 50, 0.36);
}
.led h2{
font-family: Verdana, Geneva, sans-serif;
font-size:1.0em;
color: #000;
margin: 5px;
margin-bottom: 10px;
}
</style>
<body>
<h1>Comandi on/off led</h1>
<div class="led" id="led2">
<h2>Comando led2 (pin2)</h2>
<button class="led2on" id="led2on">Accendi Led</button>
<button class="led2off" id="led2off">Spegni Led</button>
</div>
<div class="led" id="led3">
<h2>Comando led3 (pin3)</h2>
<button class="led3on" id="led3on">Accendi Led</button>
<button class="led3off" id="led3off">Spegni Led</button>
</div>
<div class="led" id="led5">
<h2>Comando led5 (pin5)</h2>
<button class="led5on" id="led5on">Accendi Led</button>
<button class="led5off" id="led5off">Spegni Led</button>
</div>
</body>
</html>
Per qualsiasi correzione o miglioramento del codice, sono sempre a disposizione.