Skip to content

Commit 5a1e649

Browse files
committed
update codes, upgrade NO SSL support codes & example
1 parent 5ec0a2e commit 5a1e649

File tree

6 files changed

+311
-33
lines changed

6 files changed

+311
-33
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* *****************************************************************
2+
*
3+
* Download latest Blinker library here:
4+
* https://github.com/blinker-iot/blinker-library/archive/master.zip
5+
*
6+
*
7+
* Blinker is a cross-hardware, cross-platform solution for the IoT.
8+
* It provides APP, device and server support,
9+
* and uses public cloud services for data transmission and storage.
10+
* It can be used in smart home, data monitoring and other fields
11+
* to help users build Internet of Things projects better and faster.
12+
*
13+
* Make sure installed 2.7.2 or later ESP8266/Arduino package,
14+
* if use ESP8266 with Blinker.
15+
* https://github.com/esp8266/Arduino/releases
16+
*
17+
* Make sure installed 1.0.4 or later ESP32/Arduino package,
18+
* if use ESP32 with Blinker.
19+
* https://github.com/espressif/arduino-esp32/releases
20+
*
21+
* Docs: https://diandeng.tech/doc
22+
* https://github.com/blinker-iot/blinker-doc/wiki
23+
*
24+
* *****************************************************************
25+
*
26+
* Blinker 库下载地址:
27+
* https://github.com/blinker-iot/blinker-library/archive/master.zip
28+
*
29+
* Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
30+
* 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
31+
* 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
32+
*
33+
* 如果使用 ESP8266 接入 Blinker,
34+
* 请确保安装了 2.7.2 或更新的 ESP8266/Arduino 支持包。
35+
* https://github.com/esp8266/Arduino/releases
36+
*
37+
* 如果使用 ESP32 接入 Blinker,
38+
* 请确保安装了 1.0.4 或更新的 ESP32/Arduino 支持包。
39+
* https://github.com/espressif/arduino-esp32/releases
40+
*
41+
* 文档: https://diandeng.tech/doc
42+
* https://github.com/blinker-iot/blinker-doc/wiki
43+
*
44+
* *****************************************************************/
45+
46+
#define BLINKER_WIFI
47+
#define BLINKER_WITHOUT_SSL
48+
49+
#include <Blinker.h>
50+
51+
char auth[] = "6650efab7f0a";
52+
char ssid[] = "有没有wifi";
53+
char pswd[] = "i8888888";
54+
55+
// 新建组件对象
56+
BlinkerButton Button1("btn-abc");
57+
BlinkerNumber Number1("num-abc");
58+
59+
int counter = 0;
60+
61+
// 按下按键即会执行该函数
62+
void button1_callback(const String & state)
63+
{
64+
BLINKER_LOG("get button state: ", state);
65+
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
66+
}
67+
68+
// 如果未绑定的组件被触发,则会执行其中内容
69+
void dataRead(const String & data)
70+
{
71+
BLINKER_LOG("Blinker readString: ", data);
72+
counter++;
73+
Number1.print(counter);
74+
}
75+
76+
void setup()
77+
{
78+
// 初始化串口
79+
Serial.begin(115200);
80+
BLINKER_DEBUG.stream(Serial);
81+
BLINKER_DEBUG.debugAll();
82+
83+
// 初始化有LED的IO
84+
pinMode(LED_BUILTIN, OUTPUT);
85+
digitalWrite(LED_BUILTIN, HIGH);
86+
// 初始化blinker
87+
Blinker.begin(auth, ssid, pswd);
88+
Blinker.attachData(dataRead);
89+
90+
Button1.attach(button1_callback);
91+
}
92+
93+
void loop() {
94+
Blinker.run();
95+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ BLINKER_ESP_SMARTCONFIG KEYWORD2
139139
BLINKER_APCONFIG KEYWORD2
140140
BLINKER_BLE KEYWORD2
141141
BLINKER_WIFI KEYWORD2
142+
BLINKER_WITHOUT_SSL KEYWORD2
142143
BLINKER_WIFI_AUTO KEYWORD2
143144
BLINKER_LOWPOWER KEYWORD2
144145
BLINKER_LOWPOWER_AIR202 KEYWORD2

src/Adapters/BlinkerMQTT.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ char* BLINKER_RRPC_SUB_TOPIC_MQTT;
228228
uint16_t MQTT_PORT_MQTT;
229229

230230
#if defined(ESP8266)
231-
BearSSL::WiFiClientSecure client_mqtt;
231+
#ifndef BLINKER_WITHOUT_SSL
232+
BearSSL::WiFiClientSecure client_mqtt;
233+
#else
234+
WiFiClient client_mqtt;
235+
#endif
232236
// WiFiClientSecure client_mqtt;
233237
#elif defined(ESP32)
234238
WiFiClientSecure client_s;
@@ -343,8 +347,10 @@ int BlinkerMQTT::connect()
343347
// BLINKER_LOG_ALL(BLINKER_F("MQTT_KEY_MQTT: "), MQTT_KEY_MQTT);
344348

345349
#if defined(ESP8266)
346-
client_mqtt.setInsecure();
347-
::delay(10);
350+
#ifndef BLINKER_WITHOUT_SSL
351+
client_mqtt.setInsecure();
352+
::delay(10);
353+
#endif
348354
#endif
349355

350356
if ((ret = mqtt_MQTT->connect()) != 0)
@@ -1914,13 +1920,16 @@ int BlinkerMQTT::connectServer() {
19141920
// String payload = _dataGet;
19151921

19161922

1923+
#ifndef BLINKER_WITHOUT_SSL
1924+
client_mqtt.stop();
19171925

1918-
client_mqtt.stop();
1919-
1920-
std::unique_ptr<BearSSL::WiFiClientSecure>client_s(new BearSSL::WiFiClientSecure);
1926+
std::unique_ptr<BearSSL::WiFiClientSecure>client_s(new BearSSL::WiFiClientSecure);
19211927

1922-
// client_s->setFingerprint(fingerprint);
1923-
client_s->setInsecure();
1928+
// client_s->setFingerprint(fingerprint);
1929+
client_s->setInsecure();
1930+
#else
1931+
WiFiClient client_s;
1932+
#endif
19241933

19251934
String url_iot = BLINKER_F("/api/v1/user/device/diy/auth?authKey=");
19261935
url_iot += _authKey;
@@ -1929,18 +1938,29 @@ int BlinkerMQTT::connectServer() {
19291938
url_iot += _miType;
19301939
url_iot += BLINKER_F("&version=");
19311940
url_iot += BLINKER_OTA_VERSION_CODE;
1941+
#ifndef BLINKER_WITHOUT_SSL
19321942
url_iot += BLINKER_F("&protocol=mqtts");
1943+
#else
1944+
url_iot += BLINKER_F("&protocol=mqtt");
1945+
#endif
19331946
// url_iot += BLINKER_OTA_VERSION_CODE;
19341947

1935-
url_iot = "https://" + host + url_iot;
1936-
1948+
#ifndef BLINKER_WITHOUT_SSL
1949+
url_iot = "https://" + host + url_iot;
1950+
#else
1951+
url_iot = "http://" + host + url_iot;
1952+
#endif
19371953
HTTPClient http;
19381954

19391955
String payload;
19401956

19411957
BLINKER_LOG_ALL(BLINKER_F("[HTTP] begin: "), url_iot);
19421958

1959+
#ifndef BLINKER_WITHOUT_SSL
19431960
if (http.begin(*client_s, url_iot)) { // HTTPS
1961+
#else
1962+
if (http.begin(client_s, url_iot)) {
1963+
#endif
19441964

19451965
// Serial.print("[HTTPS] GET...\n");
19461966
// start connection and send HTTP header
@@ -2369,7 +2389,9 @@ int BlinkerMQTT::connectServer() {
23692389
#if defined(ESP8266)
23702390
// client_s->stop();
23712391
// if (!isMQTTinit)
2372-
client_mqtt.setInsecure();
2392+
#ifndef BLINKER_WITHOUT_SSL
2393+
client_mqtt.setInsecure();
2394+
#endif
23732395
#endif
23742396
// connect();
23752397

0 commit comments

Comments
 (0)