Last change
on this file since 175 was 175, checked in by ertl-honda, 8 years ago |
ライブラリを Arduino IDE 1.7.9 にupdate
|
File size:
2.4 KB
|
Rev | Line | |
---|
[175] | 1 | /**
|
---|
| 2 | * \file
|
---|
| 3 | * ESP8266 bridge arduino library
|
---|
| 4 | * \author
|
---|
| 5 | * Tuan PM <tuanpm@live.com>
|
---|
| 6 | */
|
---|
| 7 | #ifndef _ARDUINO_WIFI_H_
|
---|
| 8 | #define _ARDUINO_WIFI_H_
|
---|
| 9 | #include <stdint.h>
|
---|
| 10 | //#include <HardwareSerial.h>
|
---|
| 11 | #include <Arduino.h>
|
---|
| 12 | #include "FP.h"
|
---|
| 13 | #include "crc16.h"
|
---|
| 14 | #include "ringbuf.h"
|
---|
| 15 |
|
---|
| 16 | #define ESP_TIMEOUT 2000
|
---|
| 17 |
|
---|
| 18 | #define SLIP_START 0x7E
|
---|
| 19 | #define SLIP_END 0x7F
|
---|
| 20 | #define SLIP_REPL 0x7D
|
---|
| 21 | #define SLIP_ESC(x) (x ^ 0x20)
|
---|
| 22 |
|
---|
| 23 | typedef enum
|
---|
| 24 | {
|
---|
| 25 | CMD_NULL = 0,
|
---|
| 26 | CMD_RESET,
|
---|
| 27 | CMD_IS_READY,
|
---|
| 28 | CMD_WIFI_CONNECT,
|
---|
| 29 | CMD_MQTT_SETUP,
|
---|
| 30 | CMD_MQTT_CONNECT,
|
---|
| 31 | CMD_MQTT_DISCONNECT,
|
---|
| 32 | CMD_MQTT_PUBLISH,
|
---|
| 33 | CMD_MQTT_SUBSCRIBE,
|
---|
| 34 | CMD_MQTT_LWT,
|
---|
| 35 | CMD_MQTT_EVENTS,
|
---|
| 36 | CMD_REST_SETUP,
|
---|
| 37 | CMD_REST_REQUEST,
|
---|
| 38 | CMD_REST_SETHEADER,
|
---|
| 39 | CMD_REST_EVENTS
|
---|
| 40 | }CMD_NAME;
|
---|
| 41 |
|
---|
| 42 | enum WIFI_STATUS{
|
---|
| 43 | STATION_IDLE = 0,
|
---|
| 44 | STATION_CONNECTING,
|
---|
| 45 | STATION_WRONG_PASSWORD,
|
---|
| 46 | STATION_NO_AP_FOUND,
|
---|
| 47 | STATION_CONNECT_FAIL,
|
---|
| 48 | STATION_GOT_IP
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | typedef struct{
|
---|
| 52 | uint8_t *buf;
|
---|
| 53 | uint16_t bufSize;
|
---|
| 54 | uint16_t dataLen;
|
---|
| 55 | uint8_t isEsc;
|
---|
| 56 | uint8_t isBegin;
|
---|
| 57 | }PROTO;
|
---|
| 58 |
|
---|
| 59 | typedef struct __attribute((__packed__)) {
|
---|
| 60 | uint16_t len;
|
---|
| 61 | uint8_t data;
|
---|
| 62 | } ARGS;
|
---|
| 63 |
|
---|
| 64 | typedef struct __attribute((__packed__)) {
|
---|
| 65 | uint16_t cmd;
|
---|
| 66 | uint32_t callback;
|
---|
| 67 | uint32_t _return;
|
---|
| 68 | uint16_t argc;
|
---|
| 69 | ARGS args;
|
---|
| 70 | }PACKET_CMD;
|
---|
| 71 |
|
---|
| 72 | class ESP;
|
---|
| 73 | class RESPONSE;
|
---|
| 74 | class MQTT;
|
---|
| 75 |
|
---|
| 76 | class RESPONSE {
|
---|
| 77 | private:
|
---|
| 78 | uint16_t arg_num;
|
---|
| 79 | uint8_t *arg_ptr;
|
---|
| 80 | PACKET_CMD* cmd;
|
---|
| 81 | public:
|
---|
| 82 | RESPONSE(void *response);
|
---|
| 83 | uint16_t getArgc();
|
---|
| 84 | int32_t popArgs(uint8_t *data, uint16_t maxLen);
|
---|
| 85 | uint16_t argLen();
|
---|
| 86 | String popString();
|
---|
| 87 | void popString(String* data);
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | class ESP
|
---|
| 92 | {
|
---|
| 93 | public:
|
---|
| 94 | ESP(Stream *serial, Stream* debug, int chip_pd);
|
---|
| 95 | ESP(Stream *serial, int chip_pd);
|
---|
| 96 | Stream *_debug;
|
---|
| 97 |
|
---|
| 98 | FP<void, void*> wifiCb;
|
---|
| 99 | uint32_t return_value;
|
---|
| 100 | uint16_t return_cmd;
|
---|
| 101 | boolean is_return;
|
---|
| 102 |
|
---|
| 103 | void wifiConnect(const char* ssid, const char* password);
|
---|
| 104 | void process();
|
---|
| 105 | uint16_t request(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc);
|
---|
| 106 | uint16_t request(uint16_t crc_in, uint8_t* data, uint16_t len);
|
---|
| 107 | uint16_t request(uint16_t crc);
|
---|
| 108 | void reset();
|
---|
| 109 | boolean ready();
|
---|
| 110 | void enable();
|
---|
| 111 | void disable();
|
---|
| 112 | boolean waitReturn(uint32_t timeout);
|
---|
| 113 | boolean waitReturn();
|
---|
| 114 |
|
---|
| 115 | private:
|
---|
| 116 | Stream *_serial;
|
---|
| 117 |
|
---|
| 118 | boolean _debugEn;
|
---|
| 119 | PROTO _proto;
|
---|
| 120 | uint8_t _protoBuf[512];
|
---|
| 121 | int _chip_pd;
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | void init();
|
---|
| 125 | void INFO(String info);
|
---|
| 126 | void protoCompletedCb(void);
|
---|
| 127 | void write(uint8_t data);
|
---|
| 128 | void write(uint8_t* data, uint16_t len);
|
---|
| 129 | };
|
---|
| 130 | #endif
|
---|
Note:
See
TracBrowser
for help on using the repository browser.