点灯科技示例

    1. #define BLINKER_PRINT Serial
    2. #define BLINKER_WIFI
    3. #define BLINKER_MIOT_OUTLET
    4. #include <ESP8266WiFi.h>
    5. #include <Blinker.h>
    6. #include <Arduino.h>
    7. #include <Servo.h>
    8. char auth[] = "b4c7a3943495";//换成自己的key吧
    9. char ssid[] = "路由器11111111";//换成自己的wifi吧
    10. char pswd[] = "12345678";//换成自己的wifi密码吧
    11. // 定义引脚
    12. const int SERVO = 14; // 舵机 D5
    13. Servo myservo;
    14. // 新建组件对象
    15. BlinkerButton btnTurn("btnturn");
    16. BlinkerSlider ranPer("ran");
    17. long counter = 0;
    18. int angle = 0;
    19. const int angle_cen = 100;
    20. const int angle_max = 130;
    21. const int angle_min = 65;
    22. void doServo(int value){
    23. angle = value;
    24. myservo.write(value);
    25. }
    26. // 小爱
    27. void miotPowerState(const String & state)
    28. {
    29. BLINKER_LOG("need set power state: ", state);
    30. if (state == BLINKER_CMD_ON) {
    31. BlinkerMIOT.powerState("on");
    32. BlinkerMIOT.print();
    33. btnTurn_callback("on");
    34. }
    35. else if (state == BLINKER_CMD_OFF) {
    36. BlinkerMIOT.powerState("off");
    37. BlinkerMIOT.print();
    38. btnTurn_callback("off");
    39. }
    40. }
    41. // 按下按键即会执行该函数
    42. void btnTurn_callback(const String & state) {
    43. BLINKER_LOG("get button state: ", state);
    44. if (counter%2){
    45. doServo(angle_max);
    46. }
    47. else{
    48. doServo(angle_min);
    49. }
    50. Blinker.delay(300);
    51. doServo(angle_cen);
    52. counter++;
    53. }
    54. void ranPer_callback(int32_t value){
    55. BLINKER_LOG("get ran state: ", value);
    56. doServo(value);
    57. }
    58. // 如果未绑定的组件被触发,则会执行其中内容
    59. void dataRead(const String & data)
    60. {
    61. BLINKER_LOG("Blinker readString: ", data);
    62. }
    63. void setup() {
    64. pinMode(LED_BUILTIN,OUTPUT);
    65. digitalWrite(LED_BUILTIN,HIGH);//熄灭板载LED 对应引脚数字2
    66. // 初始化串口
    67. Serial.begin(115200);
    68. #if defined(BLINKER_PRINT)
    69. BLINKER_DEBUG.stream(BLINKER_PRINT);
    70. #endif
    71. myservo.attach(SERVO);
    72. // 初始化blinker
    73. Blinker.begin(auth, ssid, pswd);
    74. Blinker.attachData(dataRead);
    75. BlinkerMIOT.attachPowerState(miotPowerState);
    76. btnTurn.attach(btnTurn_callback);
    77. ranPer.attach(ranPer_callback);
    78. }
    79. void loop() {
    80. Blinker.run();
    81. delay(100);
    82. }