Firmware SDK
twr_esp8266.h
1 #ifndef _TWR_ESP8266_H
2 #define _TWR_ESP8266_H
3 
4 #include <twr_scheduler.h>
5 #include <twr_gpio.h>
6 #include <twr_uart.h>
7 
11 
13 
14 #define TWR_ESP8266_TX_MAX_PACKET_SIZE 1024
15 
16 #define TWR_ESP8266_TX_FIFO_BUFFER_SIZE TWR_ESP8266_TX_MAX_PACKET_SIZE
17 #define TWR_ESP8266_RX_FIFO_BUFFER_SIZE 2048
18 
20 
22 
23 typedef enum
24 {
27 
30 
31  TWR_ESP8266_EVENT_WIFI_CONNECT_SUCCESS = 2,
32  TWR_ESP8266_EVENT_WIFI_CONNECT_ERROR = 3,
33  TWR_ESP8266_EVENT_SOCKET_CONNECT_SUCCESS = 4,
34  TWR_ESP8266_EVENT_SOCKET_CONNECT_ERROR = 5,
35  TWR_ESP8266_EVENT_SOCKET_SEND_SUCCESS = 6,
36  TWR_ESP8266_EVENT_SOCKET_SEND_ERROR = 7,
37  TWR_ESP8266_EVENT_DATA_RECEIVED = 8,
38  TWR_ESP8266_EVENT_AP_AVAILABILITY_RESULT = 9,
39  TWR_ESP8266_EVENT_DISCONNECTED = 10
40 
42 
44 
45 typedef struct twr_esp8266_t twr_esp8266_t;
46 
47 typedef enum
48 {
49  TWR_ESP8266_STATE_READY = 0,
50  TWR_ESP8266_STATE_ERROR = 1,
51  TWR_ESP8266_STATE_INITIALIZE = 2,
52  TWR_ESP8266_STATE_IDLE = 3,
53  TWR_ESP8266_STATE_RECEIVE = 4,
54  TWR_ESP8266_STATE_INITIALIZE_COMMAND_SEND = 5,
55  TWR_ESP8266_STATE_INITIALIZE_COMMAND_RESPONSE = 6,
56  TWR_ESP8266_STATE_WIFI_CONNECT_COMMAND = 7,
57  TWR_ESP8266_STATE_SOCKET_CONNECT_COMMAND = 8,
58  TWR_ESP8266_STATE_SOCKET_SEND_COMMAND = 9,
59  TWR_ESP8266_STATE_SOCKET_SEND_DATA = 10,
60  TWR_ESP8266_STATE_WIFI_CONNECT_RESPONSE = 11,
61  TWR_ESP8266_STATE_SOCKET_CONNECT_RESPONSE = 12,
62  TWR_ESP8266_STATE_SOCKET_SEND_RESPONSE = 13,
63  TWR_ESP8266_STATE_WIFI_CONNECT_ERROR = 14,
64  TWR_ESP8266_STATE_SOCKET_CONNECT_ERROR = 15,
65  TWR_ESP8266_STATE_SOCKET_SEND_ERROR = 16,
66  TWR_ESP8266_STATE_SOCKET_RECEIVE = 17,
67  TWR_ESP8266_STATE_DISCONNECTED = 18,
68  TWR_ESP8266_STATE_SNTP_CONFIG_COMMAND = 19,
69  TWR_ESP8266_STATE_SNTP_CONFIG_RESPONSE = 20,
70  TWR_ESP8266_STATE_SNTP_TIME_COMMAND = 21,
71  TWR_ESP8266_STATE_SNTP_TIME_RESPONSE = 22,
72  TWR_ESP8266_STATE_AP_AVAILABILITY_OPT_COMMAND = 23,
73  TWR_ESP8266_STATE_AP_AVAILABILITY_OPT_RESPONSE = 24,
74  TWR_ESP8266_STATE_AP_AVAILABILITY_COMMAND = 25,
75  TWR_ESP8266_STATE_AP_AVAILABILITY_RESPONSE = 26
76 
77 } twr_esp8266_state_t;
78 
79 typedef enum
80 {
81  TWR_ESP8266_CONFIG_MODE_STATION = 0,
82  TWR_ESP8266_CONFIG_MODE_AP = 1
83 } twr_esp8266_config_mode_t;
84 
85 typedef struct
86 {
87  twr_esp8266_config_mode_t mode;
88  char ssid[64];
89  char password[64];
90  uint8_t sntp_enabled;
91  int sntp_timezone;
92  char sntp_server1[128];
93  char sntp_server2[128];
94  char sntp_server3[128];
96 
98 {
99  twr_scheduler_task_id_t _task_id;
100  twr_uart_channel_t _uart_channel;
101  twr_esp8266_state_t _state;
102  twr_esp8266_state_t _state_after_init;
103  twr_fifo_t _tx_fifo;
104  twr_fifo_t _rx_fifo;
105  uint8_t _tx_fifo_buffer[TWR_ESP8266_TX_FIFO_BUFFER_SIZE];
106  uint8_t _rx_fifo_buffer[TWR_ESP8266_RX_FIFO_BUFFER_SIZE];
107  void (*_event_handler)(twr_esp8266_t *, twr_esp8266_event_t, void *);
108  void *_event_param;
109  char _command[TWR_ESP8266_TX_FIFO_BUFFER_SIZE];
110  char _response[TWR_ESP8266_RX_FIFO_BUFFER_SIZE];
111  uint8_t _message_buffer[TWR_ESP8266_TX_MAX_PACKET_SIZE];
112  size_t _message_length;
113  size_t _message_part_length;
114  uint8_t _init_command_index;
115  uint8_t _timeout_cnt;
116  twr_esp8266_config _config;
117  bool _ap_available;
118  int _rssi;
119 };
120 
122 
126 
127 void twr_esp8266_init(twr_esp8266_t *self, twr_uart_channel_t uart_channel);
128 
133 
134 void twr_esp8266_set_event_handler(twr_esp8266_t *self, void (*event_handler)(twr_esp8266_t *, twr_esp8266_event_t, void *), void *event_param);
135 
140 
141 void twr_esp8266_set_station_mode(twr_esp8266_t *self, char *ssid, char *password);
142 
146 
147 void twr_esp8266_set_sntp(twr_esp8266_t *self, int timezone);
148 
155 
156 void twr_esp8266_set_sntp_with_servers(twr_esp8266_t *self, int timezone, char *sntp_server1, char *sntp_server2, char *sntp_server3);
157 
162 
164 
169 
171 
176 
178 
185 
186 bool twr_esp8266_tcp_connect(twr_esp8266_t *self, const char *host, uint16_t port);
187 
194 
195 bool twr_esp8266_udp_connect(twr_esp8266_t *self, const char *host, uint16_t port);
196 
203 
204 bool twr_esp8266_ssl_connect(twr_esp8266_t *self, const char *host, uint16_t port);
205 
212 
213 bool twr_esp8266_send_data(twr_esp8266_t *self, const void *buffer, size_t length);
214 
218 
220 
226 
227 uint32_t twr_esp8266_get_received_message_data(twr_esp8266_t *self, uint8_t *buffer, uint32_t buffer_size);
228 
233 
235 
240 
241 void twr_esp8266_get_ap_availability(twr_esp8266_t *self, bool *available, int *rssi);
242 
246 
247 void twr_esp8266_get_ssid(twr_esp8266_t *self, char *ssid);
248 
252 
253 void twr_esp8266_set_ssid(twr_esp8266_t *self, char *ssid);
254 
258 
259 void twr_esp8266_get_password(twr_esp8266_t *self, char *password);
260 
264 
265 void twr_esp8266_set_password(twr_esp8266_t *self, char *password);
266 
268 
269 #endif // _TWR_ESP8266_H
bool twr_esp8266_connect(twr_esp8266_t *self)
Enable ESP8266 and connect to WiFi.
Definition: twr_esp8266.c:156
void twr_esp8266_get_ap_availability(twr_esp8266_t *self, bool *available, int *rssi)
Get AP availability result.
Definition: twr_esp8266.c:996
uint32_t twr_esp8266_get_received_message_data(twr_esp8266_t *self, uint8_t *buffer, uint32_t buffer_size)
Get received message data.
Definition: twr_esp8266.c:795
uint32_t twr_esp8266_get_received_message_length(twr_esp8266_t *self)
Get length of the received message.
Definition: twr_esp8266.c:790
bool twr_esp8266_ssl_connect(twr_esp8266_t *self, const char *host, uint16_t port)
Establish SSL Connection.
Definition: twr_esp8266.c:220
void twr_esp8266_get_ssid(twr_esp8266_t *self, char *ssid)
Get SSID.
Definition: twr_esp8266.c:1002
bool twr_esp8266_udp_connect(twr_esp8266_t *self, const char *host, uint16_t port)
Establish UDP Connection.
Definition: twr_esp8266.c:215
void twr_esp8266_set_station_mode(twr_esp8266_t *self, char *ssid, char *password)
Set station mode.
Definition: twr_esp8266.c:104
bool twr_esp8266_send_data(twr_esp8266_t *self, const void *buffer, size_t length)
Send data.
Definition: twr_esp8266.c:225
bool twr_esp8266_is_ready(twr_esp8266_t *self)
Check if modem is ready for commands.
Definition: twr_esp8266.c:146
bool twr_esp8266_disconnect(twr_esp8266_t *self)
Disable ESP8266 and disconnect.
Definition: twr_esp8266.c:174
bool twr_esp8266_tcp_connect(twr_esp8266_t *self, const char *host, uint16_t port)
Establish TCP Connection.
Definition: twr_esp8266.c:210
void twr_esp8266_init(twr_esp8266_t *self, twr_uart_channel_t uart_channel)
Initialize ESP8266.
Definition: twr_esp8266.c:37
void twr_esp8266_set_password(twr_esp8266_t *self, char *password)
Set password.
Definition: twr_esp8266.c:1017
void twr_esp8266_set_event_handler(twr_esp8266_t *self, void(*event_handler)(twr_esp8266_t *, twr_esp8266_event_t, void *), void *event_param)
Set callback function.
Definition: twr_esp8266.c:98
bool twr_esp8266_check_ap_availability(twr_esp8266_t *self)
Check AP availability.
Definition: twr_esp8266.c:979
void twr_esp8266_set_sntp(twr_esp8266_t *self, int timezone)
Enable SNTP and set time zone.
Definition: twr_esp8266.c:111
void twr_esp8266_get_password(twr_esp8266_t *self, char *password)
Get password.
Definition: twr_esp8266.c:1012
twr_esp8266_event_t
Callback events.
Definition: twr_esp8266.h:24
void twr_esp8266_set_ssid(twr_esp8266_t *self, char *ssid)
Set SSID.
Definition: twr_esp8266.c:1007
void twr_esp8266_set_sntp_with_servers(twr_esp8266_t *self, int timezone, char *sntp_server1, char *sntp_server2, char *sntp_server3)
Enable SNTP and set configuration.
Definition: twr_esp8266.c:116
@ TWR_ESP8266_EVENT_READY
Ready event.
Definition: twr_esp8266.h:26
@ TWR_ESP8266_EVENT_ERROR
Error event.
Definition: twr_esp8266.h:29
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
twr_uart_channel_t
UART channels.
Definition: twr_uart.h:14
Structure of FIFO instance.
Definition: twr_fifo.h:13