simon mestdagh
/
tcp-server
van Robin en Simon
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "C12832.h" 00004 00005 C12832 lcd(D11, D13, D12, D7, D10); 00006 DigitalOut led(LED1); 00007 PwmOut r (D5); 00008 PwmOut g (D8); 00009 PwmOut b (D9); 00010 EthernetInterface eth; 00011 TCPSocket client; 00012 TCPServer srv(ð); 00013 00014 void setRGB(int pot) 00015 { 00016 00017 r = (float)pot; 00018 g = (float)(255-pot); 00019 b = (float)0 ; 00020 00021 00022 } 00023 00024 void setupSocket() 00025 { 00026 00027 eth.set_network("192.168.0.18","255.255.255.0","192.168.0.1"); 00028 eth.connect(); 00029 printf("The Server IP address is '%s'\n\r", eth.get_ip_address()); 00030 srv.bind(4000); 00031 srv.listen(); 00032 } 00033 00034 void lcdScreen(float temperature){ 00035 lcd.cls(); 00036 lcd.locate(0,3); 00037 lcd.printf("temperature: %d\r\n", temperature); 00038 } 00039 00040 float temperatureDecoder(uint8_t left, uint8_t right) 00041 { 00042 uint16_t tempLowHigh = ((left & 0xFF) << 8 ) | (right & 0xFF); 00043 tempLowHigh = tempLowHigh >> 5; 00044 //Sign extend negative numbers 00045 if (tempLowHigh & (1 << 10)) { 00046 tempLowHigh |= 0xFC00; 00047 } 00048 00049 //Return the temperature in °C 00050 return tempLowHigh * 0.125; 00051 } 00052 00053 00054 int main() 00055 { 00056 printf("Server example\n\r"); 00057 setupSocket(); 00058 00059 00060 00061 while(true) { 00062 00063 SocketAddress client_addr; 00064 char *buffer = "Hello TCP client!\r\n"; 00065 00066 srv.accept(&client, &client_addr); 00067 00068 printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), 00069 client_addr.get_port()); 00070 00071 client.send(buffer, 256); 00072 00073 char rbuffer[64]; 00074 int rcount = client.recv(rbuffer, sizeof rbuffer); 00075 float temperature = temperatureDecoder(rbuffer[1],rbuffer[2]); 00076 00077 printf("received: %d\r\n", rcount); 00078 printf("packet PWM: %d\r\n", rbuffer[0]); 00079 printf("packet temp: %d\r\n", temperature); 00080 setRGB(rbuffer[0]); 00081 client.close(); 00082 00083 } 00084 }
Generated on Fri Aug 19 2022 03:46:13 by 1.7.2