![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A program using ESP8266 Huzzah to receive message from Amazon AWS service and control a servo to flip on/off the mechanical light switch.
Dependencies: RPCInterface Servo mbed
Fork of dotbot by
main.cpp@0:1fb00e911fe6, 2016-12-12 (annotated)
- Committer:
- gmiles3
- Date:
- Mon Dec 12 14:39:25 2016 +0000
- Revision:
- 0:1fb00e911fe6
- Child:
- 2:747b84e54088
freeze all motor functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmiles3 | 0:1fb00e911fe6 | 1 | #include "mbed.h" |
gmiles3 | 0:1fb00e911fe6 | 2 | #include "Car.h" |
gmiles3 | 0:1fb00e911fe6 | 3 | #include "mywifi.h" |
gmiles3 | 0:1fb00e911fe6 | 4 | #include "Motor.h" |
gmiles3 | 0:1fb00e911fe6 | 5 | #include "HALLFX_ENCODER.h" |
gmiles3 | 0:1fb00e911fe6 | 6 | #include "mbed_rpc.h" |
gmiles3 | 0:1fb00e911fe6 | 7 | |
gmiles3 | 0:1fb00e911fe6 | 8 | Serial pc(USBTX, USBRX); |
gmiles3 | 0:1fb00e911fe6 | 9 | Serial esp(p28, p27); |
gmiles3 | 0:1fb00e911fe6 | 10 | char ssid[32] = "ThugMansion"; |
gmiles3 | 0:1fb00e911fe6 | 11 | char pwd[32] = "2paclives"; |
gmiles3 | 0:1fb00e911fe6 | 12 | char port[32] = "1035"; // must be port forwarded |
gmiles3 | 0:1fb00e911fe6 | 13 | char timeout[32] = "28800"; |
gmiles3 | 0:1fb00e911fe6 | 14 | volatile int tx_in=0; |
gmiles3 | 0:1fb00e911fe6 | 15 | volatile int tx_out=0; |
gmiles3 | 0:1fb00e911fe6 | 16 | volatile int rx_in=0; |
gmiles3 | 0:1fb00e911fe6 | 17 | volatile int rx_out=0; |
gmiles3 | 0:1fb00e911fe6 | 18 | const int buffer_size = 4095; |
gmiles3 | 0:1fb00e911fe6 | 19 | char tx_buffer[buffer_size+1]; |
gmiles3 | 0:1fb00e911fe6 | 20 | char rx_buffer[buffer_size+1]; |
gmiles3 | 0:1fb00e911fe6 | 21 | char cmdbuff[1024]; |
gmiles3 | 0:1fb00e911fe6 | 22 | char rx_line[1024]; |
gmiles3 | 0:1fb00e911fe6 | 23 | |
gmiles3 | 0:1fb00e911fe6 | 24 | |
gmiles3 | 0:1fb00e911fe6 | 25 | DigitalOut led1(LED1); |
gmiles3 | 0:1fb00e911fe6 | 26 | DigitalOut led2(LED2); |
gmiles3 | 0:1fb00e911fe6 | 27 | DigitalOut led3(LED3); |
gmiles3 | 0:1fb00e911fe6 | 28 | DigitalOut led4(LED4); |
gmiles3 | 0:1fb00e911fe6 | 29 | |
gmiles3 | 0:1fb00e911fe6 | 30 | float left_speed = .5; |
gmiles3 | 0:1fb00e911fe6 | 31 | float right_speed = .5; |
gmiles3 | 0:1fb00e911fe6 | 32 | float clicksTravelled; |
gmiles3 | 0:1fb00e911fe6 | 33 | Motor left_motor(p26,p24,p25); // pwm, fwd, rev |
gmiles3 | 0:1fb00e911fe6 | 34 | Motor right_motor(p21, p23, p22); // pwm, fwd, rev |
gmiles3 | 0:1fb00e911fe6 | 35 | HALLFX_ENCODER right_hall(p13); // left hall effect encoder |
gmiles3 | 0:1fb00e911fe6 | 36 | HALLFX_ENCODER left_hall(p14); // left hall effect encoder |
gmiles3 | 0:1fb00e911fe6 | 37 | float encoderFactor = 40; |
gmiles3 | 0:1fb00e911fe6 | 38 | RPCFunction rpcTurnRight(&carTurnRight, "turnRight"); |
gmiles3 | 0:1fb00e911fe6 | 39 | RPCFunction rpcTurnLeft(&carTurnLeft, "turnLeft"); |
gmiles3 | 0:1fb00e911fe6 | 40 | RPCFunction rpcMoveForward(&carMoveForwardDistance, "moveForward"); |
gmiles3 | 0:1fb00e911fe6 | 41 | RPCFunction rpcMoveBackward(&carMoveBackwardDistance, "moveBackward"); |
gmiles3 | 0:1fb00e911fe6 | 42 | RPCFunction rpcTurn(&carTurn, "turn"); |
gmiles3 | 0:1fb00e911fe6 | 43 | |
gmiles3 | 0:1fb00e911fe6 | 44 | int main() { |
gmiles3 | 0:1fb00e911fe6 | 45 | clicksTravelled = 0.0; |
gmiles3 | 0:1fb00e911fe6 | 46 | pc.baud(9600); |
gmiles3 | 0:1fb00e911fe6 | 47 | esp.baud(9600); |
gmiles3 | 0:1fb00e911fe6 | 48 | led1=0,led2=0,led3=0,led4=0; |
gmiles3 | 0:1fb00e911fe6 | 49 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
gmiles3 | 0:1fb00e911fe6 | 50 | esp.attach(&Tx_interrupt, Serial::TxIrq); |
gmiles3 | 0:1fb00e911fe6 | 51 | wait(5); |
gmiles3 | 0:1fb00e911fe6 | 52 | connectToNetwork(); |
gmiles3 | 0:1fb00e911fe6 | 53 | char rpc_in[256]; |
gmiles3 | 0:1fb00e911fe6 | 54 | char rpc_out[256]; |
gmiles3 | 0:1fb00e911fe6 | 55 | while (1) { |
gmiles3 | 0:1fb00e911fe6 | 56 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 57 | memset(&rpc_in[0], 0, sizeof(rpc_in)); |
gmiles3 | 0:1fb00e911fe6 | 58 | memset(&rpc_in[0], 0, sizeof(rpc_out)); |
gmiles3 | 0:1fb00e911fe6 | 59 | int length = (int)rx_line[3] - 48; |
gmiles3 | 0:1fb00e911fe6 | 60 | if (length > 0 && length < 256) { |
gmiles3 | 0:1fb00e911fe6 | 61 | for (int i = 0; i < length; i++) { |
gmiles3 | 0:1fb00e911fe6 | 62 | rpc_in[i] = rx_line[i+4]; |
gmiles3 | 0:1fb00e911fe6 | 63 | } |
gmiles3 | 0:1fb00e911fe6 | 64 | RPC::call(rpc_in, rpc_out); |
gmiles3 | 0:1fb00e911fe6 | 65 | pc.printf("%s\n", rpc_out); |
gmiles3 | 0:1fb00e911fe6 | 66 | } |
gmiles3 | 0:1fb00e911fe6 | 67 | strcpy(cmdbuff, "srv:close()\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 68 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 69 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 70 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 71 | strcpy(cmdbuff, "srv=net.createServer(net.TCP,"); |
gmiles3 | 0:1fb00e911fe6 | 72 | strcat(cmdbuff, timeout); |
gmiles3 | 0:1fb00e911fe6 | 73 | strcat(cmdbuff, ")\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 74 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 75 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 76 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 77 | strcpy(cmdbuff, "srv:listen("); |
gmiles3 | 0:1fb00e911fe6 | 78 | strcat(cmdbuff, port); |
gmiles3 | 0:1fb00e911fe6 | 79 | strcat(cmdbuff, ",function(conn)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 80 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 81 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 82 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 83 | strcpy(cmdbuff, "conn:on(\"receive\", function(conn, payload) \r\n"); |
gmiles3 | 0:1fb00e911fe6 | 84 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 85 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 86 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 87 | strcpy(cmdbuff, "conn:send('"); |
gmiles3 | 0:1fb00e911fe6 | 88 | strcat(cmdbuff, reportStatus()); |
gmiles3 | 0:1fb00e911fe6 | 89 | strcat(cmdbuff, "')\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 90 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 91 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 92 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 93 | strcpy(cmdbuff, "print(payload)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 94 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 95 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 96 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 97 | strcpy(cmdbuff, "end)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 98 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 99 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 100 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 101 | strcpy(cmdbuff, "end)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 102 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 103 | wait(.5); |
gmiles3 | 0:1fb00e911fe6 | 104 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 105 | } |
gmiles3 | 0:1fb00e911fe6 | 106 | |
gmiles3 | 0:1fb00e911fe6 | 107 | } |
gmiles3 | 0:1fb00e911fe6 | 108 | |
gmiles3 | 0:1fb00e911fe6 | 109 | |
gmiles3 | 0:1fb00e911fe6 | 110 | |
gmiles3 | 0:1fb00e911fe6 | 111 | |
gmiles3 | 0:1fb00e911fe6 | 112 | |
gmiles3 | 0:1fb00e911fe6 | 113 | |
gmiles3 | 0:1fb00e911fe6 | 114 | |
gmiles3 | 0:1fb00e911fe6 | 115 | /* CAR FUNCTIONS */ |
gmiles3 | 0:1fb00e911fe6 | 116 | |
gmiles3 | 0:1fb00e911fe6 | 117 | void carStop() { |
gmiles3 | 0:1fb00e911fe6 | 118 | left_motor.speed(0); |
gmiles3 | 0:1fb00e911fe6 | 119 | right_motor.speed(0); |
gmiles3 | 0:1fb00e911fe6 | 120 | } |
gmiles3 | 0:1fb00e911fe6 | 121 | |
gmiles3 | 0:1fb00e911fe6 | 122 | void carResetEncoders() { |
gmiles3 | 0:1fb00e911fe6 | 123 | left_hall.reset(); |
gmiles3 | 0:1fb00e911fe6 | 124 | right_hall.reset(); |
gmiles3 | 0:1fb00e911fe6 | 125 | wait(0.1); |
gmiles3 | 0:1fb00e911fe6 | 126 | } |
gmiles3 | 0:1fb00e911fe6 | 127 | |
gmiles3 | 0:1fb00e911fe6 | 128 | void carMoveForwardDistance(Arguments *in, Reply *out) { |
gmiles3 | 0:1fb00e911fe6 | 129 | carResetEncoders(); |
gmiles3 | 0:1fb00e911fe6 | 130 | long diff; |
gmiles3 | 0:1fb00e911fe6 | 131 | int dist = in->getArg<int>(); |
gmiles3 | 0:1fb00e911fe6 | 132 | float enc_dist = dist * encoderFactor; |
gmiles3 | 0:1fb00e911fe6 | 133 | volatile float left_val= left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 134 | volatile float right_val = right_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 135 | while (left_val < enc_dist || left_val < enc_dist) { |
gmiles3 | 0:1fb00e911fe6 | 136 | left_val = left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 137 | right_val = right_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 138 | diff = left_val - right_val; |
gmiles3 | 0:1fb00e911fe6 | 139 | float k = 0.5; |
gmiles3 | 0:1fb00e911fe6 | 140 | if (diff < 0) { // left has moved less than right |
gmiles3 | 0:1fb00e911fe6 | 141 | left_motor.speed(left_speed); |
gmiles3 | 0:1fb00e911fe6 | 142 | right_motor.speed(k*right_speed); |
gmiles3 | 0:1fb00e911fe6 | 143 | } else if (diff > 0) { // right has moved less than left |
gmiles3 | 0:1fb00e911fe6 | 144 | left_motor.speed(k*left_speed); |
gmiles3 | 0:1fb00e911fe6 | 145 | right_motor.speed(right_speed); |
gmiles3 | 0:1fb00e911fe6 | 146 | } else { |
gmiles3 | 0:1fb00e911fe6 | 147 | left_motor.speed(left_speed); |
gmiles3 | 0:1fb00e911fe6 | 148 | right_motor.speed(right_speed); |
gmiles3 | 0:1fb00e911fe6 | 149 | } |
gmiles3 | 0:1fb00e911fe6 | 150 | } |
gmiles3 | 0:1fb00e911fe6 | 151 | clicksTravelled += left_val; |
gmiles3 | 0:1fb00e911fe6 | 152 | left_motor.speed(0); |
gmiles3 | 0:1fb00e911fe6 | 153 | right_motor.speed(0); |
gmiles3 | 0:1fb00e911fe6 | 154 | } |
gmiles3 | 0:1fb00e911fe6 | 155 | |
gmiles3 | 0:1fb00e911fe6 | 156 | void carMoveBackwardDistance(Arguments *in, Reply *out) { |
gmiles3 | 0:1fb00e911fe6 | 157 | int dist = in->getArg<int>(); |
gmiles3 | 0:1fb00e911fe6 | 158 | if (dist > 100) { |
gmiles3 | 0:1fb00e911fe6 | 159 | dist = 100; |
gmiles3 | 0:1fb00e911fe6 | 160 | } |
gmiles3 | 0:1fb00e911fe6 | 161 | carResetEncoders(); |
gmiles3 | 0:1fb00e911fe6 | 162 | long diff; |
gmiles3 | 0:1fb00e911fe6 | 163 | float enc_dist = dist * encoderFactor; |
gmiles3 | 0:1fb00e911fe6 | 164 | volatile float left_val= left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 165 | volatile float right_val = right_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 166 | while (left_val < enc_dist || right_val < enc_dist) { |
gmiles3 | 0:1fb00e911fe6 | 167 | left_val = left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 168 | right_val = right_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 169 | diff = left_val-right_val; |
gmiles3 | 0:1fb00e911fe6 | 170 | float k = 0.8; |
gmiles3 | 0:1fb00e911fe6 | 171 | if (diff < 0) { // left has moved less than right |
gmiles3 | 0:1fb00e911fe6 | 172 | left_motor.speed(-left_speed); |
gmiles3 | 0:1fb00e911fe6 | 173 | right_motor.speed(-k*right_speed); |
gmiles3 | 0:1fb00e911fe6 | 174 | } else if (diff > 0) { // right has moved less than left |
gmiles3 | 0:1fb00e911fe6 | 175 | left_motor.speed(-k*left_speed); |
gmiles3 | 0:1fb00e911fe6 | 176 | right_motor.speed(-right_speed); |
gmiles3 | 0:1fb00e911fe6 | 177 | } else { |
gmiles3 | 0:1fb00e911fe6 | 178 | left_motor.speed(-left_speed); |
gmiles3 | 0:1fb00e911fe6 | 179 | right_motor.speed(-right_speed); |
gmiles3 | 0:1fb00e911fe6 | 180 | } |
gmiles3 | 0:1fb00e911fe6 | 181 | } |
gmiles3 | 0:1fb00e911fe6 | 182 | clicksTravelled += left_val; |
gmiles3 | 0:1fb00e911fe6 | 183 | carStop(); |
gmiles3 | 0:1fb00e911fe6 | 184 | } |
gmiles3 | 0:1fb00e911fe6 | 185 | |
gmiles3 | 0:1fb00e911fe6 | 186 | void carTurn(Arguments *in, Reply *out) { |
gmiles3 | 0:1fb00e911fe6 | 187 | carResetEncoders(); |
gmiles3 | 0:1fb00e911fe6 | 188 | float enc_dist = in->getArg<float>(); |
gmiles3 | 0:1fb00e911fe6 | 189 | volatile float left_val= left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 190 | while (left_val < enc_dist) { |
gmiles3 | 0:1fb00e911fe6 | 191 | left_val = left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 192 | left_motor.speed(left_speed); |
gmiles3 | 0:1fb00e911fe6 | 193 | right_motor.speed(-right_speed); |
gmiles3 | 0:1fb00e911fe6 | 194 | } |
gmiles3 | 0:1fb00e911fe6 | 195 | carStop(); |
gmiles3 | 0:1fb00e911fe6 | 196 | } |
gmiles3 | 0:1fb00e911fe6 | 197 | |
gmiles3 | 0:1fb00e911fe6 | 198 | void carTurnRight(Arguments *in, Reply *out) { |
gmiles3 | 0:1fb00e911fe6 | 199 | carResetEncoders(); |
gmiles3 | 0:1fb00e911fe6 | 200 | float enc_dist = 170; |
gmiles3 | 0:1fb00e911fe6 | 201 | volatile float left_val= left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 202 | while (left_val < enc_dist) { |
gmiles3 | 0:1fb00e911fe6 | 203 | left_val = left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 204 | left_motor.speed(left_speed); |
gmiles3 | 0:1fb00e911fe6 | 205 | right_motor.speed(-right_speed); |
gmiles3 | 0:1fb00e911fe6 | 206 | } |
gmiles3 | 0:1fb00e911fe6 | 207 | carStop(); |
gmiles3 | 0:1fb00e911fe6 | 208 | if (out != NULL) out->putData("GOT HERE"); |
gmiles3 | 0:1fb00e911fe6 | 209 | } |
gmiles3 | 0:1fb00e911fe6 | 210 | |
gmiles3 | 0:1fb00e911fe6 | 211 | void carTurnLeft(Arguments *in, Reply *out) { |
gmiles3 | 0:1fb00e911fe6 | 212 | carResetEncoders(); |
gmiles3 | 0:1fb00e911fe6 | 213 | float enc_dist = 170; |
gmiles3 | 0:1fb00e911fe6 | 214 | volatile float left_val= left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 215 | while (left_val < enc_dist) { |
gmiles3 | 0:1fb00e911fe6 | 216 | left_val = left_hall.read(); |
gmiles3 | 0:1fb00e911fe6 | 217 | left_motor.speed(-left_speed); |
gmiles3 | 0:1fb00e911fe6 | 218 | right_motor.speed(right_speed); |
gmiles3 | 0:1fb00e911fe6 | 219 | } |
gmiles3 | 0:1fb00e911fe6 | 220 | carStop(); |
gmiles3 | 0:1fb00e911fe6 | 221 | } |
gmiles3 | 0:1fb00e911fe6 | 222 | |
gmiles3 | 0:1fb00e911fe6 | 223 | |
gmiles3 | 0:1fb00e911fe6 | 224 | |
gmiles3 | 0:1fb00e911fe6 | 225 | |
gmiles3 | 0:1fb00e911fe6 | 226 | |
gmiles3 | 0:1fb00e911fe6 | 227 | |
gmiles3 | 0:1fb00e911fe6 | 228 | |
gmiles3 | 0:1fb00e911fe6 | 229 | |
gmiles3 | 0:1fb00e911fe6 | 230 | |
gmiles3 | 0:1fb00e911fe6 | 231 | |
gmiles3 | 0:1fb00e911fe6 | 232 | /* WIFI FUNCTIONS */ |
gmiles3 | 0:1fb00e911fe6 | 233 | |
gmiles3 | 0:1fb00e911fe6 | 234 | void connectToNetwork() { |
gmiles3 | 0:1fb00e911fe6 | 235 | pc.printf("# Resetting ESP\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 236 | strcpy(cmdbuff,"node.restart()\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 237 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 238 | wait(5); |
gmiles3 | 0:1fb00e911fe6 | 239 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 240 | |
gmiles3 | 0:1fb00e911fe6 | 241 | led1=1,led2=0,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 242 | pc.printf("# Setting Mode\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 243 | strcpy(cmdbuff, "wifi.setmode(wifi.STATION)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 244 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 245 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 246 | |
gmiles3 | 0:1fb00e911fe6 | 247 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 248 | led1=0,led2=1,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 249 | pc.printf("# Connecting to AP\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 250 | pc.printf("# ssid = %s\t\tpwd = %s\r\n", ssid, pwd); |
gmiles3 | 0:1fb00e911fe6 | 251 | strcpy(cmdbuff, "wifi.sta.config(\""); |
gmiles3 | 0:1fb00e911fe6 | 252 | strcat(cmdbuff, ssid); |
gmiles3 | 0:1fb00e911fe6 | 253 | strcat(cmdbuff, "\",\""); |
gmiles3 | 0:1fb00e911fe6 | 254 | strcat(cmdbuff, pwd); |
gmiles3 | 0:1fb00e911fe6 | 255 | strcat(cmdbuff, "\")\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 256 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 257 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 258 | |
gmiles3 | 0:1fb00e911fe6 | 259 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 260 | led1=0,led2=0,led3=1; |
gmiles3 | 0:1fb00e911fe6 | 261 | pc.printf("# Get IP Address\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 262 | strcpy(cmdbuff, "print(wifi.sta.getip())\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 263 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 264 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 265 | |
gmiles3 | 0:1fb00e911fe6 | 266 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 267 | led1=1,led2=0,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 268 | pc.printf("# Get Connection Status\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 269 | strcpy(cmdbuff, "print(wifi.sta.status())\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 270 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 271 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 272 | |
gmiles3 | 0:1fb00e911fe6 | 273 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 274 | led1=0,led2=1,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 275 | pc.printf("# Listen on Port\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 276 | strcpy(cmdbuff, "srv=net.createServer(net.TCP,"); |
gmiles3 | 0:1fb00e911fe6 | 277 | strcat(cmdbuff, timeout); |
gmiles3 | 0:1fb00e911fe6 | 278 | strcat(cmdbuff, ")\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 279 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 280 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 281 | |
gmiles3 | 0:1fb00e911fe6 | 282 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 283 | led1=0,led2=0,led3=1; |
gmiles3 | 0:1fb00e911fe6 | 284 | strcpy(cmdbuff, "srv:listen("); |
gmiles3 | 0:1fb00e911fe6 | 285 | strcat(cmdbuff, port); |
gmiles3 | 0:1fb00e911fe6 | 286 | strcat(cmdbuff, ",function(conn)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 287 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 288 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 289 | |
gmiles3 | 0:1fb00e911fe6 | 290 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 291 | led1=1,led2=0,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 292 | strcpy(cmdbuff, "conn:on(\"receive\", function(conn, payload) \r\n"); |
gmiles3 | 0:1fb00e911fe6 | 293 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 294 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 295 | |
gmiles3 | 0:1fb00e911fe6 | 296 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 297 | led1=0,led2=1,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 298 | strcpy(cmdbuff, "conn:send('"); |
gmiles3 | 0:1fb00e911fe6 | 299 | strcat(cmdbuff, reportStatus()); |
gmiles3 | 0:1fb00e911fe6 | 300 | strcat(cmdbuff, "')\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 301 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 302 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 303 | |
gmiles3 | 0:1fb00e911fe6 | 304 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 305 | led1=0,led2=0,led3=1; |
gmiles3 | 0:1fb00e911fe6 | 306 | strcpy(cmdbuff, "print(payload)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 307 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 308 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 309 | |
gmiles3 | 0:1fb00e911fe6 | 310 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 311 | led1=1,led2=0,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 312 | strcpy(cmdbuff, "end)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 313 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 314 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 315 | |
gmiles3 | 0:1fb00e911fe6 | 316 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 317 | led1=0,led2=1,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 318 | strcpy(cmdbuff, "end)\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 319 | sendCMD(); |
gmiles3 | 0:1fb00e911fe6 | 320 | getReply(); |
gmiles3 | 0:1fb00e911fe6 | 321 | |
gmiles3 | 0:1fb00e911fe6 | 322 | wait(2); |
gmiles3 | 0:1fb00e911fe6 | 323 | //led1=1,led2=1,led3=1; |
gmiles3 | 0:1fb00e911fe6 | 324 | led1=0,led2=0,led3=0; |
gmiles3 | 0:1fb00e911fe6 | 325 | pc.printf("# Ready\r\n"); |
gmiles3 | 0:1fb00e911fe6 | 326 | } |
gmiles3 | 0:1fb00e911fe6 | 327 | |
gmiles3 | 0:1fb00e911fe6 | 328 | void sendCMD() |
gmiles3 | 0:1fb00e911fe6 | 329 | { |
gmiles3 | 0:1fb00e911fe6 | 330 | int i; |
gmiles3 | 0:1fb00e911fe6 | 331 | char temp_char; |
gmiles3 | 0:1fb00e911fe6 | 332 | bool empty; |
gmiles3 | 0:1fb00e911fe6 | 333 | i = 0; |
gmiles3 | 0:1fb00e911fe6 | 334 | // Start Critical Section - don't interrupt while changing global buffer variables |
gmiles3 | 0:1fb00e911fe6 | 335 | NVIC_DisableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 336 | empty = (tx_in == tx_out); |
gmiles3 | 0:1fb00e911fe6 | 337 | while ((i==0) || (cmdbuff[i-1] != '\n')) { |
gmiles3 | 0:1fb00e911fe6 | 338 | // Wait if buffer full |
gmiles3 | 0:1fb00e911fe6 | 339 | if (((tx_in + 1) % buffer_size) == tx_out) { |
gmiles3 | 0:1fb00e911fe6 | 340 | // End Critical Section - need to let interrupt routine empty buffer by sending |
gmiles3 | 0:1fb00e911fe6 | 341 | NVIC_EnableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 342 | while (((tx_in + 1) % buffer_size) == tx_out) { |
gmiles3 | 0:1fb00e911fe6 | 343 | } |
gmiles3 | 0:1fb00e911fe6 | 344 | // Start Critical Section - don't interrupt while changing global buffer variables |
gmiles3 | 0:1fb00e911fe6 | 345 | NVIC_DisableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 346 | } |
gmiles3 | 0:1fb00e911fe6 | 347 | tx_buffer[tx_in] = cmdbuff[i]; |
gmiles3 | 0:1fb00e911fe6 | 348 | i++; |
gmiles3 | 0:1fb00e911fe6 | 349 | tx_in = (tx_in + 1) % buffer_size; |
gmiles3 | 0:1fb00e911fe6 | 350 | } |
gmiles3 | 0:1fb00e911fe6 | 351 | if (esp.writeable() && (empty)) { |
gmiles3 | 0:1fb00e911fe6 | 352 | temp_char = tx_buffer[tx_out]; |
gmiles3 | 0:1fb00e911fe6 | 353 | tx_out = (tx_out + 1) % buffer_size; |
gmiles3 | 0:1fb00e911fe6 | 354 | // Send first character to start tx interrupts, if stopped |
gmiles3 | 0:1fb00e911fe6 | 355 | esp.putc(temp_char); |
gmiles3 | 0:1fb00e911fe6 | 356 | } |
gmiles3 | 0:1fb00e911fe6 | 357 | // End Critical Section |
gmiles3 | 0:1fb00e911fe6 | 358 | NVIC_EnableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 359 | return; |
gmiles3 | 0:1fb00e911fe6 | 360 | } |
gmiles3 | 0:1fb00e911fe6 | 361 | |
gmiles3 | 0:1fb00e911fe6 | 362 | // Read a line from the large rx buffer from rx interrupt routine |
gmiles3 | 0:1fb00e911fe6 | 363 | void getReply() { |
gmiles3 | 0:1fb00e911fe6 | 364 | int i; |
gmiles3 | 0:1fb00e911fe6 | 365 | i = 0; |
gmiles3 | 0:1fb00e911fe6 | 366 | // Start Critical Section - don't interrupt while changing global buffer variables |
gmiles3 | 0:1fb00e911fe6 | 367 | NVIC_DisableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 368 | // Loop reading rx buffer characters until end of line character |
gmiles3 | 0:1fb00e911fe6 | 369 | while ((i==0) || (rx_line[i-1] != '\r')) { |
gmiles3 | 0:1fb00e911fe6 | 370 | // Wait if buffer empty |
gmiles3 | 0:1fb00e911fe6 | 371 | if (rx_in == rx_out) { |
gmiles3 | 0:1fb00e911fe6 | 372 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
gmiles3 | 0:1fb00e911fe6 | 373 | NVIC_EnableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 374 | while (rx_in == rx_out) { |
gmiles3 | 0:1fb00e911fe6 | 375 | } |
gmiles3 | 0:1fb00e911fe6 | 376 | // Start Critical Section - don't interrupt while changing global buffer variables |
gmiles3 | 0:1fb00e911fe6 | 377 | NVIC_DisableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 378 | } |
gmiles3 | 0:1fb00e911fe6 | 379 | rx_line[i] = rx_buffer[rx_out]; |
gmiles3 | 0:1fb00e911fe6 | 380 | i++; |
gmiles3 | 0:1fb00e911fe6 | 381 | rx_out = (rx_out + 1) % buffer_size; |
gmiles3 | 0:1fb00e911fe6 | 382 | } |
gmiles3 | 0:1fb00e911fe6 | 383 | // End Critical Section |
gmiles3 | 0:1fb00e911fe6 | 384 | NVIC_EnableIRQ(UART1_IRQn); |
gmiles3 | 0:1fb00e911fe6 | 385 | rx_line[i-1] = 0; |
gmiles3 | 0:1fb00e911fe6 | 386 | return; |
gmiles3 | 0:1fb00e911fe6 | 387 | } |
gmiles3 | 0:1fb00e911fe6 | 388 | |
gmiles3 | 0:1fb00e911fe6 | 389 | char* reportStatus() { |
gmiles3 | 0:1fb00e911fe6 | 390 | char str[30]; |
gmiles3 | 0:1fb00e911fe6 | 391 | float inchesTravelled = clicksTravelled / encoderFactor ; |
gmiles3 | 0:1fb00e911fe6 | 392 | sprintf(str, "%.1f", inchesTravelled); |
gmiles3 | 0:1fb00e911fe6 | 393 | return str; |
gmiles3 | 0:1fb00e911fe6 | 394 | } |
gmiles3 | 0:1fb00e911fe6 | 395 | |
gmiles3 | 0:1fb00e911fe6 | 396 | // Interupt Routine to read in data from serial port |
gmiles3 | 0:1fb00e911fe6 | 397 | void Rx_interrupt() { |
gmiles3 | 0:1fb00e911fe6 | 398 | //led3=1; |
gmiles3 | 0:1fb00e911fe6 | 399 | // Loop just in case more than one character is in UART's receive FIFO buffer |
gmiles3 | 0:1fb00e911fe6 | 400 | // Stop if buffer full |
gmiles3 | 0:1fb00e911fe6 | 401 | while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
gmiles3 | 0:1fb00e911fe6 | 402 | rx_buffer[rx_in] = esp.getc(); |
gmiles3 | 0:1fb00e911fe6 | 403 | // Uncomment to Echo to USB serial to watch data flow |
gmiles3 | 0:1fb00e911fe6 | 404 | pc.putc(rx_buffer[rx_in]); |
gmiles3 | 0:1fb00e911fe6 | 405 | rx_in = (rx_in + 1) % buffer_size; |
gmiles3 | 0:1fb00e911fe6 | 406 | } |
gmiles3 | 0:1fb00e911fe6 | 407 | return; |
gmiles3 | 0:1fb00e911fe6 | 408 | } |
gmiles3 | 0:1fb00e911fe6 | 409 | |
gmiles3 | 0:1fb00e911fe6 | 410 | |
gmiles3 | 0:1fb00e911fe6 | 411 | // Interupt Routine to write out data to serial port |
gmiles3 | 0:1fb00e911fe6 | 412 | void Tx_interrupt() { |
gmiles3 | 0:1fb00e911fe6 | 413 | //led2=1; |
gmiles3 | 0:1fb00e911fe6 | 414 | // Loop to fill more than one character in UART's transmit FIFO buffer |
gmiles3 | 0:1fb00e911fe6 | 415 | // Stop if buffer empty |
gmiles3 | 0:1fb00e911fe6 | 416 | while ((esp.writeable()) && (tx_in != tx_out)) { |
gmiles3 | 0:1fb00e911fe6 | 417 | esp.putc(tx_buffer[tx_out]); |
gmiles3 | 0:1fb00e911fe6 | 418 | tx_out = (tx_out + 1) % buffer_size; |
gmiles3 | 0:1fb00e911fe6 | 419 | } |
gmiles3 | 0:1fb00e911fe6 | 420 | //led2=0; |
gmiles3 | 0:1fb00e911fe6 | 421 | return; |
gmiles3 | 0:1fb00e911fe6 | 422 | } |