Wifi controlled robot that uses ESP8266 wifi chip.
Dependencies: Motordriver Servo mbed-dev mbed-rtos
Fork of ESP8266_wifi_robot by
ESP8266.cpp@0:df754b773321, 2016-10-29 (annotated)
- Committer:
- K2Silver
- Date:
- Sat Oct 29 15:35:26 2016 +0000
- Revision:
- 0:df754b773321
Initial commit of wifi controlled robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
K2Silver | 0:df754b773321 | 1 | #include "ESP8266.h" |
K2Silver | 0:df754b773321 | 2 | |
K2Silver | 0:df754b773321 | 3 | |
K2Silver | 0:df754b773321 | 4 | ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, char * SSID, char * password) : |
K2Silver | 0:df754b773321 | 5 | _esp(tx, rx), _reset(reset) { |
K2Silver | 0:df754b773321 | 6 | strcpy(_ssid, SSID); |
K2Silver | 0:df754b773321 | 7 | strcpy(_pwd, password); |
K2Silver | 0:df754b773321 | 8 | } |
K2Silver | 0:df754b773321 | 9 | |
K2Silver | 0:df754b773321 | 10 | // Setup for ESP8266 |
K2Silver | 0:df754b773321 | 11 | void ESP8266::setup() { |
K2Silver | 0:df754b773321 | 12 | |
K2Silver | 0:df754b773321 | 13 | /* Set wifi mode */ |
K2Silver | 0:df754b773321 | 14 | send("wifi.setmode(wifi.STATION)\r\n"); |
K2Silver | 0:df754b773321 | 15 | |
K2Silver | 0:df754b773321 | 16 | /* Set SSID and password */ |
K2Silver | 0:df754b773321 | 17 | strcpy(_snd, "wifi.sta.config(\""); |
K2Silver | 0:df754b773321 | 18 | strcat(_snd, _ssid); |
K2Silver | 0:df754b773321 | 19 | strcat(_snd, "\",\""); |
K2Silver | 0:df754b773321 | 20 | strcat(_snd, _pwd); |
K2Silver | 0:df754b773321 | 21 | strcat(_snd, "\")\r\n"); |
K2Silver | 0:df754b773321 | 22 | send(); |
K2Silver | 0:df754b773321 | 23 | |
K2Silver | 0:df754b773321 | 24 | wait(7); |
K2Silver | 0:df754b773321 | 25 | |
K2Silver | 0:df754b773321 | 26 | /* Set up HTTP server */ |
K2Silver | 0:df754b773321 | 27 | send("srv=net.createServer(net.TCP)\r\n"); |
K2Silver | 0:df754b773321 | 28 | send("srv:listen(80,function(conn)\r\n"); |
K2Silver | 0:df754b773321 | 29 | send("conn:on(\"receive\",function(conn,payload)\r\n"); |
K2Silver | 0:df754b773321 | 30 | send("print(payload:match(\"Z%u\"))\r\n"); |
K2Silver | 0:df754b773321 | 31 | send("conn:send(\"<!DOCTYPE html>\")\r\n"); |
K2Silver | 0:df754b773321 | 32 | send("conn:send(\"<html>\")\r\n"); |
K2Silver | 0:df754b773321 | 33 | send("conn:send(\"<h1>Control Robot</h1>\")\r\n"); |
K2Silver | 0:df754b773321 | 34 | send("conn:send('<form action=\"\" method=\"POST\">\r\n')"); |
K2Silver | 0:df754b773321 | 35 | send("conn:send('<input type=\"submit\" name=\"ZF\" value=\"Forward\">\r\n')"); |
K2Silver | 0:df754b773321 | 36 | send("conn:send('<input type=\"submit\" name=\"ZB\" value=\"Back\">\r\n')"); |
K2Silver | 0:df754b773321 | 37 | send("conn:send('<input type=\"submit\" name=\"ZL\" value=\"Left\">\r\n')"); |
K2Silver | 0:df754b773321 | 38 | send("conn:send('<input type=\"submit\" name=\"ZR\" value=\"Right\">\r\n')"); |
K2Silver | 0:df754b773321 | 39 | send("conn:send('<input type=\"submit\" name=\"ZS\" value=\"Servo Left\">\r\n')"); |
K2Silver | 0:df754b773321 | 40 | send("conn:send('<input type=\"submit\" name=\"ZP\" value=\"Servo Right\">\r\n')"); |
K2Silver | 0:df754b773321 | 41 | send("conn:send('</form>\r\n')"); |
K2Silver | 0:df754b773321 | 42 | send("conn:send(\"</html>\")\r\n"); |
K2Silver | 0:df754b773321 | 43 | send("end)\r\n"); |
K2Silver | 0:df754b773321 | 44 | send("conn:on(\"sent\",function(conn) conn:close() end)\r\n"); |
K2Silver | 0:df754b773321 | 45 | send("end)\r\n"); |
K2Silver | 0:df754b773321 | 46 | wait(10); |
K2Silver | 0:df754b773321 | 47 | } |
K2Silver | 0:df754b773321 | 48 | |
K2Silver | 0:df754b773321 | 49 | void ESP8266::send() |
K2Silver | 0:df754b773321 | 50 | { |
K2Silver | 0:df754b773321 | 51 | _esp.printf("%s", _snd); |
K2Silver | 0:df754b773321 | 52 | Thread::wait(500); |
K2Silver | 0:df754b773321 | 53 | } |
K2Silver | 0:df754b773321 | 54 | |
K2Silver | 0:df754b773321 | 55 | void ESP8266::send(char * command) { |
K2Silver | 0:df754b773321 | 56 | _esp.printf("%s", command); |
K2Silver | 0:df754b773321 | 57 | Thread::wait(500); |
K2Silver | 0:df754b773321 | 58 | } |
K2Silver | 0:df754b773321 | 59 | |
K2Silver | 0:df754b773321 | 60 | void ESP8266::reset() { |
K2Silver | 0:df754b773321 | 61 | _reset = 0; |
K2Silver | 0:df754b773321 | 62 | wait(1); /* Not RTOS friendly */ |
K2Silver | 0:df754b773321 | 63 | _reset = 1; |
K2Silver | 0:df754b773321 | 64 | } |
K2Silver | 0:df754b773321 | 65 | |
K2Silver | 0:df754b773321 | 66 | void ESP8266::baud(int baudrate) { |
K2Silver | 0:df754b773321 | 67 | char buffer[32]; |
K2Silver | 0:df754b773321 | 68 | sprintf(buffer, "AT+CIOBAUD=%d\r\n", baudrate); |
K2Silver | 0:df754b773321 | 69 | send(buffer); |
K2Silver | 0:df754b773321 | 70 | |
K2Silver | 0:df754b773321 | 71 | _esp.baud(baudrate); |
K2Silver | 0:df754b773321 | 72 | } |
K2Silver | 0:df754b773321 | 73 | |
K2Silver | 0:df754b773321 | 74 | void ESP8266::getIP() { |
K2Silver | 0:df754b773321 | 75 | send("\r\nprint(wifi.sta.getip())\r\n"); |
K2Silver | 0:df754b773321 | 76 | } |
K2Silver | 0:df754b773321 | 77 | |
K2Silver | 0:df754b773321 | 78 | void ESP8266::getMAC() { |
K2Silver | 0:df754b773321 | 79 | send("\r\nprint(wifi.sta.getmac())\r\n"); |
K2Silver | 0:df754b773321 | 80 | } |
K2Silver | 0:df754b773321 | 81 | |
K2Silver | 0:df754b773321 | 82 | int ESP8266::putc(int c) { |
K2Silver | 0:df754b773321 | 83 | return _esp.putc(c); |
K2Silver | 0:df754b773321 | 84 | } |
K2Silver | 0:df754b773321 | 85 | |
K2Silver | 0:df754b773321 | 86 | int ESP8266::getc() { |
K2Silver | 0:df754b773321 | 87 | return _esp.getc(); |
K2Silver | 0:df754b773321 | 88 | } |
K2Silver | 0:df754b773321 | 89 | |
K2Silver | 0:df754b773321 | 90 | int ESP8266::readable() { |
K2Silver | 0:df754b773321 | 91 | return _esp.readable(); |
K2Silver | 0:df754b773321 | 92 | } |
K2Silver | 0:df754b773321 | 93 | int ESP8266::writeable() { |
K2Silver | 0:df754b773321 | 94 | return _esp.writeable(); |
K2Silver | 0:df754b773321 | 95 | } |
K2Silver | 0:df754b773321 | 96 | |
K2Silver | 0:df754b773321 | 97 | void ESP8266::attach(Callback< void()> func, SerialBase::IrqType type) { |
K2Silver | 0:df754b773321 | 98 | _esp.attach(func, type); |
K2Silver | 0:df754b773321 | 99 | } |