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.
Dependencies: UIPEthernet_GSOE
Diff: main.cpp
- Revision:
- 11:6c0b20227ca2
- Parent:
- 10:b47c7921346f
- Child:
- 12:7c46dcf6f7e2
--- a/main.cpp Tue Aug 27 15:03:19 2019 +0000
+++ b/main.cpp Tue Aug 27 22:10:13 2019 +0000
@@ -6,7 +6,9 @@
using namespace std;
-// To be used when static IP is needed
+//#define DEBUG
+
+// To be used when a static IP is needed
#define IP "192.168.1.35"
#define GATEWAY "192.168.1.1"
#define NETMASK "255.255.255.0"
@@ -15,17 +17,18 @@
// The ENC28J60 chip doesn't have a built-in MAC address.
// It's up to us to provide one which is unique within the connected network.
// So modify the one below accordingly.
-const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
-UipEthernet net(MAC, D11, D12, D13, D10); // mosi, miso, sck, cs
-TcpServer server; // TCP server running on this mbed device
-TcpClient* client; // TCP client communicating with a HTTP Browser running on a PC or Mobile device
+const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 };
+UipEthernet net(MAC, D11, D12, D13, D10); // mosi, miso, sck, cs
+TcpServer server;
+TcpClient* client;
+char receiveBuf[1024];
const int OFF = 0;
const int ON = 1;
-DigitalOut output(D3); // A digital output to be switched on/off
-float roomTemp = 21.8; // A dummy temperature sensor output
-const string PASSWORD = "secret"; // Change as you like
-string httpHeader; // HTTP header
-string httpContent; // HTTP content
+DigitalOut output(D3); // A digital output to be switched on/off
+float roomTemp = 21.8; // A temperature sensor output
+const string PASSWORD = "secret"; // Change as you like
+string httpHeader; // HTTP header
+string httpContent; // HTTP content
/**
* @brief Analyses the received URL
@@ -202,7 +205,7 @@
"Connection: About to close\r\n\r\n";
string webpage = header + content;
- client.write((uint8_t*)webpage.c_str(), webpage.length());
+ client.send((uint8_t*)webpage.c_str(), webpage.length());
/*$on*/
}
@@ -229,7 +232,7 @@
printf("Netmask: %s\r\n", netmask ? netmask : "None");
printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
printf("------------------------------------------------------\r\n");
- printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n\r\n", net.get_ip_address(), PASSWORD.c_str());
+ printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n", net.get_ip_address(), PASSWORD.c_str());
printf("------------------------------------------------------\r\n");
/* Open the server on ethernet stack */
@@ -241,69 +244,67 @@
/* Can handle 5 simultaneous connections */
server.listen(5);
- while (1) {
+ while (true) {
client = server.accept();
if (client) {
- size_t size = client->available();
-
- if (size > 0) {
- uint8_t* buf = (uint8_t*)malloc(size);
- size = client->read(buf, size);
-
- string received((char*)buf);
- free(buf);
+ client->recv((uint8_t*)receiveBuf, client->available());
+#ifdef DEBUG
+ printf("Client with IP address %s connected.\r\n\r\n", client->getpeername());
+ printf("Data received:\r\n%s\n\r", receiveBuf);
+#endif
+ string received(receiveBuf);
- if (received.substr(0, 3) != "GET") {
- httpHeader = "HTTP/1.0 200 OK";
- httpContent = "<h1>200 OK</h1>";
- sendHTTP(*client, httpHeader, httpContent);
- client->close();
- continue;
- }
+ if (received.substr(0, 3) != "GET") {
+ httpHeader = "HTTP/1.0 200 OK";
+ httpContent = "<h1>200 OK</h1>";
+ sendHTTP(*client, httpHeader, httpContent);
+ client->close();
+ continue;
+ }
- if (received.substr(0, 6) == "GET / ") {
- httpHeader = "HTTP/1.0 200 OK";
- httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
- sendHTTP(*client, httpHeader, httpContent);
- client->close();
- continue;
- }
+ if (received.substr(0, 6) == "GET / ") {
+ httpHeader = "HTTP/1.0 200 OK";
+ httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
+ sendHTTP(*client, httpHeader, httpContent);
+ client->close();
+ continue;
+ }
- int cmd = analyseURL(received);
+ int cmd = analyseURL(received);
- switch (cmd) {
- case -3:
- // update webpage
- httpHeader = "HTTP/1.0 200 OK";
- sendHTTP(*client, httpHeader, showWebPage(output));
- break;
+ switch (cmd) {
+ case -3:
+ // update webpage
+ httpHeader = "HTTP/1.0 200 OK";
+ sendHTTP(*client, httpHeader, showWebPage(output));
+ break;
- case -2:
- // redirect to the right base url
- httpHeader = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
- sendHTTP(*client, httpHeader, movedPermanently(1));
- break;
+ case -2:
+ // redirect to the right base url
+ httpHeader = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
+ sendHTTP(*client, httpHeader, movedPermanently(1));
+ break;
- case -1:
- httpHeader = "HTTP/1.0 401 Unauthorized";
- httpContent = "<h1>401 Unauthorized</h1>";
- sendHTTP(*client, httpHeader, httpContent);
- break;
+ case -1:
+ httpHeader = "HTTP/1.0 401 Unauthorized";
+ httpContent = "<h1>401 Unauthorized</h1>";
+ sendHTTP(*client, httpHeader, httpContent);
+ break;
- case 0:
- output = OFF; // output off
- httpHeader = "HTTP/1.0 200 OK";
- sendHTTP(*client, httpHeader, showWebPage(output));
- break;
+ case 0:
+ output = OFF; // output off
+ httpHeader = "HTTP/1.0 200 OK";
+ sendHTTP(*client, httpHeader, showWebPage(output));
+ break;
- case 1:
- output = ON; // output on
- httpHeader = "HTTP/1.0 200 OK";
- sendHTTP(*client, httpHeader, showWebPage(output));
- break;
- }
+ case 1:
+ output = ON; // output on
+ httpHeader = "HTTP/1.0 200 OK";
+ sendHTTP(*client, httpHeader, showWebPage(output));
+ break;
}
+
client->close();
}
}