WIZnet Remote Boat V1.0

Dependencies:   Servo WizFi250Interface mbed

Committer:
jehoon
Date:
Wed Mar 30 08:30:58 2016 +0000
Revision:
2:162fcc97fc52
Parent:
1:91f8a27fc9a0
Child:
3:3e8bf06dfa83
modify

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:5bf873326d8c 1 #include <stdio.h>
jehoon 0:5bf873326d8c 2 #include "mbed.h"
jehoon 0:5bf873326d8c 3 #include "WizFi250Interface.h"
jehoon 0:5bf873326d8c 4 #include "Servo.h"
jehoon 0:5bf873326d8c 5
jehoon 0:5bf873326d8c 6
jehoon 0:5bf873326d8c 7
jehoon 0:5bf873326d8c 8 Serial pc(USBTX, USBRX);
jehoon 0:5bf873326d8c 9
jehoon 0:5bf873326d8c 10 /* network setup */
jehoon 0:5bf873326d8c 11 WizFi250Interface wizfi250(P13,P14,P11,P12,P15,NC,115200);
jehoon 0:5bf873326d8c 12 TCPSocketServer server;
jehoon 0:5bf873326d8c 13 TCPSocketConnection client;
jehoon 0:5bf873326d8c 14
jehoon 0:5bf873326d8c 15 #define SECURE WizFi250::SEC_WPA2_MIXED
jehoon 0:5bf873326d8c 16 #define SSID "WIZnet_Boat"
jehoon 0:5bf873326d8c 17 #define PASS "wiznetboat"
jehoon 0:5bf873326d8c 18 #define PORT 5000
jehoon 0:5bf873326d8c 19
jehoon 0:5bf873326d8c 20
jehoon 0:5bf873326d8c 21 /* motor setup */
jehoon 0:5bf873326d8c 22 Servo servo(P23);
jehoon 0:5bf873326d8c 23 PwmOut motor(P21);
jehoon 0:5bf873326d8c 24
jehoon 0:5bf873326d8c 25
jehoon 0:5bf873326d8c 26
jehoon 0:5bf873326d8c 27 /* direction define */
jehoon 0:5bf873326d8c 28 #define forward "WIZnet_boat_direction:forward"
jehoon 0:5bf873326d8c 29 #define back "WIZnet_boat_direction:back"
jehoon 0:5bf873326d8c 30 #define left "WIZnet_boat_direction:left"
jehoon 0:5bf873326d8c 31 #define right "WIZnet_boat_direction:right"
jehoon 0:5bf873326d8c 32 #define stop "WIZnet_boat_direction:stop"
jehoon 0:5bf873326d8c 33
jehoon 0:5bf873326d8c 34
jehoon 0:5bf873326d8c 35
jehoon 0:5bf873326d8c 36 /* Connection Steps */
jehoon 0:5bf873326d8c 37 typedef enum ConnectionStep { DISASSOCIATED = 1, ASSOCIATED, LISTENED, CONNECTED, DISCONNECTED } conStep;
jehoon 0:5bf873326d8c 38 conStep ConnectionStep = DISASSOCIATED;
jehoon 0:5bf873326d8c 39
jehoon 0:5bf873326d8c 40 /* functions */
jehoon 0:5bf873326d8c 41 int TryApUp();
jehoon 0:5bf873326d8c 42 int TryServerUpAndWaitClient();
jehoon 0:5bf873326d8c 43
jehoon 0:5bf873326d8c 44
jehoon 0:5bf873326d8c 45 int main()
jehoon 0:5bf873326d8c 46 {
jehoon 0:5bf873326d8c 47 pc.baud(115200);
jehoon 0:5bf873326d8c 48 motor.period_ms(1);
jehoon 0:5bf873326d8c 49 float speed = 0.0;
jehoon 0:5bf873326d8c 50
jehoon 0:5bf873326d8c 51 while(1)
jehoon 0:5bf873326d8c 52 {
jehoon 0:5bf873326d8c 53 char rcvBuf[1024] = {0,};
jehoon 0:5bf873326d8c 54 int len = 0;
jehoon 1:91f8a27fc9a0 55
jehoon 0:5bf873326d8c 56 switch(ConnectionStep)
jehoon 0:5bf873326d8c 57 {
jehoon 0:5bf873326d8c 58 case DISASSOCIATED:
jehoon 0:5bf873326d8c 59 TryApUp();
jehoon 0:5bf873326d8c 60 break;
jehoon 0:5bf873326d8c 61
jehoon 0:5bf873326d8c 62 case ASSOCIATED:
jehoon 0:5bf873326d8c 63 case LISTENED:
jehoon 0:5bf873326d8c 64 TryServerUpAndWaitClient();
jehoon 0:5bf873326d8c 65 break;
jehoon 0:5bf873326d8c 66
jehoon 0:5bf873326d8c 67 case CONNECTED:
jehoon 0:5bf873326d8c 68
jehoon 1:91f8a27fc9a0 69 if( client.is_connected() == 0 ) ConnectionStep = DISCONNECTED;
jehoon 0:5bf873326d8c 70
jehoon 0:5bf873326d8c 71 if( wizfi250.readable(0) > 0 )
jehoon 0:5bf873326d8c 72 {
jehoon 0:5bf873326d8c 73 len = client.receive(rcvBuf, 1023);
jehoon 0:5bf873326d8c 74 if( len < 0 ) ConnectionStep = DISCONNECTED;
jehoon 0:5bf873326d8c 75 else if( len > 0)
jehoon 0:5bf873326d8c 76 {
jehoon 0:5bf873326d8c 77 if( strcmp(rcvBuf,forward) == 0 )
jehoon 0:5bf873326d8c 78 {
jehoon 1:91f8a27fc9a0 79 speed = speed + 0.05;
jehoon 0:5bf873326d8c 80 printf("forward\r\n");
jehoon 0:5bf873326d8c 81 }
jehoon 0:5bf873326d8c 82 else if( strcmp(rcvBuf,back) == 0 )
jehoon 0:5bf873326d8c 83 {
jehoon 1:91f8a27fc9a0 84 speed = speed - 0.05;
jehoon 0:5bf873326d8c 85 printf("back\r\n");
jehoon 0:5bf873326d8c 86 }
jehoon 0:5bf873326d8c 87 else if( strcmp(rcvBuf,left) == 0 )
jehoon 0:5bf873326d8c 88 {
jehoon 0:5bf873326d8c 89 servo = servo - 0.1;
jehoon 0:5bf873326d8c 90 printf("left\r\n");
jehoon 0:5bf873326d8c 91 }
jehoon 0:5bf873326d8c 92 else if( strcmp(rcvBuf,right) == 0 )
jehoon 0:5bf873326d8c 93 {
jehoon 0:5bf873326d8c 94 servo = servo + 0.1;
jehoon 0:5bf873326d8c 95 printf("right\r\n");
jehoon 0:5bf873326d8c 96 }
jehoon 0:5bf873326d8c 97 else if( strcmp(rcvBuf,stop) == 0 )
jehoon 0:5bf873326d8c 98 {
jehoon 0:5bf873326d8c 99 servo = 0.5; // center
jehoon 0:5bf873326d8c 100 speed = 0;
jehoon 0:5bf873326d8c 101 printf("stop\r\n");
jehoon 0:5bf873326d8c 102 }
jehoon 0:5bf873326d8c 103
jehoon 0:5bf873326d8c 104 if( speed < 0.0 ) speed = 0.0;
jehoon 1:91f8a27fc9a0 105 else if ( speed > 1.0 ) speed = 1.0;
jehoon 0:5bf873326d8c 106 printf("speed %.3f\r\n", speed);
jehoon 0:5bf873326d8c 107 motor.write(speed);
jehoon 0:5bf873326d8c 108 }
jehoon 0:5bf873326d8c 109 }
jehoon 0:5bf873326d8c 110 break;
jehoon 0:5bf873326d8c 111 case DISCONNECTED:
jehoon 0:5bf873326d8c 112 servo = 0.5;
jehoon 0:5bf873326d8c 113 motor.write(0);
jehoon 0:5bf873326d8c 114 ConnectionStep = LISTENED;
jehoon 0:5bf873326d8c 115 }
jehoon 0:5bf873326d8c 116 }
jehoon 0:5bf873326d8c 117 }
jehoon 0:5bf873326d8c 118
jehoon 0:5bf873326d8c 119 int TryApUp()
jehoon 0:5bf873326d8c 120 {
jehoon 0:5bf873326d8c 121
jehoon 0:5bf873326d8c 122 if( wizfi250.isAssociated() == 1 ) return -1;
jehoon 0:5bf873326d8c 123
jehoon 0:5bf873326d8c 124 printf(" AP UP\r\n");
jehoon 0:5bf873326d8c 125 printf("SSID: %s\r\nPASS: %s", SSID, PASS);
jehoon 0:5bf873326d8c 126
jehoon 0:5bf873326d8c 127 for(int i= 0; i<5; i++)
jehoon 0:5bf873326d8c 128 {
jehoon 2:162fcc97fc52 129 wizfi250.init("192.168.0.2","255.255.255.0","192.168.0.2");
jehoon 0:5bf873326d8c 130 if ( wizfi250.connect(SECURE, SSID, PASS, WizFi250::WM_AP) )
jehoon 0:5bf873326d8c 131 continue;
jehoon 0:5bf873326d8c 132 else {
jehoon 0:5bf873326d8c 133 printf("IP Address is %s\r\n", wizfi250.getIPAddress());
jehoon 0:5bf873326d8c 134 if( wizfi250.isAssociated() == 1 )
jehoon 0:5bf873326d8c 135 {
jehoon 0:5bf873326d8c 136 ConnectionStep = ASSOCIATED;
jehoon 0:5bf873326d8c 137 return 0;
jehoon 0:5bf873326d8c 138 } else {
jehoon 0:5bf873326d8c 139 continue;
jehoon 0:5bf873326d8c 140 }
jehoon 0:5bf873326d8c 141 }
jehoon 0:5bf873326d8c 142 }
jehoon 0:5bf873326d8c 143
jehoon 0:5bf873326d8c 144 printf("Fail to make AP\r\n");
jehoon 0:5bf873326d8c 145 ConnectionStep = DISASSOCIATED;
jehoon 0:5bf873326d8c 146 return -1;
jehoon 0:5bf873326d8c 147 }
jehoon 0:5bf873326d8c 148
jehoon 0:5bf873326d8c 149
jehoon 0:5bf873326d8c 150 int TryServerUpAndWaitClient()
jehoon 0:5bf873326d8c 151 {
jehoon 0:5bf873326d8c 152 if( wizfi250.isAssociated() != 1 ) return -1;
jehoon 0:5bf873326d8c 153
jehoon 0:5bf873326d8c 154 if( ConnectionStep == ASSOCIATED )
jehoon 0:5bf873326d8c 155 {
jehoon 0:5bf873326d8c 156 if( server.bind(PORT) < 0 )
jehoon 0:5bf873326d8c 157 {
jehoon 0:5bf873326d8c 158 printf("Bind fail..\r\n");
jehoon 0:5bf873326d8c 159 return -1;
jehoon 0:5bf873326d8c 160 }
jehoon 0:5bf873326d8c 161
jehoon 2:162fcc97fc52 162 if( server.listen(1) < 0 )
jehoon 0:5bf873326d8c 163 {
jehoon 0:5bf873326d8c 164 printf("Listen fail..\r\n");
jehoon 0:5bf873326d8c 165 return -1;
jehoon 0:5bf873326d8c 166 } else {
jehoon 0:5bf873326d8c 167 printf("Listen PORT: %d\r\n",PORT);
jehoon 0:5bf873326d8c 168 ConnectionStep = LISTENED;
jehoon 0:5bf873326d8c 169 }
jehoon 0:5bf873326d8c 170 }
jehoon 0:5bf873326d8c 171
jehoon 0:5bf873326d8c 172 while(ConnectionStep == LISTENED)
jehoon 0:5bf873326d8c 173 {
jehoon 0:5bf873326d8c 174 if( server.accept(client) < 0 )
jehoon 0:5bf873326d8c 175 {
jehoon 0:5bf873326d8c 176 printf("accept fail..\r\n");
jehoon 0:5bf873326d8c 177 return -1;
jehoon 0:5bf873326d8c 178 }else{
jehoon 0:5bf873326d8c 179 printf("Connection Success!!\r\nIP: %s\r\n", client.get_address());
jehoon 0:5bf873326d8c 180 ConnectionStep = CONNECTED;
jehoon 0:5bf873326d8c 181 return 0;
jehoon 0:5bf873326d8c 182 }
jehoon 0:5bf873326d8c 183 }
jehoon 0:5bf873326d8c 184
jehoon 0:5bf873326d8c 185 return -1;
jehoon 0:5bf873326d8c 186 }