forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlynkSimpleShieldEsp8266.h
162 lines (141 loc) · 3.92 KB
/
BlynkSimpleShieldEsp8266.h
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
/**
* @file BlynkSimpleShieldEsp8266.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Jun 2015
* @brief
*
*/
#ifndef BlynkSimpleShieldEsp8266_h
#define BlynkSimpleShieldEsp8266_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "ESP8266"
#endif
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
#include <ESP8266.h>
class BlynkTransportShieldEsp8266
{
static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
}
void onData(uint8_t mux_id, uint32_t len) {
while (len) {
if (client->getUart()->available()) {
uint8_t b = client->getUart()->read();
if(!buffer.push(b)) {
BLYNK_LOG("Buffer overflow");
}
len--;
}
}
}
public:
BlynkTransportShieldEsp8266()
: client(NULL)
, status(false)
, domain(NULL)
, port(0)
{}
void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) {
client = esp8266;
client->setOnData(onData, this);
domain = d;
port = p;
}
bool connect() {
if (!domain || !port)
return false;
status = client->createTCP(domain, port);
return status;
}
void disconnect() {
status = false;
buffer.clear();
client->releaseTCP();
}
size_t read(void* buf, size_t len) {
uint32_t start = millis();
while ((buffer.getOccupied() < len) && (millis() - start < 1500)) {
client->run();
}
return buffer.read((uint8_t*)buf, len);
}
size_t write(const void* buf, size_t len) {
if (client->send((const uint8_t*)buf, len)) {
return len;
}
return 0;
}
bool connected() { return status; }
int available() {
client->run();
return buffer.getOccupied();
}
private:
ESP8266* client;
bool status;
BlynkFifo<uint8_t,128> buffer;
const char* domain;
uint16_t port;
};
class BlynkWifi
: public BlynkProtocol<BlynkTransportShieldEsp8266>
{
typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
public:
BlynkWifi(BlynkTransportShieldEsp8266& transp)
: Base(transp)
, wifi(NULL)
{}
bool connectWiFi(const char* ssid, const char* pass)
{
BLYNK_LOG("Connecting to %s", ssid);
if (!wifi->setEcho(0)) {
BLYNK_LOG("Failed to disable Echo");
return false;
}
if (!wifi->setOprToStation()) {
BLYNK_LOG("Failed to set STA mode");
return false;
}
if (wifi->joinAP(ssid, pass)) {
BLYNK_LOG("IP: %s", wifi->getLocalIP().c_str());
} else {
BLYNK_LOG("Failed to connect WiFi");
return false;
}
if (!wifi->disableMUX()) {
BLYNK_LOG("Failed to disable MUX");
}
BLYNK_LOG("Connected to WiFi");
return true;
}
void config(ESP8266& esp8266,
const char* auth,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT)
{
Base::begin(auth);
wifi = &esp8266;
this->conn.begin_domain(wifi, domain, port);
}
void begin(const char* auth,
ESP8266& esp8266,
const char* ssid,
const char* pass,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT)
{
config(esp8266, auth, domain, port);
connectWiFi(ssid, pass);
}
private:
ESP8266* wifi;
};
static BlynkTransportShieldEsp8266 _blynkTransport;
BlynkWifi Blynk(_blynkTransport);
#include <BlynkWidgets.h>
#endif