Zoltan Hudak / Mbed OS WebSwitch_mbed-os

Fork of WebSwitch_mbed-os by Zoltan Hudak

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Thu Apr 06 18:58:40 2017 +0000
Child:
1:be0139af12f2
Commit message:
Initial release.

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
img/uvision.png Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Thu Apr 06 18:58:40 2017 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Thu Apr 06 18:58:40 2017 +0000
@@ -0,0 +1,87 @@
+# Getting started with Blinky on mbed OS
+
+This guide reviews the steps required to get Blinky working on an mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-blinky
+cd mbed-os-example-blinky
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++----------------------------+-------+-------+------+
+| Module                     | .text | .data | .bss |
++----------------------------+-------+-------+------+
+| Misc                       | 13939 |    24 | 1372 |
+| core/hal                   | 16993 |    96 |  296 |
+| core/rtos                  |  7384 |    92 | 4204 |
+| features/FEATURE_IPV4      |    80 |     0 |  176 |
+| frameworks/greentea-client |  1830 |    60 |   44 |
+| frameworks/utest           |  2392 |   512 |  292 |
+| Subtotals                  | 42618 |   784 | 6384 |
++----------------------------+-------+-------+------+
+Allocated Heap: unknown
+Allocated Stack: unknown
+Total Static RAM memory (data + bss): 7168 bytes
+Total RAM memory (data + bss + heap + stack): 7168 bytes
+Total Flash memory (text + data + misc): 43402 bytes
+Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
+```
+
+### Program your board
+
+1. Connect your mbed device to the computer over USB.
+1. Copy the binary file to the mbed device.
+1. Press the reset button to start the program.
+
+The LED on your platform turns on and off.
+
+## Export the project to Keil MDK, and debug your application
+
+From the command-line, run the following command:
+
+```
+mbed export -m K64F -i uvision
+```
+
+To debug the application:
+
+1. Start uVision.
+1. Import the uVision project generated earlier.
+1. Compile your application, and generate an `.axf` file.
+1. Make sure uVision is configured to debug over CMSIS-DAP (From the Project menu > Options for Target '...' > Debug tab > Use CMSIS-DAP Debugger).
+1. Set breakpoints, and start a debug session.
+
+![Image of uVision](img/uvision.png)
+
+## Troubleshooting
+
+1. Make sure `mbed-cli` is working correctly and its version is `>1.0.0`
+
+ ```
+ mbed --version
+ ```
+
+ If not, you can update it:
+
+ ```
+ pip install mbed-cli --upgrade
+ ```
+
+2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32 KB restriction on code size.
\ No newline at end of file
Binary file img/uvision.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Apr 06 18:58:40 2017 +0000
@@ -0,0 +1,203 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+#include <stdio.h>
+#include <string>
+
+using namespace     std;
+
+#define PORT        80
+
+EthernetInterface   ethernet;
+TCPServer           server;
+TCPSocket           clientSocket;
+SocketAddress       clientAddress;
+char                receiveBuf[1024] = { };
+
+const int           OFF = 0;
+const int           ON = 1;
+
+DigitalOut          sw(LED1);
+
+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: ";
+const string        UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
+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)
+        return(-1);
+
+    uint8_t pos = 5 + PASSWORD.size();
+
+    if(str.substr(pos, 1) == " ")
+        return(-2);
+
+    if(str.substr(pos++, 1) != "/")
+        return(-1);
+
+    string  cmd(str.substr(pos, 5));
+
+    if(cmd == "?sw=0")
+        return(OFF);
+
+    if(cmd == "?sw=1")
+        return(ON);
+
+    return(-3);
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+string& movedPermanently(uint8_t flag) {
+    if(flag == 1)
+        httpContent = "/" + PASSWORD + "/";
+    else
+        httpContent = "";
+
+    httpContent += "<h1>301 Moved Permanently</h1>\r\n";
+
+    return(httpContent);
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+string& showWebPage(uint8_t status) {
+    httpContent = "<h2>WebSwitch - Smart Home</h2>\r\n";
+
+    httpContent += "<pre>Temperature:\t21.8&deg;C\r\n</pre>";
+
+    if(status == ON) {
+        httpContent += "<pre>\r\nHeating:\t<font color=#FF0000>ON </font>";
+        httpContent += " <a href=\"./?sw=0\">[Turn off]</a>\r\n";
+    }
+    else {
+        httpContent += "<pre>\r\nHeating:\t<font color=#BBBBBB>OFF</font>";
+        httpContent += " <a href=\"./?sw=1\">[Turn on]</a>\r\n";
+    }
+
+    //httpContent += "  \r\n";
+    //httpContent += "  <a href=\".\">Refresh status]</a>\r\n";
+    httpContent += "</pre>\r\n";
+    httpContent += "<hr>\r\n";
+    httpContent += "<pre>2017 ARMmbed Open Source</pre>";
+    return httpContent;
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void sendHTTP(TCPSocket& client, string& header, string& content) {
+    char    content_length[5] = { };
+
+    header += "\r\nContent-Type: text/html\r\n";
+    header += "Content-Length: ";
+    sprintf(content_length, "%d", content.length());
+    header += string(content_length) + "\r\n";
+    header += "Pragma: no-cache\r\n";
+    header += "Connection: About to close\r\n";
+    header += "\r\n";
+
+    string  webpage = header + content;
+    client.send((char*)webpage.c_str(), webpage.length());
+    printf("HTTP sent.\n\r");
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+int main(void) {
+
+    ethernet.connect();
+    printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n", ethernet.get_ip_address(), PASSWORD);
+
+    /* Open the server on ethernet stack */
+    server.open(&ethernet);
+   
+    /* Bind the HTTP port (TCP 80) to the server */
+    server.bind(ethernet.get_ip_address(), 80);
+    
+    /* Can handle 5 simultaneous connections */
+    server.listen(5);
+
+    //listening for http GET request
+    while(true) {
+        server.accept(&clientSocket, &clientAddress);
+        printf("\r\n=========================================\r\n");
+        printf("Connection succeeded!\n\rIP: %s\n\r", clientAddress.get_ip_address());
+        clientSocket.recv(receiveBuf, 1023);
+        printf("Recieved Data: %d\n\r\n\r%.*s\n\r", strlen(receiveBuf), strlen(receiveBuf), receiveBuf);
+
+        string  received(receiveBuf);
+        if(received.substr(0, 3) != "GET") {
+            httpHeader = HTTP_OK;
+            httpContent = "<h1>200 OK</h1>";
+            sendHTTP(clientSocket, httpHeader, httpContent);
+            continue;
+        }
+
+        if(received.substr(0, 6) == "GET / ") {
+            httpHeader = HTTP_OK;
+            httpContent = "<p>Usage: Type http://ip_address/password/ into your web browser and hit ENTER</p>\r\n";
+            sendHTTP(clientSocket, httpHeader, httpContent);
+            continue;
+        }
+
+        int cmd = analyseURL(received);
+
+        if(cmd == -2) {
+
+            // redirect to the right base url
+            httpHeader = MOVED_PERM;
+            sendHTTP(clientSocket, httpHeader, movedPermanently(1));
+            continue;
+        }
+
+        if(cmd == -1) {
+            httpHeader = UNAUTHORIZED;
+            httpContent = "<h1>401 Unauthorized</h1>";
+            sendHTTP(clientSocket, httpHeader, httpContent);
+            continue;
+        }
+
+        if(cmd == ON) {
+            sw = ON;    // turn the switch on
+        }
+
+        if(cmd == OFF) {
+            sw = OFF;   // turn the switch off
+        }
+
+        httpHeader = HTTP_OK;
+        sendHTTP(clientSocket, httpHeader, showWebPage(sw));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Apr 06 18:58:40 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#50b3418e45484ebf442b88cd935a2d5355402d7d