-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathSparkFunESP8266Client.cpp
More file actions
167 lines (137 loc) · 3.29 KB
/
SparkFunESP8266Client.cpp
File metadata and controls
167 lines (137 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/******************************************************************************
SparkFunESP8266Client.cpp
ESP8266 WiFi Shield Library Client Source File
Jim Lindblom @ SparkFun Electronics
Original Creation Date: June 20, 2015
http://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library
!!! Description Here !!!
Development environment specifics:
IDE: Arduino 1.6.5
Hardware Platform: Arduino Uno
ESP8266 WiFi Shield Version: 1.0
This code is beerware; if you see me (or any other SparkFun employee) at the
local, and you've found our code helpful, please buy us a round!
Distributed as-is; no warranty is given.
******************************************************************************/
#include "SparkFunESP8266WiFi.h"
#include <Arduino.h>
#include "util/ESP8266_AT.h"
#include "SparkFunESP8266Client.h"
ESP8266Client::ESP8266Client()
{
}
ESP8266Client::ESP8266Client(uint8_t sock)
{
_socket = sock;
}
uint8_t ESP8266Client::status()
{
return esp8266.status();
}
int ESP8266Client::connect(IPAddress ip, uint16_t port)
{
return connect(ip, port, 0);
}
int ESP8266Client::connect(const char *host, uint16_t port)
{
return connect(host, port, 0);
}
int ESP8266Client::connect(String host, uint16_t port, uint32_t keepAlive)
{
return connect(host.c_str(), port, keepAlive);
}
int ESP8266Client::connect(IPAddress ip, uint16_t port, uint32_t keepAlive)
{
char ipAddress[16];
sprintf(ipAddress, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return connect((const char *)ipAddress, port, keepAlive);
}
int ESP8266Client::connect(const char* host, uint16_t port, uint32_t keepAlive)
{
_socket = getFirstSocket();
if (_socket != ESP8266_SOCK_NOT_AVAIL)
{
esp8266._state[_socket] = TAKEN;
int16_t rsp = esp8266.tcpConnect(_socket, host, port, keepAlive);
return rsp;
}
}
size_t ESP8266Client::write(uint8_t c)
{
return write(&c, 1);
}
size_t ESP8266Client::write(const uint8_t *buf, size_t size)
{
return esp8266.tcpSend(_socket, buf, size);
}
int ESP8266Client::available()
{
return receiveBuffer.available();
}
int ESP8266Client::read()
{
return receiveBuffer.read();
}
int ESP8266Client::read(uint8_t *buf, size_t size)
{
if (available() + esp8266.available() < size)
return 0;
for (int i=0; i<size; i++)
{
buf[i] = this->read();
}
return 1;
}
int ESP8266Client::peek()
{
return esp8266.peek();
}
void ESP8266Client::flush()
{
esp8266.flush();
}
void ESP8266Client::stop()
{
esp8266.close(_socket);
esp8266._state[_socket] = AVAILABLE;
}
uint8_t ESP8266Client::connected()
{
// If data is available, assume we're connected. Otherwise,
// we'll try to send the status query, and will probably end
// up timing out if data is still coming in.
if (_socket == ESP8266_SOCK_NOT_AVAIL)
return 0;
else if (available() > 0)
return 1;
else if (esp8266._status.stat == ESP8266_STATUS_CONNECTED)
return 1;
return 0;
}
ESP8266Client::operator bool()
{
return connected();
}
// Private Methods
uint8_t ESP8266Client::getFirstSocket()
{
/*
for (int i = 0; i < ESP8266_MAX_SOCK_NUM; i++)
{
if (esp8266._state[i] == AVAILABLE)
{
return i;
}
}
return ESP8266_SOCK_NOT_AVAIL;
*/
esp8266.updateStatus();
for (int i = 0; i < ESP8266_MAX_SOCK_NUM; i++)
{
if (esp8266._status.ipstatus[i].linkID == 255)
{
return i;
}
}
return ESP8266_SOCK_NOT_AVAIL;
}