Liam Grazier
/
0NicksCoursework_copywithserialtime
aaa
Fork of 0NicksCoursework-lg by
Network/network.cpp@8:582ac4c5a524, 2018-01-09 (annotated)
- Committer:
- liam_grazier
- Date:
- Tue Jan 09 05:43:43 2018 +0000
- Revision:
- 8:582ac4c5a524
dfshjsdfjhdf;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
liam_grazier | 8:582ac4c5a524 | 1 | #if !FEATURE_LWIP |
liam_grazier | 8:582ac4c5a524 | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
liam_grazier | 8:582ac4c5a524 | 3 | #endif |
liam_grazier | 8:582ac4c5a524 | 4 | #include "mbed.h" |
liam_grazier | 8:582ac4c5a524 | 5 | #include "EthernetInterface.h" |
liam_grazier | 8:582ac4c5a524 | 6 | #include "TCPServer.h" |
liam_grazier | 8:582ac4c5a524 | 7 | #include "TCPSocket.h" |
liam_grazier | 8:582ac4c5a524 | 8 | char realtime[32]; |
liam_grazier | 8:582ac4c5a524 | 9 | #include <iostream> |
liam_grazier | 8:582ac4c5a524 | 10 | #include <string> |
liam_grazier | 8:582ac4c5a524 | 11 | #include "BMP280.h" |
liam_grazier | 8:582ac4c5a524 | 12 | #include "components.hpp" |
liam_grazier | 8:582ac4c5a524 | 13 | #include "network.hpp" |
liam_grazier | 8:582ac4c5a524 | 14 | Mutex Net; |
liam_grazier | 8:582ac4c5a524 | 15 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
liam_grazier | 8:582ac4c5a524 | 16 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
liam_grazier | 8:582ac4c5a524 | 17 | #define HTTP_MESSAGE_BODY1 "" \ |
liam_grazier | 8:582ac4c5a524 | 18 | "<html>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 19 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 20 | " <div style=\"margin:auto\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 21 | " <h1>Sensor Data</h1>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 22 | " <p>Light Level LDR value: " "\r\n" |
liam_grazier | 8:582ac4c5a524 | 23 | |
liam_grazier | 8:582ac4c5a524 | 24 | #define HTTP_Pressure1 "" \ |
liam_grazier | 8:582ac4c5a524 | 25 | "<html>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 26 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 27 | " <div style=\"margin:auto\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 28 | " <h1> </h1>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 29 | " <p>Pressure(mbar): " "\r\n" |
liam_grazier | 8:582ac4c5a524 | 30 | |
liam_grazier | 8:582ac4c5a524 | 31 | #define HTTP_Pressure2 "" \ |
liam_grazier | 8:582ac4c5a524 | 32 | "</p>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 33 | " </div>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 34 | " </body>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 35 | "</html>" |
liam_grazier | 8:582ac4c5a524 | 36 | #define HTTP_TIME "" \ |
liam_grazier | 8:582ac4c5a524 | 37 | "<html>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 38 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 39 | " <div style=\"margin:auto\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 40 | " <h1> </h1>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 41 | " <p>Time: " "\r\n" |
liam_grazier | 8:582ac4c5a524 | 42 | |
liam_grazier | 8:582ac4c5a524 | 43 | #define HTTP_Temperature1 "" \ |
liam_grazier | 8:582ac4c5a524 | 44 | "<html>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 45 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 46 | " <div style=\"margin:auto\">" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 47 | " <h1> </h1>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 48 | " <p>Temperature(Degrees Celcius): " "\r\n" |
liam_grazier | 8:582ac4c5a524 | 49 | |
liam_grazier | 8:582ac4c5a524 | 50 | #define HTTP_Temperature2 "" \ |
liam_grazier | 8:582ac4c5a524 | 51 | "</p>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 52 | " </div>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 53 | " </body>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 54 | "</html>" |
liam_grazier | 8:582ac4c5a524 | 55 | #define HTTP_MESSAGE_TIME "" \ |
liam_grazier | 8:582ac4c5a524 | 56 | "</p>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 57 | " </div>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 58 | " </body>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 59 | "</html>" |
liam_grazier | 8:582ac4c5a524 | 60 | #define HTTP_MESSAGE_BODY2 "" \ |
liam_grazier | 8:582ac4c5a524 | 61 | "</p>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 62 | " </div>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 63 | " </body>" "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 64 | "</html>" |
liam_grazier | 8:582ac4c5a524 | 65 | |
liam_grazier | 8:582ac4c5a524 | 66 | |
liam_grazier | 8:582ac4c5a524 | 67 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 68 | HTTP_HEADER_FIELDS "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 69 | "\r\n" \ |
liam_grazier | 8:582ac4c5a524 | 70 | HTTP_MESSAGE_BODY "\r\n" |
liam_grazier | 8:582ac4c5a524 | 71 | |
liam_grazier | 8:582ac4c5a524 | 72 | #define IP "10.0.0.10" |
liam_grazier | 8:582ac4c5a524 | 73 | #define NETMASK "255.0.0.0" |
liam_grazier | 8:582ac4c5a524 | 74 | #define GATEWAY "10.0.0.1" |
liam_grazier | 8:582ac4c5a524 | 75 | void dispstralltime(){ |
liam_grazier | 8:582ac4c5a524 | 76 | time_t seconds = time(NULL); |
liam_grazier | 8:582ac4c5a524 | 77 | strftime(realtime, 32, "%c\n\r", localtime(&seconds)); |
liam_grazier | 8:582ac4c5a524 | 78 | } |
liam_grazier | 8:582ac4c5a524 | 79 | void networksend(void) |
liam_grazier | 8:582ac4c5a524 | 80 | { |
liam_grazier | 8:582ac4c5a524 | 81 | // interrupt routine setup |
liam_grazier | 8:582ac4c5a524 | 82 | Net.lock(); |
liam_grazier | 8:582ac4c5a524 | 83 | printf("Network Enabled\n\r");//n |
liam_grazier | 8:582ac4c5a524 | 84 | //Configure an ethernet connection |
liam_grazier | 8:582ac4c5a524 | 85 | EthernetInterface eth; |
liam_grazier | 8:582ac4c5a524 | 86 | eth.set_network(IP, NETMASK, GATEWAY); |
liam_grazier | 8:582ac4c5a524 | 87 | eth.connect(); |
liam_grazier | 8:582ac4c5a524 | 88 | // |
liam_grazier | 8:582ac4c5a524 | 89 | Net.unlock(); |
liam_grazier | 8:582ac4c5a524 | 90 | //Now setup a web server |
liam_grazier | 8:582ac4c5a524 | 91 | TCPServer srv; //TCP/IP Server |
liam_grazier | 8:582ac4c5a524 | 92 | TCPSocket clt_sock; //Socket for communication |
liam_grazier | 8:582ac4c5a524 | 93 | SocketAddress clt_addr; //Address of incoming connection |
liam_grazier | 8:582ac4c5a524 | 94 | |
liam_grazier | 8:582ac4c5a524 | 95 | /* Open the server on ethernet stack */ |
liam_grazier | 8:582ac4c5a524 | 96 | srv.open(ð); |
liam_grazier | 8:582ac4c5a524 | 97 | |
liam_grazier | 8:582ac4c5a524 | 98 | /* Bind the HTTP port (TCP 80) to the server */ |
liam_grazier | 8:582ac4c5a524 | 99 | srv.bind(eth.get_ip_address(), 80); |
liam_grazier | 8:582ac4c5a524 | 100 | |
liam_grazier | 8:582ac4c5a524 | 101 | /* Can handle 5 simultaneous connections */ |
liam_grazier | 8:582ac4c5a524 | 102 | srv.listen(5); |
liam_grazier | 8:582ac4c5a524 | 103 | |
liam_grazier | 8:582ac4c5a524 | 104 | while (true) { |
liam_grazier | 8:582ac4c5a524 | 105 | Net.lock(); |
liam_grazier | 8:582ac4c5a524 | 106 | using namespace std; |
liam_grazier | 8:582ac4c5a524 | 107 | //Block and wait on an incoming connection |
liam_grazier | 8:582ac4c5a524 | 108 | srv.accept(&clt_sock, &clt_addr); |
liam_grazier | 8:582ac4c5a524 | 109 | // printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
liam_grazier | 8:582ac4c5a524 | 110 | |
liam_grazier | 8:582ac4c5a524 | 111 | double temp = sensor.getTemperature(); |
liam_grazier | 8:582ac4c5a524 | 112 | double pres = sensor.getPressure(); |
liam_grazier | 8:582ac4c5a524 | 113 | //Uses a C++ string to make it easier to concatinate |
liam_grazier | 8:582ac4c5a524 | 114 | string response; |
liam_grazier | 8:582ac4c5a524 | 115 | //This is a C string |
liam_grazier | 8:582ac4c5a524 | 116 | char adcIn_str[64]; |
liam_grazier | 8:582ac4c5a524 | 117 | char pres_str[64]; |
liam_grazier | 8:582ac4c5a524 | 118 | char temp_str[64]; |
liam_grazier | 8:582ac4c5a524 | 119 | dispstralltime(); |
liam_grazier | 8:582ac4c5a524 | 120 | //Read the LDR value |
liam_grazier | 8:582ac4c5a524 | 121 | float u = adcIn; |
liam_grazier | 8:582ac4c5a524 | 122 | float b = pres; |
liam_grazier | 8:582ac4c5a524 | 123 | float a = temp; |
liam_grazier | 8:582ac4c5a524 | 124 | |
liam_grazier | 8:582ac4c5a524 | 125 | //Convert to a C String |
liam_grazier | 8:582ac4c5a524 | 126 | sprintf(adcIn_str, "%5.3f", u ); |
liam_grazier | 8:582ac4c5a524 | 127 | sprintf(pres_str, "%4.2f", b); |
liam_grazier | 8:582ac4c5a524 | 128 | sprintf(temp_str, "%3.1f", a); |
liam_grazier | 8:582ac4c5a524 | 129 | |
liam_grazier | 8:582ac4c5a524 | 130 | //Build the C++ string response |
liam_grazier | 8:582ac4c5a524 | 131 | response += HTTP_TIME; |
liam_grazier | 8:582ac4c5a524 | 132 | response += realtime; |
liam_grazier | 8:582ac4c5a524 | 133 | response += HTTP_MESSAGE_BODY1; |
liam_grazier | 8:582ac4c5a524 | 134 | response += adcIn_str; |
liam_grazier | 8:582ac4c5a524 | 135 | response += HTTP_Temperature1; |
liam_grazier | 8:582ac4c5a524 | 136 | response += temp_str; |
liam_grazier | 8:582ac4c5a524 | 137 | response += HTTP_Temperature2; |
liam_grazier | 8:582ac4c5a524 | 138 | response += HTTP_Pressure1; |
liam_grazier | 8:582ac4c5a524 | 139 | response += pres_str; |
liam_grazier | 8:582ac4c5a524 | 140 | response += HTTP_Pressure2; |
liam_grazier | 8:582ac4c5a524 | 141 | response += HTTP_MESSAGE_TIME; |
liam_grazier | 8:582ac4c5a524 | 142 | response += HTTP_MESSAGE_BODY2; |
liam_grazier | 8:582ac4c5a524 | 143 | //Send static HTML response (as a C string) |
liam_grazier | 8:582ac4c5a524 | 144 | clt_sock.send(response.c_str(), response.size()+6); |
liam_grazier | 8:582ac4c5a524 | 145 | Net.unlock(); |
liam_grazier | 8:582ac4c5a524 | 146 | Thread::signal_wait(SIG_NET); |
liam_grazier | 8:582ac4c5a524 | 147 | } |
liam_grazier | 8:582ac4c5a524 | 148 | } |