MiCO MXCHIP
/
MXCHIP_WIFI_Demo
It's MXCHIP WIFI demo.
MXCHIP_WIFI_Driver/MXCHIP/MXCHIP.cpp@0:cd7c54f07d25, 2017-03-21 (annotated)
- Committer:
- lizhibo32
- Date:
- Tue Mar 21 03:56:12 2017 +0000
- Revision:
- 0:cd7c54f07d25
build system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lizhibo32 | 0:cd7c54f07d25 | 1 | |
lizhibo32 | 0:cd7c54f07d25 | 2 | #include "MXCHIP.h" |
lizhibo32 | 0:cd7c54f07d25 | 3 | |
lizhibo32 | 0:cd7c54f07d25 | 4 | |
lizhibo32 | 0:cd7c54f07d25 | 5 | MXCHIP::MXCHIP(PinName tx, PinName rx, bool debug): _serial(tx, rx, 1024), _parser(_serial), _packets(0), _packets_end(&_packets) |
lizhibo32 | 0:cd7c54f07d25 | 6 | { |
lizhibo32 | 0:cd7c54f07d25 | 7 | _serial.baud(115200); |
lizhibo32 | 0:cd7c54f07d25 | 8 | _parser.debugOn(debug); |
lizhibo32 | 0:cd7c54f07d25 | 9 | } |
lizhibo32 | 0:cd7c54f07d25 | 10 | |
lizhibo32 | 0:cd7c54f07d25 | 11 | bool MXCHIP::startup(int mode) |
lizhibo32 | 0:cd7c54f07d25 | 12 | { |
lizhibo32 | 0:cd7c54f07d25 | 13 | //only 3 valid modes |
lizhibo32 | 0:cd7c54f07d25 | 14 | if(mode < 1 || mode > 3) |
lizhibo32 | 0:cd7c54f07d25 | 15 | { |
lizhibo32 | 0:cd7c54f07d25 | 16 | return false; |
lizhibo32 | 0:cd7c54f07d25 | 17 | } |
lizhibo32 | 0:cd7c54f07d25 | 18 | |
lizhibo32 | 0:cd7c54f07d25 | 19 | bool success = reset() |
lizhibo32 | 0:cd7c54f07d25 | 20 | && _parser.send("AT+CWMODE=%d", mode) |
lizhibo32 | 0:cd7c54f07d25 | 21 | && _parser.recv("OK") |
lizhibo32 | 0:cd7c54f07d25 | 22 | && _parser.send("AT+CIPMUX=1") |
lizhibo32 | 0:cd7c54f07d25 | 23 | && _parser.recv("OK"); |
lizhibo32 | 0:cd7c54f07d25 | 24 | |
lizhibo32 | 0:cd7c54f07d25 | 25 | _parser.oob("+IPD", this, &MXCHIP::_packet_handler); |
lizhibo32 | 0:cd7c54f07d25 | 26 | |
lizhibo32 | 0:cd7c54f07d25 | 27 | return success; |
lizhibo32 | 0:cd7c54f07d25 | 28 | } |
lizhibo32 | 0:cd7c54f07d25 | 29 | |
lizhibo32 | 0:cd7c54f07d25 | 30 | bool MXCHIP::reset(void) |
lizhibo32 | 0:cd7c54f07d25 | 31 | { |
lizhibo32 | 0:cd7c54f07d25 | 32 | for (int i = 0; i < 2; i++) |
lizhibo32 | 0:cd7c54f07d25 | 33 | { |
lizhibo32 | 0:cd7c54f07d25 | 34 | if (_parser.send("AT+RST") |
lizhibo32 | 0:cd7c54f07d25 | 35 | && _parser.recv("OK\r\nready")) |
lizhibo32 | 0:cd7c54f07d25 | 36 | { |
lizhibo32 | 0:cd7c54f07d25 | 37 | return true; |
lizhibo32 | 0:cd7c54f07d25 | 38 | } |
lizhibo32 | 0:cd7c54f07d25 | 39 | } |
lizhibo32 | 0:cd7c54f07d25 | 40 | |
lizhibo32 | 0:cd7c54f07d25 | 41 | return false; |
lizhibo32 | 0:cd7c54f07d25 | 42 | } |
lizhibo32 | 0:cd7c54f07d25 | 43 | |
lizhibo32 | 0:cd7c54f07d25 | 44 | bool MXCHIP::dhcp(bool enabled, int mode) |
lizhibo32 | 0:cd7c54f07d25 | 45 | { |
lizhibo32 | 0:cd7c54f07d25 | 46 | //only 3 valid modes |
lizhibo32 | 0:cd7c54f07d25 | 47 | if(mode < 0 || mode > 2) |
lizhibo32 | 0:cd7c54f07d25 | 48 | { |
lizhibo32 | 0:cd7c54f07d25 | 49 | return false; |
lizhibo32 | 0:cd7c54f07d25 | 50 | } |
lizhibo32 | 0:cd7c54f07d25 | 51 | |
lizhibo32 | 0:cd7c54f07d25 | 52 | return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode) |
lizhibo32 | 0:cd7c54f07d25 | 53 | && _parser.recv("OK"); |
lizhibo32 | 0:cd7c54f07d25 | 54 | } |
lizhibo32 | 0:cd7c54f07d25 | 55 | |
lizhibo32 | 0:cd7c54f07d25 | 56 | bool MXCHIP::connect(const char *ap, const char *passPhrase) |
lizhibo32 | 0:cd7c54f07d25 | 57 | { |
lizhibo32 | 0:cd7c54f07d25 | 58 | return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase) |
lizhibo32 | 0:cd7c54f07d25 | 59 | && _parser.recv("OK"); |
lizhibo32 | 0:cd7c54f07d25 | 60 | } |
lizhibo32 | 0:cd7c54f07d25 | 61 | |
lizhibo32 | 0:cd7c54f07d25 | 62 | bool MXCHIP::disconnect(void) |
lizhibo32 | 0:cd7c54f07d25 | 63 | { |
lizhibo32 | 0:cd7c54f07d25 | 64 | return _parser.send("AT+CWQAP") && _parser.recv("OK"); |
lizhibo32 | 0:cd7c54f07d25 | 65 | } |
lizhibo32 | 0:cd7c54f07d25 | 66 | |
lizhibo32 | 0:cd7c54f07d25 | 67 | const char *MXCHIP::getIPAddress(void) |
lizhibo32 | 0:cd7c54f07d25 | 68 | { |
lizhibo32 | 0:cd7c54f07d25 | 69 | if (!(_parser.send("AT+CIFSR") |
lizhibo32 | 0:cd7c54f07d25 | 70 | && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer) |
lizhibo32 | 0:cd7c54f07d25 | 71 | && _parser.recv("OK"))) { |
lizhibo32 | 0:cd7c54f07d25 | 72 | return 0; |
lizhibo32 | 0:cd7c54f07d25 | 73 | } |
lizhibo32 | 0:cd7c54f07d25 | 74 | |
lizhibo32 | 0:cd7c54f07d25 | 75 | return _ip_buffer; |
lizhibo32 | 0:cd7c54f07d25 | 76 | } |
lizhibo32 | 0:cd7c54f07d25 | 77 | |
lizhibo32 | 0:cd7c54f07d25 | 78 | const char *MXCHIP::getMACAddress(void) |
lizhibo32 | 0:cd7c54f07d25 | 79 | { |
lizhibo32 | 0:cd7c54f07d25 | 80 | if (!(_parser.send("AT+CIFSR") |
lizhibo32 | 0:cd7c54f07d25 | 81 | && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer) |
lizhibo32 | 0:cd7c54f07d25 | 82 | && _parser.recv("OK"))) { |
lizhibo32 | 0:cd7c54f07d25 | 83 | return 0; |
lizhibo32 | 0:cd7c54f07d25 | 84 | } |
lizhibo32 | 0:cd7c54f07d25 | 85 | |
lizhibo32 | 0:cd7c54f07d25 | 86 | return _mac_buffer; |
lizhibo32 | 0:cd7c54f07d25 | 87 | } |
lizhibo32 | 0:cd7c54f07d25 | 88 | |
lizhibo32 | 0:cd7c54f07d25 | 89 | const char *MXCHIP::getGateway() |
lizhibo32 | 0:cd7c54f07d25 | 90 | { |
lizhibo32 | 0:cd7c54f07d25 | 91 | if (!(_parser.send("AT+CIPSTA?") |
lizhibo32 | 0:cd7c54f07d25 | 92 | && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer) |
lizhibo32 | 0:cd7c54f07d25 | 93 | && _parser.recv("OK"))) { |
lizhibo32 | 0:cd7c54f07d25 | 94 | return 0; |
lizhibo32 | 0:cd7c54f07d25 | 95 | } |
lizhibo32 | 0:cd7c54f07d25 | 96 | |
lizhibo32 | 0:cd7c54f07d25 | 97 | return _gateway_buffer; |
lizhibo32 | 0:cd7c54f07d25 | 98 | } |
lizhibo32 | 0:cd7c54f07d25 | 99 | |
lizhibo32 | 0:cd7c54f07d25 | 100 | const char *MXCHIP::getNetmask() |
lizhibo32 | 0:cd7c54f07d25 | 101 | { |
lizhibo32 | 0:cd7c54f07d25 | 102 | if (!(_parser.send("AT+CIPSTA?") |
lizhibo32 | 0:cd7c54f07d25 | 103 | && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer) |
lizhibo32 | 0:cd7c54f07d25 | 104 | && _parser.recv("OK"))) |
lizhibo32 | 0:cd7c54f07d25 | 105 | { |
lizhibo32 | 0:cd7c54f07d25 | 106 | return 0; |
lizhibo32 | 0:cd7c54f07d25 | 107 | } |
lizhibo32 | 0:cd7c54f07d25 | 108 | |
lizhibo32 | 0:cd7c54f07d25 | 109 | return _netmask_buffer; |
lizhibo32 | 0:cd7c54f07d25 | 110 | } |