simon mestdagh
/
tcp-server
van Robin en Simon
main.cpp@2:390c3c1cdf42, 2020-11-07 (annotated)
- Committer:
- simonmestdagh
- Date:
- Sat Nov 07 14:52:01 2020 +0000
- Revision:
- 2:390c3c1cdf42
- Parent:
- 1:b658dfbe2a7c
server robin en simon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
deepikabhavnani | 0:ddb5698aa782 | 1 | #include "mbed.h" |
deepikabhavnani | 0:ddb5698aa782 | 2 | #include "EthernetInterface.h" |
simonmestdagh | 2:390c3c1cdf42 | 3 | #include "C12832.h" |
simonmestdagh | 2:390c3c1cdf42 | 4 | |
simonmestdagh | 2:390c3c1cdf42 | 5 | C12832 lcd(D11, D13, D12, D7, D10); |
simonmestdagh | 2:390c3c1cdf42 | 6 | DigitalOut led(LED1); |
simonmestdagh | 2:390c3c1cdf42 | 7 | PwmOut r (D5); |
simonmestdagh | 2:390c3c1cdf42 | 8 | PwmOut g (D8); |
simonmestdagh | 2:390c3c1cdf42 | 9 | PwmOut b (D9); |
simonmestdagh | 2:390c3c1cdf42 | 10 | EthernetInterface eth; |
simonmestdagh | 2:390c3c1cdf42 | 11 | TCPSocket client; |
simonmestdagh | 2:390c3c1cdf42 | 12 | TCPServer srv(ð); |
simonmestdagh | 2:390c3c1cdf42 | 13 | |
simonmestdagh | 2:390c3c1cdf42 | 14 | void setRGB(int pot) |
simonmestdagh | 2:390c3c1cdf42 | 15 | { |
simonmestdagh | 2:390c3c1cdf42 | 16 | |
simonmestdagh | 2:390c3c1cdf42 | 17 | r = (float)pot; |
simonmestdagh | 2:390c3c1cdf42 | 18 | g = (float)(255-pot); |
simonmestdagh | 2:390c3c1cdf42 | 19 | b = (float)0 ; |
pcordemans | 1:b658dfbe2a7c | 20 | |
pcordemans | 1:b658dfbe2a7c | 21 | |
simonmestdagh | 2:390c3c1cdf42 | 22 | } |
simonmestdagh | 2:390c3c1cdf42 | 23 | |
simonmestdagh | 2:390c3c1cdf42 | 24 | void setupSocket() |
simonmestdagh | 2:390c3c1cdf42 | 25 | { |
simonmestdagh | 2:390c3c1cdf42 | 26 | |
simonmestdagh | 2:390c3c1cdf42 | 27 | eth.set_network("192.168.0.18","255.255.255.0","192.168.0.1"); |
simonmestdagh | 2:390c3c1cdf42 | 28 | eth.connect(); |
simonmestdagh | 2:390c3c1cdf42 | 29 | printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); |
simonmestdagh | 2:390c3c1cdf42 | 30 | srv.bind(4000); |
simonmestdagh | 2:390c3c1cdf42 | 31 | srv.listen(); |
simonmestdagh | 2:390c3c1cdf42 | 32 | } |
simonmestdagh | 2:390c3c1cdf42 | 33 | |
simonmestdagh | 2:390c3c1cdf42 | 34 | void lcdScreen(float temperature){ |
simonmestdagh | 2:390c3c1cdf42 | 35 | lcd.cls(); |
simonmestdagh | 2:390c3c1cdf42 | 36 | lcd.locate(0,3); |
simonmestdagh | 2:390c3c1cdf42 | 37 | lcd.printf("temperature: %d\r\n", temperature); |
simonmestdagh | 2:390c3c1cdf42 | 38 | } |
simonmestdagh | 2:390c3c1cdf42 | 39 | |
simonmestdagh | 2:390c3c1cdf42 | 40 | float temperatureDecoder(uint8_t left, uint8_t right) |
simonmestdagh | 2:390c3c1cdf42 | 41 | { |
simonmestdagh | 2:390c3c1cdf42 | 42 | uint16_t tempLowHigh = ((left & 0xFF) << 8 ) | (right & 0xFF); |
simonmestdagh | 2:390c3c1cdf42 | 43 | tempLowHigh = tempLowHigh >> 5; |
simonmestdagh | 2:390c3c1cdf42 | 44 | //Sign extend negative numbers |
simonmestdagh | 2:390c3c1cdf42 | 45 | if (tempLowHigh & (1 << 10)) { |
simonmestdagh | 2:390c3c1cdf42 | 46 | tempLowHigh |= 0xFC00; |
simonmestdagh | 2:390c3c1cdf42 | 47 | } |
simonmestdagh | 2:390c3c1cdf42 | 48 | |
simonmestdagh | 2:390c3c1cdf42 | 49 | //Return the temperature in °C |
simonmestdagh | 2:390c3c1cdf42 | 50 | return tempLowHigh * 0.125; |
simonmestdagh | 2:390c3c1cdf42 | 51 | } |
simonmestdagh | 2:390c3c1cdf42 | 52 | |
deepikabhavnani | 0:ddb5698aa782 | 53 | |
deepikabhavnani | 0:ddb5698aa782 | 54 | int main() |
deepikabhavnani | 0:ddb5698aa782 | 55 | { |
pcordemans | 1:b658dfbe2a7c | 56 | printf("Server example\n\r"); |
simonmestdagh | 2:390c3c1cdf42 | 57 | setupSocket(); |
simonmestdagh | 2:390c3c1cdf42 | 58 | |
simonmestdagh | 2:390c3c1cdf42 | 59 | |
simonmestdagh | 2:390c3c1cdf42 | 60 | |
simonmestdagh | 2:390c3c1cdf42 | 61 | while(true) { |
simonmestdagh | 2:390c3c1cdf42 | 62 | |
pcordemans | 1:b658dfbe2a7c | 63 | SocketAddress client_addr; |
pcordemans | 1:b658dfbe2a7c | 64 | char *buffer = "Hello TCP client!\r\n"; |
simonmestdagh | 2:390c3c1cdf42 | 65 | |
pcordemans | 1:b658dfbe2a7c | 66 | srv.accept(&client, &client_addr); |
simonmestdagh | 2:390c3c1cdf42 | 67 | |
simonmestdagh | 2:390c3c1cdf42 | 68 | printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), |
simonmestdagh | 2:390c3c1cdf42 | 69 | client_addr.get_port()); |
simonmestdagh | 2:390c3c1cdf42 | 70 | |
pcordemans | 1:b658dfbe2a7c | 71 | client.send(buffer, 256); |
simonmestdagh | 2:390c3c1cdf42 | 72 | |
simonmestdagh | 2:390c3c1cdf42 | 73 | char rbuffer[64]; |
simonmestdagh | 2:390c3c1cdf42 | 74 | int rcount = client.recv(rbuffer, sizeof rbuffer); |
simonmestdagh | 2:390c3c1cdf42 | 75 | float temperature = temperatureDecoder(rbuffer[1],rbuffer[2]); |
simonmestdagh | 2:390c3c1cdf42 | 76 | |
simonmestdagh | 2:390c3c1cdf42 | 77 | printf("received: %d\r\n", rcount); |
simonmestdagh | 2:390c3c1cdf42 | 78 | printf("packet PWM: %d\r\n", rbuffer[0]); |
simonmestdagh | 2:390c3c1cdf42 | 79 | printf("packet temp: %d\r\n", temperature); |
simonmestdagh | 2:390c3c1cdf42 | 80 | setRGB(rbuffer[0]); |
pcordemans | 1:b658dfbe2a7c | 81 | client.close(); |
simonmestdagh | 2:390c3c1cdf42 | 82 | |
pcordemans | 1:b658dfbe2a7c | 83 | } |
simonmestdagh | 2:390c3c1cdf42 | 84 | } |