Go Down

Topic: Excellent Arduino Ethernet Shield Web Server Tutorial (Read 205313 times) previous topic - next topic

mido_1986

I have a question about the web server,  actually am interested in controlling my lights via internet. the problem  is that the security cameras device is connected on port 80 and it seems that we cant use the same port for arduino ( am not sure about it but a guy from the internet company told me that)
the question is: can i use other ports like 2500 or 3000 or any other port with the web server ??
thanks  8)

Hanneman

I love the tutorials on the starting electronics website!

There is one thing I am trying to do, but does not work the way I want it to work.

According to the AJAX tutorials on the startingelectronics website it's possible to create a html button
and turn on a device (Relay, led, buzzer whatever).
This absolutely works.

But what about increasing or decreasing a value of a variable by 1 (only when button is pushed once)?

If you make a button with a link in it, the url will be something like http://192.168.1.177/?Button.
And as long as this url is working and the page is refreshing or kept alive, the links is working.
This is very good when you are turning on a led, relay or something like that.

But when you want to increase a value of a variable by 1 at the push of a button, the value of the variable keeps on going up as the page refreshes or is being "kept alive".

Does anyone know how to do this properly or a good tutorial for this?

Thanks in advance!

franco190453

It is an Excellent tutorial!.
However, nothing has been said about the risks involved in setting up a web server
without proper Internet security.
Regards
Franco

panpira

Hi (or bette this timer: Ho, Ho, Ho  :D  ) folks.

the tutorial site is really a good work, but the code from SurferTim (greetings and thx a lot 4 ur work) is much better!!!

i tried to make it a little bit clearer for my unprofessional eyes  :smiley-eek:
hope its ok for u Tim if i post it here.

a word to ajax: it is possible to modify Tims code also for ajax use. have tried it out and it works. but i used the original Tims-code for it. if i have make it work for the new one i will post it here for u if anyone think that it helps him (or her).

a question to SurferTim (hope u will read this):
afer the sd file is written there comes a return. in my code i saw, thet it breaks the rotine (so i think) too early. is the return necessary at this place or make i a little mistake?

my code part:
Code: [Select]
while(client.available()) client.read();

          int scanCount = sscanf(tBuf,"%7s %47s %8s",methodBuffer,requestBuffer,protocolBuffer);
         
          if(!checkRequest(&scanCount,client,tBuf,methodBuffer,requestBuffer,paramBuffer,protocolBuffer,fileName,fileType))return;
          if(!checkFile(client, fileName, fileType, tBuf,requestBuffer,methodBuffer)) return;
          else {
            openFile(client, fileName, fileType, tBuf,methodBuffer);
//fragliches return?????????????????????????????????????????????????????????????????????
            return;
          }
          serialPrintTundR(paramBuffer);
         


ur code part:
Code: [Select]
myFile.close();             
#ifdef ServerDEBUG
                Serial.println(F("closed"));
#endif
                client.stop();               
#ifdef ServerDEBUG
                Serial.println(F("disconnected"));
#endif
                return;//<---------- this return i mean
              }
              else {


this version of modified Tims code is the first one (finished today). there is much more to do to make it shorter and theres a lot of parameters that i give to functions that not necessary.
hope i helps the one or other to come further withhis server-projekt.
it would be nice if others help me to make it better than it is. my next step is to implement the ajax thing.

PS: please excused my broken english. am not used to comunikate in this language  :)

special thanks and greetings to SurferTim and for all of u a nice and happy chrismas.

last but not least my code from today (edit: in the atachment of the next post): (hope it would be with a slider on the side and no worm over several sites. its my first time that i post here)


panpira

posting 4 dummies :)
overseen that i can make an atachment

here it is

SurferTim

Thanks! Happy holidays to you and yours. :)

Why are you using a return there? My server code doesn't return there. It returns only on a fail or completion.

If you make my code shorter, you must remove something that provides error checking or fault tolerance.
Just saying...

panpira

Hi Tim, nice to meet you and thanks 4 the fast reply :)

i dont want to make it shorter, in my 2560mega is enough memory left, thats not my ambition. i only need to seperate it in different funktions because it was too complex for me to understand what happens at what part of the code at which time.
for the future i want to modify it for ajax use and some other stuff. and so was it easyer for me to understand. am not really professional in programming and a newbie whith the arduino.
your code works great :) , no need to make it shorter (nice trick whith the "#ifdef"), on the contrary (i want to make it longer :) ). i only adapt it for me for the wifi shield and so i must take out the sock-things which ar not implementet in the wifi library (i think so / dont found it).

"Why are you using a return there? "
thats the question :) i think it can be removed too.

but the return is still there in ur script and prevent the code to come to the r and t thing (what is the meaning of this part?)
i run your original code (with the wifi mods - no other changes) and the result was the same - no serial printing of r and t. but this part comes after the "if(strcmp(requestBuffer,"/MYTEST.PHP") == 0)... else" part


greetings :)

...
pch = strtok(paramBuffer,"&");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("t=");
              Serial.println(t,DEC);           
#endif
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("r=");             
              Serial.println(r,DEC);
#endif
            }
...

SurferTim

The only places in my code that have a return is after the sendBadRequest() and sendFileNotFound() calls, and after the successful completion of the send.

Please post the code a couple lines before and after the return in my code that you have a question about. I don't see it.
http://playground.arduino.cc/Code/WebServerST

panpira

sure. the questioned return is in the middle (in ur original code in line 322)
but the return looks correct. have just try to remove it and then the serial log shows this:

Client request #4: GET /seite2.htm HTTP/1.1
file = /SEITE2.HTM
file type = HTM
method = GET
params =
protocol = HTTP/1.1
SD file
filename format ok
SRAM = 6141
file found..opened..send..closed
disconnected
Sending response   <--------------------this-------------

Timeout                   <---------------------is--------------
disconnected          <--------------------new-------------

think the return is correct and i dont really understand it :) (till now)
can u explain me this part (after the return) please in a few words?
i want to understand and not only a functional code. am happy if i can learn something.
a little chrismas present if u want :)

Code: [Select]

                else if(strcmp(fileType,"PNG") == 0) strcat_P(tBuf,PSTR("image/png"));
                else if(strcmp(fileType,"PDF") == 0) strcat_P(tBuf,PSTR("application/pdf"));
                else if(strcmp(fileType,"ZIP") == 0) strcat_P(tBuf,PSTR("application/zip"));
                else strcat_P(tBuf,PSTR("text/plain"));

                strcat_P(tBuf,PSTR("\r\nConnection: close\r\n\r\n"));
                client.write(tBuf);

                if(strcmp(methodBuffer,"GET") == 0)  {
#ifdef ServerDEBUG
                  Serial.print(F("send.."));
#endif

                  while(myFile.available()) {
                    tBuf[clientCount] = myFile.read();
                    clientCount++;
                    tBuf[clientCount] = 0;

                    if(clientCount > 63) {
                      client.write((byte*)tBuf,64);
                      clientCount = 0;
                    }

                  }
                  if(clientCount > 0) {
                    client.write((byte*)tBuf,clientCount);
                  }
                }

                myFile.close();             
#ifdef ServerDEBUG
                Serial.println(F("closed"));
#endif
                client.stop();               
#ifdef ServerDEBUG
                Serial.println(F("disconnected"));
#endif
                return;//<------------------------------------------------------here it is---------------
              }
              else {
#ifdef ServerDEBUG
                Serial.println(F("File not found"));
#endif
                sendFileNotFound(client);
                return;
              }

            }
          }

          pch = strtok(paramBuffer,"&");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("t=");
              Serial.println(t,DEC);           
#endif
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
#ifdef ServerDEBUG
              Serial.print("r=");             
              Serial.println(r,DEC);
#endif
            }


            pch = strtok(NULL,"& ");
          }
#ifdef ServerDEBUG
          Serial.println(F("Sending response"));
#endif


[edit] i had say good bye to my wireless bord and hello to my new ethernetshield. for an application in the garden the house wlan is to weak and the wifi shield to slow. it make more sence to plug the ether with a short cable into a repeater. the wifi shield is overpriced anyway and so i have a superfast wlan and can hold it on the newest standards easily.

harith

i used ethernet shield and SD card...saved a web page on SD card and tried to access it..but it won't work..in serial monitor it says..SD card read successful.web page found..but when i trying to access with firefox it is only being busy and then no connection...what can be the reason..help me please..

SurferTim

#70
Jan 06, 2015, 01:45 pm Last Edit: Jan 06, 2015, 02:17 pm by SurferTim
@panpira: Sorry I missed your post. The part after the return calls a function that sends an "Error 404 File not found" message.

@harith: What did you name the file, and what is the contents of that file?
edit: Also include the output of the serial monitor.

YusifNurizade

Thanks for posting!

This is a great tutorial - especially the part about controlling the Arduino from the webpage. Other tutorials I looked at explained how to get data from the MCU but not the other way around.

I'm curious whether modifying the address is the only way to control the Arduino, for example, in toggling an LED. It works for basic communication but it seems like it wouldn't be able to handle it if things got more complex. I'm new to Web Servers on the Arduino and I would really appreciate any advice.

Thanks again,
Yusif Nurizade

chrisapollon

Hello I am a new developer and new in arduino. I would like to create a WiFi server using arduino WiFi shield. I would like to control devices such as switch them on and of with a relay and also view or store some sensor data on it. How can I implement this?

myggle

Seems this might be a work in progress from the original posting till now.  I initially found this thread when trying to learn how servers work from an Arduino's perspective and how I might also connect my Mega to the web via this Ethernet shield, then when getting into SurferTim's material has opened my eyes to a broader spectrum of what actually applies.

I want to build a remotely viewed/controlled arduino project that includes use of the Mega 2560 R3, Ethernet Shield, Data Logging Shield with SD slot and Real Time Clock.  The project will control an 8 channel relay and 2 temp/humidity sensors.  So I wanted to read this thread to see if the (SPI?/I2C?) conflicts could be worked out by using a different shield with SD so that the SD and Ethernet can be utilized at the same time.

Tim, you mentioned something called "TimeServer" and I followed your link to further learn.  By chance, could this be used to replace the RTC for triggering events after the MCU like kicking on a relay at 12:00 everyday and off again at another exact time respective to a 24 hour day? 

SurferTim

#74
Feb 09, 2015, 03:36 pm Last Edit: Feb 09, 2015, 03:37 pm by SurferTim
I don't use a RTC. A modified version of the UdpNtpClient example sketch works good for me. I use a RPi for the NTP server.

Go Up