GroupZ - 05012018 1511
Diff: main.cpp
- Revision:
- 0:1c898341428b
- Child:
- 1:84581acd1333
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 05 15:10:50 2018 +0000 @@ -0,0 +1,114 @@ +#include "mbed.h" +#include "TextLCD.h" +#include "math.h" +#include "EthernetInterface.h" +#include "TCPServer.h" +#include "TCPSocket.h" +#include "BMP280.h" +#include <string> +#include <iostream> +#define HTTP_STATUS_LINE "HTTP/1.0 200 OK" +#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" +#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ + HTTP_HEADER_FIELDS "\r\n" \ + "\r\n" \ + HTTP_MESSAGE_BODY "\r\n" + +#define IP "10.0.0.10" +#define NETMASK "255.0.0.0" +#define GATEWAY "10.0.0.1" + + +BMP280 BMP(D14, D15); +Serial myserial(USBTX,USBRX); +TextLCD mylcd(D9,D8,D7,D6,D5,D4); +BusOut leds(D11,D12); +DigitalIn toggle(D2); +bool pressed = false; +int counter = 0; +AnalogIn ldrin(A0); +Thread thread; +Thread thread2; +Semaphore mysem(10); +char buffer[10]; +char buffer2[10]; +char buffer3[10]; +void enet(void const *name); + +int main(){ + myserial.baud(115200); + mylcd.cls(); + thread.start(callback(enet, (void *) "Thread\r\n")); + while(true) { + mysem.wait(); + leds = 1; + float ldr = floor(ldrin*30000)/100; + float temp = BMP.getTemperature(); + float pressure = BMP.getPressure() ; + //if (counter > 7) {counter = 0;} + //leds = counter; + //if (toggle == false) {if (pressed == false) {counter++; pressed = true;} } else {pressed = false;} + //int x = floor(ldr*8); + //leds = counter; + sprintf(buffer , "%2.2f", ldr); + sprintf(buffer2, "%2.2f", temp); + sprintf(buffer3, "%3.2f", pressure); + mylcd.locate(9,1); mylcd.printf("L:%s ", buffer ); + mylcd.locate(0,1); mylcd.printf("T:%s ", buffer2); + mylcd.locate(4,0); mylcd.printf("P:%s ", buffer3); + myserial.printf("ldr=%s temp=%s pres=%s \r\n", buffer, buffer2, buffer3); + + Thread::wait(1000); + leds = 0; + mysem.release(); + } +} + + +void enet(void const *name) +{ + //Configure an ethernet connection + EthernetInterface eth; + eth.set_network(IP, NETMASK, GATEWAY); + eth.connect(); + printf("The target IP address is '%s'\n", 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) { + mysem.wait(); + using namespace std; + TCPSocket clt_sock; + srv.accept(&clt_sock, &clt_addr); + string response; + response = "<html> \r\n "; + response += "<meta http-equiv=\"refresh\" content=\"1; url=http://10.0.0.10\">"; + response += "\r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:11%; left:37%;\"> "; + response += "16:34"; + response += "</h1> \r\n <h1 style=\"color:black; font-size: 200%; position: fixed; top:17.5%; left:55.5%;\">"; + response += "31/12/2017"; + response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:30%; left:46%;\">"; + response += buffer; + response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:51%; left:46%;\">"; + response += buffer2; + response += "</h1> \r\n <h1 style=\"color:black; font-size: 400%; position: fixed; top:72%; left:46%;\">"; + response += buffer3; + response += "</h1> \r\n <body style=\"background-image:url(https://image.ibb.co/nyTj9w/pic2.png); background-size: 100% 100%;\"></body> "; + response += "\r\n </html>"; + clt_sock.send(response.c_str(), response.size()+1); + Thread::wait(10); + mysem.release(); + } +} \ No newline at end of file