WIZnet Remote Boat V1.0

Dependencies:   Servo WizFi250Interface mbed

Committer:
jehoon
Date:
Mon Mar 28 06:48:38 2016 +0000
Revision:
0:5bf873326d8c
Child:
1:91f8a27fc9a0
WIZnet Remote Boat V1.0

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 0:5bf873326d8c 55 switch(ConnectionStep)
jehoon 0:5bf873326d8c 56 {
jehoon 0:5bf873326d8c 57 case DISASSOCIATED:
jehoon 0:5bf873326d8c 58 TryApUp();
jehoon 0:5bf873326d8c 59 break;
jehoon 0:5bf873326d8c 60
jehoon 0:5bf873326d8c 61 case ASSOCIATED:
jehoon 0:5bf873326d8c 62 case LISTENED:
jehoon 0:5bf873326d8c 63 TryServerUpAndWaitClient();
jehoon 0:5bf873326d8c 64 break;
jehoon 0:5bf873326d8c 65
jehoon 0:5bf873326d8c 66 case CONNECTED:
jehoon 0:5bf873326d8c 67
jehoon 0:5bf873326d8c 68 int isconnected = client.is_connected();
jehoon 0:5bf873326d8c 69 if( isconnected == 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 0:5bf873326d8c 79 speed = speed + 0.5;
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 0:5bf873326d8c 84 speed = speed - 0.5;
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 0:5bf873326d8c 105 else if ( speed > 255.0 ) speed = 255.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 }
jehoon 0:5bf873326d8c 120
jehoon 0:5bf873326d8c 121
jehoon 0:5bf873326d8c 122 }
jehoon 0:5bf873326d8c 123
jehoon 0:5bf873326d8c 124 int TryApUp()
jehoon 0:5bf873326d8c 125 {
jehoon 0:5bf873326d8c 126
jehoon 0:5bf873326d8c 127 if( wizfi250.isAssociated() == 1 ) return -1;
jehoon 0:5bf873326d8c 128
jehoon 0:5bf873326d8c 129 printf(" AP UP\r\n");
jehoon 0:5bf873326d8c 130 printf("SSID: %s\r\nPASS: %s", SSID, PASS);
jehoon 0:5bf873326d8c 131
jehoon 0:5bf873326d8c 132 for(int i= 0; i<5; i++)
jehoon 0:5bf873326d8c 133 {
jehoon 0:5bf873326d8c 134 wizfi250.init();
jehoon 0:5bf873326d8c 135 wizfi250.setAddress("192.168.0.2","255.255.255.0","192.168.0.2");
jehoon 0:5bf873326d8c 136 if ( wizfi250.connect(SECURE, SSID, PASS, WizFi250::WM_AP) )
jehoon 0:5bf873326d8c 137 continue;
jehoon 0:5bf873326d8c 138 else {
jehoon 0:5bf873326d8c 139 printf("IP Address is %s\r\n", wizfi250.getIPAddress());
jehoon 0:5bf873326d8c 140 if( wizfi250.isAssociated() == 1 )
jehoon 0:5bf873326d8c 141 {
jehoon 0:5bf873326d8c 142 ConnectionStep = ASSOCIATED;
jehoon 0:5bf873326d8c 143 return 0;
jehoon 0:5bf873326d8c 144 } else {
jehoon 0:5bf873326d8c 145 continue;
jehoon 0:5bf873326d8c 146 }
jehoon 0:5bf873326d8c 147 }
jehoon 0:5bf873326d8c 148 }
jehoon 0:5bf873326d8c 149
jehoon 0:5bf873326d8c 150 printf("Fail to make AP\r\n");
jehoon 0:5bf873326d8c 151 ConnectionStep = DISASSOCIATED;
jehoon 0:5bf873326d8c 152 return -1;
jehoon 0:5bf873326d8c 153 }
jehoon 0:5bf873326d8c 154
jehoon 0:5bf873326d8c 155
jehoon 0:5bf873326d8c 156 int TryServerUpAndWaitClient()
jehoon 0:5bf873326d8c 157 {
jehoon 0:5bf873326d8c 158 if( wizfi250.isAssociated() != 1 ) return -1;
jehoon 0:5bf873326d8c 159
jehoon 0:5bf873326d8c 160 if( ConnectionStep == ASSOCIATED )
jehoon 0:5bf873326d8c 161 {
jehoon 0:5bf873326d8c 162 if( server.bind(PORT) < 0 )
jehoon 0:5bf873326d8c 163 {
jehoon 0:5bf873326d8c 164 printf("Bind fail..\r\n");
jehoon 0:5bf873326d8c 165 return -1;
jehoon 0:5bf873326d8c 166 }
jehoon 0:5bf873326d8c 167
jehoon 0:5bf873326d8c 168 if( server.listen(1) < 0)
jehoon 0:5bf873326d8c 169 {
jehoon 0:5bf873326d8c 170 printf("Listen fail..\r\n");
jehoon 0:5bf873326d8c 171 return -1;
jehoon 0:5bf873326d8c 172 } else {
jehoon 0:5bf873326d8c 173 printf("Listen PORT: %d\r\n",PORT);
jehoon 0:5bf873326d8c 174 ConnectionStep = LISTENED;
jehoon 0:5bf873326d8c 175 }
jehoon 0:5bf873326d8c 176 }
jehoon 0:5bf873326d8c 177
jehoon 0:5bf873326d8c 178 while(ConnectionStep == LISTENED)
jehoon 0:5bf873326d8c 179 {
jehoon 0:5bf873326d8c 180 if( server.accept(client) < 0 )
jehoon 0:5bf873326d8c 181 {
jehoon 0:5bf873326d8c 182 printf("accept fail..\r\n");
jehoon 0:5bf873326d8c 183 return -1;
jehoon 0:5bf873326d8c 184 }else{
jehoon 0:5bf873326d8c 185 printf("Connection Success!!\r\nIP: %s\r\n", client.get_address());
jehoon 0:5bf873326d8c 186 ConnectionStep = CONNECTED;
jehoon 0:5bf873326d8c 187 return 0;
jehoon 0:5bf873326d8c 188 }
jehoon 0:5bf873326d8c 189 }
jehoon 0:5bf873326d8c 190
jehoon 0:5bf873326d8c 191 return -1;
jehoon 0:5bf873326d8c 192 }