Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 2:33e8bb5615a6
- Parent:
- 0:e455fdb56bc8
- Child:
- 3:c927a415dd38
--- a/main.cpp Thu Apr 06 21:20:56 2017 +0000
+++ b/main.cpp Sat May 06 06:21:17 2017 +0000
@@ -19,6 +19,7 @@
const int ON = 1;
DigitalOut sw(LED1);
+float roomTemp = 21.8; // A temperature sensor output
const string PASSWORD = "secret"; // change as you like
const string HTTP_OK = "HTTP/1.0 200 OK";
@@ -26,38 +27,56 @@
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.....
+
+/**
+ * @brief Defines a custom MAC address
+ * @note Uncomment the code below to define a unique MAC address.
+ * Modify the mac array items as needed.
+ * @param
+ * @retval
+ */
+//extern "C" void mbed_mac_address(char* mac) {
+// mac[0] = 0x00;
+// mac[1] = 0x01;
+// mac[2] = 0x02;
+// mac[3] = 0x03;
+// mac[4] = 0x04;
+// mac[5] = 0x05;
+//};
-// 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);
- string cmd(str.substr(pos, 5));
+ string cmd(url.substr(pos, 5));
if(cmd == "?sw=0")
- return(OFF);
+ return(0);
if(cmd == "?sw=1")
- return(ON);
+ return(1);
return(-3);
}
@@ -86,24 +105,26 @@
* @retval
*/
string& showWebPage(uint8_t status) {
- httpContent = "<h2>WebSwitch - Smart Home</h2>\r\n";
+ char roomTempStr[5];
- httpContent += "<pre>Temperature:\t21.8°C\r\n</pre>";
+ //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) + "°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";
+ 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\nHeating:\t<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 += " \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>";
+ httpContent += "<pre>2017 ARMmbed</pre>";
return httpContent;
}
@@ -151,13 +172,15 @@
//listening for http GET request
while(true) {
+ printf("\r\n=========================================\r\n");
+ printf("Ready to serve clients.\r\n");
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>";