Self-navigating IOT robot for ECE 4180. Before navigation code begins, wi-fi http server setup takes time (approx. 2 min, see serial output in terminal window)
Dependencies: HC_SR04_Ultrasonic_Library Motor mbed-rtos mbed
main.cpp@0:66c1c628e189, 2016-12-09 (annotated)
- Committer:
- ashatune
- Date:
- Fri Dec 09 14:32:59 2016 +0000
- Revision:
- 0:66c1c628e189
IOT self navigating robot code for GaTech ECE 4180 final project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashatune | 0:66c1c628e189 | 1 | #include "mbed.h" |
ashatune | 0:66c1c628e189 | 2 | #include "Motor.h" |
ashatune | 0:66c1c628e189 | 3 | #include "rtos.h" |
ashatune | 0:66c1c628e189 | 4 | #include "ultrasonic.h" |
ashatune | 0:66c1c628e189 | 5 | |
ashatune | 0:66c1c628e189 | 6 | //Account for time (approx. 2min, see serial output in terminal) for wi-fi server to set up before navigation code begins!! |
ashatune | 0:66c1c628e189 | 7 | |
ashatune | 0:66c1c628e189 | 8 | Motor ma(p25, p14, p13); |
ashatune | 0:66c1c628e189 | 9 | Motor mb(p24, p11, p12); // pwm, fwd, rev |
ashatune | 0:66c1c628e189 | 10 | DigitalOut myled(LED1); |
ashatune | 0:66c1c628e189 | 11 | DigitalOut led4(LED4); |
ashatune | 0:66c1c628e189 | 12 | DigitalOut led3(LED3); |
ashatune | 0:66c1c628e189 | 13 | |
ashatune | 0:66c1c628e189 | 14 | //speaker/sd set up |
ashatune | 0:66c1c628e189 | 15 | /*AnalogOut DACout(p18); //currently in pwm 26 |
ashatune | 0:66c1c628e189 | 16 | wave_player waver(&DACout); |
ashatune | 0:66c1c628e189 | 17 | SDFileSystem sd(p5, p6, p7, p8, "sd");*/ |
ashatune | 0:66c1c628e189 | 18 | |
ashatune | 0:66c1c628e189 | 19 | Serial pc(USBTX, USBRX); |
ashatune | 0:66c1c628e189 | 20 | Serial esp(p28, p27); // tx, rx |
ashatune | 0:66c1c628e189 | 21 | DigitalOut reset(p26); |
ashatune | 0:66c1c628e189 | 22 | Timer t; |
ashatune | 0:66c1c628e189 | 23 | |
ashatune | 0:66c1c628e189 | 24 | int count,ended,timeout; |
ashatune | 0:66c1c628e189 | 25 | char buf[2024]; |
ashatune | 0:66c1c628e189 | 26 | char snd[1024]; |
ashatune | 0:66c1c628e189 | 27 | |
ashatune | 0:66c1c628e189 | 28 | char ssid[32] = "iPhone"; // enter WiFi router ssid inside the quotes |
ashatune | 0:66c1c628e189 | 29 | char pwd [32] = "Jordan23#"; // enter WiFi router password inside the quotes |
ashatune | 0:66c1c628e189 | 30 | |
ashatune | 0:66c1c628e189 | 31 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); |
ashatune | 0:66c1c628e189 | 32 | void dev_recv() |
ashatune | 0:66c1c628e189 | 33 | { |
ashatune | 0:66c1c628e189 | 34 | myled = myled; |
ashatune | 0:66c1c628e189 | 35 | while(esp.readable()) { |
ashatune | 0:66c1c628e189 | 36 | pc.putc(esp.getc()); |
ashatune | 0:66c1c628e189 | 37 | } |
ashatune | 0:66c1c628e189 | 38 | } |
ashatune | 0:66c1c628e189 | 39 | |
ashatune | 0:66c1c628e189 | 40 | void pc_recv() |
ashatune | 0:66c1c628e189 | 41 | { |
ashatune | 0:66c1c628e189 | 42 | led4 = !led4; |
ashatune | 0:66c1c628e189 | 43 | while(pc.readable()) { |
ashatune | 0:66c1c628e189 | 44 | esp.putc(pc.getc()); |
ashatune | 0:66c1c628e189 | 45 | } |
ashatune | 0:66c1c628e189 | 46 | } |
ashatune | 0:66c1c628e189 | 47 | |
ashatune | 0:66c1c628e189 | 48 | |
ashatune | 0:66c1c628e189 | 49 | DigitalIn Enc1(p21); //right motor... |
ashatune | 0:66c1c628e189 | 50 | DigitalIn Enc2(p22); //left motor |
ashatune | 0:66c1c628e189 | 51 | int oldEnc1 = 0; |
ashatune | 0:66c1c628e189 | 52 | int oldEnc2 = 0; |
ashatune | 0:66c1c628e189 | 53 | |
ashatune | 0:66c1c628e189 | 54 | int ticksR = 0; //# times right wheel encoder changed state |
ashatune | 0:66c1c628e189 | 55 | int ticksL = 0; //" " |
ashatune | 0:66c1c628e189 | 56 | |
ashatune | 0:66c1c628e189 | 57 | void dist1(int distance) |
ashatune | 0:66c1c628e189 | 58 | { |
ashatune | 0:66c1c628e189 | 59 | //put code here to execute when the distance has changed |
ashatune | 0:66c1c628e189 | 60 | printf("Distance1 %d mm\r\n", distance); |
ashatune | 0:66c1c628e189 | 61 | } |
ashatune | 0:66c1c628e189 | 62 | void dist2(int distance2) |
ashatune | 0:66c1c628e189 | 63 | { |
ashatune | 0:66c1c628e189 | 64 | //put code here to execute when the distance has changed |
ashatune | 0:66c1c628e189 | 65 | printf("Distance2 %d mm\r\n", distance2); |
ashatune | 0:66c1c628e189 | 66 | } |
ashatune | 0:66c1c628e189 | 67 | |
ashatune | 0:66c1c628e189 | 68 | //Sonar1 variables |
ashatune | 0:66c1c628e189 | 69 | ultrasonic mu1(p15, p16, .1, 1, &dist1); |
ashatune | 0:66c1c628e189 | 70 | |
ashatune | 0:66c1c628e189 | 71 | //Sonar2 variables |
ashatune | 0:66c1c628e189 | 72 | ultrasonic mu2(p22, p21, .1, 1, &dist2); |
ashatune | 0:66c1c628e189 | 73 | |
ashatune | 0:66c1c628e189 | 74 | void wheelEnc(void const *argument){ |
ashatune | 0:66c1c628e189 | 75 | while(1){ |
ashatune | 0:66c1c628e189 | 76 | if(Enc1 != oldEnc1) { // Increment ticks every time the state has switched. |
ashatune | 0:66c1c628e189 | 77 | ticksR++; |
ashatune | 0:66c1c628e189 | 78 | oldEnc1 = Enc1; |
ashatune | 0:66c1c628e189 | 79 | |
ashatune | 0:66c1c628e189 | 80 | } |
ashatune | 0:66c1c628e189 | 81 | if(Enc2 != oldEnc2) { |
ashatune | 0:66c1c628e189 | 82 | ticksL++; |
ashatune | 0:66c1c628e189 | 83 | oldEnc2 = Enc2; |
ashatune | 0:66c1c628e189 | 84 | } |
ashatune | 0:66c1c628e189 | 85 | pc.printf("Left ticks: %d \n", ticksL); |
ashatune | 0:66c1c628e189 | 86 | pc.printf("Right ticks: %d \n", ticksR); |
ashatune | 0:66c1c628e189 | 87 | } |
ashatune | 0:66c1c628e189 | 88 | } |
ashatune | 0:66c1c628e189 | 89 | |
ashatune | 0:66c1c628e189 | 90 | void sonarThread(void const *argument){ |
ashatune | 0:66c1c628e189 | 91 | while(1){ |
ashatune | 0:66c1c628e189 | 92 | mu1.checkDistance(); //front sonar |
ashatune | 0:66c1c628e189 | 93 | mu2.checkDistance(); //right side sonar |
ashatune | 0:66c1c628e189 | 94 | int muDist1 = mu1.getCurrentDistance(); |
ashatune | 0:66c1c628e189 | 95 | int muDist2 = mu2.getCurrentDistance(); |
ashatune | 0:66c1c628e189 | 96 | |
ashatune | 0:66c1c628e189 | 97 | //always move straight forward |
ashatune | 0:66c1c628e189 | 98 | ma.speed(-.8); //Left motor |
ashatune | 0:66c1c628e189 | 99 | mb.speed(-.8); //Right motor |
ashatune | 0:66c1c628e189 | 100 | myled =0; |
ashatune | 0:66c1c628e189 | 101 | |
ashatune | 0:66c1c628e189 | 102 | //Front sensor is not detecting a wall, but right side is too close |
ashatune | 0:66c1c628e189 | 103 | if ((muDist1 > 250) && (muDist2 < 170)){ |
ashatune | 0:66c1c628e189 | 104 | ma.speed(-.2); |
ashatune | 0:66c1c628e189 | 105 | mb.speed(-.9); |
ashatune | 0:66c1c628e189 | 106 | } |
ashatune | 0:66c1c628e189 | 107 | //Front sensor is not detecting a wall, but right side is too far |
ashatune | 0:66c1c628e189 | 108 | else if ((muDist1 > 250) && (muDist2 > 250)){ |
ashatune | 0:66c1c628e189 | 109 | ma.speed(-.6); //turn |
ashatune | 0:66c1c628e189 | 110 | mb.speed(-0.4); |
ashatune | 0:66c1c628e189 | 111 | } |
ashatune | 0:66c1c628e189 | 112 | else if (muDist1 < 280){ |
ashatune | 0:66c1c628e189 | 113 | ma.speed(1); // |
ashatune | 0:66c1c628e189 | 114 | mb.speed(-1); |
ashatune | 0:66c1c628e189 | 115 | wait(0.3); |
ashatune | 0:66c1c628e189 | 116 | } |
ashatune | 0:66c1c628e189 | 117 | else if ((muDist1 > 250) && (muDist2 > 500)){ |
ashatune | 0:66c1c628e189 | 118 | ma.speed(-.8); |
ashatune | 0:66c1c628e189 | 119 | mb.speed(-.8); |
ashatune | 0:66c1c628e189 | 120 | wait(0.1); |
ashatune | 0:66c1c628e189 | 121 | mb.speed(1); |
ashatune | 0:66c1c628e189 | 122 | wait(0.1); |
ashatune | 0:66c1c628e189 | 123 | } |
ashatune | 0:66c1c628e189 | 124 | else { |
ashatune | 0:66c1c628e189 | 125 | ma.speed(-.8); |
ashatune | 0:66c1c628e189 | 126 | mb.speed(-.8); |
ashatune | 0:66c1c628e189 | 127 | wait(.1); |
ashatune | 0:66c1c628e189 | 128 | } |
ashatune | 0:66c1c628e189 | 129 | } |
ashatune | 0:66c1c628e189 | 130 | } |
ashatune | 0:66c1c628e189 | 131 | /////////////////////////// |
ashatune | 0:66c1c628e189 | 132 | /*void audio_thread(void const *argument) |
ashatune | 0:66c1c628e189 | 133 | { |
ashatune | 0:66c1c628e189 | 134 | ultra_mutex.lock(); |
ashatune | 0:66c1c628e189 | 135 | FILE *wave_file; |
ashatune | 0:66c1c628e189 | 136 | wave_file=fopen("/sd/imperial_marchEdit.wav","r"); |
ashatune | 0:66c1c628e189 | 137 | waver.play(wave_file); |
ashatune | 0:66c1c628e189 | 138 | fclose(wave_file); |
ashatune | 0:66c1c628e189 | 139 | ultra_mutex.unlock(); |
ashatune | 0:66c1c628e189 | 140 | }*/ |
ashatune | 0:66c1c628e189 | 141 | |
ashatune | 0:66c1c628e189 | 142 | |
ashatune | 0:66c1c628e189 | 143 | int main() |
ashatune | 0:66c1c628e189 | 144 | { |
ashatune | 0:66c1c628e189 | 145 | led3 = 1; |
ashatune | 0:66c1c628e189 | 146 | wait(2); |
ashatune | 0:66c1c628e189 | 147 | reset=0; //hardware reset for 8266 |
ashatune | 0:66c1c628e189 | 148 | pc.baud(9600); // set what you want here depending on your terminal program speed |
ashatune | 0:66c1c628e189 | 149 | pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); |
ashatune | 0:66c1c628e189 | 150 | wait(0.5); |
ashatune | 0:66c1c628e189 | 151 | reset=1; |
ashatune | 0:66c1c628e189 | 152 | timeout=2; |
ashatune | 0:66c1c628e189 | 153 | getreply(); |
ashatune | 0:66c1c628e189 | 154 | |
ashatune | 0:66c1c628e189 | 155 | esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time. |
ashatune | 0:66c1c628e189 | 156 | |
ashatune | 0:66c1c628e189 | 157 | //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** |
ashatune | 0:66c1c628e189 | 158 | |
ashatune | 0:66c1c628e189 | 159 | ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** |
ashatune | 0:66c1c628e189 | 160 | |
ashatune | 0:66c1c628e189 | 161 | |
ashatune | 0:66c1c628e189 | 162 | |
ashatune | 0:66c1c628e189 | 163 | pc.attach(&pc_recv, Serial::RxIrq); |
ashatune | 0:66c1c628e189 | 164 | esp.attach(&dev_recv, Serial::RxIrq); |
ashatune | 0:66c1c628e189 | 165 | //start measuring the distance |
ashatune | 0:66c1c628e189 | 166 | mu1.startUpdates(); |
ashatune | 0:66c1c628e189 | 167 | mu2.startUpdates(); |
ashatune | 0:66c1c628e189 | 168 | |
ashatune | 0:66c1c628e189 | 169 | Enc1.mode(PullUp); // requires a pullup resistor so i just used embed's feature. |
ashatune | 0:66c1c628e189 | 170 | Enc2.mode(PullUp); |
ashatune | 0:66c1c628e189 | 171 | |
ashatune | 0:66c1c628e189 | 172 | Thread thread1(wheelEnc); |
ashatune | 0:66c1c628e189 | 173 | Thread thread2(sonarThread); |
ashatune | 0:66c1c628e189 | 174 | //Thread thread3(audio_thread); |
ashatune | 0:66c1c628e189 | 175 | //Thread thread4(wifiThread); |
ashatune | 0:66c1c628e189 | 176 | |
ashatune | 0:66c1c628e189 | 177 | while(1) { |
ashatune | 0:66c1c628e189 | 178 | |
ashatune | 0:66c1c628e189 | 179 | Thread::wait(100); |
ashatune | 0:66c1c628e189 | 180 | |
ashatune | 0:66c1c628e189 | 181 | } |
ashatune | 0:66c1c628e189 | 182 | } |
ashatune | 0:66c1c628e189 | 183 | |
ashatune | 0:66c1c628e189 | 184 | |
ashatune | 0:66c1c628e189 | 185 | |
ashatune | 0:66c1c628e189 | 186 | ///WIFI SERVER GUI SETUP |
ashatune | 0:66c1c628e189 | 187 | |
ashatune | 0:66c1c628e189 | 188 | // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed |
ashatune | 0:66c1c628e189 | 189 | void ESPsetbaudrate() |
ashatune | 0:66c1c628e189 | 190 | { |
ashatune | 0:66c1c628e189 | 191 | strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate |
ashatune | 0:66c1c628e189 | 192 | SendCMD(); |
ashatune | 0:66c1c628e189 | 193 | } |
ashatune | 0:66c1c628e189 | 194 | |
ashatune | 0:66c1c628e189 | 195 | // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ |
ashatune | 0:66c1c628e189 | 196 | void ESPconfig() |
ashatune | 0:66c1c628e189 | 197 | { |
ashatune | 0:66c1c628e189 | 198 | |
ashatune | 0:66c1c628e189 | 199 | wait(5); |
ashatune | 0:66c1c628e189 | 200 | pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); |
ashatune | 0:66c1c628e189 | 201 | strcpy(snd,".\r\n.\r\n"); |
ashatune | 0:66c1c628e189 | 202 | SendCMD(); |
ashatune | 0:66c1c628e189 | 203 | wait(1); |
ashatune | 0:66c1c628e189 | 204 | pc.printf("---------- Reset & get Firmware ----------\r\n"); |
ashatune | 0:66c1c628e189 | 205 | strcpy(snd,"node.restart()\r\n"); |
ashatune | 0:66c1c628e189 | 206 | SendCMD(); |
ashatune | 0:66c1c628e189 | 207 | timeout=5; |
ashatune | 0:66c1c628e189 | 208 | getreply(); |
ashatune | 0:66c1c628e189 | 209 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 210 | |
ashatune | 0:66c1c628e189 | 211 | wait(2); |
ashatune | 0:66c1c628e189 | 212 | |
ashatune | 0:66c1c628e189 | 213 | pc.printf("\n---------- Get Version ----------\r\n"); |
ashatune | 0:66c1c628e189 | 214 | strcpy(snd,"print(node.info())\r\n"); |
ashatune | 0:66c1c628e189 | 215 | SendCMD(); |
ashatune | 0:66c1c628e189 | 216 | timeout=4; |
ashatune | 0:66c1c628e189 | 217 | getreply(); |
ashatune | 0:66c1c628e189 | 218 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 219 | |
ashatune | 0:66c1c628e189 | 220 | wait(3); |
ashatune | 0:66c1c628e189 | 221 | |
ashatune | 0:66c1c628e189 | 222 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
ashatune | 0:66c1c628e189 | 223 | pc.printf("\n---------- Setting Mode ----------\r\n"); |
ashatune | 0:66c1c628e189 | 224 | strcpy(snd, "wifi.setmode(wifi.STATION)\r\n"); |
ashatune | 0:66c1c628e189 | 225 | SendCMD(); |
ashatune | 0:66c1c628e189 | 226 | timeout=4; |
ashatune | 0:66c1c628e189 | 227 | getreply(); |
ashatune | 0:66c1c628e189 | 228 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 229 | |
ashatune | 0:66c1c628e189 | 230 | wait(2); |
ashatune | 0:66c1c628e189 | 231 | |
ashatune | 0:66c1c628e189 | 232 | |
ashatune | 0:66c1c628e189 | 233 | |
ashatune | 0:66c1c628e189 | 234 | pc.printf("\n---------- Listing Access Points ----------\r\n"); |
ashatune | 0:66c1c628e189 | 235 | strcpy(snd, "function listap(t)\r\n"); |
ashatune | 0:66c1c628e189 | 236 | SendCMD(); |
ashatune | 0:66c1c628e189 | 237 | wait(1); |
ashatune | 0:66c1c628e189 | 238 | strcpy(snd, "for k,v in pairs(t) do\r\n"); |
ashatune | 0:66c1c628e189 | 239 | SendCMD(); |
ashatune | 0:66c1c628e189 | 240 | wait(1); |
ashatune | 0:66c1c628e189 | 241 | strcpy(snd, "print(k..\" : \"..v)\r\n"); |
ashatune | 0:66c1c628e189 | 242 | SendCMD(); |
ashatune | 0:66c1c628e189 | 243 | wait(1); |
ashatune | 0:66c1c628e189 | 244 | strcpy(snd, "end\r\n"); |
ashatune | 0:66c1c628e189 | 245 | SendCMD(); |
ashatune | 0:66c1c628e189 | 246 | wait(1); |
ashatune | 0:66c1c628e189 | 247 | strcpy(snd, "end\r\n"); |
ashatune | 0:66c1c628e189 | 248 | SendCMD(); |
ashatune | 0:66c1c628e189 | 249 | wait(1); |
ashatune | 0:66c1c628e189 | 250 | strcpy(snd, "wifi.sta.getap(listap)\r\n"); |
ashatune | 0:66c1c628e189 | 251 | SendCMD(); |
ashatune | 0:66c1c628e189 | 252 | wait(1); |
ashatune | 0:66c1c628e189 | 253 | timeout=15; |
ashatune | 0:66c1c628e189 | 254 | getreply(); |
ashatune | 0:66c1c628e189 | 255 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 256 | |
ashatune | 0:66c1c628e189 | 257 | wait(2); |
ashatune | 0:66c1c628e189 | 258 | |
ashatune | 0:66c1c628e189 | 259 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
ashatune | 0:66c1c628e189 | 260 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
ashatune | 0:66c1c628e189 | 261 | strcpy(snd, "wifi.sta.config(\""); |
ashatune | 0:66c1c628e189 | 262 | strcat(snd, ssid); |
ashatune | 0:66c1c628e189 | 263 | strcat(snd, "\",\""); |
ashatune | 0:66c1c628e189 | 264 | strcat(snd, pwd); |
ashatune | 0:66c1c628e189 | 265 | strcat(snd, "\")\r\n"); |
ashatune | 0:66c1c628e189 | 266 | SendCMD(); |
ashatune | 0:66c1c628e189 | 267 | timeout=10; |
ashatune | 0:66c1c628e189 | 268 | getreply(); |
ashatune | 0:66c1c628e189 | 269 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 270 | |
ashatune | 0:66c1c628e189 | 271 | wait(5); |
ashatune | 0:66c1c628e189 | 272 | |
ashatune | 0:66c1c628e189 | 273 | pc.printf("\n---------- Get IP's ----------\r\n"); |
ashatune | 0:66c1c628e189 | 274 | strcpy(snd, "print(wifi.sta.getip())\r\n"); |
ashatune | 0:66c1c628e189 | 275 | SendCMD(); |
ashatune | 0:66c1c628e189 | 276 | timeout=3; |
ashatune | 0:66c1c628e189 | 277 | getreply(); |
ashatune | 0:66c1c628e189 | 278 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 279 | |
ashatune | 0:66c1c628e189 | 280 | wait(1); |
ashatune | 0:66c1c628e189 | 281 | |
ashatune | 0:66c1c628e189 | 282 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
ashatune | 0:66c1c628e189 | 283 | strcpy(snd, "print(wifi.sta.status())\r\n"); |
ashatune | 0:66c1c628e189 | 284 | SendCMD(); |
ashatune | 0:66c1c628e189 | 285 | timeout=5; |
ashatune | 0:66c1c628e189 | 286 | getreply(); |
ashatune | 0:66c1c628e189 | 287 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 288 | |
ashatune | 0:66c1c628e189 | 289 | pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); |
ashatune | 0:66c1c628e189 | 290 | pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); |
ashatune | 0:66c1c628e189 | 291 | pc.printf(" It saves the SSID and password settings internally\r\n"); |
ashatune | 0:66c1c628e189 | 292 | pc.printf(" Asha Harris and Teferi Lab 2 "); |
ashatune | 0:66c1c628e189 | 293 | wait(10); |
ashatune | 0:66c1c628e189 | 294 | |
ashatune | 0:66c1c628e189 | 295 | |
ashatune | 0:66c1c628e189 | 296 | pc.printf("\n---------- Setting up http server ----------\r\n"); |
ashatune | 0:66c1c628e189 | 297 | strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); |
ashatune | 0:66c1c628e189 | 298 | SendCMD(); |
ashatune | 0:66c1c628e189 | 299 | wait(1); |
ashatune | 0:66c1c628e189 | 300 | strcpy(snd, "srv:listen(80,function(conn)\r\n"); |
ashatune | 0:66c1c628e189 | 301 | SendCMD(); |
ashatune | 0:66c1c628e189 | 302 | wait(1); |
ashatune | 0:66c1c628e189 | 303 | strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n"); |
ashatune | 0:66c1c628e189 | 304 | SendCMD(); |
ashatune | 0:66c1c628e189 | 305 | wait(1); |
ashatune | 0:66c1c628e189 | 306 | strcpy(snd, "print(payload)\r\n"); |
ashatune | 0:66c1c628e189 | 307 | SendCMD(); |
ashatune | 0:66c1c628e189 | 308 | wait(1); |
ashatune | 0:66c1c628e189 | 309 | |
ashatune | 0:66c1c628e189 | 310 | strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); |
ashatune | 0:66c1c628e189 | 311 | SendCMD(); |
ashatune | 0:66c1c628e189 | 312 | wait(1); |
ashatune | 0:66c1c628e189 | 313 | |
ashatune | 0:66c1c628e189 | 314 | strcpy(snd, "conn:send(\"<html>\")\r\n"); |
ashatune | 0:66c1c628e189 | 315 | SendCMD(); |
ashatune | 0:66c1c628e189 | 316 | wait(1); |
ashatune | 0:66c1c628e189 | 317 | |
ashatune | 0:66c1c628e189 | 318 | strcpy(snd, "conn:send(\"<h1> 4180 IOT Self Nav Robot </h1>\")\r\n"); |
ashatune | 0:66c1c628e189 | 319 | SendCMD(); |
ashatune | 0:66c1c628e189 | 320 | wait(1); |
ashatune | 0:66c1c628e189 | 321 | |
ashatune | 0:66c1c628e189 | 322 | sprintf(snd, "conn:send(\"%s'\")\r\n", ticksR); |
ashatune | 0:66c1c628e189 | 323 | SendCMD(); |
ashatune | 0:66c1c628e189 | 324 | wait(1); |
ashatune | 0:66c1c628e189 | 325 | |
ashatune | 0:66c1c628e189 | 326 | strcpy(snd, "conn:send(\"<h2> cm.. </h2>\")\r\n"); |
ashatune | 0:66c1c628e189 | 327 | SendCMD(); |
ashatune | 0:66c1c628e189 | 328 | wait(1); |
ashatune | 0:66c1c628e189 | 329 | |
ashatune | 0:66c1c628e189 | 330 | strcpy(snd, "conn:send(\"</html>\")\r\n"); |
ashatune | 0:66c1c628e189 | 331 | SendCMD(); |
ashatune | 0:66c1c628e189 | 332 | wait(1); |
ashatune | 0:66c1c628e189 | 333 | |
ashatune | 0:66c1c628e189 | 334 | strcpy(snd, "end)\r\n"); |
ashatune | 0:66c1c628e189 | 335 | SendCMD(); |
ashatune | 0:66c1c628e189 | 336 | wait(1); |
ashatune | 0:66c1c628e189 | 337 | |
ashatune | 0:66c1c628e189 | 338 | strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); |
ashatune | 0:66c1c628e189 | 339 | SendCMD(); |
ashatune | 0:66c1c628e189 | 340 | wait(1); |
ashatune | 0:66c1c628e189 | 341 | strcpy(snd, "end)\r\n"); |
ashatune | 0:66c1c628e189 | 342 | SendCMD(); |
ashatune | 0:66c1c628e189 | 343 | wait(1); |
ashatune | 0:66c1c628e189 | 344 | timeout=17; |
ashatune | 0:66c1c628e189 | 345 | getreply(); |
ashatune | 0:66c1c628e189 | 346 | pc.printf(buf); |
ashatune | 0:66c1c628e189 | 347 | pc.printf("\r\nDONE"); |
ashatune | 0:66c1c628e189 | 348 | } |
ashatune | 0:66c1c628e189 | 349 | |
ashatune | 0:66c1c628e189 | 350 | void SendCMD() |
ashatune | 0:66c1c628e189 | 351 | { |
ashatune | 0:66c1c628e189 | 352 | esp.printf("%s", snd); |
ashatune | 0:66c1c628e189 | 353 | } |
ashatune | 0:66c1c628e189 | 354 | |
ashatune | 0:66c1c628e189 | 355 | void getreply() |
ashatune | 0:66c1c628e189 | 356 | { |
ashatune | 0:66c1c628e189 | 357 | memset(buf, '\0', sizeof(buf)); |
ashatune | 0:66c1c628e189 | 358 | t.start(); |
ashatune | 0:66c1c628e189 | 359 | ended=0; |
ashatune | 0:66c1c628e189 | 360 | count=0; |
ashatune | 0:66c1c628e189 | 361 | while(!ended) { |
ashatune | 0:66c1c628e189 | 362 | if(esp.readable()) { |
ashatune | 0:66c1c628e189 | 363 | buf[count] = esp.getc(); |
ashatune | 0:66c1c628e189 | 364 | count++; |
ashatune | 0:66c1c628e189 | 365 | } |
ashatune | 0:66c1c628e189 | 366 | if(t.read() > timeout) { |
ashatune | 0:66c1c628e189 | 367 | ended = 1; |
ashatune | 0:66c1c628e189 | 368 | t.stop(); |
ashatune | 0:66c1c628e189 | 369 | t.reset(); |
ashatune | 0:66c1c628e189 | 370 | } |
ashatune | 0:66c1c628e189 | 371 | } |
ashatune | 0:66c1c628e189 | 372 | } |
ashatune | 0:66c1c628e189 | 373 | |
ashatune | 0:66c1c628e189 | 374 |