simon mestdagh
/
tcp-server
van Robin en Simon
Revision 2:390c3c1cdf42, committed 2020-11-07
- Comitter:
- simonmestdagh
- Date:
- Sat Nov 07 14:52:01 2020 +0000
- Parent:
- 1:b658dfbe2a7c
- Commit message:
- server robin en simon
Changed in this revision
C12832.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r b658dfbe2a7c -r 390c3c1cdf42 C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Sat Nov 07 14:52:01 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/chris/code/C12832/#7de323fa46fe
diff -r b658dfbe2a7c -r 390c3c1cdf42 main.cpp --- a/main.cpp Sat Mar 07 19:26:48 2020 +0000 +++ b/main.cpp Sat Nov 07 14:52:01 2020 +0000 @@ -1,38 +1,84 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "C12832.h" + +C12832 lcd(D11, D13, D12, D7, D10); +DigitalOut led(LED1); +PwmOut r (D5); +PwmOut g (D8); +PwmOut b (D9); +EthernetInterface eth; +TCPSocket client; +TCPServer srv(ð); + +void setRGB(int pot) +{ + + r = (float)pot; + g = (float)(255-pot); + b = (float)0 ; -DigitalOut led(LED1); +} + +void setupSocket() +{ + + eth.set_network("192.168.0.18","255.255.255.0","192.168.0.1"); + eth.connect(); + printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); + srv.bind(4000); + srv.listen(); +} + +void lcdScreen(float temperature){ + lcd.cls(); + lcd.locate(0,3); + lcd.printf("temperature: %d\r\n", temperature); + } + +float temperatureDecoder(uint8_t left, uint8_t right) +{ + uint16_t tempLowHigh = ((left & 0xFF) << 8 ) | (right & 0xFF); + tempLowHigh = tempLowHigh >> 5; +//Sign extend negative numbers + if (tempLowHigh & (1 << 10)) { + tempLowHigh |= 0xFC00; + } + +//Return the temperature in °C + return tempLowHigh * 0.125; +} + int main() { printf("Server example\n\r"); - - EthernetInterface eth; - eth.set_network("192.168.0.40","255.255.255.0","192.168.0.1"); - eth.connect(); - - printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); - - TCPServer srv(ð); - - srv.bind(4000); - - srv.listen(); - - while(true){ - TCPSocket client; + setupSocket(); + + + + while(true) { + SocketAddress client_addr; char *buffer = "Hello TCP client!\r\n"; - + srv.accept(&client, &client_addr); - - printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), - client_addr.get_port()); - + + printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), + client_addr.get_port()); + client.send(buffer, 256); - + + char rbuffer[64]; + int rcount = client.recv(rbuffer, sizeof rbuffer); + float temperature = temperatureDecoder(rbuffer[1],rbuffer[2]); + + printf("received: %d\r\n", rcount); + printf("packet PWM: %d\r\n", rbuffer[0]); + printf("packet temp: %d\r\n", temperature); + setRGB(rbuffer[0]); client.close(); - + } -} +} \ No newline at end of file