Ciao!!
Sto sperimentando questo nuovo mondo e devo fare parecchia strada.
Ciò che volevo fare è un sistema simil domotico per camera mia: aver la possibilità di dare corrente a ciò che voglio tramite http.
lo sketch che ho trovato fa a caso mio....ma non funziona!!
Qualcuno può darmi una mano?
Grazie
/*
Network Relay System
*/
#include "etherShield.h"
#include "ETHER_28J60.h"
int i, rsize, relay[] = {8, 7, 6, 5, 3, 2, 1, 0};
static uint8_t mac[6] = {0x10, 0x10, 0x10, 0x02, 0x00, 0x00};
static uint8_t ip[4] = {192, 168, 1, 90};
static uint16_t port = 80;
unsigned long runtime, starttime;
ETHER_28J60 ether;
void setup() {
pinMode(4, OUTPUT); //
digitalWrite(4, HIGH); // Same as above.
// Serial.begin(57600);
ether.setup(mac, ip, port);
rsize = sizeof(relay) / 2;
for (i = 0; i < rsize; i = i + 1) {
pinMode(relay[i], OUTPUT);
}
runtime = 0;
starttime = millis();
}
void loop() {
int c;
char * param;
char * params;
if (params = ether.serviceRequest()) {
if (strstr(params, "?status")) {
for (i = 0; i < rsize; i = i + 1) {
ether.print(digitalRead(relay[i]));
}
} else {
if((strlen(params) > 3) && (!(strstr(params, "favicon.ico")))) {
runtime = 0;
param = strtok(params, "?&");
while (param != NULL) {
if (strstr(param, "p=")) {
i = atoi(param + 2) - 1;
} else if (strstr(param, "c=")) {
c = atoi(param + 2);
if (c == 1) {
for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
digitalWrite(relay[i], HIGH);
} else {
for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
}
} else if (strstr(param, "t=")) {
runtime = (unsigned long)(atoi(param + 2)) * 1000;
if (runtime < 15000) { runtime = 15000; }
starttime = millis();
}
param = strtok(NULL, "& ");
}
if (runtime == 0) {
runtime = 60000; // 60 sec. default run time (takes 10 seconds for valve to open).
starttime = millis();
}
}
ether.print("<tt>Network Relay System\n
\n");
for (i = 0; i < rsize; i = i + 1) {
ether.print("
Port #");
ether.print(i+1);
ether.print(": <a href='?p=");
ether.print(i+1);
if (digitalRead(relay[i]) == 1) {
ether.print("&c=0'>Off</a> in ");
ether.print((runtime - (millis() - starttime))/1000);
ether.print(" seconds.\n");
}
else {
ether.print("&c=1'>On</a>\n");
}
}
ether.print("<p><a href='/'>Refresh</a>,<a href='/?status'>Status</a>\n");
}
ether.respond();
}
if ((unsigned long)(millis() - starttime) > runtime) {
for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); }
}
}
edit by mod: per favore includere il codice usando gli appositi tag