Some time ago I showed you how to set up an internet control system for your Arduino. — but it had to remain connected to the computer via USB in order to maintain an Internet connection. If you have an ethernet shield this is not needed. Today we’ll be looking at setting up an Arduino as a primary web server so we can host our own device management website.

You will need:

  • Arduino or an Arduino clone, as detailed here.
  • Ethernet Shield (~$35) — make sure you get the version that matches your Arduino as they are not all compatible
  • Spare port on router or switch and Ethernet cable
  • WebServer example provided in Arduino app

Setting

The Ethernet shield uses pins with 10 to 13 to manage network connectivity, so don’t include them in your project; pin 4 is also used for the SD card, but we won’t be using that today.

First things first — open File -> Examples -> ethernet -> webserver, which is provided in the Arduino IDE. Find the lines referring to the MAC and IP address.

Arduino webserver

The MAC address for me was found on the box — yours may be on the shield itself. Adjust the first line of values ​​- you need to leave the previous bit 0x . From a technical standpoint, it doesn’t matter what you put in here, as long as you don’t have conflicting values ​​on your subnet — but even so, it was a manufacturer-assigned address, so we should probably stick with that. Just know that if you get it wrong, it will still work.

Arduino Web Server Example

Next comes the IP address. We need a specific fixed address so that we can properly set up port forwarding — the shield is capable of receiving an address from DHCP, but this is only useful if it will be used simply as a client and not as a server. Enter an IP address that is not used and remembered on your local network, the IP address of your router, and 255.255.255.0 (subnet). If none of this makes sense to you, read our free and detailed guide to home networking.

Port forwarding

Edit the line labeled EthernetServer (number) and change the number to 8081 . This will configure our server to listen on port 8081 in case your ISP blocks web traffic on the traditional port 80.

Download the demo at this point and test it from your local machine. You will of course also need to connect an Ethernet cable. Keep the USB plugged in as we need it for power — you can change it to 9V later, but we’re still testing.

Noob Tip: if you are getting this error, it means that you have mistakenly placed capital letter O instead of 0 in the MAC address area. There is no O! in hexadecimal values. If you’re really a beginner, it might be worth checking out our Arduino guide for !

Arduino Web Server Example

To access the Arduino, enter the address and port number directly from your browser.

Arduino Web Server Example

You should see something like this — so far so good. Unfortunately, this is only available in local network right now, so go to your router’s port configuration page to set up forwarding from port 8081 to the same port on your Arduino’s IP ( see: What is Port Forwarding? ports? )

Arduino webserver tutorial

Test it from a mobile 3G internet device with Wi-Fi disabled; don’t forget to use your public IP address not the LAN address. Your router should be able to tell you this, or just ask Google «what’s my ip».

Arduino webserver

Understanding the output

The page served in the demo reads the values ​​from the analog pins, but since nothing is connected there, you will get nonsense. Try connecting a light sensor (photoresistor) to the ports A0 and + 5V, With resistor 10 kOhm also on A0 and gnd (this is a voltage divider because the analog pins can only read voltage, not resistance) just to confirm the server. actually reads the values. Note that when the light intensity changes, the values ​​on the other pins also change — this is because they are all unrelated.

Receive Interactive

Reading these values ​​is fine, but jumping to management The Arduino from here is a pretty big leap, and it’s beyond the scope of this tutorial today. Instead of reinventing the wheel, I’ll point you to Arduino forum user Hari, who wrote the basic API for interacting with digital pins. Using this code (I modified it to work with the latest ethernet library), connect the long lead of the LED (anode) to pin 8 and the short lead to pin 7 (with a suitable resistor). You can then use URLs in the form http://ip:port/digitalWrite/8/1, to turn on the indicator, and http://ip:port/digitalWrite/8/0, to turn it off and read the analog values. With analogue Read / 0 .

This example from bildr will also teach you how to control the LED sequence by reading a GET request.

You can also use this example, which renders a form on the page to control an RGB LED, although you can easily adapt it for an RGB strip like a dynamic ambient lighting project. we built some time ago.

Well, I had fun building them, so I hope you do too. If you’ve made a library that makes it easier to maintain pages and controls, please contact us in the comments.

Похожие записи