buongiorno,
io sto cercando, attraverso lo shield ethernet con chip w5500, di assegnare l'indirizzo IP della sheda leggendolo da un file che è contenuto nella memoria SD inserita nello shield... questo è il codice ma non capisco perchè non mi funziona
#include <SPI.h>
#include <Ethernet2.h>
#include "DHT.h"
#include <SD.h>
DHT dht(2, DHT22);
String tmp = "";
File url;
bool sd = false;
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x06, 0x2E
};
IPAddress IP;
//IPAddress ip(10, 0, 0, 7);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
if(SD.begin(4)){
Serial.print("memoria SD letta con successo");
sd = true;
}
else{
Serial.print("memoria SD non letta con successo");
}
//parte per lettura intervallo in secondi per il refresh
File inter;
char temp;
inter = SD.open("inter.txt", FILE_READ);
if(sd){
while ((temp = inter.read()) > 0){
tmp += (char)temp;
if((char)temp=='\n') {
if(temp > 0) tmp="";
}
}
String ciao = "Refresh: " + tmp;
tmp = ciao;
}else{
tmp = "Refresh: 5";
}
inter.close();
//parte per la lettura della mnodalita' di salvataggio o visualizzazione dei dati acquisiti
//10.0.0.30
Url();
//parte per la lettura dell'URL di salvataggio in caso di REMOTE MOD
Ethernet.begin(mac, IP);
dht.begin();
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
/*if(SD.begin(4)){
Serial.print("memoria SD letta con successo");
sd = true;
}
else{
Serial.print("memoria SD non letta con successo");
}
*/
if (sd){
//mod = SD.open("mod.txt", FILE_READ);
//url = SD.open("url.txt", FILE_READ);
}
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println(tmp);
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>");
client.println("PROGETTO N 1");
client.println("</title>");
client.println("<head>");
client.println("<body BGCOLOR=""orange"">");
client.println(tmp);
client.println("<table border>");
client.println("<tr>");
client.println("<td>");
client.println("UMIDITA':");
client.println("</td>");
client.println("<td>");
if(Umi() == -1){
client.println("ERRORE NEL SENSORE");
}else{
client.println(Umi());
client.println("^C");
}
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>");
client.println("TEMPERATURA:");
client.println("</td>");
client.println("<td>");
if(Temp() == -1){
client.println("ERRORE NEL SENSORE");
}else{
client.println(Temp());
client.println("^C");
}
client.println("</td>");
client.println("</tr>");
client.println("</table>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disconnected");
}
}
float Umi(){
float h = dht.readHumidity();
if (isnan(h)){
h = -1;
}
return h;
}
float Temp(){
float t = dht.readTemperature();
if (isnan(t)){
t = -1;
}
return t;
}
void Intervallo (){
File inter;
int temp;
inter = SD.open("inter.txt", FILE_READ);
temp = inter.read();
if(sd){
while ((temp = inter.read())>0){
tmp += (char)temp;
if((char)temp== '\n') {
if(temp > 0) tmp="";
}
}
String ciao = "Refresh: " + tmp;
tmp = ciao;
}else{
tmp = "Refresh: 5";
}
inter.close();
}
void Url(){
File url;
String IPgen;
int numIP1, numIP2, numIP3, numIP4, tmp1 = 0;
char tm;
url = SD.open("url.txt", FILE_READ);
if(!sd){
while ((tm = url.read()) > 0){
if((char)tm != '.')
IPgen += (char)tm;
if((char)tm == '.'){
tmp1++;
if(tmp1 == 1){
numIP1 = IPgen.toInt();
IPgen = "";
}
if(tmp1 == 2){
numIP2 = IPgen.toInt();
IPgen = "";
}
if(tmp1 == 3){
numIP3 = IPgen.toInt();
IPgen = "";
}
if(tmp1 == 4){
numIP4 = IPgen.toInt();
IPgen = "";
}
}
}
}else{
numIP1 = 10;
numIP2 = 0;
numIP3 = 0;
numIP4 = 7;
}
IP = (numIP1, numIP2, numIP3, numIP4);
}