Wizwiki-W7500과 Wizfi250을 사용한 UDP통신
Dependencies: Servo WizFi250Interface mbed-src
Fork of WizFi250_AP_HelloWorld by
Revision 1:88a24339327f, committed 2015-09-09
- Comitter:
- Minsu
- Date:
- Wed Sep 09 09:19:34 2015 +0000
- Parent:
- 0:87d959d6a3f3
- Commit message:
- 123
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Wed Sep 09 09:19:34 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/WizFi250Interface.lib Tue Jul 14 06:55:18 2015 +0000 +++ b/WizFi250Interface.lib Wed Sep 09 09:19:34 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/kaizen/code/WizFi250Interface/#1f5121c5efcd +https://developer.mbed.org/users/Minsu/code/WizFi250Interface/#3a2282277ad0
--- a/main.cpp Tue Jul 14 06:55:18 2015 +0000 +++ b/main.cpp Wed Sep 09 09:19:34 2015 +0000 @@ -18,9 +18,14 @@ */ #include <stdio.h> +#include <stdlib.h> + +#include "Servo.h" + #include "mbed.h" #include "WizFi250Interface.h" +#define ECHO_SERVER_PORT 7777 #define SECURE WizFi250::SEC_WPA2_MIXED #define SSID "WizFi250_AP_Test" @@ -31,21 +36,241 @@ Serial pc(USBTX, USBRX); #endif +DigitalOut red(LED1); +DigitalOut green(LED2); -/** - * \brief Hello World - * \param none - * \return int - */ +void TCPServer(); +void UDPServer(); + +char speed_str[6]; +char direction_str[10]; +volatile float speed_values; +volatile float direction_values; +volatile int speed_temp=0; + + + +Servo myservo(D6); //서보모터 +PwmOut mypwm(D5); //브러쉬드DC모터 + int main() { pc.baud(115200); + + speed_values=0.0650; //중립value 0.0650 + direction_values=0.5; //중립value 0.5 + + red=1; //led off + green=1; //led off printf("WizFi250 Hello World demo. \r\n"); wizfi250.init(); - wizfi250.setAddress("192.168.100.1","255.255.255.0","192.168.100.1"); + wizfi250.setAddress("192.168.101.10","255.255.255.0","192.168.101.1"); if ( wizfi250.connect(SECURE, SSID, PASS, WizFi250::WM_AP)) return -1; printf("IP Address is %s\r\n", wizfi250.getIPAddress()); + mypwm=speed_values; + myservo=direction_values; + wait(2); + + //TCPServer(); + UDPServer(); + wizfi250.disconnect(); -} \ No newline at end of file + +} + +void TCPServer(){ + char buffer[1024]; + int n = 0; + + + TCPSocketServer server; + server.bind(ECHO_SERVER_PORT); + server.listen(); + + printf("\nWait for new connection...\r\n"); + green=0; + wait(0.5f); + green=1; + wait(0.5f); + green=0; + wait(0.5f); + green=1; + + TCPSocketConnection client; + server.accept(client); + client.set_blocking(false, 1500); + + printf("Connection from: %s\r\n", client.get_address()); + + + while (true) + { + if( client.is_connected() == false ) + { + //client.close(); + //printf("Client Close\r\n"); + server.accept(client); + client.set_blocking(false, 1500); + //break; + } + + n = client.receive(buffer, sizeof(buffer)); + if( n > 0 ) + { + buffer[n] = '\0'; + /* + if(buffer[0] == 'H'){ + myled=0; + printf("\n[LED On]\r\n"); + }else if(buffer[0] == 'L'){ + myled=1; + printf("\n[LED Off]\r\n"); + }else{ + printf("length : %d\r\n", n); + client.send_all(buffer, n); + }*/ + + if(buffer[0] == 'D'){ + direction_str[0]=buffer[1]; + direction_str[1]=buffer[2]; + direction_values = atoi(direction_str); + + direction_values = direction_values * 0.01; + + if(direction_values > 0.90) + direction_values = 0.9; + else if(direction_values < 0.10) + direction_values = 0.1; + printf("Direction value : %f\r\n", direction_values); + + speed_str[0]=buffer[3]; + speed_str[1]=buffer[4]; + speed_str[2]=buffer[5]; + speed_str[3]=buffer[6]; + + speed_values = atoi(speed_str); + speed_temp=speed_values; + speed_values = speed_values * 0.00001; + printf("Speed value : %f, %d\r\n", speed_values, speed_temp); + + mypwm = (double) speed_values; + myservo = (double) direction_values; + }else{ + //printf("not if\r\n"); + printf("%s\r\n", buffer); + } + //printf("length : %d\r\n", n); + //client.send_all(buffer, n); + + } + } +} + +void UDPServer(){ + UDPSocket server; + printf("Socket create\r\n"); + server.set_blocking(false); + printf("Blocking\r\n"); + server.bind(ECHO_SERVER_PORT); + printf("Bind\r\n"); + + Endpoint client; + printf("Client create\r\n"); + green=0; + wait(0.3f); + green=1; + wait(0.3f); + green=0; + wait(0.3f); + green=1; + + //char buffer[256]; + char buffer[7] = {0}; + volatile int n=0; + + while(true){ + //printf("Receive\r\n"); + n = server.receiveFrom(client, buffer, sizeof(buffer)); + + //printf("Receive\r\n"); + + if(n > 0){ + if(buffer[0] == 'D'){ + green=0; //On + + direction_str[0]=buffer[1]; + direction_str[1]=buffer[2]; + direction_values = atoi(direction_str); + + direction_values = direction_values * 0.01; + + /* + if(direction_values > 0.90) + direction_values = 0.9; + else if(direction_values < 0.10) + direction_values = 0.1; + */ + printf("Direction value : %f\r\n", direction_values); + + + printf("[%c%c%c]", buffer[0], buffer[1], buffer[2]); + + speed_str[0]=buffer[3]; + speed_str[1]=buffer[4]; + speed_str[2]=buffer[5]; + speed_str[3]=buffer[6]; + + speed_values = atoi(speed_str); + speed_temp=speed_values; + speed_values = speed_values * 0.00001; + printf("Speed value : %f, %d\r\n", speed_values, speed_temp); + + mypwm = (double) speed_values; + myservo = (double) direction_values; + }else{ + red=0; + if(buffer[1]=='D'){ + green=0; + green=1; + green=0; + green=1; + direction_str[0]=buffer[2]; + direction_str[1]=buffer[3]; + direction_values = atoi(direction_str); + + direction_values = direction_values * 0.01; + + if(direction_values > 0.90) + direction_values = 0.9; + else if(direction_values < 0.10) + direction_values = 0.1; + printf("Direction value : %f\r\n", direction_values); + + + printf("[%c%c%c]", buffer[0], buffer[1], buffer[2]); + + speed_str[0]=buffer[4]; + speed_str[1]=buffer[5]; + speed_str[2]=buffer[6]; + speed_str[3]=0; + + speed_values = atoi(speed_str); + speed_temp=speed_values; + speed_values = speed_values * 0.00001; + printf("Speed value : %f, %d\r\n", speed_values, speed_temp); + + mypwm = (double) speed_values; + myservo = (double) direction_values; + } + printf("%s\r\n", buffer); + memset(buffer, 0, sizeof(buffer)); + + } + green=1; + red=1; + memset(buffer, 0, sizeof(buffer)); + } + } +}
--- a/mbed-src.lib Tue Jul 14 06:55:18 2015 +0000 +++ b/mbed-src.lib Wed Sep 09 09:19:34 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/kaizen/code/mbed-src/#cf2abc059970 +https://developer.mbed.org/users/Minsu/code/mbed-src/#cf2abc059970