Go Down

Topic: Relays controlled via WEB interface (Read 471 times) previous topic - next topic

mackowiakp

I am new in Arduino, so I take ready sketch from Internet to control ON/OFF four relays connected to Arduino Mega via WEB interface. WEB server works on port 7400 on my unit and NMAP shows that port is open. But nothing in display in Firefox. Serial terminal shows that IP address is correct but when I try in web browser http://IP_OF_ARDUINO:7400 timeout is reported after several minutes. Below is my skech. Can anybody help me? What I am doing wrong?

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

byte mac[] = {
  0x1C, 0xAA, 0xBB, 0xCC, 0xDE, 0x22
};
EthernetServer server(7400);// WWW server port
const byte wyj1 = 46;// Przekaznik 1
const byte wyj2 = 47;// Przekaznik 2
const byte wyj3 = 48;// Przekaznik 3
const byte wyj4 = 49;// Przekaznik 4

String readString;

File plik;

void setup() {
  delay(400);
  pinMode(wyj1, OUTPUT);
  pinMode(wyj2, OUTPUT);
  pinMode(wyj3, OUTPUT);
  pinMode(wyj4, OUTPUT);
 
  Serial.begin(9600);
  Serial.println("Arduino MEGA z Ethernet");

  if (!SD.begin(4))
  {
     Serial.println("Nie wykryto karty SD");
     //return;                                              //przerwij program
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Nie mogę uzyskac adresu z DHCP");
    for (;;)
      ;
  }
  // print your local IP address:
  Serial.print("Moj adres IP: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  server.begin();
}

void loop() {
  EthernetClient client = server.available();
if (client == true)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();// Read char by char HTTP request
Serial.write(c);

if (readString.length() < 100)
{
readString = readString + c;// Store characters to string
}

if (c == '\n')

{
Serial.println(readString);
client.println("http/1.1 200 ok");// Send standard http headers
client.println("content-type: text/html");
client.println("Connection: close");
client.println();

client.println("<!doctype html><html>");
client.println("<body bgcolor='blue'>");
client.println("<center><head><title>Domowy system sterowania</title></head>");
client.println("<h2>Arduino MEGA serwer WEB(v1)</h2>");
client.println("<h4>To jest moj system</h4>");

client.println("<hr/><p> Kliknij na przycisk aby zminic stan ON/OFF <p/><hr/>");
client.print("<input type=button value='Wyjscie-1 ON' onmousedown=location.href='/?wyj1_on'>");
client.println("<input type=button value='Wyjscie-1 OFF' onmousedown=location.href='/?wyj1_off'><br/><br/>");
client.print("<input type=button value='Wyjscie-2 ON' onmousedown=location.href='/?wyj2_on'>");
client.println("<input type=button value='Wyjscie-2 OFF' onmousedown=location.href='/?wyj2_off'><br/><br/>");
client.print("<input type=button value='Wyjscie-3 ON' onmousedown=location.href='/?wy3_on'>");
client.println("<input type=button value='Wyjscie-3 OFF' onmousedown=location.href='/?wyj3_off'><br/><br/>");
client.print("<input type=button value='Wyjscie-4 ON' onmousedown=location.href='/?wyj4_on'>");
client.println("<input type=button value='Wyjscie-4 OFF' onmousedown=location.href='/?wyj4_off'><br/><hr/>");

client.println("</body></html>");

delay(1);// Page loading delay

client.stop();// Stopping client

if(readString.indexOf("/?wyj1_on") > 0) digitalWrite(wyj1, HIGH);
if(readString.indexOf("/?wyj1_off") > 0) digitalWrite(wyj1, LOW);
if(readString.indexOf("/?wyj2_on") > 0) digitalWrite(wyj2, HIGH);
if(readString.indexOf("/?wyj2_off") > 0) digitalWrite(wyj2, LOW);
if(readString.indexOf("/?wyj3_on") > 0) digitalWrite(wyj3, HIGH);
if(readString.indexOf("/?wyj3_off") > 0) digitalWrite(wyj3, LOW);
if(readString.indexOf("/?wyj4_on") > 0) digitalWrite(wyj4, HIGH);
if(readString.indexOf("/?wyj4_off") > 0) digitalWrite(wyj4, LOW);

readString = "";// Clearing string for next read

}// End of line reached
}// End of client available
}// End of client connected
}// End of client connection
}



J-M-L

if (client == true)
Do you think client is a boolean ?
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

mackowiakp

So i add in variable definition:

Code: [Select]
bool client;

But it does not help. Maybe wrong syntax?

J-M-L

client is not a boolean, it's an instance of the class EthernetClient or a NULL pointer
Code: [Select]
  EthernetClient client = server.available();

NULL evaluates to false if you just test that through automatic promotion or - if you have a client - will be a pointer in memory to a EthernetClient instance => in case you got a client, then the pointer is a memory address so won't compare correctly to true, your test will return it's not OK (address 5572 in memory for example won't be equal to 1 which is the numerical value of true)

see the documentation on how to write the if correctly
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

AshwiniGunjal

Hey you are code have following :
<code>
if (client == true)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();// Read char by char HTTP request
Serial.write(c);

</code>

you using while loop then you didnt get new request.so please colde like this:

while(! client){
return;
}
while(!client.avaiable()){
delay(10);
}
//once get client availble then read data
String c = client.readStringUntil('\r');
Serial.println(c);
 client.flush();

here In C variable you get requested url.








Go Up