├── .github └── workflows │ └── deploy.yml ├── .vscode ├── arduino.json ├── c_cpp_properties.json └── settings.json ├── ESP32 ├── README.md ├── v4.3 │ └── components │ │ ├── Blinker │ │ ├── CmakeLists.txt │ │ ├── Kconfig.projbuild │ │ ├── component.mk │ │ ├── include │ │ │ ├── blinker_api.h │ │ │ ├── blinker_button.h │ │ │ ├── blinker_config.h │ │ │ ├── blinker_http.h │ │ │ ├── blinker_mqtt.h │ │ │ ├── blinker_prov_apconfig.h │ │ │ ├── blinker_prov_smartconfig.h │ │ │ ├── blinker_storage.h │ │ │ ├── blinker_utils.h │ │ │ ├── blinker_wifi.h │ │ │ └── blinker_ws.h │ │ └── src │ │ │ ├── api │ │ │ └── blinker_api.c │ │ │ ├── mqtt │ │ │ └── blinker_mqtt.c │ │ │ ├── provisioning │ │ │ ├── blinker_prov_apconfig.c │ │ │ ├── blinker_prov_smartconfig.c │ │ │ └── blinker_wifi.c │ │ │ └── utils │ │ │ ├── blinker_button.c │ │ │ ├── blinker_http.c │ │ │ ├── blinker_mdns.c │ │ │ ├── blinker_reboot.c │ │ │ ├── blinker_storage.c │ │ │ ├── blinker_timesync.c │ │ │ ├── blinker_utils.c │ │ │ └── blinker_ws.c │ │ └── esp_http_server │ │ ├── CMakeLists.txt │ │ ├── Kconfig │ │ ├── component.mk │ │ ├── include │ │ ├── esp_http_server.h │ │ └── http_server.h │ │ ├── src │ │ ├── esp_httpd_priv.h │ │ ├── httpd_main.c │ │ ├── httpd_parse.c │ │ ├── httpd_sess.c │ │ ├── httpd_txrx.c │ │ ├── httpd_uri.c │ │ ├── httpd_ws.c │ │ ├── port │ │ │ └── esp32 │ │ │ │ └── osal.h │ │ └── util │ │ │ ├── ctrl_sock.c │ │ │ └── ctrl_sock.h │ │ └── test │ │ ├── CMakeLists.txt │ │ ├── component.mk │ │ └── test_http_server.c └── v4.4 │ └── components │ ├── Blinker │ ├── CmakeLists.txt │ ├── Kconfig.projbuild │ ├── component.mk │ ├── include │ │ ├── blinker_api.h │ │ ├── blinker_button.h │ │ ├── blinker_config.h │ │ ├── blinker_http.h │ │ ├── blinker_mqtt.h │ │ ├── blinker_prov_apconfig.h │ │ ├── blinker_prov_smartconfig.h │ │ ├── blinker_storage.h │ │ ├── blinker_utils.h │ │ ├── blinker_wifi.h │ │ └── blinker_ws.h │ └── src │ │ ├── api │ │ └── blinker_api.c │ │ ├── mqtt │ │ └── blinker_mqtt.c │ │ ├── provisioning │ │ ├── blinker_prov_apconfig.c │ │ ├── blinker_prov_smartconfig.c │ │ └── blinker_wifi.c │ │ └── utils │ │ ├── blinker_button.c │ │ ├── blinker_http.c │ │ ├── blinker_mdns.c │ │ ├── blinker_reboot.c │ │ ├── blinker_storage.c │ │ ├── blinker_timesync.c │ │ ├── blinker_utils.c │ │ └── blinker_ws.c │ └── esp_http_server │ ├── CMakeLists.txt │ ├── Kconfig │ ├── component.mk │ ├── include │ ├── esp_http_server.h │ └── http_server.h │ ├── src │ ├── esp_httpd_priv.h │ ├── httpd_main.c │ ├── httpd_parse.c │ ├── httpd_sess.c │ ├── httpd_txrx.c │ ├── httpd_uri.c │ ├── httpd_ws.c │ ├── port │ │ └── esp32 │ │ │ └── osal.h │ └── util │ │ ├── ctrl_sock.c │ │ └── ctrl_sock.h │ └── test │ ├── CMakeLists.txt │ ├── component.mk │ └── test_http_server.c ├── ESP8266 ├── README.md └── components │ ├── blinker │ ├── CMakeLists.txt │ ├── Kconfig.projbuild │ ├── component.mk │ ├── include │ │ ├── blinker_api.h │ │ ├── blinker_button.h │ │ ├── blinker_config.h │ │ ├── blinker_http.h │ │ ├── blinker_mqtt.h │ │ ├── blinker_prov_apconfig.h │ │ ├── blinker_prov_smartconfig.h │ │ ├── blinker_storage.h │ │ ├── blinker_utils.h │ │ ├── blinker_wifi.h │ │ └── blinker_ws.h │ └── src │ │ ├── api │ │ └── blinker_api.c │ │ ├── mqtt │ │ └── blinker_mqtt.c │ │ ├── provisioning │ │ ├── blinker_prov_apconfig.c │ │ ├── blinker_prov_smartconfig.c │ │ └── blinker_wifi.c │ │ └── utils │ │ ├── blinker_button.c │ │ ├── blinker_http.c │ │ ├── blinker_mdns.c │ │ ├── blinker_reboot.c │ │ ├── blinker_storage.c │ │ ├── blinker_timesync.c │ │ ├── blinker_utils.c │ │ └── blinker_ws.c │ └── esp_http_server │ ├── CMakeLists.txt │ ├── Kconfig │ ├── component.mk │ ├── include │ ├── esp_http_server.h │ └── http_server.h │ └── src │ ├── esp_httpd_priv.h │ ├── httpd_main.c │ ├── httpd_parse.c │ ├── httpd_sess.c │ ├── httpd_txrx.c │ ├── httpd_uri.c │ ├── httpd_ws.c │ ├── port │ └── esp8266 │ │ └── osal.h │ └── util │ ├── ctrl_sock.c │ └── ctrl_sock.h ├── LICENSE ├── README.md ├── examples ├── Blinker_Hello │ └── Hello_WiFi │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ └── main │ │ ├── CMakeLists.txt │ │ ├── blinker_example_main.c │ │ └── component.mk └── Blinker_Widgets │ ├── Blinker_Button │ └── Button_WiFi │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ └── main │ │ ├── CMakeLists.txt │ │ ├── blinker_example_main.c │ │ └── component.mk │ ├── Blinker_Joystick │ └── Joystick_WiFi │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ └── main │ │ ├── CMakeLists.txt │ │ ├── blinker_example_main.c │ │ └── component.mk │ ├── Blinker_RGB │ └── RGB_WiFi │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ └── main │ │ ├── CMakeLists.txt │ │ ├── blinker_example_main.c │ │ └── component.mk │ ├── Blinker_Slider │ └── Slider_WiFi │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ └── main │ │ ├── CMakeLists.txt │ │ ├── blinker_example_main.c │ │ └── component.mk │ └── Blinker_TAB │ └── TAB_WiFi │ ├── CMakeLists.txt │ ├── Makefile │ └── main │ ├── CMakeLists.txt │ ├── blinker_example_main.c │ └── component.mk └── version.json /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: auto-deploy 2 | on: 3 | push: 4 | branches: [master] 5 | 6 | jobs: 7 | archive-file: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: get version 12 | id: version 13 | uses: notiz-dev/github-action-json-property@release 14 | with: 15 | path: 'version.json' 16 | prop_path: 'version' 17 | 18 | - name: get current time 19 | uses: 1466587594/get-current-time@v2 20 | id: current-time 21 | with: 22 | format: YYYY.MM.DD 23 | utcOffset: "+08:00" 24 | 25 | - name: action-zip 26 | uses: montudor/action-zip@v0.1.1 27 | with: 28 | args: zip -qq -r blinker-esp-idf-${{steps.version.outputs.prop}}.zip . -x .github/* -x .git/* 29 | 30 | - name: create json 31 | id: jsonfile 32 | uses: jsdaniell/create-json@1.1.2 33 | with: 34 | name: "freertos.json" 35 | json: '{"img": "assets/sdk/freertos.png", "text": "IDF (FreeRTOS)", "update": "${{ steps.current-time.outputs.formattedTime}}", "version": "${{steps.version.outputs.prop}}", "github": "https://github.com/blinker-iot/blinker-esp-idf", "document": "https://diandeng.tech/doc/freertos-support", "download": "sdk/blinker-esp-idf-${{steps.version.outputs.prop}}.zip" }' 36 | 37 | # - name: upload zip 38 | # uses: garygrossgarten/github-action-scp@release 39 | # with: 40 | # local: blinker-esp-idf-${{steps.version.outputs.prop}}.zip 41 | # remote: ${{ secrets.REMOTE_PATH }}/blinker-esp-idf-${{steps.version.outputs.prop}}.zip 42 | # host: ${{ secrets.SERVER_IP }} 43 | # username: ubuntu 44 | # password: ${{ secrets.SERVER_PASSWD }} 45 | # recursive: true 46 | 47 | - name: UPload Zip 48 | uses: Dylan700/sftp-upload-action@latest 49 | with: 50 | server: ${{ secrets.SERVER_IP }} 51 | username: ubuntu 52 | password: ${{secrets.SERVER_PASSWD}} 53 | port: 22 54 | uploads: | 55 | ./ => ${{ secrets.REMOTE_PATH }} 56 | ignore: | 57 | !blinker-esp-idf-${{steps.version.outputs.prop}}.zip 58 | 59 | - name: upload json 60 | uses: garygrossgarten/github-action-scp@release 61 | with: 62 | local: freertos.json 63 | remote: ${{ secrets.REMOTE_PATH }}/freertos.json 64 | host: ${{ secrets.SERVER_IP }} 65 | username: ubuntu 66 | password: ${{ secrets.SERVER_PASSWD }} 67 | recursive: true 68 | -------------------------------------------------------------------------------- /.vscode/arduino.json: -------------------------------------------------------------------------------- 1 | { 2 | "board": "esp8266:esp8266:d1", 3 | "configuration": "xtal=80,vt=flash,exception=legacy,ssl=all,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=921600", 4 | "port": "COM36" 5 | } -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "C:\\Users\\12102\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\**", 7 | "C:\\Users\\12102\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.6.2\\**" 8 | ], 9 | "forcedInclude": [], 10 | "intelliSenseMode": "msvc-x64", 11 | "compilerPath": "F:\\msys64\\mingw64\\bin\\gcc.exe", 12 | "cStandard": "c11", 13 | "cppStandard": "c++17" 14 | } 15 | ], 16 | "version": 4 17 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iomanip": "c", 4 | "sntp.h": "c", 5 | "iot_button.h": "c" 6 | } 7 | } -------------------------------------------------------------------------------- /ESP32/README.md: -------------------------------------------------------------------------------- 1 | # Blinker hello example 2 | 3 | put the blinker componets into your projects. 4 | 5 | make the project list like this 6 | ``` 7 | Blinker_Hello 8 | |__components 9 | | |__blinker 10 | |__main 11 | ``` 12 | 13 | override the esp_http_server components with we provide. 14 | 15 | ``` 16 | idf.py menuconfig 17 | ``` 18 | 19 | enter Blinker Config 20 | 21 | * set BLINKER DEVICE TYPE 22 | 23 | * set BLINKER AUTH KEY 24 | 25 | * set Provisioning Type 26 | 27 | save and exit blinker config 28 | 29 | * Set Serial Flasher Options. 30 | 31 | enter components config 32 | 33 | * enable mDNS 34 | 35 | * disable Newlib 36 | 37 | * enable webSocket server support(http_server) 38 | 39 | save and exit 40 | 41 | ## Build and Flash 42 | 43 | Build the project and flash it to the board, then run monitor tool to view serial output: 44 | 45 | ``` 46 | idf.py -p PORT flash monitor 47 | ``` 48 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/CmakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRCS "src/api/blinker_api.c" 2 | "src/mqtt/blinker_mqtt.c" 3 | "src/provisioning/blinker_prov_apconfig.c" 4 | "src/provisioning/blinker_prov_smartconfig.c" 5 | "src/provisioning/blinker_wifi.c" 6 | "src/utils/blinker_button.c" 7 | "src/utils/blinker_http.c" 8 | "src/utils/blinker_mdns.c" 9 | "src/utils/blinker_reboot.c" 10 | "src/utils/blinker_storage.c" 11 | "src/utils/blinker_timesync.c" 12 | "src/utils/blinker_utils.c" 13 | "src/utils/blinker_ws.c" 14 | INCLUDE_DIRS "include" 15 | REQUIRES "mdns" "json" "mqtt" 16 | PRIV_REQUIRES nvs_flash esp_http_client esp_http_server app_update) -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_INCLUDEDIRS := include 2 | 3 | COMPONENT_SRCDIRS := src/api src/mqtt src/provisioning src/utils -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_api.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | #include "cJSON.h" 12 | #include "blinker_config.h" 13 | 14 | typedef enum { 15 | BLINKER_BUTTON = 0, 16 | BLINKER_IMAGE, 17 | BLINKER_JOYSTICK, 18 | BLINKER_RGB, 19 | BLINKER_SLIDER, 20 | BLINKER_TAB, 21 | BLINKER_SWITCH, 22 | } blinker_widget_type_t; 23 | 24 | typedef struct { 25 | union { 26 | int i; 27 | char *s; 28 | }; 29 | int array[4]; 30 | } blinker_widget_param_val_t; 31 | 32 | typedef enum { 33 | BLINKER_PARAM_POWER_STATE = 0, 34 | BLINKER_PARAM_COLOR, 35 | BLINKER_PARAM_MODE, 36 | BLINKER_PARAM_COLOR_TEMP, 37 | BLINKER_PARAM_COLOR_TEMP_UP, 38 | BLINKER_PARAM_COLOR_TEMP_DOWN, 39 | BLINKER_PARAM_BRIGHTNESS, 40 | BLINKER_PARAM_BRIGHTNESS_UP, 41 | BLINKER_PARAM_BRIGHTNESS_DOWN, 42 | BLINKER_PARAM_TEMP, 43 | BLINKER_PARAM_TEMP_UP, 44 | BLINKER_PARAM_TEMP_DOWN, 45 | BLINKER_PARAM_HUMI, 46 | BLINKER_PARAM_LEVEL, 47 | BLINKER_PARAM_LEVEL_UP, 48 | BLINKER_PARAM_LEVEL_DOWN, 49 | BLINKER_PARAM_HSWING, 50 | BLINKER_PARAM_VSWING, 51 | BLINKER_PARAM_MODE_STATE, 52 | BLINKER_PARAM_GET_STATE, 53 | } blinker_va_param_type_t; 54 | 55 | typedef struct { 56 | union { 57 | int i; 58 | char *s; 59 | }; 60 | 61 | union { 62 | int num; 63 | char *state; 64 | }; 65 | blinker_va_param_type_t type; 66 | } blinker_va_param_cb_t; 67 | 68 | typedef void (*blinker_va_cb_t)(const blinker_va_param_cb_t *val); 69 | typedef void (*blinker_widget_cb_t)(const blinker_widget_param_val_t *val); 70 | typedef void (*blinker_json_cb_t)(cJSON *param); 71 | typedef void (*blinker_data_cb_t)(const char *data); 72 | typedef void (*blinker_void_cb_t)(void); 73 | 74 | esp_err_t blinker_timeslot_data(cJSON *param, const char *key, const double value); 75 | 76 | esp_err_t blinker_timeslot_data_init(const uint16_t interval, const uint8_t times, const blinker_json_cb_t cb); 77 | 78 | esp_err_t blinker_data_handler(const blinker_data_cb_t cb); 79 | 80 | esp_err_t blinker_heart_beat_handler(const blinker_void_cb_t cb); 81 | 82 | esp_err_t blinker_builtin_switch_handler(const blinker_widget_cb_t cb); 83 | 84 | esp_err_t blinker_builtin_switch_state(const char *state); 85 | 86 | esp_err_t blinker_weather(char **payload, const int city); 87 | 88 | esp_err_t blinker_weather_forecast(char **payload, const int city); 89 | 90 | esp_err_t blinker_air(char **payload, const int city); 91 | 92 | esp_err_t blinker_sms(const char *msg); 93 | 94 | esp_err_t blinker_push(const char *msg); 95 | 96 | esp_err_t blinker_wechat(const char *msg); 97 | 98 | esp_err_t blinker_wechat_template(const char *title, const char *state, const char *msg); 99 | 100 | esp_err_t blinker_log(const char *msg); 101 | 102 | esp_err_t blinker_config_update(const char *msg); 103 | 104 | esp_err_t blinker_config_get(char **payload); 105 | 106 | esp_err_t blinker_va_multi_num(cJSON *param, const int num); 107 | 108 | esp_err_t blinker_aligenie_power_state(cJSON *param, const char *state); 109 | 110 | esp_err_t blinker_aligenie_color(cJSON *param, const char *color); 111 | 112 | esp_err_t blinker_aligenie_mode(cJSON *param, const char *mode); 113 | 114 | esp_err_t blinker_aligenie_color_temp(cJSON *param, const int color_temp); 115 | 116 | esp_err_t blinker_aligenie_brightness(cJSON *param, const int bright); 117 | 118 | esp_err_t blinker_aligenie_temp(cJSON *param, const int temp); 119 | 120 | esp_err_t blinker_aligenie_humi(cJSON *param, const int humi); 121 | 122 | esp_err_t blinker_aligenie_pm25(cJSON *param, const int pm25); 123 | 124 | esp_err_t blinker_aligenie_level(cJSON *param, const int level); 125 | 126 | esp_err_t blinker_aligenie_hswing(cJSON *param, const char *state); 127 | 128 | esp_err_t blinker_aligenie_vswing(cJSON *param, const char *state); 129 | 130 | esp_err_t blinker_aligenie_print(cJSON* param); 131 | 132 | esp_err_t blinker_aligenie_handler_register(blinker_va_cb_t cb); 133 | 134 | esp_err_t blinker_dueros_power_state(cJSON *param, const char *state); 135 | 136 | esp_err_t blinker_dueros_color(cJSON *param, const char *color); 137 | 138 | esp_err_t blinker_dueros_brightness(cJSON *param, const int bright); 139 | 140 | esp_err_t blinker_dueros_mode(cJSON *param, const char *mode); 141 | 142 | esp_err_t blinker_dueros_color_temp(cJSON *param, const int color_temp); 143 | 144 | esp_err_t blinker_dueros_temp(cJSON *param, const int temp); 145 | 146 | esp_err_t blinker_dueros_humi(cJSON *param, const int humi); 147 | 148 | esp_err_t blinker_dueros_pm25(cJSON *param, const int pm25); 149 | 150 | esp_err_t blinker_dueros_pm10(cJSON *param, const int pm10); 151 | 152 | esp_err_t blinker_dueros_co2(cJSON *param, const int co2); 153 | 154 | esp_err_t blinker_dueros_aqi(cJSON *param, const int aqi); 155 | 156 | esp_err_t blinker_dueros_level(cJSON *param, const int level); 157 | 158 | esp_err_t blinker_dueros_time(cJSON *param, const int time); 159 | 160 | esp_err_t blinker_dueros_print(cJSON* param); 161 | 162 | esp_err_t blinker_dueros_handler_register(const blinker_va_cb_t cb); 163 | 164 | esp_err_t blinker_miot_power_state(cJSON *param, const char *state); 165 | 166 | esp_err_t blinker_miot_color(cJSON *param, int color); 167 | 168 | esp_err_t blinker_miot_mode(cJSON *param, int mode); 169 | 170 | esp_err_t blinker_miot_mode_state(cJSON *param, const char *mode, const char *state); 171 | 172 | esp_err_t blinker_miot_color_temp(cJSON *param, const int color_temp); 173 | 174 | esp_err_t blinker_miot_brightness(cJSON *param, const int bright); 175 | 176 | esp_err_t blinker_miot_temp(cJSON *param, const int temp); 177 | 178 | esp_err_t blinker_miot_humi(cJSON *param, const int humi); 179 | 180 | esp_err_t blinker_miot_pm25(cJSON *param, const int pm25); 181 | 182 | esp_err_t blinker_miot_co2(cJSON *param, const int co2); 183 | 184 | esp_err_t blinker_miot_level(cJSON *param, const int level); 185 | 186 | esp_err_t blinker_miot_hswing(cJSON *param, const char *state); 187 | 188 | esp_err_t blinker_miot_vswing(cJSON *param, const char *state); 189 | 190 | esp_err_t blinker_miot_print(cJSON* param); 191 | 192 | esp_err_t blinker_miot_handler_register(const blinker_va_cb_t cb); 193 | 194 | esp_err_t blinker_widget_switch(cJSON *param, const char *state); 195 | 196 | esp_err_t blinker_widget_icon(cJSON *param, const char *icon); 197 | 198 | esp_err_t blinker_widget_color(cJSON *param, const char *color); 199 | 200 | esp_err_t blinker_widget_text(cJSON *param, const char *text); 201 | 202 | esp_err_t blinker_widget_text1(cJSON *param, const char *text); 203 | 204 | esp_err_t blinker_widget_text_color(cJSON *param, const char *color); 205 | 206 | esp_err_t blinker_widget_value_number(cJSON *param, const double value); 207 | 208 | esp_err_t blinker_widget_value_string(cJSON *param, const char *value); 209 | 210 | esp_err_t blinker_widget_unit(cJSON *param, const char *uint); 211 | 212 | esp_err_t blinker_widget_image(cJSON *param, const int num); 213 | 214 | esp_err_t blinker_widget_rgb_print(const char *key, const int r, const int g, const int b, const int w); 215 | 216 | esp_err_t blinker_widget_tab_print(const char *key, const int tab0, const int tab1, const int tab2, const int tab3, const int tab4); 217 | 218 | esp_err_t blinker_widget_print(const char *key, cJSON *param); 219 | 220 | esp_err_t blinker_widget_add(const char *name, const blinker_widget_type_t type, const blinker_widget_cb_t cb); 221 | 222 | esp_err_t blinker_init(void); 223 | 224 | #ifdef __cplusplus 225 | } 226 | #endif 227 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include "blinker_utils.h" 11 | 12 | #define BLINKER_FORMAT_QUEUE_TIME 200 13 | 14 | #define BLINKER_FORMAT_DATA_SIZE 1024 15 | 16 | #define BLINKER_MIN_TO_S 60 17 | 18 | #define BLINKER_MIN_TO_MS BLINKER_MIN_TO_S * 1000UL 19 | 20 | #define BLINKER_REGISTER_INTERVAL 20000U 21 | 22 | #define BLINKER_WEBSOCKET_SERVER_PORT 81 23 | 24 | #define BLINKER_MAX_TIME_INTERVAL 600 25 | 26 | #define BLINKER_MIN_TIME_INTERVAL 30 27 | 28 | #define BLINKER_MAX_TIMES_COUNT 10 29 | 30 | #define BLINKER_MIN_TIMES_COUNT 2 31 | 32 | #if defined CONFIG_BLINKER_CUSTOM_ESP 33 | 34 | #define BLINKER_REGISTER_TASK_DEEP 3 35 | 36 | #elif defined CONFIG_BLINKER_PRO_ESP 37 | 38 | #define BLINKER_REGISTER_TASK_DEEP 3 39 | #endif 40 | 41 | #define BLINKER_CMD_DETAIL "detail" 42 | 43 | #define BLINKER_CMD_AUTHKEY "authKey" 44 | 45 | #define BLINKER_CMD_REGISTER "register" 46 | 47 | #define BLINKER_CMD_MESSAGE "message" 48 | 49 | #define BLINKER_CMD_SUCCESS "success" 50 | 51 | #define BLINKER_CMD_REGISTER_SUCCESS "Registration successful" 52 | 53 | #define BLINKER_CMD_WIFI_CONFIG "wifi_config" 54 | 55 | #define BLINKER_CMD_DEVICE_NAME "deviceName" 56 | 57 | #define BLINKER_CMD_TO_DEVICE "toDevice" 58 | 59 | #define BLINKER_CMD_FROM_DEVICE "fromDevice" 60 | 61 | #define BLINKER_CMD_FROM "from" 62 | 63 | #define BLINKER_CMD_DEVICE_TYPE "deviceType" 64 | 65 | #define BLINKER_CMD_OWN_APP "OwnApp" 66 | 67 | #define BLINKER_CMD_VASSISTANT "vAssistant" 68 | 69 | #define BLINKER_CMD_SERVER_SENDER "ServerSender" 70 | 71 | #define BLINKER_CMD_SERVER_RECEIVER "ServerReceiver" 72 | 73 | #define BLINKER_CMD_MESSAGE_ID "messageId" 74 | 75 | #define BLINKER_CMD_USER_NAME "iotId" 76 | 77 | #define BLINKER_CMD_PASS_WORD "iotToken" 78 | 79 | #define BLINKER_CMD_PRODUCT_KEY "productKey" 80 | 81 | #define BLINKER_CMD_BROKER "broker" 82 | 83 | #define BLINKER_CMD_ALIYUN "aliyun" 84 | 85 | #define BLINKER_CMD_BLINKER "blinker" 86 | 87 | #define BLINKER_CMD_UUID "uuid" 88 | 89 | #define BLINKER_CMD_HOST "host" 90 | 91 | #define BLINKER_CMD_PORT "port" 92 | 93 | #define BLINKER_CMD_DATA "data" 94 | 95 | #define BLINKER_CMD_GET "get" 96 | 97 | #define BLINKER_CMD_SET "set" 98 | 99 | #define BLINKER_CMD_STATE "state" 100 | 101 | #define BLINKER_CMD_ONLINE "online" 102 | 103 | #define BLINKER_CMD_VERSION "version" 104 | 105 | #define BLINKER_CMD_BUILTIN_SWITCH "switch" 106 | 107 | #define BLINKER_CMD_SWITCH "swi" 108 | 109 | #define BLINKER_CMD_ICON "ico" 110 | 111 | #define BLINKER_CMD_COLOR "clr" 112 | 113 | #define BLINKER_CMD_COLOR_ "col" 114 | 115 | #define BLINKER_CMD_TEXT "tex" 116 | 117 | #define BLINKER_CMD_TEXT1 "tex1" 118 | 119 | #define BLINKER_CMD_TEXT_COLOR "tco" 120 | 121 | #define BLINKER_CMD_VALUE "val" 122 | 123 | #define BLINKER_CMD_IMAGE "img" 124 | 125 | #define BLINKER_CMD_UNIT "uni" 126 | 127 | #define BLINKER_CMD_ALIGENIE "AliGenie" 128 | 129 | #define BLINKER_CMD_DUEROS "DuerOS" 130 | 131 | #define BLINKER_CMD_MIOT "MIOT" 132 | 133 | #define BLINKER_CMD_POWER_STATE "pState" 134 | 135 | #define BLINKER_CMD_TRUE "true" 136 | 137 | #define BLINKER_CMD_FALSE "false" 138 | 139 | #define BLINKER_CMD_ON "on" 140 | 141 | #define BLINKER_CMD_OFF "off" 142 | 143 | #define BLINKER_CMD_MODE "mode" 144 | 145 | #define BLINKER_CMD_COLOR_TEMP "colTemp" 146 | 147 | #define BLINKER_CMD_COLOR_TEMP_UP "upColTemp" 148 | 149 | #define BLINKER_CMD_COLOR_TEMP_DOWN "downColTemp" 150 | 151 | #define BLINKER_CMD_BRIGHTNESS "bright" 152 | 153 | #define BLINKER_CMD_BRIGHTNESS_UP "upBright" 154 | 155 | #define BLINKER_CMD_BRIGHTNESS_DOWN "downBright" 156 | 157 | #define BLINKER_CMD_TEMP "temp" 158 | 159 | #define BLINKER_CMD_TEMP_UP "upTemp" 160 | 161 | #define BLINKER_CMD_TEMP_DOWN "downTemp" 162 | 163 | #define BLINKER_CMD_HUMI "humi" 164 | 165 | #define BLINKER_CMD_LEVEL "level" 166 | 167 | #define BLINKER_CMD_LEVEL_UP "upLevel" 168 | 169 | #define BLINKER_CMD_LEVEL_DOWN "downLevel" 170 | 171 | #define BLINKER_CMD_HSTATE "hsState" 172 | 173 | #define BLINKER_CMD_VSTATE "vsState" 174 | 175 | #define BLINKER_CMD_PM25 "pm25" 176 | 177 | #define BLINKER_CMD_PM10 "pm10" 178 | 179 | #define BLINKER_CMD_CO2 "co2" 180 | 181 | #define BLINKER_CMD_AQI "aqi" 182 | 183 | #define BLINKER_CMD_ECO "eco" 184 | 185 | #define BLINKER_CMD_ANION "anion" 186 | 187 | #define BLINKER_CMD_HEATER "heater" 188 | 189 | #define BLINKER_CMD_DRYER "dryer" 190 | 191 | #define BLINKER_CMD_SOFT "soft" 192 | 193 | #define BLINKER_CMD_UV "uv" 194 | 195 | #define BLINKER_CMD_UNSB "unsb" 196 | 197 | #define BLINKER_CMD_NUM "num" 198 | 199 | #define BLINKER_CMD_TIME "time" 200 | 201 | #define BLINKER_CMD_SSID "ssid" 202 | 203 | #define BLINKER_CMD_PASSWORD "pswd" 204 | 205 | #if defined(CONFIG_BLINKER_WITH_SSL) 206 | 207 | #define BLINKER_PROTOCOL_MQTT "mqtts" 208 | 209 | #define BLINKER_PROTPCOL_HTTP "https" 210 | 211 | #else 212 | 213 | #define BLINKER_PROTOCOL_MQTT "mqtt" 214 | 215 | #define BLINKER_PROTPCOL_HTTP "http" 216 | 217 | #endif 218 | 219 | #ifdef __cplusplus 220 | } 221 | #endif -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "esp_http_client.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config); 13 | 14 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url); 15 | 16 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value); 17 | 18 | esp_err_t blinker_http_get(esp_http_client_handle_t client); 19 | 20 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data); 21 | 22 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len); 23 | 24 | esp_err_t blinker_http_close(esp_http_client_handle_t client); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_mqtt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | typedef enum { 13 | BLINKER_BROKER_ALIYUN = 0, 14 | BLINKER_BROKER_BLINKER, 15 | } blinker_mqtt_broker_t; 16 | 17 | typedef struct { 18 | char *devicename; 19 | char *authkey; 20 | char *client_id; 21 | char *username; 22 | char *password; 23 | char *productkey; 24 | char *uuid; 25 | char *uri; 26 | int port; 27 | blinker_mqtt_broker_t broker; 28 | } blinker_mqtt_config_t; 29 | 30 | typedef void (*blinker_mqtt_subscribe_cb_t)(const char *topic, size_t topic_len, void *payload, size_t payload_len); 31 | 32 | blinker_mqtt_broker_t blinker_mqtt_broker(void); 33 | 34 | const char *blinker_mqtt_devicename(void); 35 | 36 | const char *blinker_mqtt_token(void); 37 | 38 | const char *blinker_mqtt_uuid(void); 39 | 40 | const char *blinker_mqtt_product_key(void); 41 | 42 | const char *blinker_mqtt_authkey(void); 43 | 44 | esp_err_t blinker_mqtt_subscribe(const char *topic, blinker_mqtt_subscribe_cb_t cb); 45 | 46 | esp_err_t blinker_mqtt_unsubscribe(const char *topic); 47 | 48 | esp_err_t blinker_mqtt_publish(const char *topic, const char *data, size_t data_len); 49 | 50 | esp_err_t blinker_mqtt_connect(void); 51 | 52 | esp_err_t blinker_mqtt_disconnect(void); 53 | 54 | esp_err_t blinker_mqtt_init(blinker_mqtt_config_t *config); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_prov_apconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_apconfig_init(void); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_prov_smartconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_smartconfig_init(void); 11 | 12 | esp_err_t blinker_prov_smartconfig_stop(void); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_storage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_storage_init(void); 11 | 12 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length); 13 | 14 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length); 15 | 16 | esp_err_t blinker_storage_erase(const char *key); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: your name 3 | * @Date: 2021-07-02 13:32:25 4 | * @LastEditTime: 2021-07-22 12:22:52 5 | * @LastEditors: Please set LastEditors 6 | * @Description: In User Settings Edit 7 | * @FilePath: \ESP8266_RTOS_SDK\0524\smart_config\examples\components\blinker\include\blinker_utils.h 8 | */ 9 | #pragma once 10 | 11 | #include 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define BLINKER_FREE(ptr) { \ 18 | if (ptr) { \ 19 | free(ptr); \ 20 | ptr = NULL; \ 21 | } \ 22 | } 23 | 24 | #define BLINKER_TS_NUM(num) data##num 25 | 26 | void blinker_log_print_heap(void); 27 | 28 | void blinker_mac_device_name(char *name); 29 | 30 | time_t blinker_time(void); 31 | 32 | esp_err_t blinker_timesync_start(void); 33 | 34 | esp_err_t blinker_mdns_init(const char *host_name); 35 | 36 | esp_err_t blinker_mdns_free(void); 37 | 38 | int blinker_reboot_unbroken_count(void); 39 | 40 | int blinker_reboot_total_count(void); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_wifi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config); 11 | 12 | esp_err_t blinker_wifi_reset(void); 13 | 14 | esp_err_t blinker_wifi_init(void); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/include/blinker_ws.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include 11 | 12 | typedef struct { 13 | httpd_handle_t hd; 14 | int fd; 15 | } async_resp_arg_t; 16 | 17 | typedef void (*blinker_websocket_data_cb_t)(async_resp_arg_t *req, const char *payload); 18 | 19 | esp_err_t blinker_websocket_async_print(async_resp_arg_t *arg, const char *payload); 20 | 21 | esp_err_t blinker_websocket_print(httpd_req_t *req, const char *payload); 22 | 23 | esp_err_t blinker_websocket_server_init(blinker_websocket_data_cb_t cb); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/provisioning/blinker_prov_apconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "freertos/event_groups.h" 5 | #include "esp_system.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | 10 | #include "blinker_prov_apconfig.h" 11 | 12 | static const char *TAG = "blinker_prov_apconfig"; 13 | 14 | static void wifi_event_handler(void* arg, esp_event_base_t event_base, 15 | int32_t event_id, void* event_data) 16 | { 17 | if (event_id == WIFI_EVENT_AP_STACONNECTED) { 18 | wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; 19 | ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", 20 | MAC2STR(event->mac), event->aid); 21 | } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { 22 | wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; 23 | ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", 24 | MAC2STR(event->mac), event->aid); 25 | } 26 | } 27 | 28 | esp_err_t blinker_prov_apconfig_init(void) 29 | { 30 | #ifndef CONFIG_IDF_TARGET_ESP8266 31 | esp_netif_create_default_wifi_ap(); 32 | #endif 33 | 34 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); 35 | 36 | uint8_t mac[6] = {0}; 37 | char mac_str[31] = { 0 }; 38 | ESP_ERROR_CHECK(esp_wifi_get_mac(WIFI_IF_STA, mac)); 39 | #if defined CONFIG_BLINKER_CUSTOM_ESP 40 | sprintf(mac_str, "DiyArduino_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 41 | #elif defined CONFIG_BLINKER_PRO_ESP 42 | sprintf(mac_str, "%s_%02X%02X%02X%02X%02X%02X", CONFIG_BLINKER_TYPE_KEY,mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 43 | #endif 44 | 45 | wifi_config_t wifi_config = { 46 | .ap = { 47 | .ssid_len = strlen(mac_str), 48 | .max_connection = 4, 49 | .channel = 1, 50 | .authmode = WIFI_AUTH_OPEN 51 | }, 52 | }; 53 | strcpy((char *)wifi_config.ap.ssid, mac_str); 54 | 55 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); 56 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); 57 | ESP_ERROR_CHECK(esp_wifi_start()); 58 | 59 | ESP_LOGI(TAG, "apconfig start"); 60 | 61 | return ESP_OK; 62 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/provisioning/blinker_prov_smartconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | #include "esp_smartconfig.h" 11 | #if CONFIG_IDF_TARGET_ESP8266 12 | #include "tcpip_adapter.h" 13 | #include "smartconfig_ack.h" 14 | #else 15 | #include "esp_netif.h" 16 | #endif 17 | #include "blinker_prov_smartconfig.h" 18 | 19 | static EventGroupHandle_t s_wifi_event_group; 20 | 21 | static const int CONNECTED_BIT = BIT0; 22 | static bool b_prov_smartconfig_start = false; 23 | static const char* TAG = "blinker_prov_smartconfig"; 24 | static void blinker_prov_smartconfig_start(void); 25 | 26 | static void event_handler(void* arg, esp_event_base_t event_base, 27 | int32_t event_id, void* event_data) 28 | { 29 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 30 | // xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL); 31 | // blinker_prov_smartconfig_start(); 32 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 33 | esp_wifi_connect(); 34 | // xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); 35 | // wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 36 | // ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 37 | // if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 38 | // ESP_LOGE(TAG, "wrong password, need reset device"); 39 | // // esp_wifi_disconnect(); 40 | // // blinker_prov_smartconfig_stop(); 41 | // // vTaskDelay(10); 42 | // // blinker_prov_smartconfig_start(); 43 | // // return; 44 | // } 45 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 46 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 47 | #if CONFIG_IDF_TARGET_ESP8266 48 | ESP_LOGI(TAG, "smartconfig connected, got ip:%s", 49 | ip4addr_ntoa(&event->ip_info.ip)); 50 | #else 51 | ESP_LOGI(TAG, "smartconfig connected, got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 52 | #endif 53 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) { 54 | ESP_LOGI(TAG, "Scan done"); 55 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) { 56 | ESP_LOGI(TAG, "Found channel"); 57 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { 58 | ESP_LOGI(TAG, "Got SSID and password"); 59 | 60 | smartconfig_event_got_ssid_pswd_t* evt = (smartconfig_event_got_ssid_pswd_t*)event_data; 61 | wifi_config_t wifi_config; 62 | uint8_t ssid[33] = { 0 }; 63 | uint8_t password[65] = { 0 }; 64 | uint8_t rvd_data[33] = { 0 }; 65 | 66 | bzero(&wifi_config, sizeof(wifi_config_t)); 67 | memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid)); 68 | memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password)); 69 | wifi_config.sta.bssid_set = evt->bssid_set; 70 | 71 | if (wifi_config.sta.bssid_set == true) { 72 | memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid)); 73 | } 74 | 75 | memcpy(ssid, evt->ssid, sizeof(evt->ssid)); 76 | memcpy(password, evt->password, sizeof(evt->password)); 77 | ESP_LOGI(TAG, "SSID:%s", ssid); 78 | ESP_LOGI(TAG, "PASSWORD:%s", password); 79 | if (evt->type == SC_TYPE_ESPTOUCH_V2) { 80 | ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) ); 81 | ESP_LOGI(TAG, "RVD_DATA:%s", rvd_data); 82 | } 83 | 84 | // ESP_ERROR_CHECK(esp_wifi_disconnect()); 85 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); 86 | ESP_ERROR_CHECK(esp_wifi_connect()); 87 | xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); 88 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) { 89 | // xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); 90 | 91 | blinker_prov_smartconfig_stop(); 92 | ESP_LOGI(TAG, "blinker_prov_smartconfig_stop"); 93 | } 94 | } 95 | 96 | esp_err_t blinker_prov_smartconfig_init(void) 97 | { 98 | s_wifi_event_group = xEventGroupCreate(); 99 | 100 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 101 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 102 | ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 103 | 104 | ESP_ERROR_CHECK(esp_wifi_start()); 105 | 106 | blinker_prov_smartconfig_start(); 107 | 108 | xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, true, false, portMAX_DELAY); 109 | 110 | return ESP_OK; 111 | } 112 | 113 | static void blinker_prov_smartconfig_start(void) 114 | { 115 | b_prov_smartconfig_start = true; 116 | #if defined CONFIG_BLINKER_SMART_CONFIG 117 | ESP_ERROR_CHECK(esp_smartconfig_set_type(CONFIG_BLINKER_PROV_SMARTCONFIG_TYPE)); 118 | smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); 119 | ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); 120 | #endif 121 | } 122 | 123 | esp_err_t blinker_prov_smartconfig_stop(void) 124 | { 125 | esp_err_t err = ESP_FAIL; 126 | 127 | if (b_prov_smartconfig_start) { 128 | err = esp_smartconfig_stop(); 129 | 130 | b_prov_smartconfig_start = false; 131 | } 132 | 133 | return err; 134 | } 135 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/provisioning/blinker_wifi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | 11 | #ifndef CONFIG_IDF_TARGET_ESP8266 12 | #include "esp_netif.h" 13 | #endif 14 | 15 | #include "blinker_wifi.h" 16 | #include "blinker_config.h" 17 | #include "blinker_storage.h" 18 | 19 | static const char* TAG = "blinker_wifi"; 20 | static const int CONNECTED_BIT = BIT0; 21 | static EventGroupHandle_t b_wifi_event_group = NULL; 22 | 23 | static void event_handler(void* arg, esp_event_base_t event_base, 24 | int32_t event_id, void* event_data) 25 | { 26 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 27 | esp_wifi_connect(); 28 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 29 | esp_wifi_connect(); 30 | 31 | wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 32 | ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 33 | if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 34 | ESP_LOGE(TAG, "wrong password"); 35 | vTaskDelay(pdMS_TO_TICKS(2000)); 36 | } 37 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 38 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 39 | #if CONFIG_IDF_TARGET_ESP8266 40 | ESP_LOGI(TAG, "got ip:%s", ip4addr_ntoa(&event->ip_info.ip)); 41 | #else 42 | ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 43 | #endif 44 | xEventGroupSetBits(b_wifi_event_group, CONNECTED_BIT); 45 | } 46 | } 47 | 48 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config) 49 | { 50 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, (wifi_config_t *)wifi_config) ); 51 | ESP_ERROR_CHECK(esp_wifi_start()); 52 | 53 | xEventGroupWaitBits(b_wifi_event_group, CONNECTED_BIT, 54 | pdFALSE, 55 | pdFALSE, 56 | portMAX_DELAY); 57 | return ESP_OK; 58 | } 59 | 60 | esp_err_t blinker_wifi_reset(void) 61 | { 62 | esp_err_t err = ESP_FAIL; 63 | 64 | err = esp_wifi_restore(); 65 | 66 | err = blinker_storage_erase(BLINKER_CMD_WIFI_CONFIG); 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_wifi_init(void) 72 | { 73 | #if CONFIG_IDF_TARGET_ESP8266 74 | tcpip_adapter_init(); 75 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 76 | #else 77 | esp_netif_init(); 78 | 79 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 80 | esp_netif_create_default_wifi_sta(); 81 | #endif 82 | 83 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 84 | 85 | ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 86 | #if CONFIG_IDF_TARGET_ESP8266 87 | ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); 88 | #endif 89 | b_wifi_event_group = xEventGroupCreate(); 90 | 91 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 92 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 93 | 94 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); 95 | 96 | return ESP_OK; 97 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_http.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "esp_log.h" 6 | #include "esp_system.h" 7 | #include "esp_netif.h" 8 | #include "esp_tls.h" 9 | 10 | #include "blinker_http.h" 11 | 12 | static const char *TAG = "blinker_http"; 13 | 14 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config) 15 | { 16 | return esp_http_client_init(config); 17 | } 18 | 19 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url) 20 | { 21 | return esp_http_client_set_url(client, url); 22 | } 23 | 24 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value) 25 | { 26 | return esp_http_client_set_header(client, key, value); 27 | } 28 | 29 | esp_err_t blinker_http_get(esp_http_client_handle_t client) 30 | { 31 | esp_err_t err = ESP_FAIL; 32 | 33 | esp_http_client_set_method(client, HTTP_METHOD_GET); 34 | 35 | err = esp_http_client_open(client, 0); 36 | 37 | if (err != ESP_OK) { 38 | ESP_LOGI(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); 39 | } else { 40 | int content_length = esp_http_client_fetch_headers(client); 41 | if (content_length < 0) { 42 | ESP_LOGI(TAG, "HTTP client fetch headers failed"); 43 | err = ESP_FAIL; 44 | } else { 45 | err = ESP_OK; 46 | } 47 | } 48 | 49 | return err; 50 | } 51 | 52 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data) 53 | { 54 | esp_http_client_set_method(client, HTTP_METHOD_POST); 55 | 56 | ESP_LOGI(TAG, "HTTP POST: %s", post_data); 57 | 58 | esp_http_client_set_post_field(client, post_data, strlen(post_data)); 59 | esp_err_t err = esp_http_client_perform(client); 60 | if (err == ESP_OK) { 61 | ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d", 62 | esp_http_client_get_status_code(client), 63 | esp_http_client_get_content_length(client)); 64 | } else { 65 | ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); 66 | } 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len) 72 | { 73 | int data_read = esp_http_client_read_response(client, buffer, len); 74 | 75 | if (data_read >= 0) { 76 | ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", 77 | esp_http_client_get_status_code(client), 78 | esp_http_client_get_content_length(client)); 79 | // ESP_LOG_BUFFER_HEX(TAG, buffer, strlen(buffer)); 80 | 81 | return ESP_OK; 82 | } else { 83 | ESP_LOGI(TAG, "Failed to read response"); 84 | 85 | return ESP_FAIL; 86 | } 87 | } 88 | 89 | esp_err_t blinker_http_close(esp_http_client_handle_t client) 90 | { 91 | esp_http_client_close(client); 92 | esp_http_client_cleanup(client); 93 | 94 | return ESP_OK; 95 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_mdns.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "esp_system.h" 5 | #include "esp_wifi.h" 6 | #include "esp_log.h" 7 | #include "mdns.h" 8 | #include 9 | #include 10 | #include "blinker_utils.h" 11 | 12 | // static const char *TAG = "blinker_mdns"; 13 | 14 | static bool m_init = false; 15 | 16 | esp_err_t blinker_mdns_init(const char *host_name) 17 | { 18 | // static bool m_init = false; 19 | if (m_init) { 20 | blinker_mdns_free(); 21 | } 22 | 23 | m_init = true; 24 | 25 | char mac_name[13] = {0}; 26 | blinker_mac_device_name(mac_name); 27 | 28 | ESP_ERROR_CHECK(mdns_init()); 29 | ESP_ERROR_CHECK(mdns_hostname_set(host_name)); 30 | ESP_ERROR_CHECK(mdns_instance_name_set(host_name)); 31 | 32 | mdns_txt_item_t serviceTxtData[2] = { 33 | {"deviceName", host_name}, 34 | {"mac", mac_name} 35 | }; 36 | 37 | ESP_ERROR_CHECK(mdns_service_add(host_name, "_blinker", "_tcp", 81, serviceTxtData, 2)); 38 | 39 | return ESP_OK; 40 | } 41 | 42 | esp_err_t blinker_mdns_free(void) 43 | { 44 | if (m_init) { 45 | m_init = false; 46 | 47 | mdns_free(); 48 | } 49 | 50 | return ESP_OK; 51 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_reboot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "freertos/FreeRTOS.h" 7 | #include "freertos/timers.h" 8 | 9 | #include 10 | #include 11 | #include "esp_sntp.h" 12 | #include "esp_log.h" 13 | #include "esp_partition.h" 14 | #include "esp_ota_ops.h" 15 | 16 | #include "blinker_utils.h" 17 | #include "blinker_storage.h" 18 | 19 | #if CONFIG_IDF_TARGET_ESP8266 20 | #include "esp8266/rtc_register.h" 21 | #elif CONFIG_IDF_TARGET_ESP32 22 | #include "esp32/rom/rtc.h" 23 | #elif CONFIG_IDF_TARGET_ESP32S2 24 | #include "esp32s2/rom/rtc.h" 25 | #elif CONFIG_IDF_TARGET_ESP32C3 26 | #include "esp32c3/rom/rtc.h" 27 | #endif 28 | 29 | #define RTC_RESET_SW_CAUSE_REG RTC_STORE0 30 | #define RTC_RESET_HW_CAUSE_REG RTC_STATE1 31 | #define RTC_WAKEUP_HW_CAUSE_REG RTC_STATE2 32 | 33 | #define RTC_RESET_HW_CAUSE_LSB 0 34 | #define RTC_RESET_HW_CAUSE_MSB 3 35 | 36 | #define REBOOT_RECORD_KEY "reboot_record" 37 | #define CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY (ESP_TASK_MAIN_PRIO + 1) 38 | 39 | typedef struct { 40 | size_t total_count; 41 | size_t unbroken_count; 42 | RESET_REASON reason; 43 | } blinker_reboot_record_t; 44 | 45 | static const char *TAG = "blinker_reboot"; 46 | static blinker_reboot_record_t g_reboot_record = {0}; 47 | 48 | static uint32_t esp_rtc_get_reset_reason(void) 49 | { 50 | #if CONFIG_IDF_TARGET_ESP8266 51 | return GET_PERI_REG_BITS(RTC_RESET_HW_CAUSE_REG, RTC_RESET_HW_CAUSE_MSB, RTC_RESET_HW_CAUSE_LSB); 52 | #else 53 | return rtc_get_reset_reason(0); 54 | #endif 55 | } 56 | 57 | static void esp_reboot_count_erase_timercb(void *priv) 58 | { 59 | g_reboot_record.unbroken_count = 0; 60 | blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 61 | 62 | ESP_LOGI(TAG, "Erase reboot count"); 63 | } 64 | 65 | static esp_err_t blinker_reboot_unbroken_init(void) 66 | { 67 | esp_err_t err = ESP_OK; 68 | g_reboot_record.reason = esp_rtc_get_reset_reason(); 69 | 70 | blinker_storage_init(); 71 | blinker_storage_get(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 72 | 73 | g_reboot_record.total_count++; 74 | 75 | /**< If the device reboots within the instruction time, 76 | the event_mdoe value will be incremented by one */ 77 | #if CONFIG_IDF_TARGET_ESP8266 78 | if (g_reboot_record.reason != DEEPSLEEP_RESET) { 79 | #else 80 | if (g_reboot_record.reason != DEEPSLEEP_RESET && g_reboot_record.reason != RTCWDT_BROWN_OUT_RESET) { 81 | #endif 82 | g_reboot_record.unbroken_count++; 83 | ESP_LOGI(TAG, "reboot unbroken count: %d", g_reboot_record.unbroken_count); 84 | } else { 85 | g_reboot_record.unbroken_count = 1; 86 | ESP_LOGI(TAG, "reboot reason: %d", g_reboot_record.reason); 87 | } 88 | 89 | err = blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 90 | 91 | esp_timer_handle_t time_handle = NULL; 92 | esp_timer_create_args_t timer_cfg = { 93 | .name = "reboot_count_erase", 94 | .callback = esp_reboot_count_erase_timercb, 95 | .dispatch_method = ESP_TIMER_TASK, 96 | }; 97 | 98 | err = esp_timer_create(&timer_cfg, &time_handle); 99 | err = esp_timer_start_once(time_handle, CONFIG_BLINKER_REBOOT_UNBROKEN_INTERVAL_TIMEOUT * 1000UL); 100 | 101 | return err; 102 | } 103 | 104 | static void reboot_unbroken_record_task(void *arg) 105 | { 106 | blinker_reboot_unbroken_init(); 107 | 108 | if (CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT && 109 | blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT) { 110 | // esp_ota_mark_app_invalid_rollback_and_reboot(); 111 | } 112 | 113 | if (blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT_RESET) { 114 | blinker_storage_erase(CONFIG_BLINKER_NVS_NAMESPACE); 115 | } 116 | 117 | ESP_LOGI(TAG, "version_fallback_task exit"); 118 | 119 | vTaskDelete(NULL); 120 | } 121 | 122 | static void __attribute__((constructor)) blinker_reboot_unbroken_record(void) 123 | { 124 | xTaskCreate(reboot_unbroken_record_task, 125 | "reboot_unbroken_record", 126 | 4 * 1024, 127 | NULL, 128 | CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY, 129 | NULL); 130 | } 131 | 132 | int blinker_reboot_unbroken_count(void) 133 | { 134 | ESP_LOGI(TAG, "blinker_reboot_unbroken_count: %d", g_reboot_record.unbroken_count); 135 | return g_reboot_record.unbroken_count; 136 | } 137 | 138 | int blinker_reboot_total_count(void) 139 | { 140 | return g_reboot_record.total_count; 141 | } 142 | 143 | // bool esp_qcloud_reboot_is_exception(bool erase_coredump) 144 | // { 145 | // esp_err_t ret = ESP_OK; 146 | // ssize_t coredump_len = 0; 147 | 148 | // const esp_partition_t *coredump_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 149 | // ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL); 150 | // ESP_QCLOUD_ERROR_CHECK(!coredump_part, false, "<%s> esp_partition_get fail", esp_err_to_name(ret)); 151 | 152 | // ret = esp_partition_read(coredump_part, 0, &coredump_len, sizeof(ssize_t)); 153 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_read fail", esp_err_to_name(ret)); 154 | 155 | // if (coredump_len <= 0) { 156 | // return false; 157 | // } 158 | 159 | // if (erase_coredump) { 160 | // /**< erase all coredump partition */ 161 | // ret = esp_partition_erase_range(coredump_part, 0, coredump_part->size); 162 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_erase_range fail", esp_err_to_name(ret)); 163 | // } 164 | 165 | // return true; 166 | // } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_storage.c: -------------------------------------------------------------------------------- 1 | #include "string.h" 2 | #include "stdio.h" 3 | #include "stdlib.h" 4 | #include "esp_log.h" 5 | #include "esp_system.h" 6 | 7 | #include "nvs.h" 8 | #include "nvs_flash.h" 9 | 10 | #include "blinker_storage.h" 11 | 12 | static const char *TAG = "blinker_storage"; 13 | 14 | esp_err_t blinker_storage_init(void) 15 | { 16 | static bool init_flag = false; 17 | 18 | if (!init_flag) { 19 | esp_err_t ret = nvs_flash_init(); 20 | 21 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {// || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { 22 | // NVS partition was truncated and needs to be erased 23 | // Retry nvs_flash_init 24 | ESP_ERROR_CHECK(nvs_flash_erase()); 25 | ret = nvs_flash_init(); 26 | } 27 | 28 | ESP_ERROR_CHECK(ret); 29 | 30 | init_flag = true; 31 | } 32 | 33 | return ESP_OK; 34 | } 35 | 36 | esp_err_t blinker_storage_erase(const char *key) 37 | { 38 | ESP_LOGI(TAG, "blinker_storage_erase: %s", key); 39 | 40 | esp_err_t ret = ESP_OK; 41 | nvs_handle handle = 0; 42 | 43 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 44 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 45 | 46 | /** 47 | * @brief If key is CONFIG_BLINKER_NVS_NAMESPACE, erase all info in CONFIG_BLINKER_NVS_NAMESPACE 48 | */ 49 | if (!strcmp(key, CONFIG_BLINKER_NVS_NAMESPACE)) { 50 | ret = nvs_erase_all(handle); 51 | } else { 52 | ret = nvs_erase_key(handle, key); 53 | } 54 | 55 | /**< Write any pending changes to non-volatile storage */ 56 | nvs_commit(handle); 57 | 58 | /**< Close the storage handle and free any allocated resources */ 59 | nvs_close(handle); 60 | 61 | return ret; 62 | } 63 | 64 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length) 65 | { 66 | esp_err_t ret = ESP_OK; 67 | nvs_handle handle = 0; 68 | 69 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 70 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 71 | 72 | /**< set variable length binary value for given key */ 73 | ret = nvs_set_blob(handle, key, value, length); 74 | 75 | /**< Write any pending changes to non-volatile storage */ 76 | nvs_commit(handle); 77 | 78 | /**< Close the storage handle and free any allocated resources */ 79 | nvs_close(handle); 80 | 81 | return ret; 82 | } 83 | 84 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length) 85 | { 86 | esp_err_t ret = ESP_OK; 87 | nvs_handle handle = 0; 88 | 89 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 90 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 91 | 92 | /**< get variable length binary value for given key */ 93 | ret = nvs_get_blob(handle, key, value, &length); 94 | 95 | /**< Close the storage handle and free any allocated resources */ 96 | nvs_close(handle); 97 | 98 | if (ret == ESP_ERR_NVS_NOT_FOUND) { 99 | ESP_LOGI(TAG, " Get value for given key, key: %s", key); 100 | return ESP_ERR_NVS_NOT_FOUND; 101 | } 102 | 103 | return ESP_OK; 104 | } 105 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_timesync.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_log.h" 7 | #include "lwip/apps/sntp.h" 8 | #include "blinker_utils.h" 9 | 10 | #define REF_TIME 1577808000 /* 2020-01-01 00:00:00 */ 11 | #define DEFAULT_TICKS (2000 / portTICK_PERIOD_MS) /* 2 seconds in ticks */ 12 | #define CONFIG_BLINKER_SNTP_SERVER_NAME "pool.ntp.org" 13 | 14 | static const char *TAG = "blinker_timesync"; 15 | // static const int NTP_DONE_BIT = BIT0; 16 | static bool g_init_done = false; 17 | // static EventGroupHandle_t b_ntp_event_group = NULL; 18 | 19 | time_t blinker_time(void) 20 | { 21 | time_t now; 22 | time(&now); 23 | 24 | return now; 25 | } 26 | 27 | static bool blinker_timesync_check(void) 28 | { 29 | time_t now; 30 | time(&now); 31 | 32 | if (now > REF_TIME) { 33 | return true; 34 | } 35 | 36 | return false; 37 | } 38 | 39 | static void blinker_timesync_task(void *arg) 40 | { 41 | if (!g_init_done) { 42 | ESP_LOGW(TAG, "Time sync not initialised using 'esp_qcloud_timesync_start'"); 43 | } 44 | 45 | for(;;) { 46 | if (blinker_timesync_check() == true) { 47 | break; 48 | } 49 | 50 | // ESP_LOGI(TAG, "Time not synchronized yet. Retrying..."); 51 | vTaskDelay(DEFAULT_TICKS); 52 | } 53 | 54 | struct tm timeinfo; 55 | char strftime_buf[64]; 56 | time_t now; 57 | time(&now); 58 | localtime_r(&now, &timeinfo); 59 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); 60 | ESP_LOGI(TAG, "The current UTC time is: %s", strftime_buf); 61 | // xEventGroupSetBits(b_ntp_event_group, NTP_DONE_BIT); 62 | 63 | vTaskDelete(NULL); 64 | } 65 | 66 | esp_err_t blinker_timesync_start(void) 67 | { 68 | if (sntp_enabled()) { 69 | ESP_LOGI(TAG, "SNTP already initialized."); 70 | g_init_done = true; 71 | return ESP_OK; 72 | } 73 | 74 | // b_ntp_event_group = xEventGroupCreate(); 75 | 76 | char *sntp_server_name = CONFIG_BLINKER_SNTP_SERVER_NAME; 77 | 78 | ESP_LOGI(TAG, "Initializing SNTP. Using the SNTP server: %s", sntp_server_name); 79 | sntp_setoperatingmode(SNTP_OPMODE_POLL); 80 | sntp_setservername(0, sntp_server_name); 81 | setenv("TZ", "CST-8", 1); 82 | tzset(); 83 | sntp_init(); 84 | g_init_done = true; 85 | 86 | xTaskCreate(blinker_timesync_task, 87 | "blinker_timesync", 88 | 2 * 1024, 89 | NULL, 90 | 6, 91 | NULL); 92 | 93 | // xEventGroupWaitBits(b_ntp_event_group, NTP_DONE_BIT, 94 | // pdFALSE, 95 | // pdFALSE, 96 | // portMAX_DELAY); 97 | 98 | return ESP_OK; 99 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include "esp_wifi.h" 11 | 12 | #include "esp_heap_caps.h" 13 | 14 | #include "blinker_utils.h" 15 | 16 | static const char *TAG = "blinker_utils"; 17 | 18 | void blinker_log_print_heap(void) 19 | { 20 | ESP_LOGI(TAG, "Free heap, current: %d", esp_get_free_heap_size()); 21 | } 22 | 23 | void blinker_mac_device_name(char *name) 24 | { 25 | uint8_t mac[6] = { 0 }; 26 | esp_wifi_get_mac(WIFI_IF_STA, mac); 27 | sprintf(name, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 28 | } -------------------------------------------------------------------------------- /ESP32/v4.3/components/Blinker/src/utils/blinker_ws.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "blinker_config.h" 10 | #include "blinker_utils.h" 11 | #include "blinker_ws.h" 12 | 13 | static const char *TAG = "blinker_ws"; 14 | static blinker_websocket_data_cb_t ws_cb = NULL; 15 | 16 | // /* 17 | // * Structure holding server handle 18 | // * and internal socket fd in order 19 | // * to use out of request send 20 | // */ 21 | // struct async_resp_arg { 22 | // httpd_handle_t hd; 23 | // int fd; 24 | // }; 25 | 26 | // /* 27 | // * async send function, which we put into the httpd work queue 28 | // */ 29 | // static void ws_async_send(void *arg) 30 | // { 31 | // static const char * data = "Async data"; 32 | // struct async_resp_arg *resp_arg = arg; 33 | // httpd_handle_t hd = resp_arg->hd; 34 | // int fd = resp_arg->fd; 35 | // httpd_ws_frame_t ws_pkt; 36 | // memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 37 | // ws_pkt.payload = (uint8_t*)data; 38 | // ws_pkt.len = strlen(data); 39 | // ws_pkt.type = HTTPD_WS_TYPE_TEXT; 40 | 41 | // httpd_ws_send_frame_async(hd, fd, &ws_pkt); 42 | // free(resp_arg); 43 | // } 44 | 45 | // static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req) 46 | // { 47 | // struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); 48 | // resp_arg->hd = req->handle; 49 | // resp_arg->fd = httpd_req_to_sockfd(req); 50 | // return httpd_queue_work(handle, ws_async_send, resp_arg); 51 | // } 52 | 53 | // typedef struct { 54 | // char *data; 55 | // async_resp_arg_t *resp_arg; 56 | // } async_data_arg_t; 57 | 58 | esp_err_t blinker_websocket_async_print(async_resp_arg_t *arg, const char *payload) 59 | { 60 | ESP_LOGI(TAG, "blinker_websocket_async_print: %s", payload); 61 | 62 | httpd_ws_frame_t ws_pkt; 63 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 64 | ws_pkt.payload = (uint8_t *)payload; 65 | ws_pkt.len = strlen(payload); 66 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 67 | 68 | esp_err_t err = httpd_ws_send_frame_async(arg->hd, arg->fd, &ws_pkt); 69 | 70 | return err; 71 | } 72 | 73 | esp_err_t blinker_websocket_print(httpd_req_t *req, const char *payload) 74 | { 75 | ESP_LOGI(TAG, "blinker_websocket_print: %s", payload); 76 | 77 | httpd_ws_frame_t ws_pkt; 78 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 79 | ws_pkt.payload = (uint8_t *)payload; 80 | ws_pkt.len = strlen(payload); 81 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 82 | 83 | esp_err_t err = httpd_ws_send_frame(req, &ws_pkt); 84 | 85 | return err; 86 | } 87 | 88 | // static void ws_async_send(void *arg) 89 | // { 90 | // async_data_arg_t *data_arg = arg; 91 | 92 | // ws_cb(data_arg->resp_arg, data_arg->data); 93 | // BLINKER_FREE(data_arg->resp_arg); 94 | // BLINKER_FREE(data_arg->data); 95 | // BLINKER_FREE(data_arg); 96 | // } 97 | 98 | static esp_err_t echo_handler(httpd_req_t *req) 99 | { 100 | uint8_t buf[128] = { 0 }; 101 | httpd_ws_frame_t ws_pkt; 102 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 103 | ws_pkt.payload = buf; 104 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 105 | esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 128); 106 | if (ret != ESP_OK) { 107 | ESP_LOGI(TAG, "httpd_ws_recv_frame failed with %d", ret); 108 | return ret; 109 | } 110 | ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload); 111 | ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type); 112 | 113 | if (ws_pkt.type == HTTPD_WS_TYPE_TEXT && ws_cb != NULL) { 114 | async_resp_arg_t *resp_arg = calloc(1, sizeof(async_resp_arg_t)); 115 | resp_arg->hd = req->handle; 116 | resp_arg->fd = httpd_req_to_sockfd(req); 117 | 118 | ws_cb(resp_arg, (char*)ws_pkt.payload); 119 | BLINKER_FREE(resp_arg); 120 | 121 | ret = ESP_OK; 122 | } 123 | return ret; 124 | } 125 | 126 | static esp_err_t ws_connect_handler(httpd_req_t *req) 127 | { 128 | char *buf = "{\"state\":\"connected\"}\n"; 129 | // httpd_ws_frame_t ws_pkt; 130 | // memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 131 | // ws_pkt.payload = (uint8_t *)buf; 132 | // ws_pkt.len = strlen(buf); 133 | // ws_pkt.type = HTTPD_WS_TYPE_TEXT; 134 | // httpd_ws_send_frame(req, &ws_pkt); 135 | blinker_websocket_print(req, buf); 136 | return ESP_OK; 137 | } 138 | 139 | static const httpd_uri_t ws = { 140 | .uri = "/", 141 | .method = HTTP_GET, 142 | .handler = echo_handler, 143 | .connect_cb = ws_connect_handler, 144 | .user_ctx = NULL, 145 | .is_websocket = true 146 | }; 147 | 148 | static httpd_handle_t start_webserver(void) 149 | { 150 | httpd_handle_t server = NULL; 151 | httpd_config_t config = HTTPD_DEFAULT_CONFIG(); 152 | config.server_port = BLINKER_WEBSOCKET_SERVER_PORT; 153 | 154 | // Start the httpd server 155 | ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); 156 | if (httpd_start(&server, &config) == ESP_OK) { 157 | // Registering the ws handler 158 | ESP_LOGI(TAG, "Registering URI handlers"); 159 | httpd_register_uri_handler(server, &ws); 160 | return server; 161 | } 162 | 163 | ESP_LOGI(TAG, "Error starting server!"); 164 | return NULL; 165 | } 166 | 167 | static void stop_webserver(httpd_handle_t server) 168 | { 169 | // Stop the httpd server 170 | httpd_stop(server); 171 | } 172 | 173 | static void disconnect_handler(void* arg, esp_event_base_t event_base, 174 | int32_t event_id, void* event_data) 175 | { 176 | httpd_handle_t* server = (httpd_handle_t*) arg; 177 | if (*server) { 178 | ESP_LOGI(TAG, "Stopping webserver"); 179 | stop_webserver(*server); 180 | *server = NULL; 181 | } 182 | } 183 | 184 | static void connect_handler(void* arg, esp_event_base_t event_base, 185 | int32_t event_id, void* event_data) 186 | { 187 | httpd_handle_t* server = (httpd_handle_t*) arg; 188 | if (*server == NULL) { 189 | ESP_LOGI(TAG, "Starting webserver"); 190 | *server = start_webserver(); 191 | } 192 | } 193 | 194 | esp_err_t blinker_websocket_server_init(blinker_websocket_data_cb_t cb) 195 | { 196 | static httpd_handle_t server = NULL; 197 | 198 | if (server == NULL) { 199 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); 200 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); 201 | 202 | server = start_webserver(); 203 | } 204 | 205 | ws_cb = cb; 206 | 207 | return ESP_OK; 208 | } 209 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRCS "src/httpd_main.c" 2 | "src/httpd_parse.c" 3 | "src/httpd_sess.c" 4 | "src/httpd_txrx.c" 5 | "src/httpd_uri.c" 6 | "src/httpd_ws.c" 7 | "src/util/ctrl_sock.c" 8 | INCLUDE_DIRS "include" 9 | PRIV_INCLUDE_DIRS "src/port/esp32" "src/util" 10 | REQUIRES nghttp # for http_parser.h 11 | PRIV_REQUIRES lwip mbedtls esp_timer) 12 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/Kconfig: -------------------------------------------------------------------------------- 1 | menu "HTTP Server" 2 | 3 | config HTTPD_MAX_REQ_HDR_LEN 4 | int "Max HTTP Request Header Length" 5 | default 512 6 | help 7 | This sets the maximum supported size of headers section in HTTP request packet to be processed by the 8 | server 9 | 10 | config HTTPD_MAX_URI_LEN 11 | int "Max HTTP URI Length" 12 | default 512 13 | help 14 | This sets the maximum supported size of HTTP request URI to be processed by the server 15 | 16 | config HTTPD_ERR_RESP_NO_DELAY 17 | bool "Use TCP_NODELAY socket option when sending HTTP error responses" 18 | default y 19 | help 20 | Using TCP_NODEALY socket option ensures that HTTP error response reaches the client before the 21 | underlying socket is closed. Please note that turning this off may cause multiple test failures 22 | 23 | config HTTPD_PURGE_BUF_LEN 24 | int "Length of temporary buffer for purging data" 25 | default 32 26 | help 27 | This sets the size of the temporary buffer used to receive and discard any remaining data that is 28 | received from the HTTP client in the request, but not processed as part of the server HTTP request 29 | handler. 30 | 31 | If the remaining data is larger than the available buffer size, the buffer will be filled in multiple 32 | iterations. The buffer should be small enough to fit on the stack, but large enough to avoid excessive 33 | iterations. 34 | 35 | config HTTPD_LOG_PURGE_DATA 36 | bool "Log purged content data at Debug level" 37 | default n 38 | help 39 | Enabling this will log discarded binary HTTP request data at Debug level. 40 | For large content data this may not be desirable as it will clutter the log. 41 | 42 | config HTTPD_WS_SUPPORT 43 | bool "WebSocket server support" 44 | default n 45 | help 46 | This sets the WebSocket server support. 47 | 48 | endmenu 49 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/component.mk: -------------------------------------------------------------------------------- 1 | 2 | COMPONENT_SRCDIRS := src src/util 3 | COMPONENT_ADD_INCLUDEDIRS := include 4 | COMPONENT_PRIV_INCLUDEDIRS := src/port/esp32 src/util 5 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/include/http_server.h: -------------------------------------------------------------------------------- 1 | #warning http_server.h has been renamed to esp_http_server.h, please update include directives 2 | #include "esp_http_server.h" 3 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/src/port/esp32/osal.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef _OSAL_H_ 16 | #define _OSAL_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #define OS_SUCCESS ESP_OK 29 | #define OS_FAIL ESP_FAIL 30 | 31 | typedef TaskHandle_t othread_t; 32 | 33 | static inline int httpd_os_thread_create(othread_t *thread, 34 | const char *name, uint16_t stacksize, int prio, 35 | void (*thread_routine)(void *arg), void *arg, 36 | BaseType_t core_id) 37 | { 38 | int ret = xTaskCreatePinnedToCore(thread_routine, name, stacksize, arg, prio, thread, core_id); 39 | if (ret == pdPASS) { 40 | return OS_SUCCESS; 41 | } 42 | return OS_FAIL; 43 | } 44 | 45 | /* Only self delete is supported */ 46 | static inline void httpd_os_thread_delete(void) 47 | { 48 | vTaskDelete(xTaskGetCurrentTaskHandle()); 49 | } 50 | 51 | static inline void httpd_os_thread_sleep(int msecs) 52 | { 53 | vTaskDelay(msecs / portTICK_RATE_MS); 54 | } 55 | 56 | static inline othread_t httpd_os_thread_handle(void) 57 | { 58 | return xTaskGetCurrentTaskHandle(); 59 | } 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif /* ! _OSAL_H_ */ 66 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/src/util/ctrl_sock.c: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "ctrl_sock.h" 23 | 24 | /* Control socket, because in some network stacks select can't be woken up any 25 | * other way 26 | */ 27 | int cs_create_ctrl_sock(int port) 28 | { 29 | int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 30 | if (fd < 0) { 31 | return -1; 32 | } 33 | 34 | int ret; 35 | struct sockaddr_in addr; 36 | memset(&addr, 0, sizeof(addr)); 37 | addr.sin_family = AF_INET; 38 | addr.sin_port = htons(port); 39 | inet_aton("127.0.0.1", &addr.sin_addr); 40 | ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); 41 | if (ret < 0) { 42 | close(fd); 43 | return -1; 44 | } 45 | return fd; 46 | } 47 | 48 | void cs_free_ctrl_sock(int fd) 49 | { 50 | close(fd); 51 | } 52 | 53 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len) 54 | { 55 | int ret; 56 | struct sockaddr_in to_addr; 57 | to_addr.sin_family = AF_INET; 58 | to_addr.sin_port = htons(port); 59 | inet_aton("127.0.0.1", &to_addr.sin_addr); 60 | ret = sendto(send_fd, data, data_len, 0, (struct sockaddr *)&to_addr, sizeof(to_addr)); 61 | 62 | if (ret < 0) { 63 | return -1; 64 | } 65 | return ret; 66 | } 67 | 68 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len) 69 | { 70 | int ret; 71 | ret = recvfrom(fd, data, data_len, 0, NULL, NULL); 72 | 73 | if (ret < 0) { 74 | return -1; 75 | } 76 | return ret; 77 | } 78 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/src/util/ctrl_sock.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * \file ctrl_sock.h 17 | * \brief Control Socket for select() wakeup 18 | * 19 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 20 | * sleeping on select. This is a common requirement for sending 21 | * control commands to a network server. This control socket API 22 | * facilitates the same. 23 | */ 24 | #ifndef _CTRL_SOCK_H_ 25 | #define _CTRL_SOCK_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /** 32 | * @brief Create a control socket 33 | * 34 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 35 | * sleeping on select. This is a common requirement for sending 36 | * control commands to a network server. This control socket API 37 | * facilitates the same. 38 | * 39 | * This API will create a UDP control socket on the specified port. It 40 | * will return a socket descriptor that can then be added to your 41 | * fd_set in select() 42 | * 43 | * @param[in] port the local port on which the control socket will listen 44 | * 45 | * @return - the socket descriptor that can be added to the fd_set in select. 46 | * - an error code if less than zero 47 | */ 48 | int cs_create_ctrl_sock(int port); 49 | 50 | /** 51 | * @brief Free the control socket 52 | * 53 | * This frees up the control socket that was earlier created using 54 | * cs_create_ctrl_sock() 55 | * 56 | * @param[in] fd the socket descriptor associated with this control socket 57 | */ 58 | void cs_free_ctrl_sock(int fd); 59 | 60 | /** 61 | * @brief Send data to control socket 62 | * 63 | * This API sends data to the control socket. If a server is blocked 64 | * on select() with the control socket, this call will wake up that 65 | * server. 66 | * 67 | * @param[in] send_fd the socket for sending ctrl messages 68 | * @param[in] port the port on which the control socket was created 69 | * @param[in] data pointer to a buffer that contains data to send on the socket 70 | * @param[in] data_len the length of the data contained in the buffer pointed to be data 71 | * 72 | * @return - the number of bytes sent to the control socket 73 | * - an error code if less than zero 74 | */ 75 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len); 76 | 77 | /** 78 | * @brief Receive data from control socket 79 | * 80 | * This API receives any data that was sent to the control 81 | * socket. This will be typically called from the server thread to 82 | * process any commands on this socket. 83 | * 84 | * @param[in] fd the socket descriptor of the control socket 85 | * @param[in] data pointer to a buffer that will be used to store 86 | * received from the control socket 87 | * @param[in] data_len the maximum length of the data that can be 88 | * stored in the buffer pointed by data 89 | * 90 | * @return - the number of bytes received from the control socket 91 | * - an error code if less than zero 92 | */ 93 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len); 94 | 95 | #ifdef __cplusplus 96 | } 97 | #endif 98 | 99 | #endif /* ! _CTRL_SOCK_H_ */ 100 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRC_DIRS "." 2 | PRIV_INCLUDE_DIRS "." 3 | PRIV_REQUIRES cmock test_utils esp_http_server) 4 | -------------------------------------------------------------------------------- /ESP32/v4.3/components/esp_http_server/test/component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive 2 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/CmakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRCS "src/api/blinker_api.c" 2 | "src/mqtt/blinker_mqtt.c" 3 | "src/provisioning/blinker_prov_apconfig.c" 4 | "src/provisioning/blinker_prov_smartconfig.c" 5 | "src/provisioning/blinker_wifi.c" 6 | "src/utils/blinker_button.c" 7 | "src/utils/blinker_http.c" 8 | "src/utils/blinker_mdns.c" 9 | "src/utils/blinker_reboot.c" 10 | "src/utils/blinker_storage.c" 11 | "src/utils/blinker_timesync.c" 12 | "src/utils/blinker_utils.c" 13 | "src/utils/blinker_ws.c" 14 | INCLUDE_DIRS "include" 15 | REQUIRES "mdns" "json" "mqtt" 16 | PRIV_REQUIRES nvs_flash esp_http_client esp_http_server app_update) -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_INCLUDEDIRS := include 2 | 3 | COMPONENT_SRCDIRS := src/api src/mqtt src/provisioning src/utils -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include "blinker_utils.h" 11 | 12 | #define BLINKER_FORMAT_QUEUE_TIME 200 13 | 14 | #define BLINKER_FORMAT_DATA_SIZE 1024 15 | 16 | #define BLINKER_MIN_TO_S 60 17 | 18 | #define BLINKER_MIN_TO_MS BLINKER_MIN_TO_S * 1000UL 19 | 20 | #define BLINKER_REGISTER_INTERVAL 20000U 21 | 22 | #define BLINKER_WEBSOCKET_SERVER_PORT 81 23 | 24 | #define BLINKER_MAX_TIME_INTERVAL 600 25 | 26 | #define BLINKER_MIN_TIME_INTERVAL 30 27 | 28 | #define BLINKER_MAX_TIMES_COUNT 10 29 | 30 | #define BLINKER_MIN_TIMES_COUNT 2 31 | 32 | #if defined CONFIG_BLINKER_CUSTOM_ESP 33 | 34 | #define BLINKER_REGISTER_TASK_DEEP 3 35 | 36 | #elif defined CONFIG_BLINKER_PRO_ESP 37 | 38 | #define BLINKER_REGISTER_TASK_DEEP 3 39 | #endif 40 | 41 | #define BLINKER_CMD_DETAIL "detail" 42 | 43 | #define BLINKER_CMD_AUTHKEY "authKey" 44 | 45 | #define BLINKER_CMD_REGISTER "register" 46 | 47 | #define BLINKER_CMD_MESSAGE "message" 48 | 49 | #define BLINKER_CMD_SUCCESS "success" 50 | 51 | #define BLINKER_CMD_REGISTER_SUCCESS "Registration successful" 52 | 53 | #define BLINKER_CMD_WIFI_CONFIG "wifi_config" 54 | 55 | #define BLINKER_CMD_DEVICE_NAME "deviceName" 56 | 57 | #define BLINKER_CMD_TO_DEVICE "toDevice" 58 | 59 | #define BLINKER_CMD_FROM_DEVICE "fromDevice" 60 | 61 | #define BLINKER_CMD_FROM "from" 62 | 63 | #define BLINKER_CMD_DEVICE_TYPE "deviceType" 64 | 65 | #define BLINKER_CMD_OWN_APP "OwnApp" 66 | 67 | #define BLINKER_CMD_VASSISTANT "vAssistant" 68 | 69 | #define BLINKER_CMD_SERVER_SENDER "ServerSender" 70 | 71 | #define BLINKER_CMD_SERVER_RECEIVER "ServerReceiver" 72 | 73 | #define BLINKER_CMD_MESSAGE_ID "messageId" 74 | 75 | #define BLINKER_CMD_USER_NAME "iotId" 76 | 77 | #define BLINKER_CMD_PASS_WORD "iotToken" 78 | 79 | #define BLINKER_CMD_PRODUCT_KEY "productKey" 80 | 81 | #define BLINKER_CMD_BROKER "broker" 82 | 83 | #define BLINKER_CMD_ALIYUN "aliyun" 84 | 85 | #define BLINKER_CMD_BLINKER "blinker" 86 | 87 | #define BLINKER_CMD_UUID "uuid" 88 | 89 | #define BLINKER_CMD_HOST "host" 90 | 91 | #define BLINKER_CMD_PORT "port" 92 | 93 | #define BLINKER_CMD_DATA "data" 94 | 95 | #define BLINKER_CMD_GET "get" 96 | 97 | #define BLINKER_CMD_SET "set" 98 | 99 | #define BLINKER_CMD_STATE "state" 100 | 101 | #define BLINKER_CMD_ONLINE "online" 102 | 103 | #define BLINKER_CMD_VERSION "version" 104 | 105 | #define BLINKER_CMD_BUILTIN_SWITCH "switch" 106 | 107 | #define BLINKER_CMD_SWITCH "swi" 108 | 109 | #define BLINKER_CMD_ICON "ico" 110 | 111 | #define BLINKER_CMD_COLOR "clr" 112 | 113 | #define BLINKER_CMD_COLOR_ "col" 114 | 115 | #define BLINKER_CMD_TEXT "tex" 116 | 117 | #define BLINKER_CMD_TEXT1 "tex1" 118 | 119 | #define BLINKER_CMD_TEXT_COLOR "tco" 120 | 121 | #define BLINKER_CMD_VALUE "val" 122 | 123 | #define BLINKER_CMD_IMAGE "img" 124 | 125 | #define BLINKER_CMD_UNIT "uni" 126 | 127 | #define BLINKER_CMD_ALIGENIE "AliGenie" 128 | 129 | #define BLINKER_CMD_DUEROS "DuerOS" 130 | 131 | #define BLINKER_CMD_MIOT "MIOT" 132 | 133 | #define BLINKER_CMD_POWER_STATE "pState" 134 | 135 | #define BLINKER_CMD_TRUE "true" 136 | 137 | #define BLINKER_CMD_FALSE "false" 138 | 139 | #define BLINKER_CMD_ON "on" 140 | 141 | #define BLINKER_CMD_OFF "off" 142 | 143 | #define BLINKER_CMD_MODE "mode" 144 | 145 | #define BLINKER_CMD_COLOR_TEMP "colTemp" 146 | 147 | #define BLINKER_CMD_COLOR_TEMP_UP "upColTemp" 148 | 149 | #define BLINKER_CMD_COLOR_TEMP_DOWN "downColTemp" 150 | 151 | #define BLINKER_CMD_BRIGHTNESS "bright" 152 | 153 | #define BLINKER_CMD_BRIGHTNESS_UP "upBright" 154 | 155 | #define BLINKER_CMD_BRIGHTNESS_DOWN "downBright" 156 | 157 | #define BLINKER_CMD_TEMP "temp" 158 | 159 | #define BLINKER_CMD_TEMP_UP "upTemp" 160 | 161 | #define BLINKER_CMD_TEMP_DOWN "downTemp" 162 | 163 | #define BLINKER_CMD_HUMI "humi" 164 | 165 | #define BLINKER_CMD_LEVEL "level" 166 | 167 | #define BLINKER_CMD_LEVEL_UP "upLevel" 168 | 169 | #define BLINKER_CMD_LEVEL_DOWN "downLevel" 170 | 171 | #define BLINKER_CMD_HSTATE "hsState" 172 | 173 | #define BLINKER_CMD_VSTATE "vsState" 174 | 175 | #define BLINKER_CMD_PM25 "pm25" 176 | 177 | #define BLINKER_CMD_PM10 "pm10" 178 | 179 | #define BLINKER_CMD_CO2 "co2" 180 | 181 | #define BLINKER_CMD_AQI "aqi" 182 | 183 | #define BLINKER_CMD_ECO "eco" 184 | 185 | #define BLINKER_CMD_ANION "anion" 186 | 187 | #define BLINKER_CMD_HEATER "heater" 188 | 189 | #define BLINKER_CMD_DRYER "dryer" 190 | 191 | #define BLINKER_CMD_SOFT "soft" 192 | 193 | #define BLINKER_CMD_UV "uv" 194 | 195 | #define BLINKER_CMD_UNSB "unsb" 196 | 197 | #define BLINKER_CMD_NUM "num" 198 | 199 | #define BLINKER_CMD_TIME "time" 200 | 201 | #define BLINKER_CMD_SSID "ssid" 202 | 203 | #define BLINKER_CMD_PASSWORD "pswd" 204 | 205 | #if defined(CONFIG_BLINKER_WITH_SSL) 206 | 207 | #define BLINKER_PROTOCOL_MQTT "mqtts" 208 | 209 | #define BLINKER_PROTPCOL_HTTP "https" 210 | 211 | #else 212 | 213 | #define BLINKER_PROTOCOL_MQTT "mqtt" 214 | 215 | #define BLINKER_PROTPCOL_HTTP "http" 216 | 217 | #endif 218 | 219 | #ifdef __cplusplus 220 | } 221 | #endif -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "esp_http_client.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config); 13 | 14 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url); 15 | 16 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value); 17 | 18 | esp_err_t blinker_http_get(esp_http_client_handle_t client); 19 | 20 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data); 21 | 22 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len); 23 | 24 | esp_err_t blinker_http_close(esp_http_client_handle_t client); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_mqtt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | typedef enum { 13 | BLINKER_BROKER_ALIYUN = 0, 14 | BLINKER_BROKER_BLINKER, 15 | } blinker_mqtt_broker_t; 16 | 17 | typedef struct { 18 | char *devicename; 19 | char *authkey; 20 | char *client_id; 21 | char *username; 22 | char *password; 23 | char *productkey; 24 | char *uuid; 25 | char *uri; 26 | int port; 27 | blinker_mqtt_broker_t broker; 28 | } blinker_mqtt_config_t; 29 | 30 | typedef void (*blinker_mqtt_subscribe_cb_t)(const char *topic, size_t topic_len, void *payload, size_t payload_len); 31 | 32 | blinker_mqtt_broker_t blinker_mqtt_broker(void); 33 | 34 | const char *blinker_mqtt_devicename(void); 35 | 36 | const char *blinker_mqtt_token(void); 37 | 38 | const char *blinker_mqtt_uuid(void); 39 | 40 | const char *blinker_mqtt_product_key(void); 41 | 42 | const char *blinker_mqtt_authkey(void); 43 | 44 | esp_err_t blinker_mqtt_subscribe(const char *topic, blinker_mqtt_subscribe_cb_t cb); 45 | 46 | esp_err_t blinker_mqtt_unsubscribe(const char *topic); 47 | 48 | esp_err_t blinker_mqtt_publish(const char *topic, const char *data, size_t data_len); 49 | 50 | esp_err_t blinker_mqtt_connect(void); 51 | 52 | esp_err_t blinker_mqtt_disconnect(void); 53 | 54 | esp_err_t blinker_mqtt_init(blinker_mqtt_config_t *config); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_prov_apconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_apconfig_init(void); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_prov_smartconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_smartconfig_init(void); 11 | 12 | esp_err_t blinker_prov_smartconfig_stop(void); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_storage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_storage_init(void); 11 | 12 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length); 13 | 14 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length); 15 | 16 | esp_err_t blinker_storage_erase(const char *key); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: your name 3 | * @Date: 2021-07-02 13:32:25 4 | * @LastEditTime: 2021-07-22 12:22:52 5 | * @LastEditors: Please set LastEditors 6 | * @Description: In User Settings Edit 7 | * @FilePath: \ESP8266_RTOS_SDK\0524\smart_config\examples\components\blinker\include\blinker_utils.h 8 | */ 9 | #pragma once 10 | 11 | #include 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define BLINKER_FREE(ptr) { \ 18 | if (ptr) { \ 19 | free(ptr); \ 20 | ptr = NULL; \ 21 | } \ 22 | } 23 | 24 | #define BLINKER_TS_NUM(num) data##num 25 | 26 | void blinker_log_print_heap(void); 27 | 28 | void blinker_mac_device_name(char *name); 29 | 30 | time_t blinker_time(void); 31 | 32 | esp_err_t blinker_timesync_start(void); 33 | 34 | esp_err_t blinker_mdns_init(const char *host_name); 35 | 36 | esp_err_t blinker_mdns_free(void); 37 | 38 | int blinker_reboot_unbroken_count(void); 39 | 40 | int blinker_reboot_total_count(void); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_wifi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config); 11 | 12 | esp_err_t blinker_wifi_reset(void); 13 | 14 | esp_err_t blinker_wifi_init(void); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/include/blinker_ws.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include 11 | 12 | typedef struct { 13 | httpd_handle_t hd; 14 | int fd; 15 | } async_resp_arg_t; 16 | 17 | typedef void (*blinker_websocket_data_cb_t)(async_resp_arg_t *req, const char *payload); 18 | 19 | esp_err_t blinker_websocket_async_print(async_resp_arg_t *arg, const char *payload); 20 | 21 | esp_err_t blinker_websocket_print(httpd_req_t *req, const char *payload); 22 | 23 | esp_err_t blinker_websocket_server_init(blinker_websocket_data_cb_t cb); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/provisioning/blinker_prov_apconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "freertos/event_groups.h" 5 | #include "esp_system.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | 10 | #include "blinker_prov_apconfig.h" 11 | 12 | static const char *TAG = "blinker_prov_apconfig"; 13 | 14 | static void wifi_event_handler(void* arg, esp_event_base_t event_base, 15 | int32_t event_id, void* event_data) 16 | { 17 | if (event_id == WIFI_EVENT_AP_STACONNECTED) { 18 | wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; 19 | ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", 20 | MAC2STR(event->mac), event->aid); 21 | } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { 22 | wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; 23 | ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", 24 | MAC2STR(event->mac), event->aid); 25 | } 26 | } 27 | 28 | esp_err_t blinker_prov_apconfig_init(void) 29 | { 30 | #ifndef CONFIG_IDF_TARGET_ESP8266 31 | esp_netif_create_default_wifi_ap(); 32 | #endif 33 | 34 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); 35 | 36 | uint8_t mac[6] = {0}; 37 | char mac_str[31] = { 0 }; 38 | ESP_ERROR_CHECK(esp_wifi_get_mac(WIFI_IF_STA, mac)); 39 | #if defined CONFIG_BLINKER_CUSTOM_ESP 40 | sprintf(mac_str, "DiyArduino_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 41 | #elif defined CONFIG_BLINKER_PRO_ESP 42 | sprintf(mac_str, "%s_%02X%02X%02X%02X%02X%02X", CONFIG_BLINKER_TYPE_KEY,mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 43 | #endif 44 | 45 | wifi_config_t wifi_config = { 46 | .ap = { 47 | .ssid_len = strlen(mac_str), 48 | .max_connection = 4, 49 | .channel = 1, 50 | .authmode = WIFI_AUTH_OPEN 51 | }, 52 | }; 53 | strcpy((char *)wifi_config.ap.ssid, mac_str); 54 | 55 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); 56 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); 57 | ESP_ERROR_CHECK(esp_wifi_start()); 58 | 59 | ESP_LOGI(TAG, "apconfig start"); 60 | 61 | return ESP_OK; 62 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/provisioning/blinker_prov_smartconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | #include "esp_smartconfig.h" 11 | #if CONFIG_IDF_TARGET_ESP8266 12 | #include "tcpip_adapter.h" 13 | #include "smartconfig_ack.h" 14 | #else 15 | #include "esp_netif.h" 16 | #endif 17 | #include "blinker_prov_smartconfig.h" 18 | 19 | static EventGroupHandle_t s_wifi_event_group; 20 | 21 | static const int CONNECTED_BIT = BIT0; 22 | static bool b_prov_smartconfig_start = false; 23 | static const char* TAG = "blinker_prov_smartconfig"; 24 | static void blinker_prov_smartconfig_start(void); 25 | 26 | static void event_handler(void* arg, esp_event_base_t event_base, 27 | int32_t event_id, void* event_data) 28 | { 29 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 30 | // xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL); 31 | // blinker_prov_smartconfig_start(); 32 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 33 | esp_wifi_connect(); 34 | // xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); 35 | // wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 36 | // ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 37 | // if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 38 | // ESP_LOGE(TAG, "wrong password, need reset device"); 39 | // // esp_wifi_disconnect(); 40 | // // blinker_prov_smartconfig_stop(); 41 | // // vTaskDelay(10); 42 | // // blinker_prov_smartconfig_start(); 43 | // // return; 44 | // } 45 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 46 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 47 | #if CONFIG_IDF_TARGET_ESP8266 48 | ESP_LOGI(TAG, "smartconfig connected, got ip:%s", 49 | ip4addr_ntoa(&event->ip_info.ip)); 50 | #else 51 | ESP_LOGI(TAG, "smartconfig connected, got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 52 | #endif 53 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) { 54 | ESP_LOGI(TAG, "Scan done"); 55 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) { 56 | ESP_LOGI(TAG, "Found channel"); 57 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { 58 | ESP_LOGI(TAG, "Got SSID and password"); 59 | 60 | smartconfig_event_got_ssid_pswd_t* evt = (smartconfig_event_got_ssid_pswd_t*)event_data; 61 | wifi_config_t wifi_config; 62 | uint8_t ssid[33] = { 0 }; 63 | uint8_t password[65] = { 0 }; 64 | uint8_t rvd_data[33] = { 0 }; 65 | 66 | bzero(&wifi_config, sizeof(wifi_config_t)); 67 | memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid)); 68 | memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password)); 69 | wifi_config.sta.bssid_set = evt->bssid_set; 70 | 71 | if (wifi_config.sta.bssid_set == true) { 72 | memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid)); 73 | } 74 | 75 | memcpy(ssid, evt->ssid, sizeof(evt->ssid)); 76 | memcpy(password, evt->password, sizeof(evt->password)); 77 | ESP_LOGI(TAG, "SSID:%s", ssid); 78 | ESP_LOGI(TAG, "PASSWORD:%s", password); 79 | if (evt->type == SC_TYPE_ESPTOUCH_V2) { 80 | ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) ); 81 | ESP_LOGI(TAG, "RVD_DATA:%s", rvd_data); 82 | } 83 | 84 | // ESP_ERROR_CHECK(esp_wifi_disconnect()); 85 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); 86 | ESP_ERROR_CHECK(esp_wifi_connect()); 87 | xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); 88 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) { 89 | // xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); 90 | 91 | blinker_prov_smartconfig_stop(); 92 | ESP_LOGI(TAG, "blinker_prov_smartconfig_stop"); 93 | } 94 | } 95 | 96 | esp_err_t blinker_prov_smartconfig_init(void) 97 | { 98 | s_wifi_event_group = xEventGroupCreate(); 99 | 100 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 101 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 102 | ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 103 | 104 | ESP_ERROR_CHECK(esp_wifi_start()); 105 | 106 | blinker_prov_smartconfig_start(); 107 | 108 | xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, true, false, portMAX_DELAY); 109 | 110 | return ESP_OK; 111 | } 112 | 113 | static void blinker_prov_smartconfig_start(void) 114 | { 115 | b_prov_smartconfig_start = true; 116 | #if defined CONFIG_BLINKER_SMART_CONFIG 117 | ESP_ERROR_CHECK(esp_smartconfig_set_type(CONFIG_BLINKER_PROV_SMARTCONFIG_TYPE)); 118 | smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); 119 | ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); 120 | #endif 121 | } 122 | 123 | esp_err_t blinker_prov_smartconfig_stop(void) 124 | { 125 | esp_err_t err = ESP_FAIL; 126 | 127 | if (b_prov_smartconfig_start) { 128 | err = esp_smartconfig_stop(); 129 | 130 | b_prov_smartconfig_start = false; 131 | } 132 | 133 | return err; 134 | } 135 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/provisioning/blinker_wifi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | 11 | #ifndef CONFIG_IDF_TARGET_ESP8266 12 | #include "esp_netif.h" 13 | #endif 14 | 15 | #include "blinker_wifi.h" 16 | #include "blinker_config.h" 17 | #include "blinker_storage.h" 18 | 19 | static const char* TAG = "blinker_wifi"; 20 | static const int CONNECTED_BIT = BIT0; 21 | static EventGroupHandle_t b_wifi_event_group = NULL; 22 | 23 | static void event_handler(void* arg, esp_event_base_t event_base, 24 | int32_t event_id, void* event_data) 25 | { 26 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 27 | esp_wifi_connect(); 28 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 29 | esp_wifi_connect(); 30 | 31 | wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 32 | ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 33 | if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 34 | ESP_LOGE(TAG, "wrong password"); 35 | vTaskDelay(pdMS_TO_TICKS(2000)); 36 | } 37 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 38 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 39 | #if CONFIG_IDF_TARGET_ESP8266 40 | ESP_LOGI(TAG, "got ip:%s", ip4addr_ntoa(&event->ip_info.ip)); 41 | #else 42 | ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 43 | #endif 44 | xEventGroupSetBits(b_wifi_event_group, CONNECTED_BIT); 45 | } 46 | } 47 | 48 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config) 49 | { 50 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, (wifi_config_t *)wifi_config) ); 51 | ESP_ERROR_CHECK(esp_wifi_start()); 52 | 53 | xEventGroupWaitBits(b_wifi_event_group, CONNECTED_BIT, 54 | pdFALSE, 55 | pdFALSE, 56 | portMAX_DELAY); 57 | return ESP_OK; 58 | } 59 | 60 | esp_err_t blinker_wifi_reset(void) 61 | { 62 | esp_err_t err = ESP_FAIL; 63 | 64 | err = esp_wifi_restore(); 65 | 66 | err = blinker_storage_erase(BLINKER_CMD_WIFI_CONFIG); 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_wifi_init(void) 72 | { 73 | #if CONFIG_IDF_TARGET_ESP8266 74 | tcpip_adapter_init(); 75 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 76 | #else 77 | esp_netif_init(); 78 | 79 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 80 | esp_netif_create_default_wifi_sta(); 81 | #endif 82 | 83 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 84 | 85 | ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 86 | #if CONFIG_IDF_TARGET_ESP8266 87 | ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); 88 | #endif 89 | b_wifi_event_group = xEventGroupCreate(); 90 | 91 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 92 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 93 | 94 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); 95 | 96 | return ESP_OK; 97 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_http.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "esp_log.h" 6 | #include "esp_system.h" 7 | #include "esp_netif.h" 8 | #include "esp_tls.h" 9 | 10 | #include "blinker_http.h" 11 | 12 | static const char *TAG = "blinker_http"; 13 | 14 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config) 15 | { 16 | return esp_http_client_init(config); 17 | } 18 | 19 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url) 20 | { 21 | return esp_http_client_set_url(client, url); 22 | } 23 | 24 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value) 25 | { 26 | return esp_http_client_set_header(client, key, value); 27 | } 28 | 29 | esp_err_t blinker_http_get(esp_http_client_handle_t client) 30 | { 31 | esp_err_t err = ESP_FAIL; 32 | 33 | esp_http_client_set_method(client, HTTP_METHOD_GET); 34 | 35 | err = esp_http_client_open(client, 0); 36 | 37 | if (err != ESP_OK) { 38 | ESP_LOGI(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); 39 | } else { 40 | int content_length = esp_http_client_fetch_headers(client); 41 | if (content_length < 0) { 42 | ESP_LOGI(TAG, "HTTP client fetch headers failed"); 43 | err = ESP_FAIL; 44 | } else { 45 | err = ESP_OK; 46 | } 47 | } 48 | 49 | return err; 50 | } 51 | 52 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data) 53 | { 54 | esp_http_client_set_method(client, HTTP_METHOD_POST); 55 | 56 | ESP_LOGI(TAG, "HTTP POST: %s", post_data); 57 | 58 | esp_http_client_set_post_field(client, post_data, strlen(post_data)); 59 | esp_err_t err = esp_http_client_perform(client); 60 | if (err == ESP_OK) { 61 | ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d", 62 | esp_http_client_get_status_code(client), 63 | esp_http_client_get_content_length(client)); 64 | } else { 65 | ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); 66 | } 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len) 72 | { 73 | int data_read = esp_http_client_read_response(client, buffer, len); 74 | 75 | if (data_read >= 0) { 76 | ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", 77 | esp_http_client_get_status_code(client), 78 | esp_http_client_get_content_length(client)); 79 | // ESP_LOG_BUFFER_HEX(TAG, buffer, strlen(buffer)); 80 | 81 | return ESP_OK; 82 | } else { 83 | ESP_LOGI(TAG, "Failed to read response"); 84 | 85 | return ESP_FAIL; 86 | } 87 | } 88 | 89 | esp_err_t blinker_http_close(esp_http_client_handle_t client) 90 | { 91 | esp_http_client_close(client); 92 | esp_http_client_cleanup(client); 93 | 94 | return ESP_OK; 95 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_mdns.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "esp_system.h" 5 | #include "esp_wifi.h" 6 | #include "esp_log.h" 7 | #include "mdns.h" 8 | #include 9 | #include 10 | #include "blinker_utils.h" 11 | 12 | // static const char *TAG = "blinker_mdns"; 13 | 14 | static bool m_init = false; 15 | 16 | esp_err_t blinker_mdns_init(const char *host_name) 17 | { 18 | // static bool m_init = false; 19 | if (m_init) { 20 | blinker_mdns_free(); 21 | } 22 | 23 | m_init = true; 24 | 25 | char mac_name[13] = {0}; 26 | blinker_mac_device_name(mac_name); 27 | 28 | ESP_ERROR_CHECK(mdns_init()); 29 | ESP_ERROR_CHECK(mdns_hostname_set(host_name)); 30 | ESP_ERROR_CHECK(mdns_instance_name_set(host_name)); 31 | 32 | mdns_txt_item_t serviceTxtData[2] = { 33 | {"deviceName", host_name}, 34 | {"mac", mac_name} 35 | }; 36 | 37 | ESP_ERROR_CHECK(mdns_service_add(host_name, "_blinker", "_tcp", 81, serviceTxtData, 2)); 38 | 39 | return ESP_OK; 40 | } 41 | 42 | esp_err_t blinker_mdns_free(void) 43 | { 44 | if (m_init) { 45 | m_init = false; 46 | 47 | mdns_free(); 48 | } 49 | 50 | return ESP_OK; 51 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_reboot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "freertos/FreeRTOS.h" 7 | #include "freertos/timers.h" 8 | 9 | #include 10 | #include 11 | #include "esp_sntp.h" 12 | #include "esp_log.h" 13 | #include "esp_partition.h" 14 | #include "esp_ota_ops.h" 15 | 16 | #include "blinker_utils.h" 17 | #include "blinker_storage.h" 18 | 19 | #if CONFIG_IDF_TARGET_ESP8266 20 | #include "esp8266/rtc_register.h" 21 | #elif CONFIG_IDF_TARGET_ESP32 22 | #include "esp32/rom/rtc.h" 23 | #elif CONFIG_IDF_TARGET_ESP32S2 24 | #include "esp32s2/rom/rtc.h" 25 | #elif CONFIG_IDF_TARGET_ESP32C3 26 | #include "esp32c3/rom/rtc.h" 27 | #endif 28 | 29 | #define RTC_RESET_SW_CAUSE_REG RTC_STORE0 30 | #define RTC_RESET_HW_CAUSE_REG RTC_STATE1 31 | #define RTC_WAKEUP_HW_CAUSE_REG RTC_STATE2 32 | 33 | #define RTC_RESET_HW_CAUSE_LSB 0 34 | #define RTC_RESET_HW_CAUSE_MSB 3 35 | 36 | #define REBOOT_RECORD_KEY "reboot_record" 37 | #define CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY (ESP_TASK_MAIN_PRIO + 1) 38 | 39 | typedef struct { 40 | size_t total_count; 41 | size_t unbroken_count; 42 | RESET_REASON reason; 43 | } blinker_reboot_record_t; 44 | 45 | static const char *TAG = "blinker_reboot"; 46 | static blinker_reboot_record_t g_reboot_record = {0}; 47 | 48 | static uint32_t esp_rtc_get_reset_reason(void) 49 | { 50 | #if CONFIG_IDF_TARGET_ESP8266 51 | return GET_PERI_REG_BITS(RTC_RESET_HW_CAUSE_REG, RTC_RESET_HW_CAUSE_MSB, RTC_RESET_HW_CAUSE_LSB); 52 | #else 53 | return rtc_get_reset_reason(0); 54 | #endif 55 | } 56 | 57 | static void esp_reboot_count_erase_timercb(void *priv) 58 | { 59 | g_reboot_record.unbroken_count = 0; 60 | blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 61 | 62 | ESP_LOGI(TAG, "Erase reboot count"); 63 | } 64 | 65 | static esp_err_t blinker_reboot_unbroken_init(void) 66 | { 67 | esp_err_t err = ESP_OK; 68 | g_reboot_record.reason = esp_rtc_get_reset_reason(); 69 | 70 | blinker_storage_init(); 71 | blinker_storage_get(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 72 | 73 | g_reboot_record.total_count++; 74 | 75 | /**< If the device reboots within the instruction time, 76 | the event_mdoe value will be incremented by one */ 77 | #if CONFIG_IDF_TARGET_ESP8266 78 | if (g_reboot_record.reason != DEEPSLEEP_RESET) { 79 | #else 80 | if (g_reboot_record.reason != DEEPSLEEP_RESET && g_reboot_record.reason != RTCWDT_BROWN_OUT_RESET) { 81 | #endif 82 | g_reboot_record.unbroken_count++; 83 | ESP_LOGI(TAG, "reboot unbroken count: %d", g_reboot_record.unbroken_count); 84 | } else { 85 | g_reboot_record.unbroken_count = 1; 86 | ESP_LOGI(TAG, "reboot reason: %d", g_reboot_record.reason); 87 | } 88 | 89 | err = blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 90 | 91 | esp_timer_handle_t time_handle = NULL; 92 | esp_timer_create_args_t timer_cfg = { 93 | .name = "reboot_count_erase", 94 | .callback = esp_reboot_count_erase_timercb, 95 | .dispatch_method = ESP_TIMER_TASK, 96 | }; 97 | 98 | err = esp_timer_create(&timer_cfg, &time_handle); 99 | err = esp_timer_start_once(time_handle, CONFIG_BLINKER_REBOOT_UNBROKEN_INTERVAL_TIMEOUT * 1000UL); 100 | 101 | return err; 102 | } 103 | 104 | static void reboot_unbroken_record_task(void *arg) 105 | { 106 | blinker_reboot_unbroken_init(); 107 | 108 | if (CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT && 109 | blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT) { 110 | // esp_ota_mark_app_invalid_rollback_and_reboot(); 111 | } 112 | 113 | if (blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT_RESET) { 114 | blinker_storage_erase(CONFIG_BLINKER_NVS_NAMESPACE); 115 | } 116 | 117 | ESP_LOGI(TAG, "version_fallback_task exit"); 118 | 119 | vTaskDelete(NULL); 120 | } 121 | 122 | static void __attribute__((constructor)) blinker_reboot_unbroken_record(void) 123 | { 124 | xTaskCreate(reboot_unbroken_record_task, 125 | "reboot_unbroken_record", 126 | 4 * 1024, 127 | NULL, 128 | CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY, 129 | NULL); 130 | } 131 | 132 | int blinker_reboot_unbroken_count(void) 133 | { 134 | ESP_LOGI(TAG, "blinker_reboot_unbroken_count: %d", g_reboot_record.unbroken_count); 135 | return g_reboot_record.unbroken_count; 136 | } 137 | 138 | int blinker_reboot_total_count(void) 139 | { 140 | return g_reboot_record.total_count; 141 | } 142 | 143 | // bool esp_qcloud_reboot_is_exception(bool erase_coredump) 144 | // { 145 | // esp_err_t ret = ESP_OK; 146 | // ssize_t coredump_len = 0; 147 | 148 | // const esp_partition_t *coredump_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 149 | // ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL); 150 | // ESP_QCLOUD_ERROR_CHECK(!coredump_part, false, "<%s> esp_partition_get fail", esp_err_to_name(ret)); 151 | 152 | // ret = esp_partition_read(coredump_part, 0, &coredump_len, sizeof(ssize_t)); 153 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_read fail", esp_err_to_name(ret)); 154 | 155 | // if (coredump_len <= 0) { 156 | // return false; 157 | // } 158 | 159 | // if (erase_coredump) { 160 | // /**< erase all coredump partition */ 161 | // ret = esp_partition_erase_range(coredump_part, 0, coredump_part->size); 162 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_erase_range fail", esp_err_to_name(ret)); 163 | // } 164 | 165 | // return true; 166 | // } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_storage.c: -------------------------------------------------------------------------------- 1 | #include "string.h" 2 | #include "stdio.h" 3 | #include "stdlib.h" 4 | #include "esp_log.h" 5 | #include "esp_system.h" 6 | 7 | #include "nvs.h" 8 | #include "nvs_flash.h" 9 | 10 | #include "blinker_storage.h" 11 | 12 | static const char *TAG = "blinker_storage"; 13 | 14 | esp_err_t blinker_storage_init(void) 15 | { 16 | static bool init_flag = false; 17 | 18 | if (!init_flag) { 19 | esp_err_t ret = nvs_flash_init(); 20 | 21 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {// || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { 22 | // NVS partition was truncated and needs to be erased 23 | // Retry nvs_flash_init 24 | ESP_ERROR_CHECK(nvs_flash_erase()); 25 | ret = nvs_flash_init(); 26 | } 27 | 28 | ESP_ERROR_CHECK(ret); 29 | 30 | init_flag = true; 31 | } 32 | 33 | return ESP_OK; 34 | } 35 | 36 | esp_err_t blinker_storage_erase(const char *key) 37 | { 38 | ESP_LOGI(TAG, "blinker_storage_erase: %s", key); 39 | 40 | esp_err_t ret = ESP_OK; 41 | nvs_handle handle = 0; 42 | 43 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 44 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 45 | 46 | /** 47 | * @brief If key is CONFIG_BLINKER_NVS_NAMESPACE, erase all info in CONFIG_BLINKER_NVS_NAMESPACE 48 | */ 49 | if (!strcmp(key, CONFIG_BLINKER_NVS_NAMESPACE)) { 50 | ret = nvs_erase_all(handle); 51 | } else { 52 | ret = nvs_erase_key(handle, key); 53 | } 54 | 55 | /**< Write any pending changes to non-volatile storage */ 56 | nvs_commit(handle); 57 | 58 | /**< Close the storage handle and free any allocated resources */ 59 | nvs_close(handle); 60 | 61 | return ret; 62 | } 63 | 64 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length) 65 | { 66 | esp_err_t ret = ESP_OK; 67 | nvs_handle handle = 0; 68 | 69 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 70 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 71 | 72 | /**< set variable length binary value for given key */ 73 | ret = nvs_set_blob(handle, key, value, length); 74 | 75 | /**< Write any pending changes to non-volatile storage */ 76 | nvs_commit(handle); 77 | 78 | /**< Close the storage handle and free any allocated resources */ 79 | nvs_close(handle); 80 | 81 | return ret; 82 | } 83 | 84 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length) 85 | { 86 | esp_err_t ret = ESP_OK; 87 | nvs_handle handle = 0; 88 | 89 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 90 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 91 | 92 | /**< get variable length binary value for given key */ 93 | ret = nvs_get_blob(handle, key, value, &length); 94 | 95 | /**< Close the storage handle and free any allocated resources */ 96 | nvs_close(handle); 97 | 98 | if (ret == ESP_ERR_NVS_NOT_FOUND) { 99 | ESP_LOGI(TAG, " Get value for given key, key: %s", key); 100 | return ESP_ERR_NVS_NOT_FOUND; 101 | } 102 | 103 | return ESP_OK; 104 | } 105 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_timesync.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_log.h" 7 | #include "lwip/apps/sntp.h" 8 | #include "blinker_utils.h" 9 | 10 | #define REF_TIME 1577808000 /* 2020-01-01 00:00:00 */ 11 | #define DEFAULT_TICKS (2000 / portTICK_PERIOD_MS) /* 2 seconds in ticks */ 12 | #define CONFIG_BLINKER_SNTP_SERVER_NAME "pool.ntp.org" 13 | 14 | static const char *TAG = "blinker_timesync"; 15 | // static const int NTP_DONE_BIT = BIT0; 16 | static bool g_init_done = false; 17 | // static EventGroupHandle_t b_ntp_event_group = NULL; 18 | 19 | time_t blinker_time(void) 20 | { 21 | time_t now; 22 | time(&now); 23 | 24 | return now; 25 | } 26 | 27 | static bool blinker_timesync_check(void) 28 | { 29 | time_t now; 30 | time(&now); 31 | 32 | if (now > REF_TIME) { 33 | return true; 34 | } 35 | 36 | return false; 37 | } 38 | 39 | static void blinker_timesync_task(void *arg) 40 | { 41 | if (!g_init_done) { 42 | ESP_LOGW(TAG, "Time sync not initialised using 'esp_qcloud_timesync_start'"); 43 | } 44 | 45 | for(;;) { 46 | if (blinker_timesync_check() == true) { 47 | break; 48 | } 49 | 50 | // ESP_LOGI(TAG, "Time not synchronized yet. Retrying..."); 51 | vTaskDelay(DEFAULT_TICKS); 52 | } 53 | 54 | struct tm timeinfo; 55 | char strftime_buf[64]; 56 | time_t now; 57 | time(&now); 58 | localtime_r(&now, &timeinfo); 59 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); 60 | ESP_LOGI(TAG, "The current UTC time is: %s", strftime_buf); 61 | // xEventGroupSetBits(b_ntp_event_group, NTP_DONE_BIT); 62 | 63 | vTaskDelete(NULL); 64 | } 65 | 66 | esp_err_t blinker_timesync_start(void) 67 | { 68 | if (sntp_enabled()) { 69 | ESP_LOGI(TAG, "SNTP already initialized."); 70 | g_init_done = true; 71 | return ESP_OK; 72 | } 73 | 74 | // b_ntp_event_group = xEventGroupCreate(); 75 | 76 | char *sntp_server_name = CONFIG_BLINKER_SNTP_SERVER_NAME; 77 | 78 | ESP_LOGI(TAG, "Initializing SNTP. Using the SNTP server: %s", sntp_server_name); 79 | sntp_setoperatingmode(SNTP_OPMODE_POLL); 80 | sntp_setservername(0, sntp_server_name); 81 | setenv("TZ", "CST-8", 1); 82 | tzset(); 83 | sntp_init(); 84 | g_init_done = true; 85 | 86 | xTaskCreate(blinker_timesync_task, 87 | "blinker_timesync", 88 | 2 * 1024, 89 | NULL, 90 | 6, 91 | NULL); 92 | 93 | // xEventGroupWaitBits(b_ntp_event_group, NTP_DONE_BIT, 94 | // pdFALSE, 95 | // pdFALSE, 96 | // portMAX_DELAY); 97 | 98 | return ESP_OK; 99 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/Blinker/src/utils/blinker_utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include "esp_wifi.h" 11 | 12 | #include "esp_heap_caps.h" 13 | 14 | #include "blinker_utils.h" 15 | 16 | static const char *TAG = "blinker_utils"; 17 | 18 | void blinker_log_print_heap(void) 19 | { 20 | ESP_LOGI(TAG, "Free heap, current: %d", esp_get_free_heap_size()); 21 | } 22 | 23 | void blinker_mac_device_name(char *name) 24 | { 25 | uint8_t mac[6] = { 0 }; 26 | esp_wifi_get_mac(WIFI_IF_STA, mac); 27 | sprintf(name, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 28 | } -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRCS "src/httpd_main.c" 2 | "src/httpd_parse.c" 3 | "src/httpd_sess.c" 4 | "src/httpd_txrx.c" 5 | "src/httpd_uri.c" 6 | "src/httpd_ws.c" 7 | "src/util/ctrl_sock.c" 8 | INCLUDE_DIRS "include" 9 | PRIV_INCLUDE_DIRS "src/port/esp32" "src/util" 10 | REQUIRES nghttp # for http_parser.h 11 | PRIV_REQUIRES lwip mbedtls esp_timer) 12 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/Kconfig: -------------------------------------------------------------------------------- 1 | menu "HTTP Server" 2 | 3 | config HTTPD_MAX_REQ_HDR_LEN 4 | int "Max HTTP Request Header Length" 5 | default 512 6 | help 7 | This sets the maximum supported size of headers section in HTTP request packet to be processed by the 8 | server 9 | 10 | config HTTPD_MAX_URI_LEN 11 | int "Max HTTP URI Length" 12 | default 512 13 | help 14 | This sets the maximum supported size of HTTP request URI to be processed by the server 15 | 16 | config HTTPD_ERR_RESP_NO_DELAY 17 | bool "Use TCP_NODELAY socket option when sending HTTP error responses" 18 | default y 19 | help 20 | Using TCP_NODEALY socket option ensures that HTTP error response reaches the client before the 21 | underlying socket is closed. Please note that turning this off may cause multiple test failures 22 | 23 | config HTTPD_PURGE_BUF_LEN 24 | int "Length of temporary buffer for purging data" 25 | default 32 26 | help 27 | This sets the size of the temporary buffer used to receive and discard any remaining data that is 28 | received from the HTTP client in the request, but not processed as part of the server HTTP request 29 | handler. 30 | 31 | If the remaining data is larger than the available buffer size, the buffer will be filled in multiple 32 | iterations. The buffer should be small enough to fit on the stack, but large enough to avoid excessive 33 | iterations. 34 | 35 | config HTTPD_LOG_PURGE_DATA 36 | bool "Log purged content data at Debug level" 37 | default n 38 | help 39 | Enabling this will log discarded binary HTTP request data at Debug level. 40 | For large content data this may not be desirable as it will clutter the log. 41 | 42 | config HTTPD_WS_SUPPORT 43 | bool "WebSocket server support" 44 | default n 45 | help 46 | This sets the WebSocket server support. 47 | 48 | endmenu 49 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/component.mk: -------------------------------------------------------------------------------- 1 | 2 | COMPONENT_SRCDIRS := src src/util 3 | COMPONENT_ADD_INCLUDEDIRS := include 4 | COMPONENT_PRIV_INCLUDEDIRS := src/port/esp32 src/util 5 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/include/http_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | #warning http_server.h has been renamed to esp_http_server.h, please update include directives 7 | #include "esp_http_server.h" 8 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/src/port/esp32/osal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | 7 | #ifndef _OSAL_H_ 8 | #define _OSAL_H_ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | #define OS_SUCCESS ESP_OK 21 | #define OS_FAIL ESP_FAIL 22 | 23 | typedef TaskHandle_t othread_t; 24 | 25 | static inline int httpd_os_thread_create(othread_t *thread, 26 | const char *name, uint16_t stacksize, int prio, 27 | void (*thread_routine)(void *arg), void *arg, 28 | BaseType_t core_id) 29 | { 30 | int ret = xTaskCreatePinnedToCore(thread_routine, name, stacksize, arg, prio, thread, core_id); 31 | if (ret == pdPASS) { 32 | return OS_SUCCESS; 33 | } 34 | return OS_FAIL; 35 | } 36 | 37 | /* Only self delete is supported */ 38 | static inline void httpd_os_thread_delete(void) 39 | { 40 | vTaskDelete(xTaskGetCurrentTaskHandle()); 41 | } 42 | 43 | static inline void httpd_os_thread_sleep(int msecs) 44 | { 45 | vTaskDelay(msecs / portTICK_RATE_MS); 46 | } 47 | 48 | static inline othread_t httpd_os_thread_handle(void) 49 | { 50 | return xTaskGetCurrentTaskHandle(); 51 | } 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* ! _OSAL_H_ */ 58 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/src/util/ctrl_sock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "ctrl_sock.h" 15 | 16 | /* Control socket, because in some network stacks select can't be woken up any 17 | * other way 18 | */ 19 | int cs_create_ctrl_sock(int port) 20 | { 21 | int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 22 | if (fd < 0) { 23 | return -1; 24 | } 25 | 26 | int ret; 27 | struct sockaddr_in addr; 28 | memset(&addr, 0, sizeof(addr)); 29 | addr.sin_family = AF_INET; 30 | addr.sin_port = htons(port); 31 | inet_aton("127.0.0.1", &addr.sin_addr); 32 | ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); 33 | if (ret < 0) { 34 | close(fd); 35 | return -1; 36 | } 37 | return fd; 38 | } 39 | 40 | void cs_free_ctrl_sock(int fd) 41 | { 42 | close(fd); 43 | } 44 | 45 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len) 46 | { 47 | int ret; 48 | struct sockaddr_in to_addr; 49 | to_addr.sin_family = AF_INET; 50 | to_addr.sin_port = htons(port); 51 | inet_aton("127.0.0.1", &to_addr.sin_addr); 52 | ret = sendto(send_fd, data, data_len, 0, (struct sockaddr *)&to_addr, sizeof(to_addr)); 53 | 54 | if (ret < 0) { 55 | return -1; 56 | } 57 | return ret; 58 | } 59 | 60 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len) 61 | { 62 | int ret; 63 | ret = recvfrom(fd, data, data_len, 0, NULL, NULL); 64 | 65 | if (ret < 0) { 66 | return -1; 67 | } 68 | return ret; 69 | } 70 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/src/util/ctrl_sock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | 7 | /** 8 | * \file ctrl_sock.h 9 | * \brief Control Socket for select() wakeup 10 | * 11 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 12 | * sleeping on select. This is a common requirement for sending 13 | * control commands to a network server. This control socket API 14 | * facilitates the same. 15 | */ 16 | #ifndef _CTRL_SOCK_H_ 17 | #define _CTRL_SOCK_H_ 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /** 24 | * @brief Create a control socket 25 | * 26 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 27 | * sleeping on select. This is a common requirement for sending 28 | * control commands to a network server. This control socket API 29 | * facilitates the same. 30 | * 31 | * This API will create a UDP control socket on the specified port. It 32 | * will return a socket descriptor that can then be added to your 33 | * fd_set in select() 34 | * 35 | * @param[in] port the local port on which the control socket will listen 36 | * 37 | * @return - the socket descriptor that can be added to the fd_set in select. 38 | * - an error code if less than zero 39 | */ 40 | int cs_create_ctrl_sock(int port); 41 | 42 | /** 43 | * @brief Free the control socket 44 | * 45 | * This frees up the control socket that was earlier created using 46 | * cs_create_ctrl_sock() 47 | * 48 | * @param[in] fd the socket descriptor associated with this control socket 49 | */ 50 | void cs_free_ctrl_sock(int fd); 51 | 52 | /** 53 | * @brief Send data to control socket 54 | * 55 | * This API sends data to the control socket. If a server is blocked 56 | * on select() with the control socket, this call will wake up that 57 | * server. 58 | * 59 | * @param[in] send_fd the socket for sending ctrl messages 60 | * @param[in] port the port on which the control socket was created 61 | * @param[in] data pointer to a buffer that contains data to send on the socket 62 | * @param[in] data_len the length of the data contained in the buffer pointed to be data 63 | * 64 | * @return - the number of bytes sent to the control socket 65 | * - an error code if less than zero 66 | */ 67 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len); 68 | 69 | /** 70 | * @brief Receive data from control socket 71 | * 72 | * This API receives any data that was sent to the control 73 | * socket. This will be typically called from the server thread to 74 | * process any commands on this socket. 75 | * 76 | * @param[in] fd the socket descriptor of the control socket 77 | * @param[in] data pointer to a buffer that will be used to store 78 | * received from the control socket 79 | * @param[in] data_len the maximum length of the data that can be 80 | * stored in the buffer pointed by data 81 | * 82 | * @return - the number of bytes received from the control socket 83 | * - an error code if less than zero 84 | */ 85 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len); 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | #endif /* ! _CTRL_SOCK_H_ */ 92 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | idf_component_register(SRC_DIRS "." 2 | PRIV_INCLUDE_DIRS "." 3 | PRIV_REQUIRES cmock test_utils esp_http_server) 4 | -------------------------------------------------------------------------------- /ESP32/v4.4/components/esp_http_server/test/component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive 2 | -------------------------------------------------------------------------------- /ESP8266/README.md: -------------------------------------------------------------------------------- 1 | # Blinker hello example 2 | 3 | put the blinker componets into your projects. 4 | 5 | make the project list like this 6 | ``` 7 | Blinker_Hello 8 | |__components 9 | | |__blinker 10 | |__main 11 | ``` 12 | 13 | override the esp_http_server components with we provide. 14 | 15 | ``` 16 | idf.py menuconfig 17 | ``` 18 | 19 | enter Blinker Config 20 | 21 | * set BLINKER DEVICE TYPE 22 | 23 | * set BLINKER AUTH KEY 24 | 25 | * set Provisioning Type 26 | 27 | save and exit blinker config 28 | 29 | * Set serial port under Serial Flasher Options. 30 | 31 | enter components config 32 | 33 | * enable mDNS 34 | 35 | * disable Newlib 36 | 37 | * enable webSocket server support(http_server) 38 | 39 | save and exit 40 | 41 | ## Build and Flash 42 | 43 | Build the project and flash it to the board, then run monitor tool to view serial output: 44 | 45 | ``` 46 | idf.py -p PORT flash monitor 47 | ``` 48 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(include_dirs include) 2 | set(srcs 3 | "src/api/blinker_api.c" 4 | "src/mqtt/blinker_mqtt.c" 5 | "src/provisioning/blinker_prov_apconfig.c" 6 | "src/provisioning/blinker_prov_smartconfig.c" 7 | "src/provisioning/blinker_wifi.c" 8 | "src/utils/blinker_button.c" 9 | "src/utils/blinker_http.c" 10 | "src/utils/blinker_mdns.c" 11 | "src/utils/blinker_reboot.c" 12 | "src/utils/blinker_storage.c" 13 | "src/utils/blinker_timesync.c" 14 | "src/utils/blinker_utils.c" 15 | "src/utils/blinker_ws.c") 16 | 17 | set(COMPONENT_REQUIRES "mdns") 18 | 19 | register_component() -------------------------------------------------------------------------------- /ESP8266/components/blinker/component.mk: -------------------------------------------------------------------------------- 1 | COMPONENT_ADD_INCLUDEDIRS := include 2 | 3 | COMPONENT_SRCDIRS := src/api src/mqtt src/provisioning src/utils -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_api.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | #include "cJSON.h" 12 | #include "blinker_config.h" 13 | 14 | typedef enum { 15 | BLINKER_BUTTON = 0, 16 | BLINKER_IMAGE, 17 | BLINKER_JOYSTICK, 18 | BLINKER_RGB, 19 | BLINKER_SLIDER, 20 | BLINKER_TAB, 21 | BLINKER_SWITCH, 22 | } blinker_widget_type_t; 23 | 24 | typedef struct { 25 | union { 26 | int i; 27 | char *s; 28 | }; 29 | int array[4]; 30 | } blinker_widget_param_val_t; 31 | 32 | typedef enum { 33 | BLINKER_PARAM_POWER_STATE = 0, 34 | BLINKER_PARAM_COLOR, 35 | BLINKER_PARAM_MODE, 36 | BLINKER_PARAM_COLOR_TEMP, 37 | BLINKER_PARAM_COLOR_TEMP_UP, 38 | BLINKER_PARAM_COLOR_TEMP_DOWN, 39 | BLINKER_PARAM_BRIGHTNESS, 40 | BLINKER_PARAM_BRIGHTNESS_UP, 41 | BLINKER_PARAM_BRIGHTNESS_DOWN, 42 | BLINKER_PARAM_TEMP, 43 | BLINKER_PARAM_TEMP_UP, 44 | BLINKER_PARAM_TEMP_DOWN, 45 | BLINKER_PARAM_HUMI, 46 | BLINKER_PARAM_LEVEL, 47 | BLINKER_PARAM_LEVEL_UP, 48 | BLINKER_PARAM_LEVEL_DOWN, 49 | BLINKER_PARAM_HSWING, 50 | BLINKER_PARAM_VSWING, 51 | BLINKER_PARAM_MODE_STATE, 52 | BLINKER_PARAM_GET_STATE, 53 | } blinker_va_param_type_t; 54 | 55 | typedef struct { 56 | union { 57 | int i; 58 | char *s; 59 | }; 60 | 61 | union { 62 | int num; 63 | char *state; 64 | }; 65 | blinker_va_param_type_t type; 66 | } blinker_va_param_cb_t; 67 | 68 | typedef void (*blinker_va_cb_t)(const blinker_va_param_cb_t *val); 69 | typedef void (*blinker_widget_cb_t)(const blinker_widget_param_val_t *val); 70 | typedef void (*blinker_json_cb_t)(cJSON *param); 71 | typedef void (*blinker_data_cb_t)(const char *data); 72 | typedef void (*blinker_void_cb_t)(void); 73 | 74 | esp_err_t blinker_timeslot_data(cJSON *param, const char *key, const double value); 75 | 76 | esp_err_t blinker_timeslot_data_init(const uint16_t interval, const uint8_t times, const blinker_json_cb_t cb); 77 | 78 | esp_err_t blinker_data_handler(const blinker_data_cb_t cb); 79 | 80 | esp_err_t blinker_heart_beat_handler(const blinker_void_cb_t cb); 81 | 82 | esp_err_t blinker_builtin_switch_handler(const blinker_widget_cb_t cb); 83 | 84 | esp_err_t blinker_builtin_switch_state(const char *state); 85 | 86 | esp_err_t blinker_weather(char **payload, const int city); 87 | 88 | esp_err_t blinker_weather_forecast(char **payload, const int city); 89 | 90 | esp_err_t blinker_air(char **payload, const int city); 91 | 92 | esp_err_t blinker_sms(const char *msg); 93 | 94 | esp_err_t blinker_push(const char *msg); 95 | 96 | esp_err_t blinker_wechat(const char *msg); 97 | 98 | esp_err_t blinker_wechat_template(const char *title, const char *state, const char *msg); 99 | 100 | esp_err_t blinker_log(const char *msg); 101 | 102 | esp_err_t blinker_config_update(const char *msg); 103 | 104 | esp_err_t blinker_config_get(char **payload); 105 | 106 | esp_err_t blinker_va_multi_num(cJSON *param, const int num); 107 | 108 | esp_err_t blinker_aligenie_power_state(cJSON *param, const char *state); 109 | 110 | esp_err_t blinker_aligenie_color(cJSON *param, const char *color); 111 | 112 | esp_err_t blinker_aligenie_mode(cJSON *param, const char *mode); 113 | 114 | esp_err_t blinker_aligenie_color_temp(cJSON *param, const int color_temp); 115 | 116 | esp_err_t blinker_aligenie_brightness(cJSON *param, const int bright); 117 | 118 | esp_err_t blinker_aligenie_temp(cJSON *param, const int temp); 119 | 120 | esp_err_t blinker_aligenie_humi(cJSON *param, const int humi); 121 | 122 | esp_err_t blinker_aligenie_pm25(cJSON *param, const int pm25); 123 | 124 | esp_err_t blinker_aligenie_level(cJSON *param, const int level); 125 | 126 | esp_err_t blinker_aligenie_hswing(cJSON *param, const char *state); 127 | 128 | esp_err_t blinker_aligenie_vswing(cJSON *param, const char *state); 129 | 130 | esp_err_t blinker_aligenie_print(cJSON* param); 131 | 132 | esp_err_t blinker_aligenie_handler_register(blinker_va_cb_t cb); 133 | 134 | esp_err_t blinker_dueros_power_state(cJSON *param, const char *state); 135 | 136 | esp_err_t blinker_dueros_color(cJSON *param, const char *color); 137 | 138 | esp_err_t blinker_dueros_brightness(cJSON *param, const int bright); 139 | 140 | esp_err_t blinker_dueros_mode(cJSON *param, const char *mode); 141 | 142 | esp_err_t blinker_dueros_color_temp(cJSON *param, const int color_temp); 143 | 144 | esp_err_t blinker_dueros_temp(cJSON *param, const int temp); 145 | 146 | esp_err_t blinker_dueros_humi(cJSON *param, const int humi); 147 | 148 | esp_err_t blinker_dueros_pm25(cJSON *param, const int pm25); 149 | 150 | esp_err_t blinker_dueros_pm10(cJSON *param, const int pm10); 151 | 152 | esp_err_t blinker_dueros_co2(cJSON *param, const int co2); 153 | 154 | esp_err_t blinker_dueros_aqi(cJSON *param, const int aqi); 155 | 156 | esp_err_t blinker_dueros_level(cJSON *param, const int level); 157 | 158 | esp_err_t blinker_dueros_time(cJSON *param, const int time); 159 | 160 | esp_err_t blinker_dueros_print(cJSON* param); 161 | 162 | esp_err_t blinker_dueros_handler_register(const blinker_va_cb_t cb); 163 | 164 | esp_err_t blinker_miot_power_state(cJSON *param, const char *state); 165 | 166 | esp_err_t blinker_miot_color(cJSON *param, int color); 167 | 168 | esp_err_t blinker_miot_mode(cJSON *param, int mode); 169 | 170 | esp_err_t blinker_miot_mode_state(cJSON *param, const char *mode, const char *state); 171 | 172 | esp_err_t blinker_miot_color_temp(cJSON *param, const int color_temp); 173 | 174 | esp_err_t blinker_miot_brightness(cJSON *param, const int bright); 175 | 176 | esp_err_t blinker_miot_temp(cJSON *param, const int temp); 177 | 178 | esp_err_t blinker_miot_humi(cJSON *param, const int humi); 179 | 180 | esp_err_t blinker_miot_pm25(cJSON *param, const int pm25); 181 | 182 | esp_err_t blinker_miot_co2(cJSON *param, const int co2); 183 | 184 | esp_err_t blinker_miot_level(cJSON *param, const int level); 185 | 186 | esp_err_t blinker_miot_hswing(cJSON *param, const char *state); 187 | 188 | esp_err_t blinker_miot_vswing(cJSON *param, const char *state); 189 | 190 | esp_err_t blinker_miot_print(cJSON* param); 191 | 192 | esp_err_t blinker_miot_handler_register(const blinker_va_cb_t cb); 193 | 194 | esp_err_t blinker_widget_switch(cJSON *param, const char *state); 195 | 196 | esp_err_t blinker_widget_icon(cJSON *param, const char *icon); 197 | 198 | esp_err_t blinker_widget_color(cJSON *param, const char *color); 199 | 200 | esp_err_t blinker_widget_text(cJSON *param, const char *text); 201 | 202 | esp_err_t blinker_widget_text1(cJSON *param, const char *text); 203 | 204 | esp_err_t blinker_widget_text_color(cJSON *param, const char *color); 205 | 206 | esp_err_t blinker_widget_value_number(cJSON *param, const double value); 207 | 208 | esp_err_t blinker_widget_value_string(cJSON *param, const char *value); 209 | 210 | esp_err_t blinker_widget_unit(cJSON *param, const char *uint); 211 | 212 | esp_err_t blinker_widget_image(cJSON *param, const int num); 213 | 214 | esp_err_t blinker_widget_rgb_print(const char *key, const int r, const int g, const int b, const int w); 215 | 216 | esp_err_t blinker_widget_tab_print(const char *key, const int tab0, const int tab1, const int tab2, const int tab3, const int tab4); 217 | 218 | esp_err_t blinker_widget_print(const char *key, cJSON *param); 219 | 220 | esp_err_t blinker_widget_add(const char *name, const blinker_widget_type_t type, const blinker_widget_cb_t cb); 221 | 222 | esp_err_t blinker_init(void); 223 | 224 | #ifdef __cplusplus 225 | } 226 | #endif 227 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include "blinker_utils.h" 11 | 12 | #define BLINKER_FORMAT_QUEUE_TIME 200 13 | 14 | #define BLINKER_FORMAT_DATA_SIZE 1024 15 | 16 | #define BLINKER_MIN_TO_S 60 17 | 18 | #define BLINKER_MIN_TO_MS BLINKER_MIN_TO_S * 1000UL 19 | 20 | #define BLINKER_REGISTER_INTERVAL 20000U 21 | 22 | #define BLINKER_WEBSOCKET_SERVER_PORT 81 23 | 24 | #define BLINKER_MAX_TIME_INTERVAL 600 25 | 26 | #define BLINKER_MIN_TIME_INTERVAL 30 27 | 28 | #define BLINKER_MAX_TIMES_COUNT 10 29 | 30 | #define BLINKER_MIN_TIMES_COUNT 2 31 | 32 | #if defined CONFIG_BLINKER_CUSTOM_ESP 33 | 34 | #define BLINKER_REGISTER_TASK_DEEP 2 35 | 36 | #elif defined CONFIG_BLINKER_PRO_ESP 37 | 38 | #define BLINKER_REGISTER_TASK_DEEP 3 39 | #endif 40 | 41 | #define BLINKER_CMD_DETAIL "detail" 42 | 43 | #define BLINKER_CMD_AUTHKEY "authKey" 44 | 45 | #define BLINKER_CMD_REGISTER "register" 46 | 47 | #define BLINKER_CMD_MESSAGE "message" 48 | 49 | #define BLINKER_CMD_SUCCESS "success" 50 | 51 | #define BLINKER_CMD_REGISTER_SUCCESS "Registration successful" 52 | 53 | #define BLINKER_CMD_WIFI_CONFIG "wifi_config" 54 | 55 | #define BLINKER_CMD_DEVICE_NAME "deviceName" 56 | 57 | #define BLINKER_CMD_TO_DEVICE "toDevice" 58 | 59 | #define BLINKER_CMD_FROM_DEVICE "fromDevice" 60 | 61 | #define BLINKER_CMD_FROM "from" 62 | 63 | #define BLINKER_CMD_DEVICE_TYPE "deviceType" 64 | 65 | #define BLINKER_CMD_OWN_APP "OwnApp" 66 | 67 | #define BLINKER_CMD_VASSISTANT "vAssistant" 68 | 69 | #define BLINKER_CMD_SERVER_SENDER "ServerSender" 70 | 71 | #define BLINKER_CMD_SERVER_RECEIVER "ServerReceiver" 72 | 73 | #define BLINKER_CMD_MESSAGE_ID "messageId" 74 | 75 | #define BLINKER_CMD_USER_NAME "iotId" 76 | 77 | #define BLINKER_CMD_PASS_WORD "iotToken" 78 | 79 | #define BLINKER_CMD_PRODUCT_KEY "productKey" 80 | 81 | #define BLINKER_CMD_BROKER "broker" 82 | 83 | #define BLINKER_CMD_ALIYUN "aliyun" 84 | 85 | #define BLINKER_CMD_BLINKER "blinker" 86 | 87 | #define BLINKER_CMD_UUID "uuid" 88 | 89 | #define BLINKER_CMD_HOST "host" 90 | 91 | #define BLINKER_CMD_PORT "port" 92 | 93 | #define BLINKER_CMD_DATA "data" 94 | 95 | #define BLINKER_CMD_GET "get" 96 | 97 | #define BLINKER_CMD_SET "set" 98 | 99 | #define BLINKER_CMD_STATE "state" 100 | 101 | #define BLINKER_CMD_ONLINE "online" 102 | 103 | #define BLINKER_CMD_VERSION "version" 104 | 105 | #define BLINKER_CMD_BUILTIN_SWITCH "switch" 106 | 107 | #define BLINKER_CMD_SWITCH "swi" 108 | 109 | #define BLINKER_CMD_ICON "ico" 110 | 111 | #define BLINKER_CMD_COLOR "clr" 112 | 113 | #define BLINKER_CMD_COLOR_ "col" 114 | 115 | #define BLINKER_CMD_TEXT "tex" 116 | 117 | #define BLINKER_CMD_TEXT1 "tex1" 118 | 119 | #define BLINKER_CMD_TEXT_COLOR "tco" 120 | 121 | #define BLINKER_CMD_VALUE "val" 122 | 123 | #define BLINKER_CMD_IMAGE "img" 124 | 125 | #define BLINKER_CMD_UNIT "uni" 126 | 127 | #define BLINKER_CMD_ALIGENIE "AliGenie" 128 | 129 | #define BLINKER_CMD_DUEROS "DuerOS" 130 | 131 | #define BLINKER_CMD_MIOT "MIOT" 132 | 133 | #define BLINKER_CMD_POWER_STATE "pState" 134 | 135 | #define BLINKER_CMD_TRUE "true" 136 | 137 | #define BLINKER_CMD_FALSE "false" 138 | 139 | #define BLINKER_CMD_ON "on" 140 | 141 | #define BLINKER_CMD_OFF "off" 142 | 143 | #define BLINKER_CMD_MODE "mode" 144 | 145 | #define BLINKER_CMD_COLOR_TEMP "colTemp" 146 | 147 | #define BLINKER_CMD_COLOR_TEMP_UP "upColTemp" 148 | 149 | #define BLINKER_CMD_COLOR_TEMP_DOWN "downColTemp" 150 | 151 | #define BLINKER_CMD_BRIGHTNESS "bright" 152 | 153 | #define BLINKER_CMD_BRIGHTNESS_UP "upBright" 154 | 155 | #define BLINKER_CMD_BRIGHTNESS_DOWN "downBright" 156 | 157 | #define BLINKER_CMD_TEMP "temp" 158 | 159 | #define BLINKER_CMD_TEMP_UP "upTemp" 160 | 161 | #define BLINKER_CMD_TEMP_DOWN "downTemp" 162 | 163 | #define BLINKER_CMD_HUMI "humi" 164 | 165 | #define BLINKER_CMD_LEVEL "level" 166 | 167 | #define BLINKER_CMD_LEVEL_UP "upLevel" 168 | 169 | #define BLINKER_CMD_LEVEL_DOWN "downLevel" 170 | 171 | #define BLINKER_CMD_HSTATE "hsState" 172 | 173 | #define BLINKER_CMD_VSTATE "vsState" 174 | 175 | #define BLINKER_CMD_PM25 "pm25" 176 | 177 | #define BLINKER_CMD_PM10 "pm10" 178 | 179 | #define BLINKER_CMD_CO2 "co2" 180 | 181 | #define BLINKER_CMD_AQI "aqi" 182 | 183 | #define BLINKER_CMD_ECO "eco" 184 | 185 | #define BLINKER_CMD_ANION "anion" 186 | 187 | #define BLINKER_CMD_HEATER "heater" 188 | 189 | #define BLINKER_CMD_DRYER "dryer" 190 | 191 | #define BLINKER_CMD_SOFT "soft" 192 | 193 | #define BLINKER_CMD_UV "uv" 194 | 195 | #define BLINKER_CMD_UNSB "unsb" 196 | 197 | #define BLINKER_CMD_NUM "num" 198 | 199 | #define BLINKER_CMD_TIME "time" 200 | 201 | #define BLINKER_CMD_SSID "ssid" 202 | 203 | #define BLINKER_CMD_PASSWORD "pswd" 204 | 205 | #if defined(CONFIG_BLINKER_WITH_SSL) 206 | 207 | #define BLINKER_PROTOCOL_MQTT "mqtts" 208 | 209 | #define BLINKER_PROTPCOL_HTTP "https" 210 | 211 | #else 212 | 213 | #define BLINKER_PROTOCOL_MQTT "mqtt" 214 | 215 | #define BLINKER_PROTPCOL_HTTP "http" 216 | 217 | #endif 218 | 219 | #ifdef __cplusplus 220 | } 221 | #endif -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "esp_http_client.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config); 13 | 14 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url); 15 | 16 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value); 17 | 18 | esp_err_t blinker_http_get(esp_http_client_handle_t client); 19 | 20 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data); 21 | 22 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len); 23 | 24 | esp_err_t blinker_http_close(esp_http_client_handle_t client); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_mqtt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | typedef enum { 13 | BLINKER_BROKER_ALIYUN = 0, 14 | BLINKER_BROKER_BLINKER, 15 | } blinker_mqtt_broker_t; 16 | 17 | typedef struct { 18 | char *devicename; 19 | char *authkey; 20 | char *client_id; 21 | char *username; 22 | char *password; 23 | char *productkey; 24 | char *uuid; 25 | char *uri; 26 | int port; 27 | blinker_mqtt_broker_t broker; 28 | } blinker_mqtt_config_t; 29 | 30 | typedef void (*blinker_mqtt_subscribe_cb_t)(const char *topic, size_t topic_len, void *payload, size_t payload_len); 31 | 32 | blinker_mqtt_broker_t blinker_mqtt_broker(void); 33 | 34 | const char *blinker_mqtt_devicename(void); 35 | 36 | const char *blinker_mqtt_token(void); 37 | 38 | const char *blinker_mqtt_uuid(void); 39 | 40 | const char *blinker_mqtt_product_key(void); 41 | 42 | const char *blinker_mqtt_authkey(void); 43 | 44 | esp_err_t blinker_mqtt_subscribe(const char *topic, blinker_mqtt_subscribe_cb_t cb); 45 | 46 | esp_err_t blinker_mqtt_unsubscribe(const char *topic); 47 | 48 | esp_err_t blinker_mqtt_publish(const char *topic, const char *data, size_t data_len); 49 | 50 | esp_err_t blinker_mqtt_connect(void); 51 | 52 | esp_err_t blinker_mqtt_disconnect(void); 53 | 54 | esp_err_t blinker_mqtt_init(blinker_mqtt_config_t *config); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_prov_apconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_apconfig_init(void); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_prov_smartconfig.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_prov_smartconfig_init(void); 11 | 12 | esp_err_t blinker_prov_smartconfig_stop(void); 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_storage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_storage_init(void); 11 | 12 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length); 13 | 14 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length); 15 | 16 | esp_err_t blinker_storage_erase(const char *key); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: your name 3 | * @Date: 2021-07-02 13:32:25 4 | * @LastEditTime: 2021-07-22 12:22:52 5 | * @LastEditors: Please set LastEditors 6 | * @Description: In User Settings Edit 7 | * @FilePath: \ESP8266_RTOS_SDK\0524\smart_config\examples\components\blinker\include\blinker_utils.h 8 | */ 9 | #pragma once 10 | 11 | #include 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define BLINKER_FREE(ptr) { \ 18 | if (ptr) { \ 19 | free(ptr); \ 20 | ptr = NULL; \ 21 | } \ 22 | } 23 | 24 | #define BLINKER_TS_NUM(num) data##num 25 | 26 | void blinker_log_print_heap(void); 27 | 28 | void blinker_mac_device_name(char *name); 29 | 30 | time_t blinker_time(void); 31 | 32 | esp_err_t blinker_timesync_start(void); 33 | 34 | esp_err_t blinker_mdns_init(const char *host_name); 35 | 36 | esp_err_t blinker_mdns_free(void); 37 | 38 | int blinker_reboot_unbroken_count(void); 39 | 40 | int blinker_reboot_total_count(void); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_wifi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config); 11 | 12 | esp_err_t blinker_wifi_reset(void); 13 | 14 | esp_err_t blinker_wifi_init(void); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/include/blinker_ws.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" 7 | { 8 | #endif 9 | 10 | #include 11 | 12 | typedef struct { 13 | httpd_handle_t hd; 14 | int fd; 15 | } async_resp_arg_t; 16 | 17 | typedef void (*blinker_websocket_data_cb_t)(async_resp_arg_t *req, const char *payload); 18 | 19 | esp_err_t blinker_websocket_async_print(async_resp_arg_t *arg, const char *payload); 20 | 21 | esp_err_t blinker_websocket_print(httpd_req_t *req, const char *payload); 22 | 23 | esp_err_t blinker_websocket_server_init(blinker_websocket_data_cb_t cb); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/provisioning/blinker_prov_apconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "freertos/event_groups.h" 5 | #include "esp_system.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | 10 | #include "blinker_prov_apconfig.h" 11 | 12 | static const char *TAG = "blinker_prov_apconfig"; 13 | 14 | static void wifi_event_handler(void* arg, esp_event_base_t event_base, 15 | int32_t event_id, void* event_data) 16 | { 17 | if (event_id == WIFI_EVENT_AP_STACONNECTED) { 18 | wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; 19 | ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", 20 | MAC2STR(event->mac), event->aid); 21 | } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { 22 | wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; 23 | ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", 24 | MAC2STR(event->mac), event->aid); 25 | } 26 | } 27 | 28 | esp_err_t blinker_prov_apconfig_init(void) 29 | { 30 | #ifndef CONFIG_IDF_TARGET_ESP8266 31 | esp_netif_create_default_wifi_ap(); 32 | #endif 33 | 34 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); 35 | 36 | uint8_t mac[6] = {0}; 37 | char mac_str[31] = { 0 }; 38 | ESP_ERROR_CHECK(esp_wifi_get_mac(WIFI_IF_STA, mac)); 39 | #if defined CONFIG_BLINKER_CUSTOM_ESP 40 | sprintf(mac_str, "DiyArduino_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 41 | #elif defined CONFIG_BLINKER_PRO_ESP 42 | sprintf(mac_str, "%s_%02X%02X%02X%02X%02X%02X", CONFIG_BLINKER_TYPE_KEY,mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 43 | #endif 44 | 45 | wifi_config_t wifi_config = { 46 | .ap = { 47 | .ssid_len = strlen(mac_str), 48 | .max_connection = 4, 49 | .channel = 1, 50 | .authmode = WIFI_AUTH_OPEN 51 | }, 52 | }; 53 | strcpy((char *)wifi_config.ap.ssid, mac_str); 54 | 55 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); 56 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); 57 | ESP_ERROR_CHECK(esp_wifi_start()); 58 | 59 | ESP_LOGI(TAG, "apconfig start"); 60 | 61 | return ESP_OK; 62 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/provisioning/blinker_prov_smartconfig.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | #include "esp_smartconfig.h" 11 | #if CONFIG_IDF_TARGET_ESP8266 12 | #include "tcpip_adapter.h" 13 | #include "smartconfig_ack.h" 14 | #else 15 | #include "esp_netif.h" 16 | #endif 17 | #include "blinker_prov_smartconfig.h" 18 | 19 | static EventGroupHandle_t s_wifi_event_group; 20 | 21 | static const int CONNECTED_BIT = BIT0; 22 | static bool b_prov_smartconfig_start = false; 23 | static const char* TAG = "blinker_prov_smartconfig"; 24 | static void blinker_prov_smartconfig_start(void); 25 | 26 | static void event_handler(void* arg, esp_event_base_t event_base, 27 | int32_t event_id, void* event_data) 28 | { 29 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 30 | // xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL); 31 | // blinker_prov_smartconfig_start(); 32 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 33 | esp_wifi_connect(); 34 | // xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); 35 | // wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 36 | // ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 37 | // if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 38 | // ESP_LOGE(TAG, "wrong password, need reset device"); 39 | // // esp_wifi_disconnect(); 40 | // // blinker_prov_smartconfig_stop(); 41 | // // vTaskDelay(10); 42 | // // blinker_prov_smartconfig_start(); 43 | // // return; 44 | // } 45 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 46 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 47 | #if CONFIG_IDF_TARGET_ESP8266 48 | ESP_LOGI(TAG, "smartconfig connected, got ip:%s", 49 | ip4addr_ntoa(&event->ip_info.ip)); 50 | #else 51 | ESP_LOGI(TAG, "smartconfig connected, got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 52 | #endif 53 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) { 54 | ESP_LOGI(TAG, "Scan done"); 55 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) { 56 | ESP_LOGI(TAG, "Found channel"); 57 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { 58 | ESP_LOGI(TAG, "Got SSID and password"); 59 | 60 | smartconfig_event_got_ssid_pswd_t* evt = (smartconfig_event_got_ssid_pswd_t*)event_data; 61 | wifi_config_t wifi_config; 62 | uint8_t ssid[33] = { 0 }; 63 | uint8_t password[65] = { 0 }; 64 | uint8_t rvd_data[33] = { 0 }; 65 | 66 | bzero(&wifi_config, sizeof(wifi_config_t)); 67 | memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid)); 68 | memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password)); 69 | wifi_config.sta.bssid_set = evt->bssid_set; 70 | 71 | if (wifi_config.sta.bssid_set == true) { 72 | memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid)); 73 | } 74 | 75 | memcpy(ssid, evt->ssid, sizeof(evt->ssid)); 76 | memcpy(password, evt->password, sizeof(evt->password)); 77 | ESP_LOGI(TAG, "SSID:%s", ssid); 78 | ESP_LOGI(TAG, "PASSWORD:%s", password); 79 | if (evt->type == SC_TYPE_ESPTOUCH_V2) { 80 | ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) ); 81 | ESP_LOGI(TAG, "RVD_DATA:%s", rvd_data); 82 | } 83 | 84 | // ESP_ERROR_CHECK(esp_wifi_disconnect()); 85 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); 86 | ESP_ERROR_CHECK(esp_wifi_connect()); 87 | xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); 88 | } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) { 89 | // xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); 90 | 91 | blinker_prov_smartconfig_stop(); 92 | ESP_LOGI(TAG, "blinker_prov_smartconfig_stop"); 93 | } 94 | } 95 | 96 | esp_err_t blinker_prov_smartconfig_init(void) 97 | { 98 | s_wifi_event_group = xEventGroupCreate(); 99 | 100 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 101 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 102 | ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 103 | 104 | ESP_ERROR_CHECK(esp_wifi_start()); 105 | 106 | blinker_prov_smartconfig_start(); 107 | 108 | xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, true, false, portMAX_DELAY); 109 | 110 | return ESP_OK; 111 | } 112 | 113 | static void blinker_prov_smartconfig_start(void) 114 | { 115 | b_prov_smartconfig_start = true; 116 | #if defined CONFIG_BLINKER_SMART_CONFIG 117 | ESP_ERROR_CHECK(esp_smartconfig_set_type(CONFIG_BLINKER_PROV_SMARTCONFIG_TYPE)); 118 | smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); 119 | ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); 120 | #endif 121 | } 122 | 123 | esp_err_t blinker_prov_smartconfig_stop(void) 124 | { 125 | esp_err_t err = ESP_FAIL; 126 | 127 | if (b_prov_smartconfig_start) { 128 | err = esp_smartconfig_stop(); 129 | 130 | b_prov_smartconfig_start = false; 131 | } 132 | 133 | return err; 134 | } 135 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/provisioning/blinker_wifi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_wifi.h" 7 | #include "esp_event.h" 8 | #include "esp_log.h" 9 | #include "esp_system.h" 10 | 11 | #ifndef CONFIG_IDF_TARGET_ESP8266 12 | #include "esp_netif.h" 13 | #endif 14 | 15 | #include "blinker_wifi.h" 16 | #include "blinker_config.h" 17 | #include "blinker_storage.h" 18 | 19 | static const char* TAG = "blinker_wifi"; 20 | static const int CONNECTED_BIT = BIT0; 21 | static EventGroupHandle_t b_wifi_event_group = NULL; 22 | 23 | static void event_handler(void* arg, esp_event_base_t event_base, 24 | int32_t event_id, void* event_data) 25 | { 26 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 27 | esp_wifi_connect(); 28 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 29 | esp_wifi_connect(); 30 | 31 | wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t*) event_data; 32 | ESP_LOGE(TAG, "Disconnect reason : %d", disconnected->reason); 33 | if(disconnected->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT){ 34 | ESP_LOGE(TAG, "wrong password"); 35 | vTaskDelay(pdMS_TO_TICKS(2000)); 36 | } 37 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { 38 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; 39 | #if CONFIG_IDF_TARGET_ESP8266 40 | ESP_LOGI(TAG, "got ip:%s", ip4addr_ntoa(&event->ip_info.ip)); 41 | #else 42 | ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); 43 | #endif 44 | xEventGroupSetBits(b_wifi_event_group, CONNECTED_BIT); 45 | } 46 | } 47 | 48 | esp_err_t blinker_wifi_start(const wifi_config_t *wifi_config) 49 | { 50 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, (wifi_config_t *)wifi_config) ); 51 | ESP_ERROR_CHECK(esp_wifi_start()); 52 | 53 | xEventGroupWaitBits(b_wifi_event_group, CONNECTED_BIT, 54 | pdFALSE, 55 | pdFALSE, 56 | portMAX_DELAY); 57 | return ESP_OK; 58 | } 59 | 60 | esp_err_t blinker_wifi_reset(void) 61 | { 62 | esp_err_t err = ESP_FAIL; 63 | 64 | err = esp_wifi_restore(); 65 | 66 | err = blinker_storage_erase(BLINKER_CMD_WIFI_CONFIG); 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_wifi_init(void) 72 | { 73 | #if CONFIG_IDF_TARGET_ESP8266 74 | tcpip_adapter_init(); 75 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 76 | #else 77 | esp_netif_init(); 78 | 79 | ESP_ERROR_CHECK(esp_event_loop_create_default()); 80 | esp_netif_create_default_wifi_sta(); 81 | #endif 82 | 83 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 84 | 85 | ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 86 | #if CONFIG_IDF_TARGET_ESP8266 87 | ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); 88 | #endif 89 | b_wifi_event_group = xEventGroupCreate(); 90 | 91 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 92 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); 93 | 94 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); 95 | 96 | return ESP_OK; 97 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_http.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "esp_log.h" 6 | #include "esp_system.h" 7 | #include "esp_netif.h" 8 | #include "esp_tls.h" 9 | 10 | #include "blinker_http.h" 11 | 12 | static const char *TAG = "blinker_http"; 13 | 14 | esp_http_client_handle_t blinker_http_init(esp_http_client_config_t *config) 15 | { 16 | return esp_http_client_init(config); 17 | } 18 | 19 | esp_err_t blinker_http_set_url(esp_http_client_handle_t client, const char *url) 20 | { 21 | return esp_http_client_set_url(client, url); 22 | } 23 | 24 | esp_err_t blinker_http_set_header(esp_http_client_handle_t client, const char *key, const char *value) 25 | { 26 | return esp_http_client_set_header(client, key, value); 27 | } 28 | 29 | esp_err_t blinker_http_get(esp_http_client_handle_t client) 30 | { 31 | esp_err_t err = ESP_FAIL; 32 | 33 | esp_http_client_set_method(client, HTTP_METHOD_GET); 34 | 35 | err = esp_http_client_open(client, 0); 36 | 37 | if (err != ESP_OK) { 38 | ESP_LOGI(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); 39 | } else { 40 | int content_length = esp_http_client_fetch_headers(client); 41 | if (content_length < 0) { 42 | ESP_LOGI(TAG, "HTTP client fetch headers failed"); 43 | err = ESP_FAIL; 44 | } else { 45 | err = ESP_OK; 46 | } 47 | } 48 | 49 | return err; 50 | } 51 | 52 | esp_err_t blinker_http_post(esp_http_client_handle_t client, const char *post_data) 53 | { 54 | esp_http_client_set_method(client, HTTP_METHOD_POST); 55 | 56 | ESP_LOGI(TAG, "HTTP POST: %s", post_data); 57 | 58 | esp_http_client_set_post_field(client, post_data, strlen(post_data)); 59 | esp_err_t err = esp_http_client_perform(client); 60 | if (err == ESP_OK) { 61 | ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d", 62 | esp_http_client_get_status_code(client), 63 | esp_http_client_get_content_length(client)); 64 | } else { 65 | ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); 66 | } 67 | 68 | return err; 69 | } 70 | 71 | esp_err_t blinker_http_read_response(esp_http_client_handle_t client, char *buffer, int len) 72 | { 73 | int data_read = esp_http_client_read_response(client, buffer, len); 74 | 75 | if (data_read >= 0) { 76 | ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", 77 | esp_http_client_get_status_code(client), 78 | esp_http_client_get_content_length(client)); 79 | // ESP_LOG_BUFFER_HEX(TAG, buffer, strlen(buffer)); 80 | 81 | return ESP_OK; 82 | } else { 83 | ESP_LOGI(TAG, "Failed to read response"); 84 | 85 | return ESP_FAIL; 86 | } 87 | } 88 | 89 | esp_err_t blinker_http_close(esp_http_client_handle_t client) 90 | { 91 | esp_http_client_close(client); 92 | esp_http_client_cleanup(client); 93 | 94 | return ESP_OK; 95 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_mdns.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "esp_system.h" 5 | #include "esp_wifi.h" 6 | #include "esp_log.h" 7 | #include "mdns.h" 8 | #include 9 | #include 10 | #include "blinker_utils.h" 11 | 12 | // static const char *TAG = "blinker_mdns"; 13 | 14 | static bool m_init = false; 15 | 16 | esp_err_t blinker_mdns_init(const char *host_name) 17 | { 18 | // static bool m_init = false; 19 | if (m_init) { 20 | blinker_mdns_free(); 21 | } 22 | 23 | m_init = true; 24 | 25 | char mac_name[13] = {0}; 26 | blinker_mac_device_name(mac_name); 27 | 28 | ESP_ERROR_CHECK(mdns_init()); 29 | ESP_ERROR_CHECK(mdns_hostname_set(host_name)); 30 | ESP_ERROR_CHECK(mdns_instance_name_set(host_name)); 31 | 32 | mdns_txt_item_t serviceTxtData[2] = { 33 | {"deviceName", host_name}, 34 | {"mac", mac_name} 35 | }; 36 | 37 | ESP_ERROR_CHECK(mdns_service_add(host_name, "_blinker", "_tcp", 81, serviceTxtData, 2)); 38 | 39 | return ESP_OK; 40 | } 41 | 42 | esp_err_t blinker_mdns_free(void) 43 | { 44 | if (m_init) { 45 | m_init = false; 46 | 47 | mdns_free(); 48 | } 49 | 50 | return ESP_OK; 51 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_reboot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "freertos/FreeRTOS.h" 7 | #include "freertos/timers.h" 8 | 9 | #include 10 | #include 11 | #include "esp_sntp.h" 12 | #include "esp_log.h" 13 | #include "esp_partition.h" 14 | #include "esp_ota_ops.h" 15 | 16 | #include "blinker_utils.h" 17 | #include "blinker_storage.h" 18 | 19 | #if CONFIG_IDF_TARGET_ESP8266 20 | #include "esp8266/rtc_register.h" 21 | #elif CONFIG_IDF_TARGET_ESP32 22 | #include "esp32/rom/rtc.h" 23 | #elif CONFIG_IDF_TARGET_ESP32S2 24 | #include "esp32s2/rom/rtc.h" 25 | #elif CONFIG_IDF_TARGET_ESP32C3 26 | #include "esp32c3/rom/rtc.h" 27 | #endif 28 | 29 | #define RTC_RESET_SW_CAUSE_REG RTC_STORE0 30 | #define RTC_RESET_HW_CAUSE_REG RTC_STATE1 31 | #define RTC_WAKEUP_HW_CAUSE_REG RTC_STATE2 32 | 33 | #define RTC_RESET_HW_CAUSE_LSB 0 34 | #define RTC_RESET_HW_CAUSE_MSB 3 35 | 36 | #define REBOOT_RECORD_KEY "reboot_record" 37 | #define CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY (ESP_TASK_MAIN_PRIO + 1) 38 | 39 | typedef struct { 40 | size_t total_count; 41 | size_t unbroken_count; 42 | RESET_REASON reason; 43 | } blinker_reboot_record_t; 44 | 45 | static const char *TAG = "blinker_reboot"; 46 | static blinker_reboot_record_t g_reboot_record = {0}; 47 | 48 | static uint32_t esp_rtc_get_reset_reason(void) 49 | { 50 | #if CONFIG_IDF_TARGET_ESP8266 51 | return GET_PERI_REG_BITS(RTC_RESET_HW_CAUSE_REG, RTC_RESET_HW_CAUSE_MSB, RTC_RESET_HW_CAUSE_LSB); 52 | #else 53 | return rtc_get_reset_reason(0); 54 | #endif 55 | } 56 | 57 | static void esp_reboot_count_erase_timercb(void *priv) 58 | { 59 | g_reboot_record.unbroken_count = 0; 60 | blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 61 | 62 | ESP_LOGI(TAG, "Erase reboot count"); 63 | } 64 | 65 | static esp_err_t blinker_reboot_unbroken_init(void) 66 | { 67 | esp_err_t err = ESP_OK; 68 | g_reboot_record.reason = esp_rtc_get_reset_reason(); 69 | 70 | blinker_storage_init(); 71 | blinker_storage_get(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 72 | 73 | g_reboot_record.total_count++; 74 | 75 | /**< If the device reboots within the instruction time, 76 | the event_mdoe value will be incremented by one */ 77 | #if CONFIG_IDF_TARGET_ESP8266 78 | if (g_reboot_record.reason != DEEPSLEEP_RESET) { 79 | #else 80 | if (g_reboot_record.reason != DEEPSLEEP_RESET && g_reboot_record.reason != RTCWDT_BROWN_OUT_RESET) { 81 | #endif 82 | g_reboot_record.unbroken_count++; 83 | ESP_LOGI(TAG, "reboot unbroken count: %d", g_reboot_record.unbroken_count); 84 | } else { 85 | g_reboot_record.unbroken_count = 1; 86 | ESP_LOGI(TAG, "reboot reason: %d", g_reboot_record.reason); 87 | } 88 | 89 | err = blinker_storage_set(REBOOT_RECORD_KEY, &g_reboot_record, sizeof(blinker_reboot_record_t)); 90 | 91 | esp_timer_handle_t time_handle = NULL; 92 | esp_timer_create_args_t timer_cfg = { 93 | .name = "reboot_count_erase", 94 | .callback = esp_reboot_count_erase_timercb, 95 | .dispatch_method = ESP_TIMER_TASK, 96 | }; 97 | 98 | err = esp_timer_create(&timer_cfg, &time_handle); 99 | err = esp_timer_start_once(time_handle, CONFIG_BLINKER_REBOOT_UNBROKEN_INTERVAL_TIMEOUT * 1000UL); 100 | 101 | return err; 102 | } 103 | 104 | static void reboot_unbroken_record_task(void *arg) 105 | { 106 | blinker_reboot_unbroken_init(); 107 | 108 | if (CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT && 109 | blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT) { 110 | // esp_ota_mark_app_invalid_rollback_and_reboot(); 111 | } 112 | 113 | if (blinker_reboot_unbroken_count() >= CONFIG_BLINKER_REBOOT_UNBROKEN_FALLBACK_COUNT_RESET) { 114 | blinker_storage_erase(CONFIG_BLINKER_NVS_NAMESPACE); 115 | } 116 | 117 | ESP_LOGI(TAG, "version_fallback_task exit"); 118 | 119 | vTaskDelete(NULL); 120 | } 121 | 122 | static void __attribute__((constructor)) blinker_reboot_unbroken_record(void) 123 | { 124 | xTaskCreate(reboot_unbroken_record_task, 125 | "reboot_unbroken_record", 126 | 4 * 1024, 127 | NULL, 128 | CONFIG_UNBROKEN_RECORD_TASK_DEFAULT_PRIOTY, 129 | NULL); 130 | } 131 | 132 | int blinker_reboot_unbroken_count(void) 133 | { 134 | ESP_LOGI(TAG, "blinker_reboot_unbroken_count: %d", g_reboot_record.unbroken_count); 135 | return g_reboot_record.unbroken_count; 136 | } 137 | 138 | int blinker_reboot_total_count(void) 139 | { 140 | return g_reboot_record.total_count; 141 | } 142 | 143 | // bool esp_qcloud_reboot_is_exception(bool erase_coredump) 144 | // { 145 | // esp_err_t ret = ESP_OK; 146 | // ssize_t coredump_len = 0; 147 | 148 | // const esp_partition_t *coredump_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 149 | // ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL); 150 | // ESP_QCLOUD_ERROR_CHECK(!coredump_part, false, "<%s> esp_partition_get fail", esp_err_to_name(ret)); 151 | 152 | // ret = esp_partition_read(coredump_part, 0, &coredump_len, sizeof(ssize_t)); 153 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_read fail", esp_err_to_name(ret)); 154 | 155 | // if (coredump_len <= 0) { 156 | // return false; 157 | // } 158 | 159 | // if (erase_coredump) { 160 | // /**< erase all coredump partition */ 161 | // ret = esp_partition_erase_range(coredump_part, 0, coredump_part->size); 162 | // ESP_QCLOUD_ERROR_CHECK(ret, false, "<%s> esp_partition_erase_range fail", esp_err_to_name(ret)); 163 | // } 164 | 165 | // return true; 166 | // } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_storage.c: -------------------------------------------------------------------------------- 1 | #include "string.h" 2 | #include "stdio.h" 3 | #include "stdlib.h" 4 | #include "esp_log.h" 5 | #include "esp_system.h" 6 | 7 | #include "nvs.h" 8 | #include "nvs_flash.h" 9 | 10 | #include "blinker_storage.h" 11 | 12 | static const char *TAG = "blinker_storage"; 13 | 14 | esp_err_t blinker_storage_init(void) 15 | { 16 | static bool init_flag = false; 17 | 18 | if (!init_flag) { 19 | esp_err_t ret = nvs_flash_init(); 20 | 21 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {// || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { 22 | // NVS partition was truncated and needs to be erased 23 | // Retry nvs_flash_init 24 | ESP_ERROR_CHECK(nvs_flash_erase()); 25 | ret = nvs_flash_init(); 26 | } 27 | 28 | ESP_ERROR_CHECK(ret); 29 | 30 | init_flag = true; 31 | } 32 | 33 | return ESP_OK; 34 | } 35 | 36 | esp_err_t blinker_storage_erase(const char *key) 37 | { 38 | ESP_LOGI(TAG, "blinker_storage_erase: %s", key); 39 | 40 | esp_err_t ret = ESP_OK; 41 | nvs_handle handle = 0; 42 | 43 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 44 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 45 | 46 | /** 47 | * @brief If key is CONFIG_BLINKER_NVS_NAMESPACE, erase all info in CONFIG_BLINKER_NVS_NAMESPACE 48 | */ 49 | if (!strcmp(key, CONFIG_BLINKER_NVS_NAMESPACE)) { 50 | ret = nvs_erase_all(handle); 51 | } else { 52 | ret = nvs_erase_key(handle, key); 53 | } 54 | 55 | /**< Write any pending changes to non-volatile storage */ 56 | nvs_commit(handle); 57 | 58 | /**< Close the storage handle and free any allocated resources */ 59 | nvs_close(handle); 60 | 61 | return ret; 62 | } 63 | 64 | esp_err_t blinker_storage_set(const char *key, const void *value, size_t length) 65 | { 66 | esp_err_t ret = ESP_OK; 67 | nvs_handle handle = 0; 68 | 69 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 70 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 71 | 72 | /**< set variable length binary value for given key */ 73 | ret = nvs_set_blob(handle, key, value, length); 74 | 75 | /**< Write any pending changes to non-volatile storage */ 76 | nvs_commit(handle); 77 | 78 | /**< Close the storage handle and free any allocated resources */ 79 | nvs_close(handle); 80 | 81 | return ret; 82 | } 83 | 84 | esp_err_t blinker_storage_get(const char *key, void *value, size_t length) 85 | { 86 | esp_err_t ret = ESP_OK; 87 | nvs_handle handle = 0; 88 | 89 | /**< Open non-volatile storage with a given namespace from the default NVS partition */ 90 | ret = nvs_open(CONFIG_BLINKER_NVS_NAMESPACE, NVS_READWRITE, &handle); 91 | 92 | /**< get variable length binary value for given key */ 93 | ret = nvs_get_blob(handle, key, value, &length); 94 | 95 | /**< Close the storage handle and free any allocated resources */ 96 | nvs_close(handle); 97 | 98 | if (ret == ESP_ERR_NVS_NOT_FOUND) { 99 | ESP_LOGI(TAG, " Get value for given key, key: %s", key); 100 | return ESP_ERR_NVS_NOT_FOUND; 101 | } 102 | 103 | return ESP_OK; 104 | } 105 | -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_timesync.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "freertos/FreeRTOS.h" 4 | #include "freertos/task.h" 5 | #include "freertos/event_groups.h" 6 | #include "esp_log.h" 7 | #include "lwip/apps/sntp.h" 8 | #include "blinker_utils.h" 9 | 10 | #define REF_TIME 1577808000 /* 2020-01-01 00:00:00 */ 11 | #define DEFAULT_TICKS (2000 / portTICK_PERIOD_MS) /* 2 seconds in ticks */ 12 | #define CONFIG_BLINKER_SNTP_SERVER_NAME "pool.ntp.org" 13 | 14 | static const char *TAG = "blinker_timesync"; 15 | // static const int NTP_DONE_BIT = BIT0; 16 | static bool g_init_done = false; 17 | // static EventGroupHandle_t b_ntp_event_group = NULL; 18 | 19 | time_t blinker_time(void) 20 | { 21 | time_t now; 22 | time(&now); 23 | 24 | return now; 25 | } 26 | 27 | static bool blinker_timesync_check(void) 28 | { 29 | time_t now; 30 | time(&now); 31 | 32 | if (now > REF_TIME) { 33 | return true; 34 | } 35 | 36 | return false; 37 | } 38 | 39 | static void blinker_timesync_task(void *arg) 40 | { 41 | if (!g_init_done) { 42 | ESP_LOGW(TAG, "Time sync not initialised using 'esp_qcloud_timesync_start'"); 43 | } 44 | 45 | for(;;) { 46 | if (blinker_timesync_check() == true) { 47 | break; 48 | } 49 | 50 | // ESP_LOGI(TAG, "Time not synchronized yet. Retrying..."); 51 | vTaskDelay(DEFAULT_TICKS); 52 | } 53 | 54 | struct tm timeinfo; 55 | char strftime_buf[64]; 56 | time_t now; 57 | time(&now); 58 | localtime_r(&now, &timeinfo); 59 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); 60 | ESP_LOGI(TAG, "The current UTC time is: %s", strftime_buf); 61 | // xEventGroupSetBits(b_ntp_event_group, NTP_DONE_BIT); 62 | 63 | vTaskDelete(NULL); 64 | } 65 | 66 | esp_err_t blinker_timesync_start(void) 67 | { 68 | if (sntp_enabled()) { 69 | ESP_LOGI(TAG, "SNTP already initialized."); 70 | g_init_done = true; 71 | return ESP_OK; 72 | } 73 | 74 | // b_ntp_event_group = xEventGroupCreate(); 75 | 76 | char *sntp_server_name = CONFIG_BLINKER_SNTP_SERVER_NAME; 77 | 78 | ESP_LOGI(TAG, "Initializing SNTP. Using the SNTP server: %s", sntp_server_name); 79 | sntp_setoperatingmode(SNTP_OPMODE_POLL); 80 | sntp_setservername(0, sntp_server_name); 81 | setenv("TZ", "CST-8", 1); 82 | tzset(); 83 | sntp_init(); 84 | g_init_done = true; 85 | 86 | xTaskCreate(blinker_timesync_task, 87 | "blinker_timesync", 88 | 2 * 1024, 89 | NULL, 90 | 6, 91 | NULL); 92 | 93 | // xEventGroupWaitBits(b_ntp_event_group, NTP_DONE_BIT, 94 | // pdFALSE, 95 | // pdFALSE, 96 | // portMAX_DELAY); 97 | 98 | return ESP_OK; 99 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include "esp_wifi.h" 11 | 12 | #include "esp_heap_caps.h" 13 | 14 | #include "blinker_utils.h" 15 | 16 | static const char *TAG = "blinker_utils"; 17 | 18 | void blinker_log_print_heap(void) 19 | { 20 | ESP_LOGI(TAG, "Free heap, current: %d", esp_get_free_heap_size()); 21 | } 22 | 23 | void blinker_mac_device_name(char *name) 24 | { 25 | uint8_t mac[6] = { 0 }; 26 | esp_wifi_get_mac(WIFI_IF_STA, mac); 27 | sprintf(name, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 28 | } -------------------------------------------------------------------------------- /ESP8266/components/blinker/src/utils/blinker_ws.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "blinker_config.h" 10 | #include "blinker_utils.h" 11 | #include "blinker_ws.h" 12 | 13 | static const char *TAG = "blinker_ws"; 14 | static blinker_websocket_data_cb_t ws_cb = NULL; 15 | 16 | // /* 17 | // * Structure holding server handle 18 | // * and internal socket fd in order 19 | // * to use out of request send 20 | // */ 21 | // struct async_resp_arg { 22 | // httpd_handle_t hd; 23 | // int fd; 24 | // }; 25 | 26 | // /* 27 | // * async send function, which we put into the httpd work queue 28 | // */ 29 | // static void ws_async_send(void *arg) 30 | // { 31 | // static const char * data = "Async data"; 32 | // struct async_resp_arg *resp_arg = arg; 33 | // httpd_handle_t hd = resp_arg->hd; 34 | // int fd = resp_arg->fd; 35 | // httpd_ws_frame_t ws_pkt; 36 | // memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 37 | // ws_pkt.payload = (uint8_t*)data; 38 | // ws_pkt.len = strlen(data); 39 | // ws_pkt.type = HTTPD_WS_TYPE_TEXT; 40 | 41 | // httpd_ws_send_frame_async(hd, fd, &ws_pkt); 42 | // free(resp_arg); 43 | // } 44 | 45 | // static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req) 46 | // { 47 | // struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); 48 | // resp_arg->hd = req->handle; 49 | // resp_arg->fd = httpd_req_to_sockfd(req); 50 | // return httpd_queue_work(handle, ws_async_send, resp_arg); 51 | // } 52 | 53 | // typedef struct { 54 | // char *data; 55 | // async_resp_arg_t *resp_arg; 56 | // } async_data_arg_t; 57 | 58 | esp_err_t blinker_websocket_async_print(async_resp_arg_t *arg, const char *payload) 59 | { 60 | ESP_LOGI(TAG, "blinker_websocket_async_print: %s", payload); 61 | 62 | httpd_ws_frame_t ws_pkt; 63 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 64 | ws_pkt.payload = (uint8_t *)payload; 65 | ws_pkt.len = strlen(payload); 66 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 67 | 68 | esp_err_t err = httpd_ws_send_frame_async(arg->hd, arg->fd, &ws_pkt); 69 | 70 | return err; 71 | } 72 | 73 | esp_err_t blinker_websocket_print(httpd_req_t *req, const char *payload) 74 | { 75 | ESP_LOGI(TAG, "blinker_websocket_print: %s", payload); 76 | 77 | httpd_ws_frame_t ws_pkt; 78 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 79 | ws_pkt.payload = (uint8_t *)payload; 80 | ws_pkt.len = strlen(payload); 81 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 82 | 83 | esp_err_t err = httpd_ws_send_frame(req, &ws_pkt); 84 | 85 | return err; 86 | } 87 | 88 | // static void ws_async_send(void *arg) 89 | // { 90 | // async_data_arg_t *data_arg = arg; 91 | 92 | // ws_cb(data_arg->resp_arg, data_arg->data); 93 | // BLINKER_FREE(data_arg->resp_arg); 94 | // BLINKER_FREE(data_arg->data); 95 | // BLINKER_FREE(data_arg); 96 | // } 97 | 98 | static esp_err_t echo_handler(httpd_req_t *req) 99 | { 100 | uint8_t buf[128] = { 0 }; 101 | httpd_ws_frame_t ws_pkt; 102 | memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 103 | ws_pkt.payload = buf; 104 | ws_pkt.type = HTTPD_WS_TYPE_TEXT; 105 | esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 128); 106 | if (ret != ESP_OK) { 107 | ESP_LOGI(TAG, "httpd_ws_recv_frame failed with %d", ret); 108 | return ret; 109 | } 110 | ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload); 111 | ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type); 112 | 113 | if (ws_pkt.type == HTTPD_WS_TYPE_TEXT && ws_cb != NULL) { 114 | async_resp_arg_t *resp_arg = calloc(1, sizeof(async_resp_arg_t)); 115 | resp_arg->hd = req->handle; 116 | resp_arg->fd = httpd_req_to_sockfd(req); 117 | 118 | ws_cb(resp_arg, (char*)ws_pkt.payload); 119 | BLINKER_FREE(resp_arg); 120 | 121 | ret = ESP_OK; 122 | } 123 | return ret; 124 | } 125 | 126 | static esp_err_t ws_connect_handler(httpd_req_t *req) 127 | { 128 | char *buf = "{\"state\":\"connected\"}\n"; 129 | // httpd_ws_frame_t ws_pkt; 130 | // memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); 131 | // ws_pkt.payload = (uint8_t *)buf; 132 | // ws_pkt.len = strlen(buf); 133 | // ws_pkt.type = HTTPD_WS_TYPE_TEXT; 134 | // httpd_ws_send_frame(req, &ws_pkt); 135 | blinker_websocket_print(req, buf); 136 | return ESP_OK; 137 | } 138 | 139 | static const httpd_uri_t ws = { 140 | .uri = "/", 141 | .method = HTTP_GET, 142 | .handler = echo_handler, 143 | .connect_cb = ws_connect_handler, 144 | .user_ctx = NULL, 145 | .is_websocket = true 146 | }; 147 | 148 | static httpd_handle_t start_webserver(void) 149 | { 150 | httpd_handle_t server = NULL; 151 | httpd_config_t config = HTTPD_DEFAULT_CONFIG(); 152 | config.server_port = BLINKER_WEBSOCKET_SERVER_PORT; 153 | 154 | // Start the httpd server 155 | ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); 156 | if (httpd_start(&server, &config) == ESP_OK) { 157 | // Registering the ws handler 158 | ESP_LOGI(TAG, "Registering URI handlers"); 159 | httpd_register_uri_handler(server, &ws); 160 | return server; 161 | } 162 | 163 | ESP_LOGI(TAG, "Error starting server!"); 164 | return NULL; 165 | } 166 | 167 | static void stop_webserver(httpd_handle_t server) 168 | { 169 | // Stop the httpd server 170 | httpd_stop(server); 171 | } 172 | 173 | static void disconnect_handler(void* arg, esp_event_base_t event_base, 174 | int32_t event_id, void* event_data) 175 | { 176 | httpd_handle_t* server = (httpd_handle_t*) arg; 177 | if (*server) { 178 | ESP_LOGI(TAG, "Stopping webserver"); 179 | stop_webserver(*server); 180 | *server = NULL; 181 | } 182 | } 183 | 184 | static void connect_handler(void* arg, esp_event_base_t event_base, 185 | int32_t event_id, void* event_data) 186 | { 187 | httpd_handle_t* server = (httpd_handle_t*) arg; 188 | if (*server == NULL) { 189 | ESP_LOGI(TAG, "Starting webserver"); 190 | *server = start_webserver(); 191 | } 192 | } 193 | 194 | esp_err_t blinker_websocket_server_init(blinker_websocket_data_cb_t cb) 195 | { 196 | static httpd_handle_t server = NULL; 197 | 198 | if (server == NULL) { 199 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); 200 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); 201 | 202 | server = start_webserver(); 203 | } 204 | 205 | ws_cb = cb; 206 | 207 | return ESP_OK; 208 | } 209 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_ADD_INCLUDEDIRS include) 2 | set(COMPONENT_PRIV_INCLUDEDIRS src/port/esp8266 src/util) 3 | set(COMPONENT_SRCS "src/httpd_main.c" 4 | "src/httpd_parse.c" 5 | "src/httpd_sess.c" 6 | "src/httpd_txrx.c" 7 | "src/httpd_uri.c" 8 | "src/httpd_ws.c" 9 | "src/util/ctrl_sock.c") 10 | 11 | set(COMPONENT_PRIV_REQUIRES lwip) 12 | set(COMPONENT_REQUIRES http_parser) 13 | 14 | register_component() 15 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/Kconfig: -------------------------------------------------------------------------------- 1 | menu "HTTP Server" 2 | 3 | config HTTPD_MAX_REQ_HDR_LEN 4 | int "Max HTTP Request Header Length" 5 | default 512 6 | help 7 | This sets the maximum supported size of headers section in HTTP request packet to be processed by the server 8 | 9 | config HTTPD_MAX_URI_LEN 10 | int "Max HTTP URI Length" 11 | default 512 12 | help 13 | This sets the maximum supported size of HTTP request URI to be processed by the server 14 | 15 | config HTTPD_WS_SUPPORT 16 | bool "WebSocket server support" 17 | default n 18 | help 19 | This sets the WebSocket server support. 20 | endmenu 21 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/component.mk: -------------------------------------------------------------------------------- 1 | 2 | COMPONENT_SRCDIRS := src src/util 3 | COMPONENT_ADD_INCLUDEDIRS := include 4 | COMPONENT_PRIV_INCLUDEDIRS := src/port/esp8266 src/util 5 | 6 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/include/http_server.h: -------------------------------------------------------------------------------- 1 | #warning http_server.h has been renamed to esp_http_server.h, please update include directives 2 | #include "esp_http_server.h" 3 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/src/port/esp8266/osal.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef _OSAL_H_ 16 | #define _OSAL_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #define OS_SUCCESS ESP_OK 29 | #define OS_FAIL ESP_FAIL 30 | 31 | typedef TaskHandle_t othread_t; 32 | 33 | static inline int httpd_os_thread_create(othread_t *thread, 34 | const char *name, uint16_t stacksize, int prio, 35 | void (*thread_routine)(void *arg), void *arg) 36 | { 37 | int ret = xTaskCreate(thread_routine, name, stacksize, arg, prio, thread); 38 | if (ret == pdPASS) { 39 | return OS_SUCCESS; 40 | } 41 | return OS_FAIL; 42 | } 43 | 44 | /* Only self delete is supported */ 45 | static inline void httpd_os_thread_delete() 46 | { 47 | vTaskDelete(xTaskGetCurrentTaskHandle()); 48 | } 49 | 50 | static inline void httpd_os_thread_sleep(int msecs) 51 | { 52 | vTaskDelay(msecs / portTICK_RATE_MS); 53 | } 54 | 55 | static inline othread_t httpd_os_thread_handle() 56 | { 57 | return xTaskGetCurrentTaskHandle(); 58 | } 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif /* ! _OSAL_H_ */ 65 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/src/util/ctrl_sock.c: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "ctrl_sock.h" 23 | 24 | /* Control socket, because in some network stacks select can't be woken up any 25 | * other way 26 | */ 27 | int cs_create_ctrl_sock(int port) 28 | { 29 | int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 30 | if (fd < 0) { 31 | return -1; 32 | } 33 | 34 | int ret; 35 | struct sockaddr_in addr; 36 | memset(&addr, 0, sizeof(addr)); 37 | addr.sin_family = AF_INET; 38 | addr.sin_port = htons(port); 39 | inet_aton("127.0.0.1", &addr.sin_addr); 40 | ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); 41 | if (ret < 0) { 42 | close(fd); 43 | return -1; 44 | } 45 | return fd; 46 | } 47 | 48 | void cs_free_ctrl_sock(int fd) 49 | { 50 | close(fd); 51 | } 52 | 53 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len) 54 | { 55 | int ret; 56 | struct sockaddr_in to_addr; 57 | to_addr.sin_family = AF_INET; 58 | to_addr.sin_port = htons(port); 59 | inet_aton("127.0.0.1", &to_addr.sin_addr); 60 | ret = sendto(send_fd, data, data_len, 0, (struct sockaddr *)&to_addr, sizeof(to_addr)); 61 | 62 | if (ret < 0) { 63 | return -1; 64 | } 65 | return ret; 66 | } 67 | 68 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len) 69 | { 70 | int ret; 71 | ret = recvfrom(fd, data, data_len, 0, NULL, NULL); 72 | 73 | if (ret < 0) { 74 | return -1; 75 | } 76 | return ret; 77 | } 78 | -------------------------------------------------------------------------------- /ESP8266/components/esp_http_server/src/util/ctrl_sock.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * \file ctrl_sock.h 17 | * \brief Control Socket for select() wakeup 18 | * 19 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 20 | * sleeping on select. This is a common requirement for sending 21 | * control commands to a network server. This control socket API 22 | * facilitates the same. 23 | */ 24 | #ifndef _CTRL_SOCK_H_ 25 | #define _CTRL_SOCK_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /** 32 | * @brief Create a control socket 33 | * 34 | * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread 35 | * sleeping on select. This is a common requirement for sending 36 | * control commands to a network server. This control socket API 37 | * facilitates the same. 38 | * 39 | * This API will create a UDP control socket on the specified port. It 40 | * will return a socket descriptor that can then be added to your 41 | * fd_set in select() 42 | * 43 | * @param[in] port the local port on which the control socket will listen 44 | * 45 | * @return - the socket descriptor that can be added to the fd_set in select. 46 | * - an error code if less than zero 47 | */ 48 | int cs_create_ctrl_sock(int port); 49 | 50 | /** 51 | * @brief Free the control socket 52 | * 53 | * This frees up the control socket that was earlier created using 54 | * cs_create_ctrl_sock() 55 | * 56 | * @param[in] fd the socket descriptor associated with this control socket 57 | */ 58 | void cs_free_ctrl_sock(int fd); 59 | 60 | /** 61 | * @brief Send data to control socket 62 | * 63 | * This API sends data to the control socket. If a server is blocked 64 | * on select() with the control socket, this call will wake up that 65 | * server. 66 | * 67 | * @param[in] send_fd the socket for sending ctrl messages 68 | * @param[in] port the port on which the control socket was created 69 | * @param[in] data pointer to a buffer that contains data to send on the socket 70 | * @param[in] data_len the length of the data contained in the buffer pointed to be data 71 | * 72 | * @return - the number of bytes sent to the control socket 73 | * - an error code if less than zero 74 | */ 75 | int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len); 76 | 77 | /** 78 | * @brief Receive data from control socket 79 | * 80 | * This API receives any data that was sent to the control 81 | * socket. This will be typically called from the server thread to 82 | * process any commands on this socket. 83 | * 84 | * @param[in] fd the socket descriptor of the control socket 85 | * @param[in] data pointer to a buffer that will be used to store 86 | * received from the control socket 87 | * @param[in] data_len the maximum length of the data that can be 88 | * stored in the buffer pointed by data 89 | * 90 | * @return - the number of bytes received from the control socket 91 | * - an error code if less than zero 92 | */ 93 | int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len); 94 | 95 | #ifdef __cplusplus 96 | } 97 | #endif 98 | 99 | #endif /* ! _CTRL_SOCK_H_ */ 100 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 blinker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # blinker-esp-idf 2 | An IoT Solution,Blinker library for embedded hardware. Works with ESP8266, ESP32 (idf) 3 | 4 | Supprot ESP-IDF 5 | 6 | stable version support: 7 | - [ESP8266_RTOS_SDK v3.4](https://github.com/espressif/ESP8266_RTOS_SDK/releases/tag/v3.4) 8 | - [ESP_IDF v4.3/v4.4](https://dl.espressif.cn/dl/esp-idf/?idf=4.4) 9 | 10 | # TODO 11 | - BLINKER_PRO_ESP OTA support 12 | 13 | # Blinker hello example 14 | 15 | put the blinker componets into your projects. 16 | 17 | make the project list like this 18 | ``` 19 | Blinker_Hello 20 | |__components 21 | | |__blinker 22 | |__main 23 | ``` 24 | 25 | override the esp_http_server components with we provide. 26 | 27 | ``` 28 | idf.py menuconfig 29 | ``` 30 | 31 | enter Blinker Config 32 | 33 | * set BLINKER DEVICE TYPE 34 | 35 | * set BLINKER AUTH KEY 36 | 37 | * set Provisioning Type 38 | 39 | save and exit blinker config 40 | 41 | * Set Serial Flasher Options. 42 | 43 | enter components config 44 | 45 | * enable mDNS 46 | 47 | * disable Newlib 48 | 49 | * enable webSocket server support(http_server) 50 | 51 | save and exit 52 | 53 | ## Build and Flash 54 | 55 | Build the project and flash it to the board, then run monitor tool to view serial output: 56 | 57 | ``` 58 | idf.py -p PORT flash monitor 59 | ``` 60 | -------------------------------------------------------------------------------- /examples/Blinker_Hello/Hello_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Hello/Hello_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Hello/Hello_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Hello/Hello_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | 41 | static int count = 0; 42 | 43 | void button1_callback(const blinker_widget_param_val_t *val) 44 | { 45 | ESP_LOGI(TAG, "button state: %s", val->s); 46 | count++; 47 | 48 | cJSON *param = cJSON_CreateObject(); 49 | blinker_widget_switch(param, val->s); 50 | blinker_widget_print(BUTTON_1, param); 51 | cJSON_Delete(param); 52 | 53 | cJSON *num_param = cJSON_CreateObject(); 54 | blinker_widget_color(num_param, "#FF00FF"); 55 | blinker_widget_text(num_param, "按键测试"); 56 | blinker_widget_unit(num_param, "次"); 57 | blinker_widget_value_number(num_param, count); 58 | blinker_widget_print(NUM_1, num_param); 59 | cJSON_Delete(num_param); 60 | } 61 | 62 | static void data_callback(const char *data) 63 | { 64 | ESP_LOGI(TAG, "data: %s", data); 65 | } 66 | 67 | void app_main() 68 | { 69 | blinker_init(); 70 | 71 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button1_callback); 72 | blinker_data_handler(data_callback); 73 | } 74 | -------------------------------------------------------------------------------- /examples/Blinker_Hello/Hello_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Button/Button_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Button/Button_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Button/Button_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Button/Button_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | 41 | static int count = 0; 42 | 43 | void button1_callback(const blinker_widget_param_val_t *val) 44 | { 45 | ESP_LOGI(TAG, "button state: %s", val->s); 46 | count++; 47 | 48 | cJSON *param = cJSON_CreateObject(); 49 | blinker_widget_switch(param, val->s); 50 | blinker_widget_print(BUTTON_1, param); 51 | cJSON_Delete(param); 52 | 53 | cJSON *num_param = cJSON_CreateObject(); 54 | blinker_widget_color(num_param, "#FF00FF"); 55 | blinker_widget_text(num_param, "按键测试"); 56 | blinker_widget_unit(num_param, "次"); 57 | blinker_widget_value_number(num_param, count); 58 | blinker_widget_print(NUM_1, num_param); 59 | cJSON_Delete(num_param); 60 | } 61 | 62 | static void data_callback(const char *data) 63 | { 64 | ESP_LOGI(TAG, "data: %s", data); 65 | } 66 | 67 | void app_main() 68 | { 69 | blinker_init(); 70 | 71 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button_callback); 72 | blinker_data_handler(data_callback); 73 | } 74 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Button/Button_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Joystick/Joystick_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Joystick/Joystick_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Joystick/Joystick_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Joystick/Joystick_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | 41 | static int count = 0; 42 | 43 | void button1_callback(const blinker_widget_param_val_t *val) 44 | { 45 | ESP_LOGI(TAG, "button state: %s", val->s); 46 | count++; 47 | 48 | cJSON *param = cJSON_CreateObject(); 49 | blinker_widget_switch(param, val->s); 50 | blinker_widget_print(BUTTON_1, param); 51 | cJSON_Delete(param); 52 | 53 | cJSON *num_param = cJSON_CreateObject(); 54 | blinker_widget_color(num_param, "#FF00FF"); 55 | blinker_widget_text(num_param, "按键测试"); 56 | blinker_widget_unit(num_param, "次"); 57 | blinker_widget_value_number(num_param, count); 58 | blinker_widget_print(NUM_1, num_param); 59 | cJSON_Delete(num_param); 60 | } 61 | 62 | static void data_callback(const char *data) 63 | { 64 | ESP_LOGI(TAG, "data: %s", data); 65 | } 66 | 67 | void app_main() 68 | { 69 | blinker_init(); 70 | 71 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button_callback); 72 | blinker_data_handler(data_callback); 73 | } 74 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Joystick/Joystick_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_RGB/RGB_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_RGB/RGB_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_RGB/RGB_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_RGB/RGB_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | #define RGB_1 "rgb" 41 | 42 | static int count = 0; 43 | 44 | void button1_callback(const blinker_widget_param_val_t *val) 45 | { 46 | ESP_LOGI(TAG, "button state: %s", val->s); 47 | count++; 48 | 49 | cJSON *param = cJSON_CreateObject(); 50 | blinker_widget_switch(param, val->s); 51 | blinker_widget_print(BUTTON_1, param); 52 | cJSON_Delete(param); 53 | 54 | cJSON *num_param = cJSON_CreateObject(); 55 | blinker_widget_color(num_param, "#FF00FF"); 56 | blinker_widget_text(num_param, "按键测试"); 57 | blinker_widget_unit(num_param, "次"); 58 | blinker_widget_value_number(num_param, count); 59 | blinker_widget_print(NUM_1, num_param); 60 | cJSON_Delete(num_param); 61 | } 62 | 63 | static void data_callback(const char *data) 64 | { 65 | ESP_LOGI(TAG, "data: %s", data); 66 | } 67 | 68 | static void rgb_callback(const blinker_widget_param_val_t *val) 69 | { 70 | ESP_LOGI(TAG, "rgb r: %d, g: %d, b: %d, w: %d", val->array[0], val->array[1], val->array[2], val->array[3]); 71 | 72 | blinker_widget_rgb_print(RGB_1, val->array[0]/2, val->array[1]/2, val->array[2]/2, val->array[3]/3); 73 | } 74 | 75 | void app_main() 76 | { 77 | blinker_init(); 78 | 79 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button_callback); 80 | blinker_widget_add(RGB_1, BLINKER_RGB, rgb_callback); 81 | blinker_data_handler(data_callback); 82 | } 83 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_RGB/RGB_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Slider/Slider_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Slider/Slider_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Slider/Slider_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Slider/Slider_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | #define JOY_1 "joystick" 41 | 42 | static int count = 0; 43 | 44 | void button1_callback(const blinker_widget_param_val_t *val) 45 | { 46 | ESP_LOGI(TAG, "button state: %s", val->s); 47 | count++; 48 | 49 | cJSON *param = cJSON_CreateObject(); 50 | blinker_widget_switch(param, val->s); 51 | blinker_widget_print(BUTTON_1, param); 52 | cJSON_Delete(param); 53 | 54 | cJSON *num_param = cJSON_CreateObject(); 55 | blinker_widget_color(num_param, "#FF00FF"); 56 | blinker_widget_text(num_param, "按键测试"); 57 | blinker_widget_unit(num_param, "次"); 58 | blinker_widget_value_number(num_param, count); 59 | blinker_widget_print(NUM_1, num_param); 60 | cJSON_Delete(num_param); 61 | } 62 | 63 | static void data_callback(const char *data) 64 | { 65 | ESP_LOGI(TAG, "data: %s", data); 66 | } 67 | 68 | static void joystick_callback(const blinker_widget_param_val_t *val) 69 | { 70 | ESP_LOGI(TAG, "joystick x: %d, y: %d", val->array[0], val->array[1]); 71 | } 72 | 73 | void app_main() 74 | { 75 | blinker_init(); 76 | 77 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button_callback); 78 | blinker_widget_add(JOY_1, BLINKER_JOYSTICK, joystick_callback); 79 | blinker_data_handler(data_callback); 80 | } 81 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_Slider/Slider_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_TAB/TAB_WiFi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # The following four lines of boilerplate have to be in your project's CMakeLists 2 | # in this exact order for cmake to work correctly 3 | cmake_minimum_required(VERSION 3.5) 4 | 5 | include($ENV{IDF_PATH}/tools/cmake/project.cmake) 6 | project(blinker_example) 7 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_TAB/TAB_WiFi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := blinker_example 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_TAB/TAB_WiFi/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMPONENT_SRCS "blinker_example_main.c") 2 | 3 | register_component() 4 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_TAB/TAB_WiFi/main/blinker_example_main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************** 2 | * 3 | * Download latest Blinker library here: 4 | * https://github.com/blinker-iot/blinker-freertos/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 | * Docs: https://doc.blinker.app/ 14 | * https://github.com/blinker-iot/blinker-doc/wiki 15 | * 16 | * ***************************************************************** 17 | * 18 | * Blinker 库下载地址: 19 | * https://github.com/blinker-iot/blinker-freertos/archive/master.zip 20 | * 21 | * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、 22 | * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、 23 | * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。 24 | * 25 | * 文档: https://doc.blinker.app/ 26 | * https://github.com/blinker-iot/blinker-doc/wiki 27 | * 28 | * *****************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "blinker_api.h" 35 | 36 | static const char *TAG = "blinker"; 37 | 38 | #define BUTTON_1 "btn-abc" 39 | #define NUM_1 "num-abc" 40 | #define TAB_1 "tab" 41 | 42 | static int count = 0; 43 | 44 | void button1_callback(const blinker_widget_param_val_t *val) 45 | { 46 | ESP_LOGI(TAG, "button state: %s", val->s); 47 | count++; 48 | 49 | cJSON *param = cJSON_CreateObject(); 50 | blinker_widget_switch(param, val->s); 51 | blinker_widget_print(BUTTON_1, param); 52 | cJSON_Delete(param); 53 | 54 | cJSON *num_param = cJSON_CreateObject(); 55 | blinker_widget_color(num_param, "#FF00FF"); 56 | blinker_widget_text(num_param, "按键测试"); 57 | blinker_widget_unit(num_param, "次"); 58 | blinker_widget_value_number(num_param, count); 59 | blinker_widget_print(NUM_1, num_param); 60 | cJSON_Delete(num_param); 61 | } 62 | 63 | static void tab_callback(const blinker_widget_param_val_t *val) 64 | { 65 | ESP_LOGI(TAG, "tab0: %d, 1: %d, 2: %d, 3: %d, 4: %d", \ 66 | val->i >> 0 & 0x01, \ 67 | val->i >> 1 & 0x01, \ 68 | val->i >> 2 & 0x01, \ 69 | val->i >> 3 & 0x01, \ 70 | val->i >> 4 & 0x01); 71 | 72 | blinker_widget_tab_print(TAB_1, val->i >> 0 & 0x01, val->i >> 1 & 0x01, val->i >> 2 & 0x01, val->i >> 3 & 0x01, val->i >> 4 & 0x01); 73 | } 74 | 75 | static void data_callback(const char *data) 76 | { 77 | ESP_LOGI(TAG, "data: %s", data); 78 | } 79 | 80 | void app_main() 81 | { 82 | blinker_init(); 83 | 84 | blinker_widget_add(BUTTON_1, BLINKER_BUTTON, button_callback); 85 | blinker_widget_add(TAB_1, BLINKER_TAB, tab_callback); 86 | blinker_data_handler(data_callback); 87 | } 88 | -------------------------------------------------------------------------------- /examples/Blinker_Widgets/Blinker_TAB/TAB_WiFi/main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | -------------------------------------------------------------------------------- /version.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.01221211" 3 | } 4 | --------------------------------------------------------------------------------