Arduino - sending and receiving web requests

Hello,

I am using a Mega 2560 and a Ethernet Shield.

Is it it possible to do the following both at the same time?

  • host a web server and when a certain route is visited, it turns on an output
  • when a button is pressed, trigger a http request to a server on a local network

I know you can do both separately, but a project requires this to be done on one Arduino.

Thanks.

Yes. Just like your PC the Arduino can have both clients and servers in the same sketch.

Would it cause problems if I wanted to scale this up and use 20 inputs triggering web requests and 20 outputs listening for web requests so to speak?

Thanks

Although your project is possible it's not as easy as it might look first. The Ethernet library (for your Ethernet shield, I guess you're using the "original" one from Arduino itself) has a server implementation that uses all 4 sockets available on the WizNet5100 to listen for incoming connections. You'll have to change that in the library to free at least one for outgoing connections.

Thanks for your response.

Yes Im using the default Ethernet library. Is there a better library more suited to this? Or can you point me in the right direction on what to change in the standard one.

In my loop function I have a for loop iterating over all the input pins I want monitored which is something like pins 30 to 53. I also have a Webserver function listening for web requests and turning on specific outputs based on the URL.

I have it pretty well working already. The only problem I am experiencing that I know of is that pins 50, 51 and 52 do not submit web requests. 53 does.

I will post some code in a bit.

The only problem I am experiencing that I know of is that pins 50, 51 and 52 do not submit web requests. 53 does.

Wrong. All of them are used for this purpose except pin 53 (CS). The Arduino Ethernet shield is using pin 10 (also on the Mega) as the CS of the WizNet5100 chip. SCKL, MISO and MOSI are the other pins (50, 51 and 52) but the Ethernet Shield is using the according lines of the ICSP header.

pylon:
Wrong. All of them are used for this purpose except pin 53 (CS). The Arduino Ethernet shield is using pin 10 (also on the Mega) as the CS of the WizNet5100 chip. SCKL, MISO and MOSI are the other pins (50, 51 and 52) but the Ethernet Shield is using the according lines of the ICSP header.

huh? Haha.

Here is my code...

huh? Haha.

Don't you agree?

Here is my code...

Does it work as expected?

pylon:
Don't you agree?

No, I just have no idea what you meant.

pylon:
Does it work as expected?

Everything works, apart from inputs on pins 50, 51 and 52. They seem to read Button presses but fail when trying to send a web request to a server. All of the other inputs (22-49) seem to work fine including 53.

Pins 50, 51 and 52 are the SPI pins on the Mega. When you use the SPI interface (the Ethernet shield does) these pins are not available to use as GPIOs. That's what I tried to say in my previous post.

pylon:
Pins 50, 51 and 52 are the SPI pins on the Mega. When you use the SPI interface (the Ethernet shield does) these pins are not available to use as GPIOs. That's what I tried to say in my previous post.

Ok, that makes sense. Thanks for clarifying.

What pins do I actually have available to me to use as inputs and outputs? Assume I am also using the memory card slot on the ethernet shield as well.

What pins do I actually have available to me to use as inputs and outputs?

Usually you stroke away the pins already used, the others are free. The Ethernet shield uses the SPI pins (50, 51 and 52) for serial communication, pin 10 is chip select for the WizNet5100, pin 4 is chip select for the SD card slot (although this is available for use if no card is in the slot). If you don't use other shields or breakouts all the other pins are free for you to use. Keep in mind that some of them have multiple functions. Most have at least the digital IO functionality, some are additionally ADC (analog) inputs, some have PWM functionality and some are used for the internal serial interfaces (USART, I2C and SPI).