Jack Hansdampf / Mbed OS WebSwitch_ENC28J60_GSOE

Dependencies:   UIPEthernet_GSOE

Revision:
7:f5e11393836d
Parent:
6:b38a3b476a45
Child:
8:3bd85b731cca
--- a/main.cpp	Sun Aug 28 11:47:29 2016 +0000
+++ b/main.cpp	Mon May 01 20:06:24 2017 +0000
@@ -1,21 +1,22 @@
-/* 
+/*
  * In this project LED1 on the mbed board is switched on/off using a web browser.
  * However, you can easily modify the project to remotely switch on/off any external device.
  * The HTTP server is built from an mbed board and an ENC28J60 board.
  * ENC28J60 is driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>.
  * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
  */
- 
-//#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!                                    
+
+//#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!
 
 #if defined(TARGET_STM32F103C8T6)
-    #define LED_PIN PC_13
-    const int OFF = 1;
-    const int ON  = 0;
+#include "stm32f103c8t6.h"
+#define LED_PIN PC_13
+const int OFF = 1;
+const int ON  = 0;
 #else
-    #define LED_PIN LED1
-    const int OFF = 0;
-    const int ON  = 1;
+#define LED_PIN LED1
+const int OFF = 0;
+const int ON  = 1;
 #endif
 
 #include "mbed.h"
@@ -42,7 +43,9 @@
    || 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(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
 
 // If your board/plaform is not present yet then uncomment
 // the following two lines and replace TARGET_YOUR_BOARD as appropriate.
@@ -65,7 +68,9 @@
 EthernetServer      myServer = EthernetServer(MY_PORT);
 
 // In this example we are turning on/off LED1.
-DigitalOut          sw(LED_PIN);            // Change LED_PIN to a pin of your choice.
+DigitalOut          sw(LED_PIN);        // Change LED_PIN to a pin of your choice.
+//DigitalOut          sw(D9);           // Change LED_PIN to a pin of your choice.
+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!
@@ -76,33 +81,33 @@
 string              httpHeader;     // HTTP header
 string              httpContent;    // HTTP content
 
-// analyse the url given
-// return values: -1 invalid password
-//                -2 no command given but password valid
-//                -3 just refresh page
-//                 0 switch off
-//                 1 switch on
-//
-//                The string passed to this function will look like this:
-//                GET /password HTTP/1.....
-//                GET /password/ HTTP/1.....
-//                GET /password/?sw=1 HTTP/1.....
-//                GET /password/?sw=0 HTTP/1.....
-int8_t analyseURL(string& str) {
-    if(str.substr(5, PASSWORD.size()) != PASSWORD)
+/**
+ * @brief   Analyses the received URL
+ * @note    The string passed to this function will look like this:
+ *          GET /password HTTP/1.....
+ *          GET /password/ HTTP/1.....
+ *          GET /password/?sw=1 HTTP/1.....
+ *          GET /password/?sw=0 HTTP/1.....
+ * @param   url URL string
+ * @retval -1 invalid password
+ *         -2 no command given but password valid
+ *         -3 just refresh page
+ *          0 switch off
+ *          1 switch on
+ */
+int8_t analyseURL(string& url) {
+    if(url.substr(5, PASSWORD.size()) != PASSWORD)
         return(-1);
 
     uint8_t pos = 5 + PASSWORD.size();
 
-    if(str.substr(pos, 1) == " ")
+    if(url.substr(pos, 1) == " ")
         return(-2);
 
-    if(str.substr(pos, 1) != "/")
+    if(url.substr(pos++, 1) != "/")
         return(-1);
 
-    pos++;
-
-    string  cmd(str.substr(pos, 5));
+    string  cmd(url.substr(pos, 5));
 
     if(cmd == "?sw=0")
         return(OFF);
@@ -119,7 +124,8 @@
  * @param
  * @retval
  */
-string& movedPermanently(uint8_t flag) {
+string& movedPermanently(uint8_t flag)
+{
     if(flag == 1)
         httpContent = "/" + PASSWORD + "/";
     else
@@ -137,20 +143,26 @@
  * @retval
  */
 string& showWebPage(uint8_t status) {
-    httpContent = "<h2>Web Switch</h2>\r\n";
+    char roomTempStr[5];
+
+    //roomTemp = ds1820.read();
+    sprintf(roomTempStr, "%3.1f", roomTemp);
+
+    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) {
-        httpContent += "<pre>\r\n  <font color=#FF0000>ON </font>";
-        httpContent += " <a href=\"./?sw=0\">[Turn off]</a>\r\n";
+        httpContent += "<pre>\r\nHeating:\t<font color=#FF0000>On </font>";
+        httpContent += " <a href=\"./?sw=0\"><button>Turn off</button></a>\r\n";
     }
     else {
-        httpContent += "<pre>\r\n  <font color=#BBBBBB>OFF</font>";
-        httpContent += " <a href=\"./?sw=1\">[Turn on]</a>\r\n";
+        httpContent += "<pre>\r\nHeating:\t<font color=#999999>Off</font>";
+        httpContent += " <a href=\"./?sw=1\"><button>Turn on</button></a>\r\n";
     }
 
-//    httpContent += "  <a href=\".\">[refresh status]</a>\r\n";
     httpContent += "</pre>\r\n";
     httpContent += "<hr>\r\n";
+    httpContent += "<pre>2017 ARMmbed</pre>";
     return httpContent;
 }
 
@@ -160,7 +172,8 @@
  * @param
  * @retval
  */
-void sendHTTP(EthernetClient& client, string& header, string& content) {
+void sendHTTP(EthernetClient& client, string& header, string& content)
+{
     char    content_length[5] = { };
 
     header += "\r\nContent-Type: text/html\r\n";
@@ -181,7 +194,11 @@
  * @param
  * @retval
  */
-int main(void) {
+int main(void)
+{
+#if defined(TARGET_STM32F103C8T6)
+    confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
+#endif
 #if defined(DHCP)
     pc.printf("Searching for DHCP server..\r\n");
     if(UIPEthernet.begin(MY_MAC) != 1) {
@@ -210,7 +227,7 @@
                 size = client.read(buf, size);
                 string  received((char*)buf);
                 free(buf);
-                
+
                 if(received.substr(0, 3) != "GET") {
                     httpHeader = HTTP_OK;
                     httpContent = "<h1>200 OK</h1>";