现象:连上WIFI但发送数据失败
原因:WIFI网络延时过大,或者程序设置的等待超时时间过小
解法:换个网络延时小的WIFI连,或者增加程序等待超时的时间
现象:连不上WIFI
原因:esp8266_mqtt_init()中的的延迟过长,测试4S不行,要2S
解法:将4秒延时改回2S
1 int32_t esp8266_mqtt_init(void) 2 { 3 int32_t rt; 4 delay_ms(2000); 5 //esp8266初始化 6 esp8266_init(); 7 8 // printf("esp8266_mqtt_init 397"); 9 10 //退出透传模式,才能输入AT指令 11 rt=esp8266_exit_transparent_transmission(); 12 if(rt) 13 { 14 printf("esp8266_exit_transparent_transmission fail\r\n"); 15 return -1; 16 } 17 printf("esp8266_exit_transparent_transmission success\r\n"); 18 delay_ms(2000); 19 20 //复位模块 21 rt=esp8266_reset(); 22 if(rt) 23 { 24 printf("esp8266_reset fail\r\n"); 25 return -2; 26 } 27 printf("esp8266_reset success\r\n"); 28 delay_ms(2000); 29 30 //关闭回显 31 rt=esp8266_enable_echo(0); 32 if(rt) 33 { 34 printf("esp8266_enable_echo(0) fail\r\n"); 35 return -3; 36 } 37 printf("esp8266_enable_echo(0)success\r\n"); 38 delay_ms(2000); 39 40 //连接热点 41 rt = esp8266_connect_ap(WIFI_SSID,WIFI_PASSWORD); 42 if(rt) 43 { 44 printf("esp8266_connect_ap fail\r\n"); 45 return -4; 46 } 47 printf("esp8266_connect_ap success\r\n"); 48 delay_ms(2000); 49 //连接网址 50 rt =esp8266_connect_server("TCP",MQTT_BROKERADDRESS,1883); 51 if(rt) 52 { 53 printf("esp8266_connect_server fail\r\n"); 54 return -5; 55 } 56 printf("esp8266_connect_server success\r\n"); 57 delay_ms(2000); 58 59 //进入透传模式 60 rt =esp8266_entry_transparent_transmission(); 61 if(rt) 62 { 63 printf("esp8266_entry_transparent_transmission fail\r\n"); 64 return -6; 65 } 66 printf("esp8266_entry_transparent_transmission success\r\n"); 67 delay_ms(2000); 68 //连接mqtt 69 if(mqtt_connect(MQTT_CLIENTID, MQTT_USARNAME, MQTT_PASSWD)) 70 { 71 printf("mqtt_connect fail\r\n"); 72 return -7; 73 74 } 75 printf("mqtt_connect success\r\n"); 76 delay_ms(2000); 77 //订阅 78 if(mqtt_subscribe_topic(MQTT_SUBSCRIBE_TOPIC,0,1)) 79 { 80 printf("mqtt_subscribe_topic fail\r\n"); 81 return -8; 82 } 83 printf("mqtt_subscribe_topic success\r\n"); 84 85 return 0; 86 }View Code
现象:
原因:esp8266_mqtt_init()中的延时过短
解法:测试270ms,也是可以的但不够稳定,建议没事保存默认2S不要改
标签:rt,return,esp8266,stm32,mqtt,connect,测试,printf From: https://www.cnblogs.com/xuweihui/p/18013321