|
| 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.4 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.5 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 | + * |
| 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.4 或更新的 ESP8266/Arduino 支持包。 |
| 35 | + * https://github.com/esp8266/Arduino/releases |
| 36 | + * |
| 37 | + * 如果使用 ESP32 接入 Blinker, |
| 38 | + * 请确保安装了 1.0.5 或更新的 ESP32/Arduino 支持包。 |
| 39 | + * https://github.com/espressif/arduino-esp32/releases |
| 40 | + * |
| 41 | + * 文档: https://diandeng.tech/doc |
| 42 | + * |
| 43 | + * |
| 44 | + * *****************************************************************/ |
| 45 | + |
| 46 | +#define BLINKER_WIFI |
| 47 | + |
| 48 | +#include <Blinker.h> |
| 49 | + |
| 50 | +char auth[] = "Your Device Secret Key"; |
| 51 | +char ssid[] = "Your WiFi network SSID or name"; |
| 52 | +char pswd[] = "Your WiFi network WPA password or WEP key"; |
| 53 | + |
| 54 | +// 新建组件对象 |
| 55 | +BlinkerButton Button1("btn-abc"); |
| 56 | +BlinkerNumber Number1("num-abc"); |
| 57 | + |
| 58 | +int counter = 0; |
| 59 | + |
| 60 | +// 按下按键即会执行该函数 |
| 61 | +void button1_callback(const String & state) |
| 62 | +{ |
| 63 | + BLINKER_LOG("get button state: ", state); |
| 64 | + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); |
| 65 | +} |
| 66 | + |
| 67 | +// 如果未绑定的组件被触发,则会执行其中内容 |
| 68 | +void dataRead(const String & data) |
| 69 | +{ |
| 70 | + BLINKER_LOG("Blinker readString: ", data); |
| 71 | + counter++; |
| 72 | + Number1.print(counter); |
| 73 | + |
| 74 | + if (BLINKER_PROTOCOL_MQTT != NULL) { |
| 75 | + String pub_topic = "/device/" + Blinker.deviceName() + "/s"; |
| 76 | + String pub_data = "{\"toDevice\":\"the device name you need pub to\",\"data\":{\"hello\":\"blinker\"}}"; |
| 77 | + BLINKER_PROTOCOL_MQTT->publish(pub_topic.c_str(), pub_data.c_str()); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void setup() |
| 82 | +{ |
| 83 | + // 初始化串口 |
| 84 | + Serial.begin(115200); |
| 85 | + BLINKER_DEBUG.stream(Serial); |
| 86 | + BLINKER_DEBUG.debugAll(); |
| 87 | + |
| 88 | + // 初始化有LED的IO |
| 89 | + pinMode(LED_BUILTIN, OUTPUT); |
| 90 | + digitalWrite(LED_BUILTIN, HIGH); |
| 91 | + // 初始化blinker |
| 92 | + Blinker.begin(auth, ssid, pswd); |
| 93 | + Blinker.attachData(dataRead); |
| 94 | + |
| 95 | + Button1.attach(button1_callback); |
| 96 | +} |
| 97 | + |
| 98 | +void loop() { |
| 99 | + Blinker.run(); |
| 100 | +} |
0 commit comments