-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BlynkSimpleEsp32_SSL.h
139 lines (120 loc) · 3.94 KB
/
BlynkSimpleEsp32_SSL.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
/**
* @file BlynkSimpleEsp32_SSL.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Dec 2017
* @brief
*
*/
#ifndef BlynkSimpleEsp32_SSL_h
#define BlynkSimpleEsp32_SSL_h
#ifndef ESP32
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif
static const char BLYNK_DEFAULT_ROOT_CA[] =
#include <certs/letsencrypt_pem.h>
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <Adapters/BlynkArduinoClient.h>
#include <WiFiClientSecure.h>
template <typename Client>
class BlynkArduinoClientSecure
: public BlynkArduinoClientGen<Client>
{
public:
BlynkArduinoClientSecure(Client& client)
: BlynkArduinoClientGen<Client>(client)
, caCert(NULL)
{}
void setRootCA(const char* fp) { caCert = fp; }
bool connect() {
this->client->setHandshakeTimeout(30);
this->client->setCACert(caCert);
if (BlynkArduinoClientGen<Client>::connect()) {
BLYNK_LOG1(BLYNK_F("Certificate OK"));
return true;
} else {
BLYNK_LOG1(BLYNK_F("Secure connection failed"));
}
return false;
}
private:
const char* caCert;
};
template <typename Transport>
class BlynkWifi
: public BlynkProtocol<Transport>
{
typedef BlynkProtocol<Transport> Base;
public:
BlynkWifi(Transport& transp)
: Base(transp)
{}
void connectWiFi(const char* ssid, const char* pass)
{
BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
WiFi.mode(WIFI_STA);
if (pass && strlen(pass)) {
WiFi.begin(ssid, pass);
} else {
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) {
BlynkDelay(500);
}
BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
IPAddress myip = WiFi.localIP();
(void)myip; // Eliminate warnings about unused myip
BLYNK_LOG_IP("IP: ", myip);
}
void config(const char* auth,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT_SSL,
const char* root_ca = BLYNK_DEFAULT_ROOT_CA)
{
Base::begin(auth);
this->conn.begin(domain, port);
this->conn.setRootCA(root_ca);
}
void config(const char* auth,
IPAddress ip,
uint16_t port = BLYNK_DEFAULT_PORT_SSL,
const char* root_ca = BLYNK_DEFAULT_ROOT_CA)
{
Base::begin(auth);
this->conn.begin(ip, port);
this->conn.setRootCA(root_ca);
}
void begin(const char* auth,
const char* ssid,
const char* pass,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT_SSL,
const char* root_ca = BLYNK_DEFAULT_ROOT_CA)
{
connectWiFi(ssid, pass);
config(auth, domain, port, root_ca);
while(this->connect() != true) {}
}
void begin(const char* auth,
const char* ssid,
const char* pass,
IPAddress ip,
uint16_t port = BLYNK_DEFAULT_PORT_SSL,
const char* root_ca = BLYNK_DEFAULT_ROOT_CA)
{
connectWiFi(ssid, pass);
config(auth, ip, port, root_ca);
while(this->connect() != true) {}
}
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_BLYNK)
static WiFiClientSecure _blynkWifiClient;
static BlynkArduinoClientSecure<WiFiClientSecure> _blynkTransport(_blynkWifiClient);
BlynkWifi<BlynkArduinoClientSecure<WiFiClientSecure> > Blynk(_blynkTransport);
#else
extern BlynkWifi<BlynkArduinoClientSecure<WiFiClientSecure> > Blynk;
#endif
#include <BlynkWidgets.h>
#endif