GroupZ - 05012018 1511
main.cpp@0:1c898341428b, 2018-01-05 (annotated)
- Committer:
- mslade
- Date:
- Fri Jan 05 15:10:50 2018 +0000
- Revision:
- 0:1c898341428b
- Child:
- 1:84581acd1333
group Z 05012018-1510
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mslade | 0:1c898341428b | 1 | #include "mbed.h" |
mslade | 0:1c898341428b | 2 | #include "TextLCD.h" |
mslade | 0:1c898341428b | 3 | #include "math.h" |
mslade | 0:1c898341428b | 4 | #include "EthernetInterface.h" |
mslade | 0:1c898341428b | 5 | #include "TCPServer.h" |
mslade | 0:1c898341428b | 6 | #include "TCPSocket.h" |
mslade | 0:1c898341428b | 7 | #include "BMP280.h" |
mslade | 0:1c898341428b | 8 | #include <string> |
mslade | 0:1c898341428b | 9 | #include <iostream> |
mslade | 0:1c898341428b | 10 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
mslade | 0:1c898341428b | 11 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
mslade | 0:1c898341428b | 12 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
mslade | 0:1c898341428b | 13 | HTTP_HEADER_FIELDS "\r\n" \ |
mslade | 0:1c898341428b | 14 | "\r\n" \ |
mslade | 0:1c898341428b | 15 | HTTP_MESSAGE_BODY "\r\n" |
mslade | 0:1c898341428b | 16 | |
mslade | 0:1c898341428b | 17 | #define IP "10.0.0.10" |
mslade | 0:1c898341428b | 18 | #define NETMASK "255.0.0.0" |
mslade | 0:1c898341428b | 19 | #define GATEWAY "10.0.0.1" |
mslade | 0:1c898341428b | 20 | |
mslade | 0:1c898341428b | 21 | |
mslade | 0:1c898341428b | 22 | BMP280 BMP(D14, D15); |
mslade | 0:1c898341428b | 23 | Serial myserial(USBTX,USBRX); |
mslade | 0:1c898341428b | 24 | TextLCD mylcd(D9,D8,D7,D6,D5,D4); |
mslade | 0:1c898341428b | 25 | BusOut leds(D11,D12); |
mslade | 0:1c898341428b | 26 | DigitalIn toggle(D2); |
mslade | 0:1c898341428b | 27 | bool pressed = false; |
mslade | 0:1c898341428b | 28 | int counter = 0; |
mslade | 0:1c898341428b | 29 | AnalogIn ldrin(A0); |
mslade | 0:1c898341428b | 30 | Thread thread; |
mslade | 0:1c898341428b | 31 | Thread thread2; |
mslade | 0:1c898341428b | 32 | Semaphore mysem(10); |
mslade | 0:1c898341428b | 33 | char buffer[10]; |
mslade | 0:1c898341428b | 34 | char buffer2[10]; |
mslade | 0:1c898341428b | 35 | char buffer3[10]; |
mslade | 0:1c898341428b | 36 | void enet(void const *name); |
mslade | 0:1c898341428b | 37 | |
mslade | 0:1c898341428b | 38 | int main(){ |
mslade | 0:1c898341428b | 39 | myserial.baud(115200); |
mslade | 0:1c898341428b | 40 | mylcd.cls(); |
mslade | 0:1c898341428b | 41 | thread.start(callback(enet, (void *) "Thread\r\n")); |
mslade | 0:1c898341428b | 42 | while(true) { |
mslade | 0:1c898341428b | 43 | mysem.wait(); |
mslade | 0:1c898341428b | 44 | leds = 1; |
mslade | 0:1c898341428b | 45 | float ldr = floor(ldrin*30000)/100; |
mslade | 0:1c898341428b | 46 | float temp = BMP.getTemperature(); |
mslade | 0:1c898341428b | 47 | float pressure = BMP.getPressure() ; |
mslade | 0:1c898341428b | 48 | //if (counter > 7) {counter = 0;} |
mslade | 0:1c898341428b | 49 | //leds = counter; |
mslade | 0:1c898341428b | 50 | //if (toggle == false) {if (pressed == false) {counter++; pressed = true;} } else {pressed = false;} |
mslade | 0:1c898341428b | 51 | //int x = floor(ldr*8); |
mslade | 0:1c898341428b | 52 | //leds = counter; |
mslade | 0:1c898341428b | 53 | sprintf(buffer , "%2.2f", ldr); |
mslade | 0:1c898341428b | 54 | sprintf(buffer2, "%2.2f", temp); |
mslade | 0:1c898341428b | 55 | sprintf(buffer3, "%3.2f", pressure); |
mslade | 0:1c898341428b | 56 | mylcd.locate(9,1); mylcd.printf("L:%s ", buffer ); |
mslade | 0:1c898341428b | 57 | mylcd.locate(0,1); mylcd.printf("T:%s ", buffer2); |
mslade | 0:1c898341428b | 58 | mylcd.locate(4,0); mylcd.printf("P:%s ", buffer3); |
mslade | 0:1c898341428b | 59 | myserial.printf("ldr=%s temp=%s pres=%s \r\n", buffer, buffer2, buffer3); |
mslade | 0:1c898341428b | 60 | |
mslade | 0:1c898341428b | 61 | Thread::wait(1000); |
mslade | 0:1c898341428b | 62 | leds = 0; |
mslade | 0:1c898341428b | 63 | mysem.release(); |
mslade | 0:1c898341428b | 64 | } |
mslade | 0:1c898341428b | 65 | } |
mslade | 0:1c898341428b | 66 | |
mslade | 0:1c898341428b | 67 | |
mslade | 0:1c898341428b | 68 | void enet(void const *name) |
mslade | 0:1c898341428b | 69 | { |
mslade | 0:1c898341428b | 70 | //Configure an ethernet connection |
mslade | 0:1c898341428b | 71 | EthernetInterface eth; |
mslade | 0:1c898341428b | 72 | eth.set_network(IP, NETMASK, GATEWAY); |
mslade | 0:1c898341428b | 73 | eth.connect(); |
mslade | 0:1c898341428b | 74 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
mslade | 0:1c898341428b | 75 | |
mslade | 0:1c898341428b | 76 | //Now setup a web server |
mslade | 0:1c898341428b | 77 | TCPServer srv; //TCP/IP Server |
mslade | 0:1c898341428b | 78 | //TCPSocket clt_sock; //Socket for communication |
mslade | 0:1c898341428b | 79 | SocketAddress clt_addr; //Address of incoming connection |
mslade | 0:1c898341428b | 80 | |
mslade | 0:1c898341428b | 81 | /* Open the server on ethernet stack */ |
mslade | 0:1c898341428b | 82 | srv.open(ð); |
mslade | 0:1c898341428b | 83 | |
mslade | 0:1c898341428b | 84 | /* Bind the HTTP port (TCP 80) to the server */ |
mslade | 0:1c898341428b | 85 | srv.bind(eth.get_ip_address(), 80); |
mslade | 0:1c898341428b | 86 | |
mslade | 0:1c898341428b | 87 | /* Can handle 5 simultaneous connections */ |
mslade | 0:1c898341428b | 88 | srv.listen(5); |
mslade | 0:1c898341428b | 89 | |
mslade | 0:1c898341428b | 90 | while (true) { |
mslade | 0:1c898341428b | 91 | mysem.wait(); |
mslade | 0:1c898341428b | 92 | using namespace std; |
mslade | 0:1c898341428b | 93 | TCPSocket clt_sock; |
mslade | 0:1c898341428b | 94 | srv.accept(&clt_sock, &clt_addr); |
mslade | 0:1c898341428b | 95 | string response; |
mslade | 0:1c898341428b | 96 | response = "<html> \r\n "; |
mslade | 0:1c898341428b | 97 | response += "<meta http-equiv=\"refresh\" content=\"1; url=http://10.0.0.10\">"; |
mslade | 0:1c898341428b | 98 | response += "\r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:11%; left:37%;\"> "; |
mslade | 0:1c898341428b | 99 | response += "16:34"; |
mslade | 0:1c898341428b | 100 | response += "</h1> \r\n <h1 style=\"color:black; font-size: 200%; position: fixed; top:17.5%; left:55.5%;\">"; |
mslade | 0:1c898341428b | 101 | response += "31/12/2017"; |
mslade | 0:1c898341428b | 102 | response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:30%; left:46%;\">"; |
mslade | 0:1c898341428b | 103 | response += buffer; |
mslade | 0:1c898341428b | 104 | response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:51%; left:46%;\">"; |
mslade | 0:1c898341428b | 105 | response += buffer2; |
mslade | 0:1c898341428b | 106 | response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:72%; left:46%;\">"; |
mslade | 0:1c898341428b | 107 | response += buffer3; |
mslade | 0:1c898341428b | 108 | response += "</h1> \r\n <body style=\"background-image:url(https://image.ibb.co/nyTj9w/pic2.png); background-size: 100% 100%;\"></body> "; |
mslade | 0:1c898341428b | 109 | response += "\r\n </html>"; |
mslade | 0:1c898341428b | 110 | clt_sock.send(response.c_str(), response.size()+1); |
mslade | 0:1c898341428b | 111 | Thread::wait(10); |
mslade | 0:1c898341428b | 112 | mysem.release(); |
mslade | 0:1c898341428b | 113 | } |
mslade | 0:1c898341428b | 114 | } |