forked from anakod/ESP8266pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266pro.cpp
138 lines (118 loc) · 3.16 KB
/
ESP8266pro.cpp
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
// ESP8266pro
// This software distributed under the terms of the MIT license
// (c) Skurydin Alexey, 2014
#include "ESP8266pro.h"
ESP8266pro::ESP8266pro(Stream &espStreamReference)
: ESP8266pro_Parser(espStreamReference)
{
}
ESP8266pro::ESP8266pro(Stream &espStreamReference, Stream &debugStreamReference)
: ESP8266pro_Parser(espStreamReference, debugStreamReference)
{
}
bool ESP8266pro::begin(OutputDebugMode debugOutMode/* = eODM_Data*/)
{
#ifndef NODEBUG
debugPrint("<ESP8266pro>");
#endif
if (!initializeParser(debugOutMode))
restart(); // Trying to restart module
if (!initializeParser(debugOutMode))
{
#ifndef NODEBUG
debugPrint("<ERROR>");
#endif
return false;
}
// 1 = Station mode
execute("AT+CWMODE=1");
// 1 = multiple connection
if (!execute("AT+CIPMUX=1"))
{
restart(); // Trying to restart module
execute("AT+CIPMUX=1");
}
}
bool ESP8266pro::stationConnect(const String& ssid, const String& password)
{
return execute("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", eCEM_LongTimeOut);
}
bool ESP8266pro::stationDisconnect()
{
return execute("AT+CWQAP", eCEM_LongTimeOut);
}
String ESP8266pro::stationIP()
{
if (!execute("AT+CIPSTA?")) return NULL_IP;
return getLine(0);
}
String ESP8266pro::stationMAC()
{
if (!execute("AT+CIPSTAMAC?")) return "";
return getLine(0);
}
bool ESP8266pro::accessPointStart(const String& ssid, const String& password, EncriptionMode encription/* = eEM_WPA2_PSK*/, uint8_t wifiChannel/* = 5*/)
{
if (!execute("AT+CWMODE=3")) return false; // 3 = AP + Station mode
return execute("AT+CWSAP=\"" + ssid + "\",\"" + password + "\"," + wifiChannel + "," + encription, eCEM_LongTimeOut);
}
bool ESP8266pro::accessPointStop()
{
execute("AT+CWMODE=1"); // 1 = Station mode
}
String ESP8266pro::accessPointIP()
{
if (!execute("AT+CIPAP?")) return NULL_IP;
return getLine(0);
}
String ESP8266pro::accessPointMAC()
{
if (!execute("AT+CIPAPMAC?")) return NULL_IP;
return getLine(0);
}
void ESP8266pro::onDataReceive(uint8_t connectionId, char* buffer, int length, DataReceiveAction action)
{
if (connectionId >= 0 && connectionId < ESP_MAX_CONNECTIONS)
{
if (connections[connectionId] != NULL)
connections[connectionId]->onDataReceive(connectionId, buffer, length, action);
else
server->onDataReceive(connectionId, buffer, length, action);
}
}
uint8_t ESP8266pro::addConnection(IESP8266proBaseReceiver* target)
{
for (int8_t i = ESP_MAX_CONNECTIONS - 1; i >= 0; i--)
{
if (connections[i] == NULL)
{
connections[i] = target;
return i;
}
}
return ESP_INVALID_CONNECTION;
}
uint8_t ESP8266pro::getConnectionId(IESP8266proBaseReceiver* target)
{
for (int8_t i = ESP_MAX_CONNECTIONS - 1; i >= 0; i--)
if (connections[i] == target)
return i;
return ESP_INVALID_CONNECTION;
}
void ESP8266pro::removeConnection(IESP8266proBaseReceiver* target)
{
for (int8_t i = ESP_MAX_CONNECTIONS - 1; i >= 0; i--)
if (connections[i] == target)
connections[i] = NULL;
}
bool ESP8266pro::setServer(IESP8266proBaseReceiver* serverInstance)
{
// Allowed only if server not started OR for reset server
if (serverInstance == NULL || server == NULL)
{
server = serverInstance;
return true;
}
else
return false;
}