#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = { 172, 16, 0, 10 }; // ip in lan
byte gateway[] = { 172, 16, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int pin = 9;
boolean ligado = true;
//////////////////////
void setup(){
pinMode(pin, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Automação Residencial - EPB"); // so I can keep track of what is loaded
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>Remote Automation</title>");
client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
client.println("</head>");
client.println("<body>");
client.println("<div id='wrapper'>Remote Automation V1.1");
client.print("<div id='rele'></div><div id='estado' style='visibility: hidden;'>");
client.print(ligado);
client.println("</div>");
client.println("<div id='botao'></div>");
client.println("</div>");
client.println("<script>AlteraEstadoRele()</script>");
client.println("</body>");
client.println("</head>");
}
client.println("</html>");
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"ZXBiOmVwYjIwMTU=")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
Serial.println(readString); //print to serial monitor for debuging
if(readString.indexOf("?ligar") >0)//checks for on
{
digitalWrite(pin, HIGH); // set pin 4 high
Serial.println("On");
ligado = false;
}
else{
if(readString.indexOf("?desligar") >0)//checks for off
{
digitalWrite(pin, LOW); // set pin 4 low
Serial.println("Off");
ligado = true;
}
}
//clearing string for next read
readString="";
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Mensagem de erro:
sketch_may16a.ino: In function 'void setup()':
sketch_may16a.ino:32:1: error: a function-definition is not allowed here before '{' token
sketch_may16a.ino:53:11: error: 'client' was not declared in this scope
sketch_may16a.ino: In function 'void loop()':
sketch_may16a.ino:94:30: error: 'SendOKpage' was not declared in this scope
Erro ao compilar.
Alguém consegue ajudar pfv??
Aqui está ele sem erros:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = {
172, 16, 0, 10 }; // ip in lan
byte gateway[] = {
172, 16, 0, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int pin = 9;
boolean ligado = true;
//////////////////////
void setup(){
pinMode(pin, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Automação Residencial - EPB"); // so I can keep track of what is loaded
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>Remote Automation</title>");
client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
client.println("</head>");
client.println("<body>");
client.println("<div id='wrapper'>Remote Automation V1.1");
client.print("<div id='rele'></div><div id='estado' style='visibility: hidden;'>");
client.print(ligado);
client.println("</div>");
client.println("<div id='botao'></div>");
client.println("</div>");
client.println("<script>AlteraEstadoRele()</script>");
client.println("</body>");
client.println("</head>");
client.println("</html>");
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"ZXBiOmVwYjIwMTU=")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
Serial.println(readString); //print to serial monitor for debuging
if(readString.indexOf("?ligar") >0)//checks for on
{
digitalWrite(pin, HIGH); // set pin 4 high
Serial.println("On");
ligado = false;
}
else{
if(readString.indexOf("?desligar") >0)//checks for off
{
digitalWrite(pin, LOW); // set pin 4 low
Serial.println("Off");
ligado = true;
}
}
//clearing string for next read
readString="";
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Muito obrigado!
Vou analisar onde foi que falhei :o
Boas novamente, espero que seja a última vez que venho pedir ajuda..
Eu tinha os seguintes códigos
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = {
172, 16, 0, 10 }; // ip in lan
byte gateway[] = {
172, 16, 0, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int pin = 9;
boolean ligado = true;
//////////////////////
void setup(){
pinMode(pin, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Automação Residencial - EPB"); // so I can keep track of what is loaded
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>Remote Automation</title>");
client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
client.println("<link rel='stylesheet' type='text/css' href='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.css' />");
client.println("<script type='text/javascript' src='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.js'></script>");
client.println("</head>");
client.println("<body>");
client.println("<div id='wrapper'>Remote Automation V1.1");
client.print("<div id='rele'></div><div id='estado' style='visibility: hidden;'>");
client.print(ligado);
client.println("</div>");
client.println("<div id='botao'></div>");
client.println("</div>");
client.println("<script>AlteraEstadoRele()</script>");
client.println("</body>");
client.println("</head>");
client.println("</html>");
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"ZXBiOmVwYjIwMTU=")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
//clearing string for next read
readString="";
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Serial.println(readString); //print to serial monitor for debuging
if(readString.indexOf("?ligar") >0)//checks for on
{
digitalWrite(pin, HIGH); // set pin 4 high
Serial.println("On");
ligado = false;
}
else{
if(readString.indexOf("?desligar") >0)//checks for off
{
digitalWrite(pin, LOW); // set pin 4 low
Serial.println("Off");
ligado = true;
}
}
Juntei e ficou assim
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = {
172, 16, 0, 10 }; // ip in lan
byte gateway[] = {
172, 16, 0, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int pin = 9;
boolean ligado = true;
//////////////////////
void setup(){
pinMode(pin, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Automação Residencial - EPB"); // so I can keep track of what is loaded
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>Remote Automation</title>");
client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
client.println("<link rel='stylesheet' type='text/css' href='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.css' />");
client.println("<script type='text/javascript' src='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.js'></script>");
client.println("</head>");
client.println("<body>");
client.println("<div id='wrapper'>Remote Automation V1.1");
client.print("<div id='rele'></div><div id='estado' style='visibility: hidden;'>");
client.print(ligado);
client.println("</div>");
client.println("<div id='botao'></div>");
client.println("</div>");
client.println("<script>AlteraEstadoRele()</script>");
client.println("</body>");
client.println("</head>");
client.println("</html>");
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"ZXBiOmVwYjIwMTU=")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
Serial.println(readString); //print to serial monitor for debuging
if(readString.indexOf("?ligar") >0)//checks for on
{
digitalWrite(pin, HIGH); // set pin 4 high
Serial.println("On");
ligado = false;
}
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
else{
if(readString.indexOf("?desligar") >0)//checks for off
{
digitalWrite(pin, LOW); // set pin 4 low
Serial.println("Off");
ligado = true;
}
}
}
}
//clearing string for next read
readString="";
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Mas não sei porquê o código deixou de ser funcional, será devido a condição?
Obrigado
A 2ª parte do código não está completa. Como era o código completo? O que significa "não funcionar"?
A segunda parte do código é esta, a qual eu tentei juntar
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip[] = { 192, 168, 0, 99 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int pin = 9;
boolean ligado = true;
//////////////////////
void setup(){
pinMode(pin, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println("RoboCore Remote Automation V1.1"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////////// control arduino pin
Serial.println(readString); //print to serial monitor for debuging
if(readString.indexOf("?ligar") >0)//checks for on
{
digitalWrite(pin, HIGH); // set pin 4 high
Serial.println("On");
ligado = false;
}
else{
if(readString.indexOf("?desligar") >0)//checks for off
{
digitalWrite(pin, LOW); // set pin 4 low
Serial.println("Off");
ligado = true;
}
}
//clearing string for next read
readString="";
///////////////
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>RoboCore - Remote Automation</title>");
client.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
client.println("<link rel='stylesheet' type='text/css' href='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.css' />");
client.println("<script type='text/javascript' src='http://www.robocore.net/upload/projetos/RemoteAutomationV1.0.js'></script>");
client.println("</head>");
client.println("<body>");
client.println("<div id='wrapper'>RoboCore Remote Automation V1.1");
client.print("<div id='rele'></div><div id='estado' style='visibility: hidden;'>");
client.print(ligado);
client.println("</div>");
client.println("<div id='botao'></div>");
client.println("</div>");
client.println("<script>AlteraEstadoRele()</script>");
client.println("</body>");
client.println("</head>");
delay(1);
//stopping client
client.stop();
}
}
}
}
}
Faço o login, abre a página, carrego no botão para ligar a LED e não liga
(anteriormente sem sistema de login ligava