HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.

Dependencies:   UIPEthernet

Turn LED1, or other digital output, on/off using a web browser.

In this example we create a HTTP server that will serve a simple Web page to remotely turn LED1, or other digital output on the mbed board, on/off by using a web browser. An inexpensive ENC28J60 Ethernet module is used to assure connection between the mbed board and the Ethernet network (Internet). The ENC28J60 Ethernet module is driven by the UIPEthernet library.

Needed parts:

  • mbed board
  • ENC28J60 Ethernet module
  • Wires
  • Web browser (Internet Explorer, Safari, Firefox, Chrome ...) running on Windows, Mac, Linux, iPhone or Android device.
/media/uploads/hudakz/webswitch_enc.jpg/media/uploads/hudakz/webswitch_mobile01.jpg

Notice that DHCP is turned on by default. If you prefer to use static IP address then uncomment line 234

The IP address assigned to the WebSwitch server along with an instruction how to use it is printed in the connected PC's serial terminal window during program start up.

Warning

Please notice that the 3.3V power supply chip (RT8183-B) installed on an STM32F103C8T6 board is not rated to power also the ENC28J60 board.


The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!

NOTE:

Revision:
8:3bd85b731cca
Parent:
7:f5e11393836d
Child:
9:6f63abd61de8
--- a/main.cpp	Mon May 01 20:06:24 2017 +0000
+++ b/main.cpp	Fri Jun 30 20:07:32 2017 +0000
@@ -29,36 +29,36 @@
 
 Serial  pc(USBTX, USBRX);
 
-#define DHCP    1   // if you'd like to use static IP address comment out this line 
+#define DHCP    1   // if you'd like to use static IP address comment out this line
 
-// UIPEthernet is the name of a global instance of UIPEthernetClass.
+// uIPEthernet is the name of a global instance of UIPEthernet.
 // Do not change the name! It is used within the UIPEthernet library.
 #if defined(TARGET_LPC1768)
-UIPEthernetClass    UIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
 #elif defined(TARGET_LPC1114)
-UIPEthernetClass    UIPEthernet(dp2, dp1, dp6, dp25);       // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(dp2, dp1, dp6, dp25);       // mosi, miso, sck, cs
 #elif defined(TARGET_LPC11U68)
-UIPEthernetClass    UIPEthernet(P0_9, P0_8, P1_29, P0_2);   // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(P0_9, P0_8, P1_29, P0_2);   // mosi, miso, sck, cs
 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8)  \
    || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8)  \
    || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB)  \
    || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB)
-//UIPEthernetClass    UIPEthernet(D4, D5, D3, D2);            // mosi, miso, sck, cs
-//UIPEthernetClass    UIPEthernet(D11, D12, D13, D10);            // mosi, miso, sck, cs
-UIPEthernetClass    UIPEthernet(PA_7, PA_6, PA_5, PB_6);            // mosi, miso, sck, cs
+//UIPEthernet  uIPEthernet(D4, D5, D3, D2);             // mosi, miso, sck, cs
+//UIPEthernet  uIPEthernet(D11, D12, D13, D10);            // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(PA_7, PA_6, PA_5, PB_6);            // mosi, miso, sck, cs
 
 // If your board/plaform is not present yet then uncomment
 // the following two lines and replace TARGET_YOUR_BOARD as appropriate.
 
 //#elif defined(TARGET_YOUR_BOARD)
-//UIPEthernetClass    UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);   // mosi, miso, sck, cs
+//UIPEthernet  uIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);   // mosi, miso, sck, cs
 
 #endif
 
 // Note:
 // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
 // then either use different SPI port (if available on the board) and change the pin names
-// in the constructor UIPEthernet(...) accordingly or instead of using LED1 pin, select
+// in the constructor uIPEthernet(...) accordingly or instead of using LED1 pin, select
 // a free pin (not used by SPI port) and connect to it an external LED which is connected
 // to a 220 Ohm resitor that is connected to the groud.
 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
@@ -73,7 +73,7 @@
 float               roomTemp = 21.8;    // A temperature sensor output
 
 // However, make sure that it does not collide with any of the SPI pins
-// already used in the UIPEthernet(...) constructor above!
+// already used in the uIPEthernet(...) constructor above!
 const string        PASSWORD = "secret";    // change as you like
 const string        HTTP_OK = "HTTP/1.0 200 OK";
 const string        MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
@@ -148,7 +148,7 @@
     //roomTemp = ds1820.read();
     sprintf(roomTempStr, "%3.1f", roomTemp);
 
-    httpContent = "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>"; 
+    httpContent = "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>";
     httpContent += "<pre>Temperature:\t" + string(roomTempStr) + "&deg;C\r\n</pre>";
 
     if(status == ON) {
@@ -201,7 +201,7 @@
 #endif
 #if defined(DHCP)
     pc.printf("Searching for DHCP server..\r\n");
-    if(UIPEthernet.begin(MY_MAC) != 1) {
+    if(uIPEthernet.begin(MY_MAC) != 1) {
         pc.printf("No DHCP server found.\r\n");
         pc.printf("Exiting application.\r\n");
         return 0;
@@ -210,14 +210,11 @@
 #else
     // IP address must be unique and compatible with your network.
     const IPAddress MY_IP(192, 168, 1, 181);    //  Change as appropriate.
-    UIPEthernet.begin(MY_MAC, MY_IP);
+    uIPEthernet.begin(MY_MAC, MY_IP);
 #endif
-    IPAddress   localIP = UIPEthernet.localIP();
-    pc.printf("Type ");
-    for(uint8_t i = 0; i < 3; i++)
-        pc.printf("%d.", localIP[i]);
-    pc.printf("%d/secret/ into your web browser and hit ENTER\r\n", localIP[3]);
-    myServer.begin();
+    IPAddress   localIP = uIPEthernet.localIP();
+    pc.printf("Type %s secret/ into your web browser and hit ENTER\r\n", localIP.toString());
+myServer.begin();
     while(1) {
         EthernetClient  client = myServer.available();
         if(client) {