[已解决] 连接MQTT服务器异常

M_xiaotao
Posts: 24
Joined: Tue May 25, 2021 1:35 am

[已解决] 连接MQTT服务器异常

Postby M_xiaotao » Thu Jun 17, 2021 3:41 am

  1. [#include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #include "esp_log.h"
  6. #include "esp_err.h"
  7.  
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/queue.h"
  11. #include "freertos/event_groups.h"
  12. #include "driver/gpio.h"
  13.  
  14. #include "lwip/err.h"
  15. #include "lwip/sockets.h"
  16. #include "lwip/sys.h"
  17. #include "lwip/dns.h"
  18. #include "lwip/netdb.h"
  19.  
  20. #include "mqtt_client.h"
  21.  
  22. #define LOG_TAG     "MQTT"
  23.  
  24. esp_mqtt_client_handle_t mqtt_client = NULL;
  25.  
  26. static void mqtt_event_handler(void* arg, esp_event_base_t event_base,
  27.                                int32_t event_id, void* event_data )
  28. {
  29.     printf("event ID %d\n", event_id );
  30.  
  31.     switch(event_id )
  32.     {
  33.         case MQTT_EVENT_CONNECTED:
  34.             printf("MQTT_EVENT_CONNECTED\n" );
  35.            
  36.             esp_mqtt_client_subscribe(mqtt_client, "Test_1", 2 );
  37.             esp_mqtt_client_subscribe(mqtt_client, "Test_2", 1 );
  38.             esp_mqtt_client_subscribe(mqtt_client, "Picture", 2 );
  39.  
  40.         break;
  41.  
  42.         case MQTT_EVENT_DISCONNECTED:
  43.             printf("MQTT_EVENT_DISCONNECTED\n" );
  44.  
  45.             // esp_mqtt_client_reconnect(mqtt_client );
  46.         break;
  47.  
  48.         case MQTT_EVENT_SUBSCRIBED:
  49.             printf("MQTT_EVENT_SUBSCRIBED\n" );
  50.             // printf("MQTT_EVENT_SUBSCRIBED, msg_id=%d", msg_id);
  51.  
  52.         break;
  53.  
  54.         case MQTT_EVENT_UNSUBSCRIBED:
  55.             printf("MQTT_EVENT_DISCONNECTED\n" );
  56.         break;
  57.  
  58.         case MQTT_EVENT_PUBLISHED:
  59.             printf("MQTT_EVENT_PUBLISHED\n" );
  60.  
  61.         break;
  62.  
  63.         case MQTT_EVENT_DATA:
  64.             printf("MQTT_EVENT_DATA\n" );
  65.  
  66.             // printf("topic length:%d data length:%d\n",event->topic_len,event->data_len);
  67.  
  68.             // printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
  69.             // printf("DATA=%.*s\r\n", event->data_len, event->data);
  70.         break;
  71.  
  72.         case MQTT_EVENT_ERROR:
  73.             printf("MQTT_EVENT_ERROR (%s)\n", (char *)event_data);
  74.         break;
  75.  
  76.         default:
  77.             printf("Other event id:%d\n", event_id );
  78.         break;
  79.     }
  80.  
  81. }
  82.  
  83. void esp_mqtt_app_start(void * arg )
  84. {
  85.     const esp_mqtt_client_config_t mqtt_client_cfg = {
  86.             .host = "127.0.0.1",
  87.             .port= 1883,
  88.         };
  89.  
  90.     mqtt_client = esp_mqtt_client_init(&mqtt_client_cfg );
  91.  
  92.     esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
  93.  
  94.     /**
  95.      *
  96.     */
  97.     esp_mqtt_client_start(mqtt_client);
  98.  
  99.     for ( ;; )
  100.     {
  101.         esp_mqtt_client_publish(mqtt_client, "Test_1", "1234567890", 0, 2, 0 );
  102.  
  103.         vTaskDelay(2000/portTICK_RATE_MS);
  104.     }
  105. }/Codebox][Codebox=c file=Untitled.c]/* GPIO Example
  106.  
  107.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  108.  
  109.    Unless required by applicable law or agreed to in writing, this
  110.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  111.    CONDITIONS OF ANY KIND, either express or implied.
  112. */
  113. #include <stdio.h>
  114. #include <string.h>
  115. #include <stdlib.h>
  116. #include "freertos/FreeRTOS.h"
  117. #include "freertos/task.h"
  118. #include "freertos/queue.h"
  119. #include "freertos/event_groups.h"
  120. #include "driver/gpio.h"
  121.  
  122. /**
  123.  * 2021-06-03
  124.  */
  125. #include "driver/spi_master.h"
  126. #include "driver/rtc_cntl.h"
  127. #include "soc/rtc.h"
  128. #include "driver/adc.h"
  129.  
  130. #include "nvs_flash.h"
  131. #include "esp_wifi.h"
  132. #include "esp_wpa2.h"
  133. #include "esp_event.h"
  134. #include "esp_netif.h"
  135. #include "esp_smartconfig.h"
  136.  
  137. #include "esp_partition.h"
  138.  
  139. #include "epd_gdeh.h"
  140.  
  141. #include "mqtt_client.h"
  142. /**
  143.  * User modefied by 2021-05-27
  144.  */
  145. static EventGroupHandle_t       s_wifi_event_group  = NULL;
  146.  
  147. #define SPI_MOSI_IO_NUM  4
  148. #define SPI_MISO_IO_NUM  -1
  149. #define SPI_SCLK_IO_NUM  16
  150.  
  151. extern void gweh_test(void);
  152. extern void esp_mqtt_app_start(void * arg);
  153.  
  154. void usr_spi_tran_cb(spi_transaction_t *trans)
  155. {
  156.     /**
  157.      * dc = Data / Cmd
  158.     */
  159.     int dc = (int)trans->user;
  160.  
  161.     gpio_set_level(SPI_DC_IO_NUM, dc );
  162.     return;
  163. }
  164.  
  165. spi_bus_config_t buscfg = {
  166.     .mosi_io_num = SPI_MOSI_IO_NUM,
  167.     .miso_io_num = SPI_MISO_IO_NUM,
  168.     .sclk_io_num = SPI_SCLK_IO_NUM,
  169.     .quadhd_io_num = -1,
  170.     .quadwp_io_num = -1,
  171.     .max_transfer_sz = 100
  172. };
  173.  
  174. rtc_cpu_freq_config_t rtc_freq_cfg = {
  175.     .source = RTC_CPU_FREQ_SRC_8M,
  176.     .source_freq_mhz = 8000000,
  177.     .div = 8,
  178.     .freq_mhz = 1000000
  179. };
  180.  
  181. spi_device_handle_t usr_spi_handler = NULL;
  182.  
  183. spi_device_interface_config_t usr_spi_dev_cfg = {
  184.     .flags = SPI_DEVICE_3WIRE|SPI_DEVICE_HALFDUPLEX,
  185.     .command_bits = 0,
  186.     .address_bits = 0,
  187.     .mode = 0,
  188.     .duty_cycle_pos = 0,
  189.     .cs_ena_pretrans= 10,
  190.     .clock_speed_hz = SPI_MASTER_FREQ_8M,
  191.     .input_delay_ns = 0,
  192.     .spics_io_num   = SPI_CS_IO_NUM,
  193.     .queue_size     = 7,
  194.     .pre_cb         = usr_spi_tran_cb
  195. };
  196.  
  197. uint8_t temp_spi_data;
  198. /* Send a command to the LCD. Uses spi_device_polling_transmit, which waits
  199.  * until the transfer is complete.
  200.  *
  201.  * Since command transactions are usually small, they are handled in polling
  202.  * mode for higher speed. The overhead of interrupt transactions is more than
  203.  * just waiting for the transaction to complete.
  204.  */
  205. void lcd_cmd(const uint8_t cmd)
  206. {
  207.     esp_err_t ret;
  208.     spi_transaction_t t;
  209.     memset(&t, 0, sizeof(t));       //Zero out the transaction
  210.  
  211.     temp_spi_data = cmd;
  212.  
  213.     t.length=8;                     //Command is 8 bits
  214.     t.tx_buffer=&temp_spi_data;     //The data is the cmd itself
  215.     t.user=(void*)0;                //D/C needs to be set to 0
  216.     ret=spi_device_polling_transmit(usr_spi_handler, &t);  //Transmit!
  217.     assert(ret==ESP_OK);            //Should have had no issues.
  218. }
  219.  
  220. /* Send data to the LCD. Uses spi_device_polling_transmit, which waits until the
  221.  * transfer is complete.
  222.  *
  223.  * Since data transactions are usually small, they are handled in polling
  224.  * mode for higher speed. The overhead of interrupt transactions is more than
  225.  * just waiting for the transaction to complete.
  226.  */
  227. void lcd_data(const uint8_t data )
  228. {
  229.     esp_err_t ret;
  230.     spi_transaction_t t;
  231.  
  232.     temp_spi_data = data;
  233.  
  234.     memset(&t, 0, sizeof(t));       //Zero out the transaction
  235.     t.length    = 8;                //Len is in bytes, transaction length is in bits.
  236.     t.tx_buffer = &temp_spi_data;   //Data
  237.     t.user=(void*)1;                //D/C needs to be set to 1
  238.     ret=spi_device_polling_transmit(usr_spi_handler, &t);  //Transmit!
  239.     assert(ret==ESP_OK);            //Should have had no issues.
  240. }
  241.  
  242. void ini_spi_test(void * arg )
  243. {
  244.     for ( ;; )
  245.     {
  246.         printf("Display Picture Start\n");
  247.         gweh_test();
  248.         printf("Display Picture End\n");
  249.  
  250.         vTaskDelay(60000/portTICK_RATE_MS);
  251.     }
  252. }
  253. /* The event group allows multiple bits for each event,
  254.    but we only care about one event - are we connected
  255.    to the AP with an IP? */
  256. static const int CONNECTED_BIT = BIT0;
  257. static const int ESPTOUCH_DONE_BIT = BIT1;
  258.  
  259. void event_group_task(void * arg )
  260. {
  261.     static int   init_ok        = 0;
  262.     EventBits_t  cur_event_flag = 0;
  263.  
  264.     ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
  265.     smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
  266.     ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
  267.  
  268.     for ( ;; )
  269.     {
  270.         cur_event_flag = xEventGroupWaitBits(s_wifi_event_group,
  271.                                             CONNECTED_BIT | ESPTOUCH_DONE_BIT,
  272.                                             true, false, portMAX_DELAY );
  273.  
  274.         if (0x00 != (cur_event_flag&CONNECTED_BIT) )
  275.         {
  276.             printf("the device connected to AP\n");
  277.         }
  278.         else if (0x00 != (cur_event_flag&ESPTOUCH_DONE_BIT) )
  279.         {
  280.             printf("Config Done!");
  281.  
  282.             if (0x00 == init_ok )
  283.             {
  284.                 init_ok = 1;
  285.  
  286.                 xTaskCreate(esp_mqtt_app_start, "esp mqtt app", 2048, NULL, 10, NULL);
  287.             }
  288.  
  289.             vTaskDelete(NULL );
  290.         }
  291.     }
  292. }
  293.  
  294. static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void *event_data_arg )
  295. {
  296.     static uint8_t s_cnt   = 0;
  297.     s_cnt++;
  298.  
  299.     if (WIFI_EVENT == event_base )
  300.     {
  301.         printf("------%d------ Wifi (%d) ", s_cnt, event_id);
  302.  
  303.         if (WIFI_EVENT_STA_START == event_id )
  304.         {
  305.             printf("WIFI_EVENT_STA_START\n" );
  306.  
  307.             xTaskCreate(event_group_task, "smartconfig_task", 4096, NULL, 3, NULL );
  308.         }
  309.         else if (WIFI_EVENT_STA_STOP == event_id )
  310.         {
  311.             printf("WIFI_EVENT_STA_STOP\n" );
  312.         }
  313.         else if (WIFI_EVENT_STA_CONNECTED == event_id )
  314.         {
  315.             printf("WIFI_EVENT_STA_CONNECTED\n" );
  316.         }
  317.         else if (WIFI_EVENT_SCAN_DONE == event_id )
  318.         {
  319.             printf("WIFI_EVENT_SCAN_DONE\n" );
  320.         }
  321.         else if (WIFI_EVENT_STA_DISCONNECTED == event_id )
  322.         {
  323.             printf("WIFI_EVENT_SCAN_DONE\n" );
  324.             esp_wifi_connect();
  325.         }
  326.         else{
  327.             printf("Unknow WIFI EVENT ID\n" );
  328.         }
  329.     }
  330.     else if (IP_EVENT == event_base )
  331.     {
  332.         printf("------%d------ IP (%d) ", s_cnt, event_id);
  333.  
  334.         if (IP_EVENT_STA_GOT_IP == event_id )
  335.         {
  336.             /**
  337.              * Device connect suceed.
  338.             */
  339.             printf("Unknow WIFI EVENT ID\n" );
  340.         }
  341.         else{
  342.             printf("Unknow WIFI EVENT ID\n" );
  343.         }
  344.     }
  345.     else if (SC_EVENT == event_base )
  346.     {
  347.         printf("------%d------ SC (%d) ", s_cnt, event_id);
  348.  
  349.         if (SC_EVENT_SCAN_DONE == event_id )
  350.         {
  351.             printf(" SCAN DONE(%d)\n", event_id );
  352.         }
  353.         else if (SC_EVENT_GOT_SSID_PSWD == event_id )
  354.         {
  355.             printf(" GOT SSID PSWD(%d)\n", event_id );
  356.  
  357.             smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data_arg;
  358.  
  359.             wifi_config_t wifi_config;
  360.             uint8_t ssid[33] = { 0 };
  361.             uint8_t password[65] = { 0 };
  362.  
  363.             bzero(&wifi_config, sizeof(wifi_config_t) );
  364.             memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid) );
  365.             memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password) );
  366.             wifi_config.sta.bssid_set = evt->bssid_set;
  367.             if (true == wifi_config.sta.bssid_set ){
  368.                 memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid) );
  369.             }
  370.  
  371.             printf("Wifi ssid (%s)\n", wifi_config.sta.ssid);
  372.             printf("Wifi password (%s)\n", wifi_config.sta.password);
  373.             printf("Wifi bssid (%s)\n", wifi_config.sta.bssid);
  374.  
  375.             ESP_ERROR_CHECK(esp_wifi_disconnect() );
  376.             ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config ) );
  377.             esp_wifi_connect();
  378.  
  379.             xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT );
  380.         }
  381.         else if (SC_EVENT_FOUND_CHANNEL == event_id )
  382.         {
  383.             printf(" FOUND CHANNEL(%d)\n", event_id );
  384.         }
  385.         else if (SC_EVENT_SEND_ACK_DONE == event_id )
  386.         {
  387.             printf(" ACK from DEVICE to ESPTOUCH (%d)\n", event_id );
  388.             xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT );
  389.         }
  390.         else{
  391.             printf(" Unknow SC Message (%d)\n", event_id );
  392.         }
  393.     }
  394.     else{
  395.         printf("Unknow EVENT BASE\n" );
  396.     }
  397. }
  398.  
  399. static void init_wifi(void )
  400. {
  401.     ESP_ERROR_CHECK(nvs_flash_init() );
  402.  
  403.     //static esp_partition_t * s_user_partiton  = NULL;
  404.     // uint8_t *               usr_test_buf     = calloc(0x100, sizeof(uint8_t));
  405.     // if (NULL == usr_test_buf )
  406.     // {
  407.     //     printf("Not have enough space\n");
  408.     //     return;
  409.     // }
  410.  
  411.     // int ret = 0x00;
  412.     // memset(usr_test_buf, 0x00, 0x100);
  413.  
  414.     // /**
  415.     //  * 打印flash 中数据。对flash中数据进行读写操作。
  416.     // */
  417.     // s_user_partiton = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, "usr_data" );
  418.  
  419.     // if (NULL == s_user_partiton )
  420.     // {
  421.     //     printf("find (usr_data) Err \n");
  422.     // }
  423.     // else{
  424.     //     printf("Partiton esp_partition_type_t: %d\n", s_user_partiton->type );
  425.     //     printf("Partiton esp_partition_subtype_t: %d\n", s_user_partiton->subtype );
  426.  
  427.     //     printf("Partiton label: %s\n", s_user_partiton->label );
  428.     //     printf("Partiton address: 0x%08x\n", s_user_partiton->address );
  429.     //     printf("Partiton size: %d\n", s_user_partiton->size );
  430.     // }
  431.    
  432.     // /**
  433.     //  * Read Partition.
  434.     // */
  435.     // ret = esp_partition_read(s_user_partiton, 0x00, usr_test_buf, 0x100 );
  436.     // if (ESP_OK != ret )
  437.     // {   printf("Read partiton Err %d", ret ); }
  438.     // printf("Read data:\n");
  439.     // for (int i = 0; i < 0x100; i++ )
  440.     // {
  441.     //     printf("0x%02x ", usr_test_buf[i] );
  442.  
  443.     //     if (63 == i%64 )
  444.     //     {
  445.     //         printf("\n");
  446.     //     }
  447.     // }
  448.     // printf("Read data end\n");
  449.  
  450.     // /**
  451.     //  * Write Partition.
  452.     // */
  453.     // memset(usr_test_buf, 0xaa, 0x100);
  454.  
  455.     // ret = esp_partition_erase_range(s_user_partiton, 0x00, 0x1000);
  456.     // if (ESP_OK != ret )
  457.     // {   printf("Erase partiton Err %d\n", ret ); }
  458.     // else
  459.     // {   printf("Erase partiton OK\n");}
  460.  
  461.     // ret = esp_partition_write(s_user_partiton, 0x00, usr_test_buf, 0x100 );
  462.     // if (ESP_OK != ret )
  463.     // {   printf("Write partiton Err %d\n", ret ); }
  464.  
  465.     // memset(usr_test_buf, 0x00, 0x100 );
  466.  
  467.     // /**
  468.     //  * Read Partition Again.
  469.     // */
  470.     // ret = esp_partition_read(s_user_partiton, 0x00, usr_test_buf, 0x100 );
  471.     // if (ESP_OK != ret )
  472.     // {   printf("Read partiton Err %d", ret ); }
  473.  
  474.     // printf("Read data Again:\n");
  475.     // for (int i = 0; i < 0x100; i++ )
  476.     // {
  477.     //     printf("0x%02x ", usr_test_buf[i] );
  478.  
  479.     //     if (63 == i%64 )
  480.     //     {
  481.     //         printf("\n");
  482.     //     }
  483.     // }
  484.    
  485.     // printf("Read data again end\n");
  486.  
  487.     // ret = esp_partition_erase_range(s_user_partiton, 0x00, 0x1000);
  488.     // if (ESP_OK != ret )
  489.     // {   printf("Erase partiton Err %d\n", ret ); }
  490.     // else
  491.     // {   printf("Erase partiton OK\n");}
  492.  
  493.     // memset(usr_test_buf, 0x55, 0x100);
  494.  
  495.     // ret = esp_partition_write(s_user_partiton, 0x00, usr_test_buf, 0x100 );
  496.     // if (ESP_OK != ret )
  497.     // {   printf("Write partiton Err %d", ret ); }
  498.  
  499.     // free(usr_test_buf);
  500.  
  501.     ESP_ERROR_CHECK(esp_netif_init() );
  502.     s_wifi_event_group = xEventGroupCreate();
  503.     ESP_ERROR_CHECK(esp_event_loop_create_default() );
  504.     esp_netif_t * sta_netif = esp_netif_create_default_wifi_sta();
  505.     /**
  506.      * assert 用于判断指针是否为0. 若不为0,打印相关信息。
  507.     */
  508.     assert(sta_netif );
  509.  
  510.     /**
  511.      * wifi 初始化.
  512.     */
  513.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  514.     ESP_ERROR_CHECK(esp_wifi_init(&cfg ) );
  515.  
  516.     /**
  517.      * 注册事件处理方法.
  518.     */
  519.     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,    event_handler, NULL) );
  520.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT,   IP_EVENT_STA_GOT_IP, event_handler, NULL) );
  521.     ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT,   ESP_EVENT_ANY_ID, event_handler, NULL) );
  522.  
  523.     /**
  524.      * 设置wifi模式,启动wifi
  525.     */
  526.    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  527.    ESP_ERROR_CHECK(esp_wifi_start() );
  528. }
  529. /**
  530.  * Brief:
  531.  * This test code shows how to configure gpio and how to use gpio interrupt.
  532.  *
  533.  * GPIO status:
  534.  * GPIO18: output
  535.  * GPIO19: output
  536.  * GPIO4:  input, pulled up, interrupt from rising edge and falling edge
  537.  * GPIO5:  input, pulled up, interrupt from rising edge.
  538.  *
  539.  * Test:
  540.  * Connect GPIO18 with GPIO4
  541.  * Connect GPIO19 with GPIO5
  542.  * Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
  543.  *
  544.  */
  545.  
  546. #define GPIO_OUTPUT_IO_0    25
  547. #define GPIO_OUTPUT_IO_1    26
  548. #define GPIO_OUTPUT_PIN_SEL  ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
  549. #define GPIO_INPUT_IO_0     2
  550. #define GPIO_INPUT_IO_1     15
  551. #define GPIO_INPUT_PIN_SEL  ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
  552. #define ESP_INTR_FLAG_DEFAULT 0
  553.  
  554. static xQueueHandle gpio_evt_queue = NULL;
  555.  
  556. static void IRAM_ATTR gpio_isr_handler(void* arg)
  557. {
  558.     uint32_t gpio_num = (uint32_t) arg;
  559.     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  560. }
  561.  
  562. static void gpio_task_example(void* arg)
  563. {
  564.     uint32_t io_num;
  565.     for(;;) {
  566.         if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  567.             printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
  568.         }
  569.     }
  570. }
  571.  
  572. void app_main(void)
  573. {
  574.     gpio_config_t io_conf;
  575.     //disable interrupt
  576.     io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  577.     //set as output mode
  578.     io_conf.mode = GPIO_MODE_OUTPUT;
  579.     //bit mask of the pins that you want to set,e.g.GPIO18/19
  580.     io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  581.     //disable pull-down mode
  582.     io_conf.pull_down_en = 0;
  583.     //disable pull-up mode
  584.     io_conf.pull_up_en = 0;
  585.     //configure GPIO with the given settings
  586.     gpio_config(&io_conf);
  587.  
  588.     //interrupt of rising edge
  589.     io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
  590.     //bit mask of the pins, use GPIO4/5 here
  591.     io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  592.     //set as input mode    
  593.     io_conf.mode = GPIO_MODE_INPUT;
  594.     //enable pull-up mode
  595.     io_conf.pull_up_en = 1;
  596.     gpio_config(&io_conf);
  597.  
  598.     //change gpio intrrupt type for one pin
  599.     gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  600.  
  601.     gpio_set_direction(SPI_DC_IO_NUM,   GPIO_MODE_OUTPUT );
  602.     gpio_set_direction(SPI_RST_IO_NUM,  GPIO_MODE_OUTPUT );
  603.     gpio_set_direction(SPI_BUSY_IO_NUM, GPIO_MODE_INPUT );
  604.  
  605.     spi_bus_initialize(SPI2_HOST, &buscfg, 0 );
  606.     ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &usr_spi_dev_cfg, &usr_spi_handler) );
  607.  
  608.     //create a queue to handle gpio event from isr
  609.     gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  610.     //start gpio task
  611.     xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  612.  
  613.     //install gpio isr service
  614.     gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  615.     //hook isr handler for specific gpio pin
  616.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  617.     //hook isr handler for specific gpio pin
  618.     gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  619.  
  620.     //remove isr handler for gpio number.
  621.     gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  622.     //hook isr handler for specific gpio pin again
  623.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  624.  
  625.     init_wifi();
  626.  
  627.     xTaskCreate(ini_spi_test, "ini spi test\n", 4096, NULL, 3, NULL );
  628.  
  629.     int cnt = 0;
  630.     while(1) {
  631.         printf("cnt: %d\n", cnt++);
  632.         vTaskDelay(1000 / portTICK_RATE_MS);
  633.         gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  634.         gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  635.     }
  636. }
  637.  
  1. E (861562) TRANS_TCP: [sock=54] delayed connect error: Connection reset by peer
  2. E (861562) MQTT_CLIENT: Error transport connect
使用ESP Touch连接wifi成功,但是不能连接EMQ远端MQTT服务器。

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 连接MQTT服务器异常

Postby ESP_Gargamel » Thu Jun 17, 2021 4:12 am

你应该是要去连你 PC 上的 MQTT server,而非 127.0.0.1 吧。

M_xiaotao
Posts: 24
Joined: Tue May 25, 2021 1:35 am

Re: 连接MQTT服务器异常

Postby M_xiaotao » Thu Jun 17, 2021 5:08 am

MQTT.png
MQTT.png (236.65 KiB) Viewed 3550 times
PC端的定义的MQTT Client 可以连接到MQTT Server.

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 连接MQTT服务器异常

Postby ESP_Gargamel » Thu Jun 17, 2021 5:33 am


M_xiaotao
Posts: 24
Joined: Tue May 25, 2021 1:35 am

Re: 连接MQTT服务器异常

Postby M_xiaotao » Thu Jun 17, 2021 6:03 am

  1. static void mqtt_event_handler(void* arg, esp_event_base_t event_base,
  2.                                int32_t event_id, void* event_data )
  3. {
  4.     printf("event ID %d\n", event_id );
  5.  
  6.     switch(event_id )
  7.     {
  8.         case MQTT_EVENT_CONNECTED:
  9.             if (0 == mqtt_state )
  10.             {
  11.                 mqtt_state = 1;
  12.             }
  13.  
  14.             printf("MQTT_EVENT_CONNECTED\n" );
  15.            
  16.             esp_mqtt_client_subscribe(mqtt_client, "Test_1", 2 );
  17.             esp_mqtt_client_subscribe(mqtt_client, "Test_2", 1 );
  18.             esp_mqtt_client_subscribe(mqtt_client, "Picture", 2 );
  19.         break;
  20.  
  21.         case MQTT_EVENT_DISCONNECTED:
  22.             if (0 != mqtt_state )
  23.             {
  24.                 mqtt_state = 0;
  25.             }
  26.  
  27.             printf("MQTT_EVENT_DISCONNECTED\n" );
  28.         break;
  29.  
  30.         case MQTT_EVENT_SUBSCRIBED:
  31.             printf("MQTT_EVENT_SUBSCRIBED\n" );
  32.         break;
  33.  
  34.         case MQTT_EVENT_UNSUBSCRIBED:
  35.             printf("MQTT_EVENT_DISCONNECTED\n" );
  36.         break;
  37.  
  38.         case MQTT_EVENT_PUBLISHED:
  39.             printf("MQTT_EVENT_PUBLISHED\n" );
  40.         break;
  41.  
  42.         case MQTT_EVENT_DATA:
  43.             printf("MQTT_EVENT_DATA\n" );
  44.  
  45.             // printf("topic length:%d data length:%d\n",event->topic_len,event->data_len);
  46.  
  47.             // printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
  48.             // printf("DATA=%.*s\r\n", event->data_len, event->data);
  49.         break;
  50.  
  51.         case MQTT_EVENT_ERROR:
  52.             printf("MQTT_EVENT_ERROR (%s)\n", (char *)event_data);
  53.         break;
  54.  
  55.         default:
  56.             printf("Other event id:%d\n", event_id );
  57.         break;
  58.     }
  59. }
  60. esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL)
请问使用这个回调函数,如何获取数据信息呢?

M_xiaotao
Posts: 24
Joined: Tue May 25, 2021 1:35 am

Re: [已解决] 连接MQTT服务器异常

Postby M_xiaotao » Thu Jun 17, 2021 8:27 am

https://jeason.blog.csdn.net/article/details/116462207
通过参考这个博客,可以了解如何处理接收的数据。

Who is online

Users browsing this forum: No registered users and 67 guests