首页 > 技术文章 > 开发快平台(M302I小e开发板系列教程)

hbtmwangjin 2018-04-19 18:08 原文

开发快平台(M302I小e开发板系列教程)

开发块平台ESP8266模块相关理解

一. M302I小e开发板源码注释,源码基于:v1.4.0.8-u34.zip

1. user_main.c

  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擦除方法:

A.  ESP8266擦除工具路径:

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模块后能够运行新的应用;

 

 

 

 

 

 

 

 

推荐阅读