Updated
Revision 0:9df0dd20731a, committed 2019-01-05
- Comitter:
- Swabey89
- Date:
- Sat Jan 05 15:02:17 2019 +0000
- Commit message:
- Updated
Changed in this revision
Networking.cpp | Show annotated file Show diff for this revision Revisions of this file |
Networking.hpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9df0dd20731a Networking.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Networking.cpp Sat Jan 05 15:02:17 2019 +0000 @@ -0,0 +1,92 @@ +#include "Networking.hpp" + +//Network thread - responsible for listening for connectinos and responding with updated tempature values +void network() +{ + EthernetInterface eth; + eth.set_network(IP, NETMASK, GATEWAY); + eth.connect(); + + printQueue.call(printf, "The target IP address is '%s'\n\n\n\r", eth.get_ip_address()); + + //Now setup a web server + TCPServer srv; //TCP/IP Server + TCPSocket clt_sock; //Socket for communication + SocketAddress clt_addr; //Address of incoming connection + + /* Open the server on ethernet stack */ + srv.open(ð); + + /* Bind the HTTP port (TCP 80) to the server */ + srv.bind(eth.get_ip_address(), 80); + + /* Can handle 5 simultaneous connections */ + srv.listen(5); + + while(true) + { + //Block and wait on an incoming connection + srv.accept(&clt_sock, &clt_addr); + + network_tout.attach(network_toutISR,TOUT_TIME_DEF); + + if(logging) + { + printQueue.call(printf,"Incoming connection accepted on %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port()); + } + + //Uses a C++ string to make it easier to concatinate + { + string response; + char temp_str[10]; + char press_str[10]; + char light_str[10]; + + bufferLock.lock(); + double temp = buffer[newestIndex].gettemp(); + double press = buffer[newestIndex].getpress(); + float light = buffer[newestIndex].getlight()*100; + string time_str = buffer[newestIndex].getTime(); + bufferLock.unlock(); + + //Convert to a C String + sprintf(temp_str, "%5.2f", temp); + sprintf(press_str, "%5.2f", press); + sprintf(light_str, "%5.2f", light); + + + //Build the C++ string response + response = HTTP_RESPONSE; + + response += HTTP_MESSAGE_TIME; + response += time_str; + + response += HTTP_MESSAGE_TEMP; + response += temp_str; + + response += HTTP_MESSAGE_PRESSURE; + response += press_str; + + response += HTTP_MESSAGE_LIGHT; + response += light_str; + + response += HTTP_MESSAGE_FOOT; + + response += "\r\n"; + + //Send static HTML response (as a C string) + clt_sock.send(response.c_str(), response.size()); + + if(logging) + { + printQueue.call(printf, "Network thread responded with time: %s, temperature: %s, pressure:%s and light level: %s\r\n\n",time_str, temp_str, press_str, light_str); + } + } + network_tout.detach(); + } +} + +void network_toutISR(void) +{ + threadstates |= NETWORK; +} \ No newline at end of file
diff -r 000000000000 -r 9df0dd20731a Networking.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Networking.hpp Sat Jan 05 15:02:17 2019 +0000 @@ -0,0 +1,101 @@ +#ifndef MBED_NETWORKING_H +#define MBED_NETWORKING_H + +#include "mbed.h" +#include "main.h" +#include "sample_hardware.hpp" +#include "EthernetInterface.h" +#include "TCPServer.h" +#include "TCPSocket.h" +#include <iostream> +#include <string> + +#define IP "10.0.0.10" +#define NETMASK "255.0.0.0" +#define GATEWAY "10.0.0.1" + +#define HTTP_STATUS_LINE "HTTP/1.0 200 OK" + +#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" + +#define HTTP_MESSAGE_BODY "" \ +" <html>" "\r\n" \ +" <head>" "\r\n" \ +" <style>" "\r\n" \ +" .title {" "\r\n" \ +" color: dodgerblue;" "\r\n" \ +" font-family: verdana;" "\r\n" \ +" text-align: center;" "\r\n" \ +" padding: 5px;" "\r\n" \ +" }" "\r\n" \ +" </style>" "\r\n" \ +" <style>" "\r\n" \ +" .textbox {" "\r\n" \ +" background-color: dodgerblue;" "\r\n" \ +" color: white;" "\r\n" \ +" font-family: verdana;" "\r\n" \ +" margin: 20px;" "\r\n" \ +" padding: 20px;" "\r\n" \ +" }" "\r\n" \ +" </style>" "\r\n" \ +" <style>" "\r\n" \ +" .footer {" "\r\n" \ +" color: dodgerblue;" "\r\n" \ +" font-family: verdana;" "\r\n" \ +" text-align: right;" "\r\n" \ +" padding: 20px;" "\r\n" \ +" }" "\r\n" \ +" </style>" "\r\n" \ +" </head>" "\r\n" \ +" <body>" "\r\n" \ + +#define HTTP_MESSAGE_TIME "" \ +" <div class=\"title\">" "\r\n" \ +" <h1>Environmental Sensor</h1>" "\r\n" \ +" <p> Sample Time: " + +#define HTTP_MESSAGE_TEMP "" \ +" </p>" "\r\n" \ +" </div>" "\r\n" \ +" <div style=\"text-align: center\">" "\r\n" \ +" <input type='button' onclick='location.reload();' value='Refresh Data' />" "\r\n" \ +" </div>" "\r\n" \ +" <div class=\"textbox\">" "\r\n" \ +" <h2>Temperature (<sup>o</sup>C)</h2>" "\r\n" \ +" <p>" + +#define HTTP_MESSAGE_PRESSURE "" \ +" </p>" "\r\n" \ +" </div>" "\r\n" \ +" <div class=\"textbox\">" "\r\n" \ +" <h2>Pressure (mbar)</h2>" "\r\n" \ +" <p>" + +#define HTTP_MESSAGE_LIGHT "" \ +" </p>" "\r\n" \ +" </div>" "\r\n" \ +" <div class=\"textbox\">" "\r\n" \ +" <h2>Light Level (%)</h2>" "\r\n" \ +" <p>" + +#define HTTP_MESSAGE_FOOT "" \ +" </p>" "\r\n" \ +" </div>" "\r\n" \ +" <footer class=\"footer\">" "\r\n" \ +" <img src=\"https://cdn-edu.wpmhost.net/53544f/6bf071deb4/A1F4-0FF4-4F67-AE2B-9DAC.png\" width=\"250\" height=\"60\">" "\r\n" \ +" <p>ELEC350 - Low Power Environmental Sensor Coursework</p>" "\r\n" \ +" </footer>" "\r\n" \ +" </body>" "\r\n" \ +" </html>" + +#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ + HTTP_HEADER_FIELDS "\r\n" \ + "\r\n" \ + HTTP_MESSAGE_BODY "\r\n" \ + + + +extern void network(void); +extern void network_toutISR(void); + +#endif \ No newline at end of file