開發快平臺(M302I小e開發板系列教程)php
開發塊平臺ESP8266模塊相關理解html
一. M302I小e開發板源碼註釋,源碼基於:v1.4.0.8-u34.zippython
1. user_main.capi
1 /****************************************************************************** 2 * Copyright 2013-2014 Espressif Systems (Wuxi) 3 * 4 * FileName: user_main.c 5 * 6 * Description: entry file of user application 7 * 8 * Modification history: 9 * 2014/12/1, v1.0 create this file. 10 *******************************************************************************/ 11 #include "esp_common.h" 12 13 #include "freertos/FreeRTOS.h" 14 #include "freertos/task.h" 15 #include "freertos/semphr.h" 16 17 #include "lwip/sockets.h" 18 #include "lwip/dns.h" 19 #include "lwip/netdb.h" 20 #include "driver/uart.h" 21 #include "espressif/esp_system.h" 22 #include "et_types.h" 23 24 #include "driver/i2c_master.h" 25 #include "driver/OLED_I2C.h" 26 27 #include "driver/RGB_light.h" 28 #include "driver/delay.h" 29 30 #include "user_config.h" 31 #include "driver/gpio.h" 32 #include "espressif/smartconfig.h" 33 #include "driver/i2s.h" 34 #include "factory.h" 35 #include "et_api_compatible.h" 36 #ifdef IR_DEMO 37 #include "driver/ir.h" 38 #endif 39 40 extern void et_user_main(void *pvParameters); 41 extern void read_uart_buf_task(void *pvParameters); 42 extern void send_to_mqtt_task(void *pvParameters); 43 44 LOCAL os_timer_t test_timer; 45 LOCAL WORK_MODE_T work_mode = WORK_MODE_BUTT; 46 47 extern et_cloud_handle g_cloud_handle; 48 extern et_int32 to_stop_app; 49 50 et_int8 air_kiss_start_flag = 0; 51 et_int8 user_main_start_flag = 0; 52 et_int8 wifi_reconnect_start_flag = 0; 53 54 /****************************************************************************** 55 * FunctionName : user_get_mode_str 56 * Description : Get mode string 57 * Parameters : 58 * Returns : NONE 59 *******************************************************************************/ 60 et_uchar* ICACHE_FLASH_ATTR 61 user_get_mode_str(et_uint32 mode) 62 { 63 et_uchar *mode_str[] = 64 { 65 "DEFAULT", 66 "AUDIO", 67 "RGB", 68 "BAROMETRIC", 69 "OLED", 70 "INVALID" 71 }; 72 73 return (WORK_MODE_BUTT <= mode ? mode_str[WORK_MODE_BUTT] : mode_str[mode]); 74 } 75 76 /****************************************************************************** 77 * FunctionName : user_esp_platform_check_ip 78 * Description : check whether get ip addr or not 79 * Parameters : none 80 * Returns : none 81 *******************************************************************************/ 82 void ICACHE_FLASH_ATTR 83 user_esp_platform_check_ip(void) 84 { 85 static et_uint32 time = 0; 86 et_uint32 connect_error_flag=0; 87 struct ip_info ipconfig; 88 89 os_timer_disarm(&test_timer); 90 //get ip info of ESP8266 station 91 wifi_get_ip_info(STATION_IF, &ipconfig); 92 if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) 93 { 94 os_printf("got ip !!! \r\n"); 95 if (user_main_start_flag == 0) 96 { 97 user_main_start_flag = 1; 98 xTaskCreate(et_user_main, "et_user_main", 1024, NULL, 2, NULL); 99 } 100 wifi_reconnect_start_flag = 1; 101 } 102 else 103 { 104 if(wifi_station_get_connect_status() == STATION_WRONG_PASSWORD) 105 { 106 if ((system_get_time() - time) >= 5000000) 107 { 108 os_printf("connect fail, wrong password!!! \r\n"); 109 time = system_get_time(); 110 } 111 connect_error_flag = 1; 112 } 113 else if(wifi_station_get_connect_status() == STATION_NO_AP_FOUND) 114 { 115 if ((system_get_time() - time) >= 5000000) 116 { 117 os_printf("connect fail, no AP found!!! \r\n"); 118 time = system_get_time(); 119 } 120 connect_error_flag = 1; 121 } 122 else if(wifi_station_get_connect_status() == STATION_CONNECT_FAIL) 123 { 124 if ((system_get_time() - time) >= 5000000) 125 { 126 os_printf("connect fail, connect fail!!! \r\n"); 127 time = system_get_time(); 128 } 129 connect_error_flag = 1; 130 } 131 132 if(connect_error_flag == 1) 133 { 134 if (air_kiss_start_flag == 1) 135 { 136 wifi_station_set_reconnect_policy(false); 137 smartconfig_stop(); 138 air_kiss_start_flag = 0; 139 if(work_mode == WORK_MODE_OLED) 140 { 141 OLED_clear(); 142 OLED_show_chn(0, 0, 15); //show 小e: 143 OLED_show_str(18, 0, "e:", 2); 144 OLED_show_chn(0, 2, 8); //show 網絡配置失敗 145 OLED_show_chn(18, 2, 9); 146 OLED_show_chn(36, 2, 10); 147 OLED_show_chn(54, 2, 11); 148 OLED_show_chn(72, 2, 24); 149 OLED_show_chn(90, 2, 25); 150 OLED_show_str(108, 2, " ", 2); 151 } 152 } 153 } 154 //re-arm timer to check ip 155 os_timer_setfn(&test_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL); 156 os_timer_arm(&test_timer, 1000, 0); 157 } 158 } 159 160 /****************************************************************************** 161 * FunctionName : user_get_run_mode 162 * Description : Get the current working mode 163 * Parameters : 164 * Returns : NONE 165 *******************************************************************************/ 166 et_uint32 ICACHE_FLASH_ATTR 167 user_get_run_mode() 168 { 169 return work_mode; 170 } 171 172 void audio_init(void) 173 { 174 //audio_key_init(); // SPEAKER init 175 i2s_audio_init(); 176 177 } 178 179 /****************************************************************************** 180 * FunctionName : user_init_work_mode 181 * Description : Initialization work mode 182 * Parameters : 183 * Returns : NONE 184 *******************************************************************************/ 185 et_int32 ICACHE_FLASH_ATTR 186 user_init_work_mode(et_uint32 mode, et_uchar fac_norm_mode) 187 { 188 if (WORK_MODE_BUTT <= mode) 189 { 190 os_printf("The work mode=%u is invalid !!!\n", mode); 191 return RETURN_ERR; 192 } 193 194 //#ifdef USER_PRINT_DEBUG 195 os_printf("get work mode=%s is success !!!\n", user_get_mode_str(mode)); 196 //#endif 197 198 switch (mode) 199 { 200 case WORK_MODE_DEFAULT: 201 DHT11_init(); 202 break; 203 204 case WORK_MODE_AUDIO: 205 DHT11_init(); 206 audio_init(); 207 break; 208 209 case WORK_MODE_RGB: 210 RGB_light_init(); // RGB init 211 DHT11_init(); 212 break; 213 214 case WORK_MODE_BAROMETRIC: 215 i2c_master_gpio_init(); // BAROMETRIC init 216 DHT11_init(); // temperature init 217 break; 218 219 case WORK_MODE_OLED: 220 i2c_master_gpio_init(); // I2C init 221 OLED_init(); // OLED init 222 OLED_clear(); 223 DHT11_init(); 224 OLED_show_chn(0, 3, 16); 225 OLED_show_chn(16, 3, 17); 226 OLED_show_chn(32, 3, 18); 227 OLED_show_chn(48, 3, 19); 228 OLED_show_chn(64, 3, 20); 229 OLED_show_chn(80, 3, 21); 230 OLED_show_chn(96, 3, 22); 231 OLED_show_chn(112, 3, 23); 232 break; 233 234 default: 235 break; 236 } 237 238 return RETURN_OK; 239 } 240 241 /****************************************************************************** 242 * FunctionName : user_get_work_mode 243 * Description : Get work mode from ADC 244 * Parameters : 245 * Returns : NONE 246 *******************************************************************************/ 247 et_int32 ICACHE_FLASH_ATTR 248 user_get_work_mode(et_uint32 *mode) 249 { 250 et_uint32 adc = system_adc_read(); 251 if (adc > 1024) 252 { 253 os_printf("The adc value=%u is invalid !!!\n", adc); 254 return RETURN_ERR; 255 } 256 257 #ifdef USER_PRINT_DEBUG 258 os_printf("get adc value=%u is success !!!\n", adc); 259 #endif 260 261 // ADC turn into work mode 262 if(adc < 100) 263 { 264 *mode = WORK_MODE_DEFAULT; 265 } 266 else if (adc < 350) 267 { 268 *mode = WORK_MODE_AUDIO; 269 } 270 else if(adc < 550) 271 { 272 *mode = WORK_MODE_RGB; 273 } 274 else if(adc < 750) 275 { 276 *mode = WORK_MODE_BAROMETRIC; 277 } 278 else if(adc < 1000) 279 { 280 *mode = WORK_MODE_OLED; 281 } 282 else 283 { 284 *mode = WORK_MODE_BUTT; 285 } 286 return RETURN_OK; 287 } 288 289 void ICACHE_FLASH_ATTR 290 smartconfig_done(sc_status status, void *pdata) 291 { 292 switch(status) 293 { 294 case SC_STATUS_WAIT: 295 os_printf("SC_STATUS_WAIT\n"); 296 break; 297 298 case SC_STATUS_FIND_CHANNEL: 299 set_wifi_spark_timer(300); 300 os_printf("SC_STATUS_FIND_CHANNEL\n"); 301 break; 302 303 case SC_STATUS_GETTING_SSID_PSWD: 304 os_printf("SC_STATUS_GETTING_SSID_PSWD\n"); 305 sc_type *type = pdata; 306 if (*type == SC_TYPE_ESPTOUCH) 307 { 308 os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n"); 309 } 310 else 311 { 312 os_printf("SC_TYPE:SC_TYPE_AIRKISS\n"); 313 } 314 break; 315 316 case SC_STATUS_LINK: 317 { 318 os_printf("SC_STATUS_LINK\n"); 319 struct station_config *sta_conf = pdata; 320 321 wifi_station_set_config(sta_conf); 322 wifi_station_disconnect(); 323 wifi_station_connect(); 324 } 325 break; 326 327 case SC_STATUS_LINK_OVER: { 328 os_printf("SC_STATUS_LINK_OVER\n"); 329 smartconfig_stop(); 330 if(work_mode == WORK_MODE_OLED) 331 { 332 OLED_clear(); 333 OLED_show_chn(0, 0, 15); //show 小e: 334 OLED_show_str(18, 0, "e:", 2); 335 OLED_show_chn(0, 2, 8); //show 網絡配置完成 336 OLED_show_chn(18, 2, 9); 337 OLED_show_chn(36, 2, 10); 338 OLED_show_chn(54, 2, 11); 339 OLED_show_chn(72, 2, 13); 340 OLED_show_chn(90, 2, 14); 341 OLED_show_str(108, 2, " ", 2); 342 } 343 delay_s(2); 344 system_restart(); 345 break; 346 } 347 } 348 349 } 350 351 void airkiss_key_init(key_gpio_t*key) 352 { 353 et_uint32 io_reg; 354 io_reg = GPIO_PIN_REG(key->key_num); 355 356 PIN_PULLUP_EN(io_reg); 357 PIN_FUNC_SELECT(io_reg, 0); 358 GPIO_AS_INPUT(key->key_gpio_pin); 359 } 360 361 void ICACHE_FLASH_ATTR 362 airkiss_key_poll_task(void *pvParameters) 363 { 364 et_uint32 value, i; 365 366 while(1) 367 { 368 value = gpio_get_value(AIRKISS_KEY_IO_NUM); 369 if(!air_kiss_start_flag && !value) 370 { 371 delay_s(1); 372 value = gpio_get_value(AIRKISS_KEY_IO_NUM); 373 if(!air_kiss_start_flag && !value) 374 { 375 os_printf("begin to airkiss\n"); 376 air_kiss_start_flag = 1; 377 os_timer_disarm(&test_timer); 378 to_stop_app = 1; //in airkiss mode, stop et_user_main thread 379 if(g_cloud_handle != NULL) 380 { 381 et_logout_cloud(g_cloud_handle); 382 et_destroy_context(g_cloud_handle); 383 g_cloud_handle = NULL; 384 } 385 delay_s(1); 386 wifi_reconnect_start_flag = 0; 387 smartconfig_start(smartconfig_done); //airkiss start 388 if(work_mode == WORK_MODE_OLED) 389 { 390 OLED_clear(); 391 OLED_show_chn(0, 0, 15); //show 小e: 392 OLED_show_str(18, 0, "e:", 2); 393 OLED_show_chn(0, 2, 8); //show 網絡配置中... 394 OLED_show_chn(18, 2, 9); 395 OLED_show_chn(36, 2, 10); 396 OLED_show_chn(54, 2, 11); 397 OLED_show_chn(72, 2, 12); 398 OLED_show_str(90, 2, "...", 2); 399 } 400 os_timer_setfn(&test_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL); 401 os_timer_arm(&test_timer, 1000, 0); 402 } 403 } 404 delay_ms(500); 405 } 406 os_printf("end airkiss\n"); 407 vTaskDelete(NULL); 408 } 409 410 void ICACHE_FLASH_ATTR 411 user_show_logo() 412 { 413 extern et_uchar BMP1[]; 414 et_uint32 len = 1024; // BMP1 member 415 416 i2c_master_gpio_init(); // I2C init 417 OLED_init(); // OLED init 418 OLED_clear(); 419 420 // show logo 421 OLED_show_bmp(0, 0, 128, 8, BMP1, len); 422 } 423 424 void et_wifi_event_cb(System_Event_t *event) 425 { 426 switch(event->event_id) 427 { 428 case EVENT_STAMODE_SCAN_DONE: //ESP8266 station finish scanning AP 429 430 break; 431 case EVENT_STAMODE_CONNECTED: //ESP8266 station connected to AP 432 433 os_printf("et connect to ssid %s, channel %d\n", event->event_info.connected.ssid, event->event_info.connected.channel); 434 break; 435 case EVENT_STAMODE_DISCONNECTED: //ESP8266 station disconnected to AP 436 disarm_wifi_spark_timer(); 437 wifi_led_off(); 438 if(true != wifi_station_get_reconnect_policy()) 439 { 440 os_printf("et wifi set to reconnect\n"); 441 wifi_station_set_reconnect_policy(true); 442 } 443 //os_printf("et disconnect from ssid %s, reason %d\n", event->event_info.disconnected.ssid, event->event_info.disconnected.reason); 444 if(wifi_reconnect_start_flag != 1) 445 { 446 os_printf("airkiss start or start first don't restart %d\n",wifi_reconnect_start_flag); 447 } 448 else 449 { 450 os_printf("et wifi station connect status %d, restart system\n",wifi_station_get_connect_status()); 451 system_restart(); 452 } 453 break; 454 case EVENT_STAMODE_AUTHMODE_CHANGE: //the auth mode of AP connected by ESP8266 station changed 455 os_printf("mode: %d -> %d\n", event->event_info.auth_change.old_mode, event->event_info.auth_change.new_mode); 456 break; 457 case EVENT_STAMODE_GOT_IP: //ESP8266 station got IP from connected AP 458 set_wifi_spark_timer(1000); 459 //os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR, IP2STR(&event->event_info.got_ip.ip), IP2STR(&event->event_info.got_ip.mask), 460 // IP2STR(&event->event_info.got_ip.gw)); 461 break; 462 // case EVENT_STAMODE_DHCP_TIMEOUT: //ESP8266 station dhcp client got IP timeout 463 // break; 464 case EVENT_SOFTAPMODE_STACONNECTED: //a station connected to ESP8266 soft-AP 465 os_printf("et station: " MACSTR "join, AID = %d\n", MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid); 466 break; 467 case EVENT_SOFTAPMODE_STADISCONNECTED: //a station disconnected to ESP8266 soft-AP 468 os_printf("et station: " MACSTR "leave, AID = %d\n", MAC2STR(event->event_info.sta_disconnected.mac), event->event_info.sta_disconnected.aid); 469 break; 470 // case EVENT_SOFTAPMODE_PROBEREQRECVED: 471 // break; 472 // case EVENT_MAX: 473 // break; 474 default: 475 break; 476 } 477 } 478 479 #ifdef IR_DEMO 480 void ir_tx_key(void *pvParameters) 481 { 482 et_uchar value=0; 483 484 ir_tx_msg_t tx_data; 485 tx_data.ir_tx_addr = 0x55; 486 tx_data.ir_tx_data = 0x28; 487 tx_data.ir_tx_rep = 0; 488 489 while(1) 490 { 491 value = gpio_get_value(AIRKISS_KEY_IO_NUM); 492 if(!value) 493 { 494 delay_ms(200); 495 value = gpio_get_value(AIRKISS_KEY_IO_NUM); 496 if(!value) 497 { //Always press down 498 ir_tx_func(&tx_data); 499 } 500 } 501 delay_ms(500); 502 } 503 504 vTaskDelete(NULL); 505 506 } 507 #endif 508 509 /****************************************************************************** 510 * FunctionName : user_init 511 * Description : entry of user application, init user function here 512 * Parameters : none 513 * Returns : none 514 *******************************************************************************/ 515 void user_init(void) 516 { 517 os_printf("software version:%s\n", SOFTWARE_VERSION); //打印版本信息 518 519 et_uchar result=0; 520 521 if(get_fac_norm_mode(&result) != SPI_FLASH_RESULT_OK) //檢測工做模式 522 { 523 os_printf("get_fac_norm_mode error, NORMAL mode\n"); 524 } 525 526 if(result == FAC_MODE) //運行在工廠模式 527 { 528 os_printf("run in factory mode\n"); 529 uart_init_new_uart1(BIT_RATE_115200); //設置串口1工做模式 530 UART_SetPrintPort(UART1); //設置串口1爲打印 531 uart_init_new(BIT_RATE_115200, result); //設置串口0工做模式 532 return; 533 } 534 535 os_printf("run in normal mode\n"); //運行在正常模式 536 537 //if define IR_DEMO, ir rx or tx test //定義紅外Demo測試 538 #ifdef IR_DEMO 539 struct station_config config; 540 541 memset(&config, 0, sizeof(config)); 542 wifi_set_opmode(STATION_MODE); 543 wifi_station_set_config_current(&config); 544 545 //if define IR_RX, ir rx test 546 #ifdef IR_RX 547 ir_rx_init(); 548 549 //ir tx test 550 #else 551 ir_tx_init(0); 552 xTaskCreate(ir_tx_key, "ir_tx_key", 256, NULL, 2, NULL); 553 #endif 554 555 #else //沒有定義紅外Demo測試 556 key_gpio_t key; 557 struct station_config config; 558 struct ip_info info; 559 560 // show logo 561 user_show_logo(); //OLED顯示Logo 562 563 if (RETURN_OK != user_get_work_mode(&work_mode)) //獲取工做模式(因使用按鍵進行設置工做模式) 564 { 565 os_printf("get work mode fail !!!\n"); 566 return; 567 } 568 569 if (RETURN_OK != user_init_work_mode(work_mode, result)) //根據工做模式來對模塊進行初始化操做 570 { 571 os_printf("init work mode fail !!!\n"); 572 return; 573 } 574 575 //wifi event handle 576 //WIFI 的配置,首先硬件查找一下FLASH裏面有沒有存儲當前局域網有的WIFI有的話鏈接它,沒有就執行下一步 577 wifi_set_event_handler_cb(et_wifi_event_cb); 578 579 memset(&key, 0, sizeof(key_gpio_t)); 580 key.key_gpio_pin = AIRKISS_KEY_IO_PIN; 581 key.key_num = AIRKISS_KEY_IO_NUM; 582 airkiss_key_init(&key); 583 //對 WIFI 考慮到了兩種狀況, 一種是當前smartconfig 軟件配置一下。調用了一個任務函數, 用來鏈接網絡, 584 wifi_set_opmode(STATION_MODE); 585 wifi_reconnect_start_flag = 0; 586 xTaskCreate(airkiss_key_poll_task, "smartconfig_task", 256, NULL, 2, NULL); 587 588 589 wifi_led_init(); 590 memset(&config, 0, sizeof(struct station_config)); 591 if(wifi_station_get_config_default(&config) == true) 592 { 593 os_printf("ssid=%s\n", config.ssid); 594 wifi_station_set_config_current(&config); 595 //for static ip set 596 /*wifi_station_dhcpc_stop(); 597 IP4_ADDR(&info.ip, 192, 168, 1, 43); 598 IP4_ADDR(&info.gw, 192, 168, 1, 1); 599 IP4_ADDR(&info.netmask, 255, 255, 255, 0); 600 wifi_set_ip_info(STATION_IF, &info);*/ 601 } 602 603 os_timer_disarm(&test_timer); 604 os_timer_setfn(&test_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL); 605 os_timer_arm(&test_timer, 1000, 0); 606 #endif 607 }
2. et_user_app.c服務器
1 /******************************************************************************* 2 * Copyright (c) 2012, 2013 Beidouapp Corp. 3 * 4 * All rights reserved. 5 * 6 * Contributors: 7 * Peter Peng - initial contribution 8 *******************************************************************************/ 9 10 #include <stdio.h> 11 #include <string.h> 12 #include <stdlib.h> 13 14 #include "esp_common.h" 15 16 #include "freertos/FreeRTOS.h" 17 #include "freertos/task.h" 18 #include "freertos/queue.h" 19 #include "freertos/semphr.h" 20 #include "driver/uart.h" 21 22 #include "user_config.h" 23 #include "et_fifo.h" 24 #include "lwip/netdb.h" 25 #include "factory.h" 26 #include "espressif/upgrade.h" 27 #include "et_api_compatible.h" 28 29 #define MAX_USER_ID 32 30 #define MAX_ILINK_CONNECT 5 31 32 et_char g_user_id[ET_USER_ID_LEN_MAX] = {0}; 33 et_char g_group_id[ET_USER_ID_LEN_MAX] = {0}; 34 et_uint32 g_group_message = 0; 35 struct hostent *file_server_ip_addr = NULL; 36 msg_to_net_t msg={0}; 37 et_uchar code[32]={0}; 38 extern et_int32 read_uart_data_flag; 39 extern xQueueHandle xQueueUart; 40 et_cloud_handle g_cloud_handle = NULL; 41 et_cloud_connect_type g_cloud_con_para = {ET_FALSE, ET_TRUE, 90}; 42 et_uchar kick_out=0; 43 et_dfs_file_info_type i_file_info; 44 45 struct ilink_connect_client 46 { 47 et_int32 state; 48 et_char user_id[MAX_USER_ID]; 49 50 }; 51 52 static struct ilink_connect_client g_ilink_connect_clients[MAX_ILINK_CONNECT]; 53 54 static et_uchar uart_recv_buff[UART_MAX_READ_BUFFER] = {0}; 55 static et_uchar uart_send_buff[UART_MAX_SEND_BUFFER] = {0}; 56 et_int32 to_stop_app = 0; 57 58 et_uint32 write_flash_end=0; 59 et_int64 file_total_size=0; 60 et_uint32 audio_voice_data=0; 61 et_uint16 sector=AUDIO_FLASH_START_SECTOR; 62 63 //打印16進制 64 void print_hex(et_uchar *label, et_uchar *str, et_int32 len) 65 { 66 et_int32 i; 67 68 os_printf("%s : ", label); 69 for(i = 0; i < len; i++) 70 os_printf("%02x ", str[i]); 71 os_printf("\n"); 72 } 73 74 //Check數據校驗 75 et_uchar check_sum(et_uchar *buf, et_int32 len) 76 { 77 et_uchar bcc=0; 78 et_int32 i; 79 80 for(i = 0; i < len; i ++) 81 bcc ^= buf[i]; 82 83 return bcc; 84 } 85 86 // ascii to 8421bcd,say 0x32 = 50 87 et_uint32 ascii_2_dec(et_int32 ascii) 88 { 89 et_int32 n =1, dec=0; 90 91 while(ascii > 0) 92 { 93 dec += (ascii % 10) * n; 94 ascii /=10; 95 n *= 16; 96 } 97 98 return dec; 99 } 100 101 //爲服務器準備數據 102 static et_int32 ack_to_mqtt(et_uchar *msg_buf, et_uchar *ack_buf, et_int32 rc) 103 { 104 et_int32 len = START_LEN; 105 106 ack_buf[0] = 0xFF; 107 ack_buf[1] = 0xFF; 108 ack_buf[2] = 0x00; 109 ack_buf[3] = ACK_ERROR_LEN; 110 ack_buf[4] = msg_buf[4] | 0x0F; 111 ack_buf[5] = msg_buf[5]; 112 ack_buf[6] = msg_buf[6]; 113 ack_buf[7] = rc; 114 ack_buf[8] = check_sum(&ack_buf[2], ACK_ERROR_LEN + 1); 115 len += ACK_ERROR_LEN; 116 117 return len; 118 119 } 120 121 //爲服務器準備溫溼度數據 122 static et_int32 ack_temp_hum_to_mqtt(et_uchar *msg_buf, et_uchar *temp_hum, et_uchar *ack_buf, et_uchar rc) 123 { 124 et_int32 len=START_LEN; 125 126 ack_buf[0] = 0xFF; 127 ack_buf[1] = 0xFF; 128 ack_buf[2] = 0x00; 129 ack_buf[3] = ACK_TEMP_HUM_LEN; 130 ack_buf[4] = msg_buf[4] | 0x0F; 131 ack_buf[5] = msg_buf[5]; 132 ack_buf[6] = msg_buf[6]; 133 ack_buf[7] = rc; 134 ack_buf[8] = ascii_2_dec(temp_hum[0]); //we transform to 8421bcd 0x50 135 ack_buf[9] = ascii_2_dec(temp_hum[1]); 136 ack_buf[10] = ascii_2_dec(temp_hum[2]); 137 ack_buf[11] = ascii_2_dec(temp_hum[3]); 138 ack_buf[12] = check_sum(&ack_buf[2], ACK_TEMP_HUM_LEN + 1); 139 len += ACK_TEMP_HUM_LEN; 140 141 return len; 142 } 143 144 //爲服務器準備氣壓傳感器數據 145 static et_int32 ack_barometric_to_mqtt(et_uchar *msg_buf, et_int32 barometric, et_int32 temp, et_uchar *ack_buf, et_uchar rc) 146 { 147 et_uchar baro[4]={0}, t; 148 et_int32 i, tmp, len=START_LEN; 149 150 tmp = ascii_2_dec(barometric); 151 for(i = 0; i < 4; i++) 152 baro[i] = tmp >> (3 - i) * 8; 153 154 tmp = ascii_2_dec(temp); 155 t = tmp; 156 157 ack_buf[0] = 0xFF; 158 ack_buf[1] = 0xFF; 159 ack_buf[2] = 0x00; 160 ack_buf[3] = ACK_BAROMETRIC_LEN; 161 ack_buf[4] = msg_buf[4] | 0x0F; 162 ack_buf[5] = msg_buf[5]; 163 ack_buf[6] = msg_buf[6]; 164 ack_buf[7] = rc; 165 ack_buf[8] = baro[0]; 166 ack_buf[9] = baro[1]; 167 ack_buf[10] = baro[2]; 168 ack_buf[11] = baro[3]; 169 ack_buf[12] = t; 170 ack_buf[13] = check_sum(&ack_buf[2], ACK_BAROMETRIC_LEN + 1); 171 len += ACK_BAROMETRIC_LEN; 172 173 return len; 174 } 175 176 //上傳回調函數 177 void upgrade_callback(void *arg) 178 { 179 struct upgrade_server_info *server = arg; 180 181 if(server->upgrade_flag == true) 182 { 183 printf("upgrade success.\n"); 184 system_upgrade_reboot(); 185 } 186 else 187 printf("upgrade failed.\n"); 188 189 free(server->url); 190 server->url = NULL; 191 } 192 193 //解析服務器發送信息 194 et_int32 parse_msg_from_mqtt(et_uchar *msg_buf, et_int32 data_len) 195 { 196 et_int32 i, pos=0, rc = -1; 197 et_uchar cmd, type, bcc; 198 et_int32 len, seq; 199 et_uint16 gb_code=0; 200 WORK_MODE_T mode; 201 202 #ifdef USER_PRINT_DEBUG 203 print_hex("msg_buf", msg_buf, data_len); 204 #endif 205 206 if(msg_buf[pos] != 0xFF ||msg_buf[pos + 1] != 0xFF) 207 { 208 os_printf("parse packet head error\n"); 209 return rc; 210 } 211 pos += 2; 212 213 len = (msg_buf[pos] << 8) | msg_buf[pos + 1]; 214 if(len < 3 || len != data_len - 2 - 2) 215 { 216 os_printf("parse packet length error\n"); 217 return rc; 218 } 219 220 bcc = check_sum(&msg_buf[pos], len + 2 - 1); 221 if(bcc != msg_buf[data_len - 1]) 222 { 223 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_BCC_ERROR); 224 os_printf("bcc error\n"); 225 rc = et_chat_to(g_cloud_handle,msg.buf, msg.len, g_user_id, SEND_TO_ALL); 226 return rc; 227 } 228 229 pos += 2; 230 231 cmd = msg_buf[pos]; 232 pos += 1; 233 234 seq += msg_buf[pos]; 235 pos += 1; 236 237 type = msg_buf[pos]; 238 pos += 1; 239 240 mode = user_get_run_mode(); //get board mode from adc 241 switch(cmd) 242 { 243 case CMD_VER_UPDATE_NOTE: //版本升級事件 244 { 245 et_uint version_len= len - 4; 246 et_uchar version[8]={0}, user_bin[16]={0}, bin=0; 247 struct upgrade_server_info server={0}; 248 249 strncpy(version, &msg_buf[pos], version_len); 250 if(strcmp(version, SOFTWARE_VERSION)) 251 { 252 server.sockaddrin.sin_addr.s_addr = inet_addr("192.168.13.103"); 253 server.sockaddrin.sin_port = htons(80); 254 server.sockaddrin.sin_family = AF_INET; 255 server.check_cb = upgrade_callback; 256 server.check_times = 120000; 257 strncpy(server.pre_version, SOFTWARE_VERSION, strlen(SOFTWARE_VERSION)); 258 strncpy(server.upgrade_version, version, version_len); 259 if(server.url == NULL) 260 server.url = (et_uchar *)malloc(512); 261 memset(server.url, 0, 128); 262 263 bin = system_upgrade_userbin_check(); 264 if(bin == UPGRADE_FW_BIN1) 265 { 266 memcpy(user_bin, "test/user2.bin", 16); 267 } 268 else if(bin == UPGRADE_FW_BIN2) 269 { 270 memcpy(user_bin, "test/user1.bin", 16); 271 } 272 273 sprintf(server.url, "GET /%s HTTP/1.0\r\nHost: %s:%d\r\n"pheadbuffer"", user_bin, "192.168.13.103", 80); 274 275 if(system_upgrade_start(&server) == false) 276 { 277 printf("upgrade is already started\n"); 278 } 279 280 } 281 } 282 break; 283 284 case CMD_CONTROL: //控制命令 285 { 286 switch(type) 287 { 288 case TYPE_RGB_LIGHT_DEV: //RGB_light 289 { 290 if(mode == WORK_MODE_RGB) 291 { //code undefine 292 et_uchar red; 293 et_uchar gre; 294 et_uchar blu; 295 296 red = msg_buf[pos]; 297 pos += 1; 298 gre = msg_buf[pos]; 299 pos += 1; 300 blu = msg_buf[pos]; 301 302 RGB_light_set_color(red, gre, blu); //set rgb color 303 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_SUCCESS); 304 rc = msg.len; 305 } 306 else 307 { 308 printf("mode error, mode = %u\n", mode); 309 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_MODE_ERR); 310 rc = msg.len; 311 } 312 } 313 break; 314 315 case TYPE_OLED_DEV: //OLED 316 { 317 et_uchar z_code, b_code; 318 et_uint offset; 319 et_uchar num,i=0,j,line,colum[4]={0}; 320 321 //OLED 128x64, character is 16x16, per line can show 128/16 =8 character, 322 //in total lines is 64/16 = 4, the OLED can display 32 charaters in total 323 if(mode == WORK_MODE_OLED) 324 { 325 num = (len - 4) / 2; //numbers of characters 326 if(num == 0) 327 break; 328 329 if(num > 32) 330 num = 32; 331 332 line = (num - 1) /8 + 1; 333 334 for(i = 0; i < line - 1; i++) //0 to line -1 is 8 characters 335 { 336 colum[i] = 8; 337 } 338 339 colum[i] = num % 8; //the last line 340 if(colum[i] == 0) 341 colum[i] = 8; 342 343 OLED_clear(); //clear oled 344 for(i = 0; i < line; i++) 345 { 346 for(j = 0; j < colum[i] ; j++) 347 { 348 z_code = msg_buf[pos++]; 349 b_code = msg_buf[pos++]; 350 351 gb_code = (z_code << 8) | b_code; 352 if(gb_code > 0x3759) //after 0xd7f9 ,five space 353 { 354 gb_code -= 5; 355 if(b_code >= 0x01 && b_code <= 0x04) 356 gb_code -= 0xa2; 357 } 358 359 z_code = (gb_code >> 8) & 0xff; 360 b_code = gb_code & 0xff; 361 offset = ((et_uint32)94 * (z_code - 0x10) + b_code - 0x01)*(16 * 2); 362 spi_flash_read(GB_DZK_START_SECTOR * SPI_FLASH_SEC_SIZE + offset, (et_uint *)code , 32); 363 oled_show_gb_chn(16 * j, 2 * i, code); 364 } 365 } 366 367 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_SUCCESS); 368 rc = msg.len; 369 } 370 else 371 { 372 os_printf("mode error, mode = %d\n", mode); 373 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_MODE_ERR); 374 rc = msg.len; 375 } 376 } 377 break; 378 379 default: 380 break; 381 } 382 } 383 break; 384 385 case CMD_QUERY: //查詢命令 386 { 387 switch(type & 0xF0) 388 { 389 case TYPE_TEMP_HUM_SENSOR: //溫溼度傳感器 390 { 391 et_uchar temp_hum[HUM_DATA_SIZE]={0}; 392 393 if(DHT11_read_temp_hum(temp_hum, HUM_DATA_SIZE) != RETURN_OK) 394 { 395 os_printf("DHT11_read_temp_hum error\n"); 396 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_DEV_FAILED); 397 rc = msg.len; 398 break; 399 } 400 401 msg.len = ack_temp_hum_to_mqtt(msg_buf, temp_hum, msg.buf, ACK_SUCCESS); 402 rc = msg.len; 403 404 if(mode == WORK_MODE_OLED) 405 { 406 et_uchar display_temp[7]={0}; 407 et_uchar display_hum[8]={0}; 408 sprintf(display_temp, ":%d.%d", temp_hum[2], temp_hum[3]); 409 sprintf(display_hum, ":%d.%d%%", temp_hum[0], temp_hum[1]); 410 OLED_clear(); //clear oled 411 switch(type & 0x0F) 412 { 413 case TEMP_AND_HUM: //溫度和溼度 414 { 415 //display tempeature and hummity, such as: 416 //溫度:20.08℃ 417 //溼度:60.89% 418 OLED_show_chn(0, 0, 15); //show 小e: 419 OLED_show_str(18, 0, "e:", 2); 420 OLED_show_chn(0, 2, 0); //溫 421 OLED_show_chn(18, 2, 1); //度 422 OLED_show_str(36, 2, display_temp, 2); //such as :20.08 423 OLED_show_chn(86, 2, 2); //℃ 424 425 OLED_show_chn(0, 4, 3); //溼 426 OLED_show_chn(18, 4, 4); //度 427 OLED_show_str(36, 4, display_hum, 2);//such as :80.05% 428 429 } 430 break; 431 432 case TEMPERATUR: //溫度 433 { 434 OLED_show_chn(0, 0, 15); //show 小e: 435 OLED_show_str(18, 0, "e:",2 ); 436 OLED_show_chn(0, 2, 0); //溫 437 OLED_show_chn(18, 2, 1); //度 438 OLED_show_str(36, 2, display_temp, 2); //such as :20.08 439 OLED_show_chn(86, 2, 2); //℃ 440 } 441 break; 442 443 case HUMMITY: //溼度 444 { 445 OLED_show_chn(0, 0, 15); //show 小e: 446 OLED_show_str(18, 0, "e:", 2); 447 OLED_show_chn(0, 2, 3); //溼 448 OLED_show_chn(18, 2, 4); //度 449 OLED_show_str(36, 2, display_hum, 2);//such as :80.05% 450 } 451 break; 452 } 453 } 454 } 455 break; 456 457 case TYPE_BAROMETRIC_SENSOR: //氣壓傳感器 458 { 459 et_long32 barometric=0, temperature=0; 460 if(mode == WORK_MODE_BAROMETRIC) 461 { 462 barometric = barometric_collect(&temperature); 463 if(barometric == -1) 464 { 465 os_printf("barometric_collect error\n"); 466 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_DEV_FAILED); 467 rc = msg.len; 468 break; 469 } 470 msg.len = ack_barometric_to_mqtt(msg_buf, (et_int32)barometric, (et_int32)temperature, msg.buf, ACK_SUCCESS); 471 rc = msg.len; 472 } 473 else 474 { 475 os_printf("mode error, mode = %d\n", mode); 476 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_MODE_ERR); 477 rc = msg.len; 478 } 479 } 480 break; 481 482 default: 483 break; 484 } 485 486 } 487 break; 488 489 default: 490 msg.len = ack_to_mqtt(msg_buf, msg.buf, ACK_CMD_ILLIGAL); 491 rc = msg.len; 492 break; 493 } 494 495 //上傳信息 496 rc = et_chat_to(g_cloud_handle,msg.buf, msg.len, g_user_id, SEND_TO_CLOUD_FIRST); 497 498 return rc; 499 } 500 501 et_int32 add_userid_to_clients(et_char *userid) 502 { 503 et_int32 i = 0,j = 0; 504 et_int32 ret = -1; 505 et_char *str = "now device has max users"; 506 //et_int32 add_flag = 0; 507 508 for(i = 0; i < MAX_ILINK_CONNECT;i++) 509 { 510 if(g_ilink_connect_clients[i].state == 1) 511 { 512 ret = strcmp(userid,g_ilink_connect_clients[i].user_id); 513 if(ret == 0) 514 { 515 return -1; 516 } 517 } 518 519 } 520 521 for(j = 0; j < MAX_ILINK_CONNECT;j ++) 522 { 523 if(g_ilink_connect_clients[j].state == -1) 524 { 525 g_ilink_connect_clients[j].state = 1; 526 strcpy(g_ilink_connect_clients[j].user_id,userid); 527 os_printf("now add userid is %s\n",g_ilink_connect_clients[j].user_id); 528 break; 529 530 } 531 532 } 533 534 if(j >= MAX_ILINK_CONNECT) 535 { 536 et_chat_to(g_cloud_handle,str,strlen(str),userid,SEND_TO_CLOUD); 537 return -1; 538 } 539 else 540 { 541 return 0; 542 } 543 } 544 545 et_int32 remove_userid_clients(et_char *userid) 546 { 547 et_int32 i = 0; 548 et_int32 ret = -1; 549 for(i = 0; i < MAX_ILINK_CONNECT;i++) 550 { 551 if(g_ilink_connect_clients[i].state == 1) 552 { 553 ret = strcmp(userid,g_ilink_connect_clients[i].user_id); 554 if(ret == 0) 555 { 556 os_printf("now userid is %s\n",g_ilink_connect_clients[i].user_id); 557 g_ilink_connect_clients[i].state = -1; 558 memset(g_ilink_connect_clients[i].user_id,0,sizeof(g_ilink_connect_clients[i].user_id)); 559 os_printf("now status is %d\n",g_ilink_connect_clients[i].state); 560 return 0; 561 } 562 } 563 564 } 565 566 return -1; 567 568 } 569 570 void read_uart_buf_task(void *pvParameters) 571 { 572 os_event_t e; 573 et_int32 rc = 0; 574 et_int32 cmd,seq,len; 575 et_uchar *data; 576 577 for (;;) 578 { 579 if (xQueueReceive(xQueueUart, (void *)&e, (portTickType)portMAX_DELAY)) 580 { 581 switch (e.event) 582 { 583 case UART_EVENT_RX_CHAR: 584 { 585 read_uart_data_flag = 1; 586 memset(uart_recv_buff,0,sizeof(uart_recv_buff)); 587 rc = recv_data_uart(uart_recv_buff, e.param, e.param * 50); 588 read_uart_data_flag = 0; 589 printf("receive uart %d\n", rc); 590 } 591 break; 592 593 default: 594 break; 595 } 596 } 597 } 598 599 vTaskDelete(NULL); 600 } 601 602 void init_clients(void) 603 { 604 et_int32 j = 0; 605 606 for(j = 0; j < MAX_ILINK_CONNECT;j ++) 607 { 608 g_ilink_connect_clients[j].state = -1; 609 memset(g_ilink_connect_clients[j].user_id,0,sizeof(g_ilink_connect_clients[j].user_id)); 610 611 } 612 613 } 614 615 //寫Flash回調函數 616 et_int32 write_flash_callback (void *arg, const et_int64 file_size, const et_char *data, const et_int32 current_size) 617 { 618 et_int32 result=-1; 619 620 if (arg == NULL) 621 { 622 return EINVAL; 623 } 624 625 if(sector > AUDIO_FLASH_STOP_SECTOR) //超過flash,flash從新覆蓋初始地址 626 { 627 file_total_size += current_size; 628 if(file_total_size >= file_size) 629 { 630 sector = AUDIO_FLASH_START_SECTOR; 631 file_total_size = AUDIO_FLASH_LEN - 1; 632 audio_voice_data = 1; //to inform interrupt of voice data coming //越界不播放,仍然下載完,只是覆蓋 633 write_flash_end = 1; //to inform interrupt of being able to send voice data to i2s 634 printf("file size > flash size.just play length of flash size(the former 45 seconds).\n"); 635 } 636 637 return 0; 638 } 639 640 result = spi_flash_erase_sector(sector); 641 if(result != SPI_FLASH_RESULT_OK) 642 { 643 printf("spi_flash_erase_sector error\n"); 644 return -1; 645 } 646 647 result = spi_flash_write(sector * SPI_FLASH_SEC_SIZE, (et_uint32 *)data, current_size); //write received voice data(4096 bytes) to flash 648 if(result != SPI_FLASH_RESULT_OK) 649 { 650 printf("spi_flash_write error\n"); 651 return -1; 652 } 653 654 file_total_size += current_size; 655 sector++; 656 657 if(file_total_size >= file_size) //下載完成 658 { 659 if(file_size > AUDIO_FLASH_LEN) //文件總長度打印flash長度,只播放音頻的flash長度 660 file_total_size = AUDIO_FLASH_LEN; 661 662 printf("down load file end.\n"); 663 sector = AUDIO_FLASH_START_SECTOR; 664 audio_voice_data = 1; //to inform interrupt of voice data coming 665 write_flash_end = 1; //to inform interrupt of being able to send voice data to i2s 666 } 667 668 return 0; 669 } 670 671 //消息處理任務 672 //其實對於消息和時間的分支定義,在 smart_e_taste_v1.4.x.x\et_app\include 的 et_types.h 的文件裏。開發快官方寫得很是詳細。 673 void et_message_process(et_int32 type, et_char *send_userid, et_char *topic_name, et_int32 topic_len, et_context_mes_type *message) 674 { 675 int rc = -1; 676 677 switch(type) 678 { 679 case MES_CHAT_TO_CHAT: //點對點消息事件 680 { 681 os_printf("mes chat to chat: %s\n", send_userid); 682 memset(g_user_id, 0, sizeof(g_user_id)); 683 snprintf(g_user_id, sizeof(g_user_id), "%s",send_userid); 684 parse_msg_from_mqtt(message->payload, message->payload_len); 685 } 686 break; 687 688 case MES_FILE_TRANSFERS: //文件傳輸消息事件 689 { 690 if(user_get_run_mode() != WORK_MODE_AUDIO) 691 { 692 printf("run in mode %d ,error\n", user_get_run_mode()); 693 break; 694 } 695 696 os_printf("File trans mes from %s:%s\n", send_userid, topic_name); 697 memset(&i_file_info, 0, sizeof(et_dfs_file_info_type)); 698 rc = et_file_info(g_cloud_handle,message->payload, &i_file_info); 699 if(rc == -1) 700 { 701 os_printf("file info parse failed\n"); 702 break; 703 } 704 705 if(file_server_ip_addr == NULL) 706 { 707 file_server_ip_addr = gethostbyname(i_file_info.source_ip_addr); 708 if(file_server_ip_addr == NULL) 709 { 710 os_printf("failed, get ip from im server\n"); 711 break; 712 } 713 } 714 715 memset(i_file_info.source_ip_addr, 0, sizeof(i_file_info.source_ip_addr)); 716 strcpy(i_file_info.source_ip_addr, inet_ntoa(*(file_server_ip_addr->h_addr))); 717 718 while(write_flash_end) 719 { 720 os_delay_us(500); 721 } 722 723 rc = et_down_file(g_cloud_handle,&i_file_info, write_flash_callback); 724 } 725 break; 726 727 case MES_FROM_GROUP_SUBTOPIC: //羣消息和主題消息事件 728 { 729 os_printf("Group mes from %s\n", topic_name); 730 731 parse_msg_from_mqtt(message->payload, message->payload_len); 732 } 733 break; 734 735 case MES_FROM_LOCAL: //內網消息事件 736 { 737 os_printf("Local mes from %s:%s\n", send_userid, topic_name); 738 memset(g_user_id, 0, sizeof(g_user_id)); 739 snprintf(g_user_id, sizeof(g_user_id), "%s",send_userid); 740 parse_msg_from_mqtt(message->payload, message->payload_len); 741 } 742 break; 743 744 case MES_NOTICE_ADD_BUDDY: //添加用戶消息事件 745 os_printf("You are be add buddy by %s:%s\n", send_userid, topic_name); 746 break; 747 748 case MES_NOTICE_REMOVE_BUDDY: //刪除用戶消息事件 749 os_printf("You are be remove buddy by %s:%s\n", send_userid, topic_name); 750 break; 751 752 case MES_USER_OFFLINE: //用戶下線消息事件 753 os_printf("%s Offline:%s\n", send_userid, topic_name); 754 break; 755 756 case MES_USER_ONLINE: //用戶上線消息事件 757 os_printf("%s Online:%s\n", send_userid, topic_name); 758 break; 759 760 case MES_USER_STATUS: //用戶狀態消息事件 761 os_printf("%s Status:%s\n", send_userid, topic_name); 762 break; 763 764 case MES_ONLINE_BUDDIES_LIST: //在線好友列表消息事件 765 os_printf("Get online buddies list%s:%s\n", send_userid, topic_name); 766 break; 767 } 768 } 769 770 //事件處理任務 771 void et_event_process(et_event_type event) 772 { 773 et_int32 rc = -1; 774 775 switch(event.event_no) 776 { 777 case EVENT_CLOUD_CONNECT: //ilink鏈接事件 778 { 779 disarm_wifi_spark_timer(); 780 wifi_led_on(); 781 os_printf("You are connect:0x%x\n", event.event_no); 782 } 783 break; 784 case EVENT_CLOUD_DISCONNECT: //ilink斷連事件 785 { 786 set_wifi_spark_timer(1000); 787 os_printf("You are disconnect:0x%x\n", event.event_no); 788 //et_logout_cloud(g_cloud_handle); 789 //et_stop_srv(g_cloud_handle); 790 } 791 break; 792 case EVENT_LOGIN_KICK: //被踢下線事件 793 kick_out = 1; 794 os_printf("Your account was login by others:0x%x\n", event.event_no); 795 //et_stop_srv(g_cloud_handle); 796 //et_logout_cloud(g_cloud_handle); 797 break; 798 case EVENT_CLOUD_SEND_FAILED: //外網消息發送失敗事件 799 os_printf("Cloud send failed\n"); 800 break; 801 case EVENT_CLOUD_SEND_SUCCESS: //外網消息發送成功事件 802 os_printf("Cloud send success\n"); 803 break; 804 case EVENT_LOCAL_SEND_FAILED: //內網消息發送失敗事件 805 os_printf("Local send failed\n"); 806 break; 807 case EVENT_LOCAL_SEND_SUCCESS: //內網消息發送成功事件 808 os_printf("Local send success\n"); 809 break; 810 } 811 } 812 813 //外網處理任務 814 void et_ilink_task(void *pvParameters) 815 { 816 while(1) 817 { 818 if(!kick_out) 819 et_ilink_loop(g_cloud_handle); 820 taskYIELD(); 821 } 822 vTaskDelete(NULL); 823 } 824 825 //內網處理任務 826 void et_local_task(void *pvParameters) 827 { 828 while(1) 829 { 830 et_server_loop(g_cloud_handle); 831 taskYIELD(); 832 } 833 834 vTaskDelete(NULL); 835 } 836 837 //用戶主任務 838 void et_user_main(void *pvParameters) 839 { 840 et_int32 rc = -1; 841 id_info_t id; 842 et_server_info_t server_info; 843 et_net_addr_type g_cloud_addr_para = {NULL, 0}; 844 845 os_printf("ET U-SDK var%s\n",et_sdk_ver()); //輸出et_SDK版本 846 to_stop_app = 0; 847 848 memset(&id, 0, sizeof(id_info_t)); //開闢id信息空間 849 if(get_uid(&id) != SPI_FLASH_RESULT_OK) //判斷獲取id狀態 850 { 851 os_printf("et_user_main, get_uid error\n"); 852 return ; 853 } 854 855 id.uid[UID_LEN - 1] = '\0'; //使用Printf,遇到'\0'中止->添加結尾數符 856 id.appkey[APPKEY_LEN - 1] = '\0'; 857 id.secretkey[SECRETKEY_LEN - 1] = '\0'; 858 859 memset(&server_info, 0, sizeof(et_server_info_t)); //開闢服務器信息空間 860 if(get_server(&server_info) != SPI_FLASH_RESULT_OK) //獲取服務器信息 861 { 862 os_printf("et_user_main, get_server error\n"); 863 return ; 864 } 865 866 server_info.lb_addr[ET_LB_ADDR_MAX - 1] = '\0'; 867 server_info.lb_port[ET_LB_PORT_MAX - 1] = '\0'; 868 869 g_cloud_addr_para.name_ip = server_info.lb_addr; //綁定雲平臺地址 870 g_cloud_addr_para.port = atoi(server_info.lb_port); //綁定雲平臺端口 871 872 os_printf("uid : %s\n", id.uid); 873 os_printf("appkey : %s\n", id.appkey); 874 os_printf("secretkey : %s\n", id.secretkey); 875 os_printf("server information %s : %s\n", server_info.lb_addr, server_info.lb_port); 876 877 //建立用戶信息上下問,在et_client文件中有說明 878 g_cloud_handle = et_create_context(id.uid, id.appkey, id.secretkey, g_cloud_addr_para); 879 if(NULL == g_cloud_handle) //若是爲Null表示沒有用戶 880 { 881 os_printf("Init et account failed\n"); 882 goto error; 883 } 884 885 //設置消息發送回調函數,事件回調函數 886 if(et_set_callback(g_cloud_handle,et_message_process, et_event_process) < 0) 887 { 888 os_printf("%s et_set_callback failed.\n"); 889 et_destroy_context(g_cloud_handle); //設置失敗銷燬用戶信息上下文 890 goto error; 891 } 892 893 //內網鏈接,開始鏈接雲 894 rc = et_start_svr(g_cloud_handle); 895 if(rc != 0){ 896 os_printf("%s et_start_svr fail.\n", __FUNCTION__); 897 et_destroy_context(g_cloud_handle); 898 return; 899 } 900 901 //建立內網任務 902 if(pdPASS != xTaskCreate(et_local_task, "local_task", 512, NULL, 2, NULL)) 903 { 904 os_printf("%s et_local_task failed.\n", __FUNCTION__); 905 et_stop_srv(g_cloud_handle); 906 et_destroy_context(g_cloud_handle); 907 return; 908 } 909 910 //外網鏈接,註冊用戶信息 911 do 912 { 913 rc = et_login_cloud(g_cloud_handle, g_cloud_con_para); 914 if(rc != 0) 915 { 916 os_printf("login_cloud fail\n"); 917 } 918 vTaskDelay(2000 / portTICK_RATE_MS); //若是失敗,每隔2s註冊 919 } 920 while(rc != 0 && to_stop_app == 0); 921 922 //建立一個鏈接任務 923 if(pdPASS != xTaskCreate(et_ilink_task, "ilink_task", 768, NULL, 2, NULL)) 924 { 925 os_printf("%s et_ilink_task failed.\n", __FUNCTION__); 926 et_logout_cloud(g_cloud_handle); 927 et_destroy_context(g_cloud_handle); 928 goto error; 929 } 930 931 error: 932 vTaskDelete(NULL); 933 return ; 934 } 935 936 //函數 et_create_context ,et_destroy_context,et_set_callback的位置是在 smart_e_taste_v1.4.x.x\et_app\include 的 et_client.h 的文件裏。 937 //函數 et_start_svr,et_stop_srv,et_login_cloud,et_logout_cloud的位置是在 smart_e_taste_v1.4.x.x\et_app\include 的 et_api_compatible.h 的文件裏。
二. 開發塊平臺ESP8266模塊Flash功能分區表微信
地址 | 固件名 功能 | |
0x0 boot_v1.5.bin | 啓動引導,系統固件(原廠) | |
0x1000 | user1.2048.new.3.bin | 上節編譯生成的固件,用戶應用程序 |
0xfc000(4KB) | fac.bin 進入工廠模式的bin,燒入該bin,用戶能夠從新燒入uid等信息到flash | |
0xfe000(4KB)> | Uid/appkey/securtkey存放地址 | |
0xff000(4KB) | 服務器ip、端口號存放地址 | |
0x100000-0x1AE000 | 該分區用於存放當前體驗語音留言功能時微信端下發的音頻文件 | |
0x1b0000 | gb2312_only_characters.bin | gb2312中文字庫 |
0x1fc000 | esp_init_data_default.bin | 系統參數(原廠) |
0x1fe000 | blank.bin | 系統固件(原廠) |
三. 燒寫固件方法以下:網絡
1. ESP8266模塊Flash擦除方法:app
A. ESP8266擦除工具路徑:socket
http://down.liangchan.net/ESP8266%B2%C1%B3%FD%B9%A4%BE%DF%CD%EA%D5%FB%B0%B2%D7%B0.zip函數
http://www.liangchan.net/liangchan/9183.html
B 操做方法:
a、安裝python27。
b、下載get-pip.py到python安裝目錄。
c、進入到pip安裝包所在的目錄,如「cd C:\Python27\Scripts」,運行python get-pip.py指令,等候一分鐘。
d、把esptool-master文件夾放到python目錄下。
e、安裝esptool和pyserial。直接運行pip install esptool和pip install pyserial兩條指令便可自動完成。
f、把ESP8266清除flash工具放入esptool-master目錄,運行軟件便可。(備註: ESP8266工做在升級模式,即:GPIO0接地 )
2. 配置燒錄工具
解壓FLASH_DOWNLOAD_TOOLS_v2.4_150924.rar,並打開燒寫軟件:ESP_DOWNLOAD_TOOL_V2.4.exe,在Download Path Config選中文件並填寫正確的地址,在前面的小框中打勾,表示選中該文件;按照下圖配置SPI FLASH CONFIG參數;並在COM PORT選中開發板的串口PORT;
3. 燒錄
配置完成以後,點擊START,出現下圖所示表示等待燒錄,此時按住airkiss按鍵不放,同時經過開關對開發板上電;當燒錄進度條開始增加,表示開始燒寫,此時釋放airkiss按鍵,等待進度條變化直到顯示燒錄完成;
4. 固件燒寫注意事項
(1)user1.2048.new.3.bin文件在bin/upgrade下面,其它文件在bin目錄下面;
(2)M302I開發板出廠時已經燒入了表1所列的全部固件,用戶在編譯本身的用戶程序時,能夠僅僅燒入生成的用戶固件(即user1.2048.new.3.bin),而不用燒入其餘參數和固件;
(3)fac.bin文件是使開發板處於工廠生產模式的文件,若是燒入該文件,系統重啓後將進入工廠模式,在該模式下能夠從新燒入appkey/uid/securtkey以及服務器地址和端口號,若是在必要的狀況下(好比appkey/uid/securtkey以及服務器地址和端口號分區不當心被燒入其餘固件而使他們丟失了),能夠從新燒入這些參數。
四. 操做步驟模式:
1. 獲取ESP8266模塊
a. 新的模塊,內置已經下載好原廠固件,能夠直接按照步驟2操做;
b. 若是是其它途徑獲取的模塊,對下載的固件不瞭解,能夠直接對Flash進行擦除操做,按照步驟2操做;
2. 使用開發快平臺上的固件進行下載,固件能夠到官網進行下載,或者找一個工廠文件進行下載,下載操做參考: 「三. 燒寫固件方法以下」
3. 經過單獨或者下載所有文件(包含fac.bin文件),可以在串口工具中看到模塊進入到工廠模式,在工廠模式下,可以經過: "設備標識碼配置工具 - EtDevQuickTool.exe"來燒寫相關信息 ,修改後,經過按恢復出廠設置,可以進入到正常工做模式;以下圖:
4. 須要填寫的相關信息以下:
APPKEY: 32d64ec4-9999-219200
SECRETKEY: 1fc54501b999999999f42b6b9280237f
U-SDK-UID: Fc5wGsTuv9999999992rtasEjmj9QPfFZR
ADDRESS: lb.kaifakuai.com
PORT: 8085
草料二維碼生成器:https://cli.im/
UID信息格式: {"appkey":"32d64ec4-9999-219200","uid":"Fc5wGsTuv9999999992rtasEjmj9QPfFZR"}
以上信息須要註冊開發快,進入到雲平臺,在其中建立一個項目,ESP8266模塊應該選擇MCU平臺,以下圖:
5. 參考地址:
http://bbs.kaifakuai.com/forum.php?mod=viewthread&tid=1893&extra=page%3D1
6. 在正常工做模式下,經過按鍵操做,可以進入到Smart Config 模式下,用於經過微信平臺鏈接,可以讓ESP8266模塊鏈接到Wifi;
7. 經過對源碼進行修改,在Ubuntu環境下進行編譯,生產新的user1.2048.new.3.bin文件,經過下載到ESP8266模塊後可以運行新的應用;